1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use na::{Mat4, one, Vec3};
use crate::mesh;
use blender;
use std::iter;
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub enum MirrorFlags{
Clipping = 1 << 0,
MirrorU = 1 << 1,
MirrorV = 1 << 2,
AxisX = 1 << 3,
AxisY = 1 << 4,
AxisZ = 1 << 5,
VGroup = 1 << 6,
NoMerge = 1 << 7,
}
#[derive(Copy, Clone, Debug)]
pub enum MirrorAxis{
X = 0,
Y = 1,
Z = 2,
}
fn find_flip_group(vertex_groups: &[String], group_idx: i32) -> Option<i32>{
let vertex_group = vertex_groups.get(group_idx as usize)?.clone();
let separators = ['.', ' ', '-', '_'];
let mirrored_group = if separators.contains(&vertex_group[vertex_group.len() - 2..].chars().next().unwrap()){
let suffix = &vertex_group[vertex_group.len() - 1..];
let mirror_suffix = match suffix{
"l" => "r",
"r" => "l",
"L" => "R",
"R" => "L",
_ => return None,
};
vertex_group[0..vertex_group.len()-1].to_string() + mirror_suffix
}else if separators.contains(&vertex_group[1..].chars().next().unwrap()){
let suffix = &vertex_group[0..1];
let mirror_suffix = match suffix{
"l" => "r",
"r" => "l",
"L" => "R",
"R" => "L",
_ => return None,
};
mirror_suffix.to_string() + &vertex_group[1..]
}else if vertex_group.len()>=4 && vertex_group[0..4].to_lowercase() == "left"{
let rest = &vertex_group[4..];
if vertex_group.starts_with("l"){
"right".to_string() + rest
}else if &vertex_group[1..2] == "E"{
"RIGHT".to_string() + rest
}else{
"Right".to_string() + rest
}
}else if vertex_group.len()>=5 && vertex_group[0..5].to_lowercase() == "right"{
let rest = &vertex_group[5..];
if vertex_group.starts_with("r"){
"left".to_string() + rest
}else if &vertex_group[1..2] == "I"{
"LEFT".to_string() + rest
}else{
"Left".to_string() + rest
}
}else{
return None;
};
vertex_groups.iter().position(|name| mirrored_group == *name).map(|pos| pos as i32)
}
pub fn mirror_mesh(mirror: &blender::Object, object: &blender::Object, mesh: &mut mesh::Mesh, vertex_groups: &[String], axis: MirrorAxis){
let flag: u16 = *mirror.get("flag").unwrap();
let tolerance: f32 = *mirror.get("tolerance").unwrap();
let tolerance_sq = tolerance * tolerance;
let mut mirror_mat: Mat4<f32> = one();
mirror_mat[(axis as usize, axis as usize)] = -1.0;
if let Ok(mirror_ob) = mirror.get_object("mirror_ob"){
let mirror_obj_mat: &Mat4<f32> = mirror_ob.get("obmat").unwrap();
let object_mat: &Mat4<f32> = object.get("obmat").unwrap();
let tmp = mirror_obj_mat.try_inverse().unwrap() * *object_mat;
let itmp = tmp.try_inverse().unwrap();
mirror_mat = itmp * mirror_mat * tmp;
}
let num_verts = mesh.mvert.len();
let num_edges = mesh.medge.len();
let num_loops = mesh.mloop.len();
let mirror_verts: Vec<mesh::MVert> = mesh.mvert.iter_mut().map(|v| {
let mirror_v = (mirror_mat * vec4!(v.position, 1.0)).xyz();
let l = (mirror_v - v.position).norm();
if flag & MirrorFlags::NoMerge as u16 == 0 && l*l < tolerance_sq{
let merge_v = (v.position + mirror_v) * 0.5;
v.position = merge_v;
*v
}else{
let mut v = *v;
v.position = mirror_v;
v
}
}).collect();
mesh.mvert.extend_from_slice(&mirror_verts);
let edges_clone = mesh.medge.clone();
let mirror_edges = edges_clone.iter().map(|edge|{
let mut mirror_edge = *edge;
mirror_edge.v1 += num_verts as u32;
mirror_edge.v2 += num_verts as u32;
mirror_edge
});
mesh.medge.extend(mirror_edges);
let uvs_clone = mesh.mloopuv.clone();
mesh.mloopuv.extend_from_slice(&uvs_clone);
{
let loops_clone = mesh.mloop.clone();
let mirror_loops = mesh.mpoly.iter().flat_map(|poly| {
let start = poly.loopstart;
let end = poly.loopstart + poly.totloop;
iter::once(start).chain((start+1..end).rev()).map(|i|{
let mut mirror_loop = loops_clone[i as usize];
mirror_loop.v += num_verts as u32;
mirror_loop.e += num_edges as u32;
mirror_loop
})
});
mesh.mloop.extend(mirror_loops);
}
let polys_clone = mesh.mpoly.clone();
let mirror_polys = polys_clone.iter().map(|poly|{
let mut mirror_poly = *poly;
mirror_poly.loopstart += num_loops as u32;
mirror_poly
});
mesh.mpoly.extend(mirror_polys);
if flag & MirrorFlags::VGroup as u16 == 0{
let dvert = mesh.dvert.clone();
mesh.dvert.extend_from_slice(&dvert);
}else{
let current_dvert_len = mesh.dvert.len();
for i in 0..current_dvert_len{
let dw = mesh.dvert(i).iter().map(|weight|{
let mut weight = *weight;
match find_flip_group(vertex_groups, weight.def_nr){
Some(mirror_group) => {
weight.def_nr = mirror_group;
}
None => {},
}
weight
}).collect::<Vec<_>>();
let dw_start = mesh.dweights.len();
mesh.dweights.extend_from_slice(&dw);
let dw_end = mesh.dweights.len();
let flag = mesh.dvert[i].flag;
mesh.dvert.push(mesh::MDeformVert{
dw_start,
dw_end,
flag: flag,
});
}
}
mesh.recalculate_normals();
}
pub fn array_mesh(modifier: &blender::Object, mesh: &mut mesh::Mesh){
let offset: Vec3 = *modifier.get("offset").unwrap();
let count: u32 = *modifier.get("count").unwrap();
let num_verts = mesh.mvert.len() as u32;
let num_edges = mesh.medge.len() as u32;
let num_loops = mesh.mloop.len() as u32;
let mvert = (0..count).flat_map(|i|{
mesh.mvert.iter().map(move |v|{
let mut v2 = v.clone();
v2.position += offset * i as f32;
v2
})
}).collect();
let edges_clone = mesh.medge.clone();
let edges = (0..count).flat_map(|i|{
edges_clone.iter().map(move |edge|{
let mut mirror_edge = *edge;
mirror_edge.v1 += num_verts * i;
mirror_edge.v2 += num_verts * i;
mirror_edge
})
}).collect();
let uvs = (0..count).flat_map(|_|{
mesh.mloopuv.clone()
}).collect();
let poly_clone = mesh.mpoly.clone();
let loops_clone = mesh.mloop.clone();
let loops = (0..count).flat_map(|j|{
let loops_clone = loops_clone.clone();
poly_clone.clone().into_iter().flat_map(move |poly| {
let loopstart = poly.loopstart;
let totloop = poly.totloop;
let loops_clone = loops_clone.clone();
(loopstart..loopstart+1).chain((loopstart+1..loopstart+totloop).rev()).map(move |i|{
let mut mirror_loop = loops_clone[i as usize];
mirror_loop.v += num_verts * j;
mirror_loop.e += num_edges * j;
mirror_loop
})
})
}).collect();
let polys_clone = mesh.mpoly.clone();
let polys = (0..count).flat_map(|i|{
polys_clone.iter().map(move |poly|{
let mut mirror_poly = *poly;
mirror_poly.loopstart += num_loops as u32 * i;
mirror_poly
})
}).collect();
let weights = (0..count).flat_map(|_i|{
mesh.dvert.iter().map(|weights|{
weights.clone()
})
}).collect();
mesh.mvert = mvert;
mesh.medge = edges;
mesh.mloopuv = uvs;
mesh.mloop = loops;
mesh.mpoly = polys;
mesh.dvert = weights;
}
#[allow(dead_code)]
pub enum SubsurfModifierFlag{
Incremental = 1 << 0,
DebugIncr = 1 << 1,
ControlEdges = 1 << 2,
SubsurfUv = 1 << 3,
}