Debian Codegraph @

scene/resources/surface_tool.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* surface_tool.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "surface_tool.h"
32
33 #include "core/templates/a_hash_map.h"
34
35 #define EQ_VERTEX_DIST 0.00001
36
37 SurfaceTool::OptimizeVertexCacheFunc SurfaceTool::optimize_vertex_cache_func = nullptr;
38 SurfaceTool::OptimizeVertexFetchRemapFunc SurfaceTool::optimize_vertex_fetch_remap_func = nullptr;
39 SurfaceTool::SimplifyFunc SurfaceTool::simplify_func = nullptr;
40 SurfaceTool::SimplifyWithAttribFunc SurfaceTool::simplify_with_attrib_func = nullptr;
41 SurfaceTool::SimplifyScaleFunc SurfaceTool::simplify_scale_func = nullptr;
42 SurfaceTool::GenerateRemapFunc SurfaceTool::generate_remap_func = nullptr;
43 SurfaceTool::RemapVertexFunc SurfaceTool::remap_vertex_func = nullptr;
44 SurfaceTool::RemapIndexFunc SurfaceTool::remap_index_func = nullptr;
45
46 void SurfaceTool::strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
47 ERR_FAIL_COND_MSG(!generate_remap_func || !remap_vertex_func || !remap_index_func, "Meshoptimizer library is not initialized.");
48
49 Vector<uint32_t> remap;
50 remap.resize(r_vertices.size());
51 uint32_t new_vertex_count = generate_remap_func(remap.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), r_vertices.ptr(), r_vertices.size(), sizeof(Vector3));
52 remap_vertex_func(r_vertices.ptrw(), r_vertices.ptr(), r_vertices.size(), sizeof(Vector3), remap.ptr());
53 r_vertices.resize(new_vertex_count);
54 remap_index_func((unsigned int *)r_indices.ptrw(), (unsigned int *)r_indices.ptr(), r_indices.size(), remap.ptr());
55
56 AHashMap<const int *, bool, TriangleHasher, TriangleHasher> found_triangles;
57 int *idx_ptr = r_indices.ptrw();
58
59 int filtered_indices_count = 0;
60 for (int i = 0; i < r_indices.size() / 3; i++) {
61 const int *tri = idx_ptr + (i * 3);
62
63 if (tri[0] == tri[1] || tri[1] == tri[2] || tri[2] == tri[0]) {
64 continue;
65 }
66
67 if (found_triangles.has(tri)) {
68 continue;
69 }
70
71 if (i != filtered_indices_count) {
72 memcpy(idx_ptr + (filtered_indices_count * 3), tri, sizeof(int) * 3);
73 }
74
75 found_triangles.insert_new(tri, true);
76 filtered_indices_count++;
77 }
78 r_indices.resize(filtered_indices_count * 3);
79 }
80
81 bool SurfaceTool::Vertex::operator==(const Vertex &p_vertex) const {
82 if (vertex != p_vertex.vertex) {
83 return false;
84 }
85
86 if (uv != p_vertex.uv) {
87 return false;
88 }
89
90 if (uv2 != p_vertex.uv2) {
91 return false;
92 }
93
94 if (normal != p_vertex.normal) {
95 return false;
96 }
97
98 if (binormal != p_vertex.binormal) {
99 return false;
100 }
101
102 if (tangent != p_vertex.tangent) {
103 return false;
104 }
105
106 if (color != p_vertex.color) {
107 return false;
108 }
109
110 if (bones.size() != p_vertex.bones.size()) {
111 return false;
112 }
113
114 for (int i = 0; i < bones.size(); i++) {
115 if (bones[i] != p_vertex.bones[i]) {
116 return false;
117 }
118 }
119
120 for (int i = 0; i < weights.size(); i++) {
121 if (weights[i] != p_vertex.weights[i]) {
122 return false;
123 }
124 }
125
126 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
127 if (custom[i] != p_vertex.custom[i]) {
128 return false;
129 }
130 }
131
132 if (smooth_group != p_vertex.smooth_group) {
133 return false;
134 }
135
136 return true;
137 }
138
139 uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
140 uint32_t h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int));
141 h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
142
143 const int64_t length = (int64_t)&p_vtx.vertex - (int64_t)&p_vtx.smooth_group + sizeof(p_vtx.vertex);
144 const void *key = &p_vtx.smooth_group;
145 h = hash_murmur3_buffer(key, length, h);
146 return h;
147 }
148
149 bool SurfaceTool::SmoothGroupVertex::operator==(const SmoothGroupVertex &p_vertex) const {
150 if (vertex != p_vertex.vertex) {
151 return false;
152 }
153
154 if (smooth_group != p_vertex.smooth_group) {
155 return false;
156 }
157
158 return true;
159 }
160
161 uint32_t SurfaceTool::SmoothGroupVertexHasher::hash(const SmoothGroupVertex &p_vtx) {
162 uint32_t h = HashMapHasherDefault::hash(p_vtx.vertex);
163 h = hash_murmur3_one_32(p_vtx.smooth_group, h);
164 h = hash_fmix32(h);
165 return h;
166 }
167
168 uint32_t SurfaceTool::TriangleHasher::hash(const int *p_triangle) {
169 int t0 = p_triangle[0];
170 int t1 = p_triangle[1];
171 int t2 = p_triangle[2];
172
173 if (t0 > t1) {
174 SWAP(t0, t1);
175 }
176 if (t1 > t2) {
177 SWAP(t1, t2);
178 }
179 if (t0 > t1) {
180 SWAP(t0, t1);
181 }
182
183 return (t0 * 73856093) ^ (t1 * 19349663) ^ (t2 * 83492791);
184 }
185
186 bool SurfaceTool::TriangleHasher::compare(const int *p_lhs, const int *p_rhs) {
187 int r0 = p_rhs[0];
188 int r1 = p_rhs[1];
189 int r2 = p_rhs[2];
190
191 if (r0 > r1) {
192 SWAP(r0, r1);
193 }
194 if (r1 > r2) {
195 SWAP(r1, r2);
196 }
197 if (r0 > r1) {
198 SWAP(r0, r1);
199 }
200
201 int l0 = p_lhs[0];
202 int l1 = p_lhs[1];
203 int l2 = p_lhs[2];
204
205 if (l0 > l1) {
206 SWAP(l0, l1);
207 }
208 if (l1 > l2) {
209 SWAP(l1, l2);
210 }
211 if (l0 > l1) {
212 SWAP(l0, l1);
213 }
214
215 return l0 == r0 && l1 == r1 && l2 == r2;
216 }
217
218 void SurfaceTool::begin(Mesh::PrimitiveType p_primitive) {
219 clear();
220
221 primitive = p_primitive;
222 begun = true;
223 first = true;
224 }
225
226 void SurfaceTool::add_vertex(const Vector3 &p_vertex) {
227 ERR_FAIL_COND(!begun);
228
229 Vertex vtx;
230 vtx.vertex = p_vertex;
231 vtx.color = last_color;
232 vtx.normal = last_normal;
233 vtx.uv = last_uv;
234 vtx.uv2 = last_uv2;
235 vtx.weights = last_weights;
236 vtx.bones = last_bones;
237 vtx.tangent = last_tangent.normal;
238 vtx.binormal = last_normal.cross(last_tangent.normal).normalized() * last_tangent.d;
239 vtx.smooth_group = last_smooth_group;
240
241 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
242 vtx.custom[i] = last_custom[i];
243 }
244
245 const int expected_vertices = skin_weights == SKIN_8_WEIGHTS ? 8 : 4;
246
247 if ((format & Mesh::ARRAY_FORMAT_WEIGHTS || format & Mesh::ARRAY_FORMAT_BONES) && (vtx.weights.size() != expected_vertices || vtx.bones.size() != expected_vertices)) {
248 //ensure vertices are the expected amount
249 ERR_FAIL_COND(vtx.weights.size() != vtx.bones.size());
250 if (vtx.weights.size() < expected_vertices) {
251 //less than required, fill
252 for (int i = vtx.weights.size(); i < expected_vertices; i++) {
253 vtx.weights.push_back(0);
254 vtx.bones.push_back(0);
255 }
256 } else if (vtx.weights.size() > expected_vertices) {
257 //more than required, sort, cap and normalize.
258 Vector<WeightSort> weights;
259 for (int i = 0; i < vtx.weights.size(); i++) {
260 WeightSort ws;
261 ws.index = vtx.bones[i];
262 ws.weight = vtx.weights[i];
263 weights.push_back(ws);
264 }
265
266 //sort
267 weights.sort();
268 //cap
269 weights.resize(expected_vertices);
270 //renormalize
271 float total = 0.0;
272 for (int i = 0; i < expected_vertices; i++) {
273 total += weights[i].weight;
274 }
275
276 vtx.weights.resize(expected_vertices);
277 vtx.bones.resize(expected_vertices);
278
279 for (int i = 0; i < expected_vertices; i++) {
280 if (total > 0) {
281 vtx.weights.write[i] = weights[i].weight / total;
282 } else {
283 vtx.weights.write[i] = 0;
284 }
285 vtx.bones.write[i] = weights[i].index;
286 }
287 }
288 }
289
290 vertex_array.push_back(vtx);
291 first = false;
292
293 format |= Mesh::ARRAY_FORMAT_VERTEX;
294 }
295
296 void SurfaceTool::set_color(Color p_color) {
297 ERR_FAIL_COND(!begun);
298
299 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_COLOR));
300
301 format |= Mesh::ARRAY_FORMAT_COLOR;
302 last_color = p_color;
303 }
304
305 void SurfaceTool::set_normal(const Vector3 &p_normal) {
306 ERR_FAIL_COND(!begun);
307
308 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_NORMAL));
309
310 format |= Mesh::ARRAY_FORMAT_NORMAL;
311 last_normal = p_normal;
312 }
313
314 void SurfaceTool::set_tangent(const Plane &p_tangent) {
315 ERR_FAIL_COND(!begun);
316 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TANGENT));
317
318 format |= Mesh::ARRAY_FORMAT_TANGENT;
319 last_tangent = p_tangent;
320 }
321
322 void SurfaceTool::set_uv(const Vector2 &p_uv) {
323 ERR_FAIL_COND(!begun);
324 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV));
325
326 format |= Mesh::ARRAY_FORMAT_TEX_UV;
327 last_uv = p_uv;
328 }
329
330 void SurfaceTool::set_uv2(const Vector2 &p_uv2) {
331 ERR_FAIL_COND(!begun);
332 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV2));
333
334 format |= Mesh::ARRAY_FORMAT_TEX_UV2;
335 last_uv2 = p_uv2;
336 }
337
338 void SurfaceTool::set_custom(int p_channel_index, const Color &p_custom) {
339 ERR_FAIL_INDEX(p_channel_index, RS::ARRAY_CUSTOM_COUNT);
340 ERR_FAIL_COND(!begun);
341 ERR_FAIL_COND(last_custom_format[p_channel_index] == CUSTOM_MAX);
342 static const uint32_t mask[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0, Mesh::ARRAY_FORMAT_CUSTOM1, Mesh::ARRAY_FORMAT_CUSTOM2, Mesh::ARRAY_FORMAT_CUSTOM3 };
343 ERR_FAIL_COND(!first && !(format & mask[p_channel_index]));
344
345 if (first) {
346 format |= mask[p_channel_index];
347 }
348 last_custom[p_channel_index] = p_custom;
349 }
350
351 void SurfaceTool::set_bones(const Vector<int> &p_bones) {
352 ERR_FAIL_COND(!begun);
353 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_BONES));
354
355 format |= Mesh::ARRAY_FORMAT_BONES;
356 if (skin_weights == SKIN_8_WEIGHTS) {
357 format |= Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
358 }
359 last_bones = p_bones;
360 }
361
362 void SurfaceTool::set_weights(const Vector<float> &p_weights) {
363 ERR_FAIL_COND(!begun);
364 ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_WEIGHTS));
365
366 format |= Mesh::ARRAY_FORMAT_WEIGHTS;
367 if (skin_weights == SKIN_8_WEIGHTS) {
368 format |= Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
369 }
370 last_weights = p_weights;
371 }
372
373 void SurfaceTool::set_smooth_group(uint32_t p_group) {
374 last_smooth_group = p_group;
375 }
376
377 void SurfaceTool::_add_triangle_fan(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<Vector2> &p_uv2s, const Vector<Vector3> &p_normals, const TypedArray<Plane> &p_tangents) {
378 add_triangle_fan(p_vertices, p_uvs, p_colors, p_uv2s, p_normals, Variant(p_tangents));
379 }
380
381 void SurfaceTool::add_triangle_fan(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<Vector2> &p_uv2s, const Vector<Vector3> &p_normals, const Vector<Plane> &p_tangents) {
382 ERR_FAIL_COND(!begun);
383 ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
384 ERR_FAIL_COND(p_vertices.size() < 3);
385
386 #define ADD_POINT(n) \
387 { \
388 if (p_colors.size() > n) \
389 set_color(p_colors[n]); \
390 if (p_uvs.size() > n) \
391 set_uv(p_uvs[n]); \
392 if (p_uv2s.size() > n) \
393 set_uv2(p_uv2s[n]); \
394 if (p_normals.size() > n) \
395 set_normal(p_normals[n]); \
396 if (p_tangents.size() > n) \
397 set_tangent(p_tangents[n]); \
398 add_vertex(p_vertices[n]); \
399 }
400
401 for (int i = 0; i < p_vertices.size() - 2; i++) {
402 ADD_POINT(0);
403 ADD_POINT(i + 1);
404 ADD_POINT(i + 2);
405 }
406
407 #undef ADD_POINT
408 }
409
410 void SurfaceTool::add_index(int p_index) {
411 ERR_FAIL_COND(!begun);
412 ERR_FAIL_COND(p_index < 0);
413
414 format |= Mesh::ARRAY_FORMAT_INDEX;
415 index_array.push_back(p_index);
416 }
417
418 Array SurfaceTool::commit_to_arrays() {
419 int varr_len = vertex_array.size();
420
421 Array a;
422 a.resize(Mesh::ARRAY_MAX);
423
424 for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
425 if (!(format & (1ULL << i))) {
426 continue; //not in format
427 }
428
429 switch (i) {
430 case Mesh::ARRAY_VERTEX:
431 case Mesh::ARRAY_NORMAL: {
432 Vector<Vector3> array;
433 array.resize(varr_len);
434 Vector3 *w = array.ptrw();
435
436 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
437 const Vertex &v = vertex_array[idx];
438
439 switch (i) {
440 case Mesh::ARRAY_VERTEX: {
441 w[idx] = v.vertex;
442 } break;
443 case Mesh::ARRAY_NORMAL: {
444 w[idx] = v.normal;
445 } break;
446 }
447 }
448
449 a[i] = array;
450
451 } break;
452
453 case Mesh::ARRAY_TEX_UV:
454 case Mesh::ARRAY_TEX_UV2: {
455 Vector<Vector2> array;
456 array.resize(varr_len);
457 Vector2 *w = array.ptrw();
458
459 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
460 const Vertex &v = vertex_array[idx];
461
462 switch (i) {
463 case Mesh::ARRAY_TEX_UV: {
464 w[idx] = v.uv;
465 } break;
466 case Mesh::ARRAY_TEX_UV2: {
467 w[idx] = v.uv2;
468 } break;
469 }
470 }
471
472 a[i] = array;
473 } break;
474 case Mesh::ARRAY_TANGENT: {
475 Vector<float> array;
476 array.resize(varr_len * 4);
477 float *w = array.ptrw();
478
479 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
480 const Vertex &v = vertex_array[idx];
481
482 w[idx * 4 + 0] = v.tangent.x;
483 w[idx * 4 + 1] = v.tangent.y;
484 w[idx * 4 + 2] = v.tangent.z;
485
486 //float d = v.tangent.dot(v.binormal,v.normal);
487 float d = v.binormal.dot(v.normal.cross(v.tangent));
488 w[idx * 4 + 3] = d < 0 ? -1 : 1;
489 }
490
491 a[i] = array;
492
493 } break;
494 case Mesh::ARRAY_COLOR: {
495 Vector<Color> array;
496 array.resize(varr_len);
497 Color *w = array.ptrw();
498
499 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
500 const Vertex &v = vertex_array[idx];
501
502 w[idx] = v.color;
503 }
504
505 a[i] = array;
506 } break;
507 case Mesh::ARRAY_CUSTOM0:
508 case Mesh::ARRAY_CUSTOM1:
509 case Mesh::ARRAY_CUSTOM2:
510 case Mesh::ARRAY_CUSTOM3: {
511 int fmt = i - Mesh::ARRAY_CUSTOM0;
512 switch (last_custom_format[fmt]) {
513 case CUSTOM_RGBA8_UNORM: {
514 Vector<uint8_t> array;
515 array.resize(varr_len * 4);
516 uint8_t *w = array.ptrw();
517
518 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
519 const Vertex &v = vertex_array[idx];
520
521 const Color &c = v.custom[fmt];
522 w[idx * 4 + 0] = CLAMP(int32_t(c.r * 255.0), 0, 255);
523 w[idx * 4 + 1] = CLAMP(int32_t(c.g * 255.0), 0, 255);
524 w[idx * 4 + 2] = CLAMP(int32_t(c.b * 255.0), 0, 255);
525 w[idx * 4 + 3] = CLAMP(int32_t(c.a * 255.0), 0, 255);
526 }
527
528 a[i] = array;
529 } break;
530 case CUSTOM_RGBA8_SNORM: {
531 Vector<uint8_t> array;
532 array.resize(varr_len * 4);
533 uint8_t *w = array.ptrw();
534
535 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
536 const Vertex &v = vertex_array[idx];
537
538 const Color &c = v.custom[fmt];
539 w[idx * 4 + 0] = uint8_t(int8_t(CLAMP(int32_t(c.r * 127.0), -128, 127)));
540 w[idx * 4 + 1] = uint8_t(int8_t(CLAMP(int32_t(c.g * 127.0), -128, 127)));
541 w[idx * 4 + 2] = uint8_t(int8_t(CLAMP(int32_t(c.b * 127.0), -128, 127)));
542 w[idx * 4 + 3] = uint8_t(int8_t(CLAMP(int32_t(c.a * 127.0), -128, 127)));
543 }
544
545 a[i] = array;
546 } break;
547 case CUSTOM_RG_HALF: {
548 Vector<uint8_t> array;
549 array.resize(varr_len * 4);
550 uint16_t *w = (uint16_t *)array.ptrw();
551
552 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
553 const Vertex &v = vertex_array[idx];
554
555 const Color &c = v.custom[fmt];
556 w[idx * 2 + 0] = Math::make_half_float(c.r);
557 w[idx * 2 + 1] = Math::make_half_float(c.g);
558 }
559
560 a[i] = array;
561 } break;
562 case CUSTOM_RGBA_HALF: {
563 Vector<uint8_t> array;
564 array.resize(varr_len * 8);
565 uint16_t *w = (uint16_t *)array.ptrw();
566
567 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
568 const Vertex &v = vertex_array[idx];
569
570 const Color &c = v.custom[fmt];
571 w[idx * 4 + 0] = Math::make_half_float(c.r);
572 w[idx * 4 + 1] = Math::make_half_float(c.g);
573 w[idx * 4 + 2] = Math::make_half_float(c.b);
574 w[idx * 4 + 3] = Math::make_half_float(c.a);
575 }
576
577 a[i] = array;
578 } break;
579 case CUSTOM_R_FLOAT: {
580 Vector<float> array;
581 array.resize(varr_len);
582 float *w = (float *)array.ptrw();
583
584 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
585 const Vertex &v = vertex_array[idx];
586
587 const Color &c = v.custom[fmt];
588 w[idx] = c.r;
589 }
590
591 a[i] = array;
592 } break;
593 case CUSTOM_RG_FLOAT: {
594 Vector<float> array;
595 array.resize(varr_len * 2);
596 float *w = (float *)array.ptrw();
597
598 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
599 const Vertex &v = vertex_array[idx];
600
601 const Color &c = v.custom[fmt];
602 w[idx * 2 + 0] = c.r;
603 w[idx * 2 + 1] = c.g;
604 }
605
606 a[i] = array;
607 } break;
608 case CUSTOM_RGB_FLOAT: {
609 Vector<float> array;
610 array.resize(varr_len * 3);
611 float *w = (float *)array.ptrw();
612
613 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
614 const Vertex &v = vertex_array[idx];
615
616 const Color &c = v.custom[fmt];
617 w[idx * 3 + 0] = c.r;
618 w[idx * 3 + 1] = c.g;
619 w[idx * 3 + 2] = c.b;
620 }
621
622 a[i] = array;
623 } break;
624 case CUSTOM_RGBA_FLOAT: {
625 Vector<float> array;
626 array.resize(varr_len * 4);
627 float *w = (float *)array.ptrw();
628
629 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
630 const Vertex &v = vertex_array[idx];
631
632 const Color &c = v.custom[fmt];
633 w[idx * 4 + 0] = c.r;
634 w[idx * 4 + 1] = c.g;
635 w[idx * 4 + 2] = c.b;
636 w[idx * 4 + 3] = c.a;
637 }
638
639 a[i] = array;
640 } break;
641 default: {
642 } //unreachable but compiler warning anyway
643 }
644 } break;
645 case Mesh::ARRAY_BONES: {
646 int count = skin_weights == SKIN_8_WEIGHTS ? 8 : 4;
647 Vector<int> array;
648 array.resize_initialized(varr_len * count);
649 int *w = array.ptrw();
650
651 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
652 const Vertex &v = vertex_array[idx];
653
654 if (v.bones.size() != count) {
655 ERR_PRINT_ONCE(vformat("Invalid bones size %d vs count %d", v.bones.size(), count));
656 continue;
657 }
658
659 for (int j = 0; j < count; j++) {
660 w[idx * count + j] = v.bones[j];
661 }
662 }
663
664 a[i] = array;
665
666 } break;
667 case Mesh::ARRAY_WEIGHTS: {
668 Vector<float> array;
669 int count = skin_weights == SKIN_8_WEIGHTS ? 8 : 4;
670
671 array.resize_initialized(varr_len * count);
672 float *w = array.ptrw();
673
674 for (uint32_t idx = 0; idx < vertex_array.size(); idx++) {
675 const Vertex &v = vertex_array[idx];
676
677 if (v.weights.size() != count) {
678 ERR_PRINT_ONCE(vformat("Invalid weight size %d vs count %d", v.weights.size(), count));
679 continue;
680 }
681
682 for (int j = 0; j < count; j++) {
683 w[idx * count + j] = v.weights[j];
684 }
685 }
686
687 a[i] = array;
688
689 } break;
690 case Mesh::ARRAY_INDEX: {
691 ERR_CONTINUE(index_array.is_empty());
692
693 Vector<int> array;
694 array.resize(index_array.size());
695 int *w = array.ptrw();
696
697 for (uint32_t idx = 0; idx < index_array.size(); idx++) {
698 w[idx] = index_array[idx];
699 }
700
701 a[i] = array;
702 } break;
703
704 default: {
705 }
706 }
707 }
708
709 return a;
710 }
711
712 Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing, uint64_t p_compress_flags) {
713 Ref<ArrayMesh> mesh;
714 if (p_existing.is_valid()) {
715 mesh = p_existing;
716 } else {
717 mesh.instantiate();
718 }
719
720 int varr_len = vertex_array.size();
721
722 if (varr_len == 0) {
723 return mesh;
724 }
725
726 int surface = mesh->get_surface_count();
727
728 Array a = commit_to_arrays();
729
730 uint64_t compress_flags = (p_compress_flags >> RS::ARRAY_COMPRESS_FLAGS_BASE) << RS::ARRAY_COMPRESS_FLAGS_BASE;
731 static const uint64_t shift[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM1_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM2_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM3_SHIFT };
732 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
733 if (last_custom_format[i] != CUSTOM_MAX) {
734 compress_flags |= uint64_t(last_custom_format[i]) << shift[i];
735 }
736 }
737
738 mesh->add_surface_from_arrays(primitive, a, Array(), Dictionary(), compress_flags);
739
740 if (material.is_valid()) {
741 mesh->surface_set_material(surface, material);
742 }
743
744 return mesh;
745 }
746
747 void SurfaceTool::index() {
748 if (index_array.size()) {
749 return; //already indexed
750 }
751
752 AHashMap<Vertex &, int, VertexHasher> indices = vertex_array.size();
753
754 uint32_t new_size = 0;
755 for (Vertex &vertex : vertex_array) {
756 int *idxptr = indices.getptr(vertex);
757 int idx;
758 if (!idxptr) {
759 idx = indices.size();
760 vertex_array[new_size] = vertex;
761 indices.insert_new(vertex_array[new_size], idx);
762 new_size++;
763 } else {
764 idx = *idxptr;
765 }
766
767 index_array.push_back(idx);
768 }
769
770 vertex_array.resize(new_size);
771 format |= Mesh::ARRAY_FORMAT_INDEX;
772 }
773
774 void SurfaceTool::deindex() {
775 if (index_array.is_empty()) {
776 return; //nothing to deindex
777 }
778
779 LocalVector<Vertex> old_vertex_array = vertex_array;
780 vertex_array.clear();
781 for (const int &index : index_array) {
782 ERR_FAIL_COND(uint32_t(index) >= old_vertex_array.size());
783 vertex_array.push_back(old_vertex_array[index]);
784 }
785 format &= ~Mesh::ARRAY_FORMAT_INDEX;
786 index_array.clear();
787 }
788
789 void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat) {
790 ERR_FAIL_COND_MSG(p_existing.is_null(), "First argument in SurfaceTool::_create_list() must be a valid object of type Mesh");
791
792 Array arr = p_existing->surface_get_arrays(p_surface);
793 ERR_FAIL_COND(arr.size() != RS::ARRAY_MAX);
794 _create_list_from_arrays(arr, r_vertex, r_index, lformat);
795 }
796
797 const uint32_t SurfaceTool::custom_mask[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0, Mesh::ARRAY_FORMAT_CUSTOM1, Mesh::ARRAY_FORMAT_CUSTOM2, Mesh::ARRAY_FORMAT_CUSTOM3 };
798 const uint32_t SurfaceTool::custom_shift[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM1_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM2_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM3_SHIFT };
799
800 void SurfaceTool::create_vertex_array_from_arrays(const Array &p_arrays, LocalVector<SurfaceTool::Vertex> &ret, uint64_t *r_format) {
801 ERR_FAIL_INDEX(RS::ARRAY_WEIGHTS, p_arrays.size());
802
803 ret.clear();
804
805 Vector<Vector3> varr = p_arrays[RS::ARRAY_VERTEX];
806 Vector<Vector3> narr = p_arrays[RS::ARRAY_NORMAL];
807 Vector<float> tarr = p_arrays[RS::ARRAY_TANGENT];
808 Vector<Color> carr = p_arrays[RS::ARRAY_COLOR];
809 Vector<Vector2> uvarr = p_arrays[RS::ARRAY_TEX_UV];
810 Vector<Vector2> uv2arr = p_arrays[RS::ARRAY_TEX_UV2];
811 Vector<int> barr = p_arrays[RS::ARRAY_BONES];
812 Vector<float> warr = p_arrays[RS::ARRAY_WEIGHTS];
813 Vector<float> custom_float[RS::ARRAY_CUSTOM_COUNT];
814
815 int vc = varr.size();
816 if (vc == 0) {
817 if (r_format) {
818 *r_format = 0;
819 }
820 return;
821 }
822
823 uint64_t lformat = 0;
824 if (varr.size()) {
825 lformat |= RS::ARRAY_FORMAT_VERTEX;
826 }
827 if (narr.size()) {
828 lformat |= RS::ARRAY_FORMAT_NORMAL;
829 }
830 if (tarr.size()) {
831 lformat |= RS::ARRAY_FORMAT_TANGENT;
832 }
833 if (carr.size()) {
834 lformat |= RS::ARRAY_FORMAT_COLOR;
835 }
836 if (uvarr.size()) {
837 lformat |= RS::ARRAY_FORMAT_TEX_UV;
838 }
839 if (uv2arr.size()) {
840 lformat |= RS::ARRAY_FORMAT_TEX_UV2;
841 }
842 int wcount = 0;
843 if (barr.size() && warr.size()) {
844 lformat |= RS::ARRAY_FORMAT_BONES;
845 lformat |= RS::ARRAY_FORMAT_WEIGHTS;
846
847 wcount = barr.size() / varr.size();
848 if (wcount == 8) {
849 lformat |= RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
850 }
851 }
852
853 if (warr.size()) {
854 lformat |= RS::ARRAY_FORMAT_WEIGHTS;
855 }
856
857 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
858 ERR_CONTINUE_MSG(p_arrays[RS::ARRAY_CUSTOM0 + i].get_type() == Variant::PACKED_BYTE_ARRAY, "Extracting Byte/Half formats is not supported");
859 if (p_arrays[RS::ARRAY_CUSTOM0 + i].get_type() == Variant::PACKED_FLOAT32_ARRAY) {
860 lformat |= custom_mask[i];
861 custom_float[i] = p_arrays[RS::ARRAY_CUSTOM0 + i];
862 int fmt = custom_float[i].size() / varr.size();
863 if (fmt == 1) {
864 lformat |= CUSTOM_R_FLOAT << custom_shift[i];
865 } else if (fmt == 2) {
866 lformat |= CUSTOM_RG_FLOAT << custom_shift[i];
867 } else if (fmt == 3) {
868 lformat |= CUSTOM_RGB_FLOAT << custom_shift[i];
869 } else if (fmt == 4) {
870 lformat |= CUSTOM_RGBA_FLOAT << custom_shift[i];
871 }
872 }
873 }
874
875 for (int i = 0; i < vc; i++) {
876 Vertex v;
877 if (lformat & RS::ARRAY_FORMAT_VERTEX) {
878 v.vertex = varr[i];
879 }
880 if (lformat & RS::ARRAY_FORMAT_NORMAL) {
881 v.normal = narr[i];
882 }
883 if (lformat & RS::ARRAY_FORMAT_TANGENT) {
884 v.tangent = Vector3(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2]);
885 float d = tarr[i * 4 + 3];
886 v.binormal = v.normal.cross(v.tangent).normalized() * d;
887 }
888 if (lformat & RS::ARRAY_FORMAT_COLOR) {
889 v.color = carr[i];
890 }
891 if (lformat & RS::ARRAY_FORMAT_TEX_UV) {
892 v.uv = uvarr[i];
893 }
894 if (lformat & RS::ARRAY_FORMAT_TEX_UV2) {
895 v.uv2 = uv2arr[i];
896 }
897 if (lformat & RS::ARRAY_FORMAT_BONES) {
898 Vector<int> b;
899 b.resize(wcount);
900 for (int j = 0; j < wcount; j++) {
901 b.write[j] = barr[i * wcount + j];
902 }
903 v.bones = b;
904 }
905 if (lformat & RS::ARRAY_FORMAT_WEIGHTS) {
906 Vector<float> w;
907 w.resize(wcount);
908 for (int j = 0; j < wcount; j++) {
909 w.write[j] = warr[i * wcount + j];
910 }
911 v.weights = w;
912 }
913
914 for (int j = 0; j < RS::ARRAY_CUSTOM_COUNT; j++) {
915 if (lformat & custom_mask[j]) {
916 int cc = custom_float[j].size() / varr.size();
917 for (int k = 0; k < cc; k++) {
918 v.custom[j][k] = custom_float[j][i * cc + k];
919 }
920 }
921 }
922
923 ret.push_back(v);
924 }
925
926 if (r_format) {
927 *r_format = lformat;
928 }
929 }
930
931 void SurfaceTool::_create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat) {
932 create_vertex_array_from_arrays(arr, *r_vertex, &lformat);
933 ERR_FAIL_COND(r_vertex->size() == 0);
934
935 //indices
936 r_index->clear();
937
938 Vector<int> idx = arr[RS::ARRAY_INDEX];
939 int is = idx.size();
940 if (is) {
941 lformat |= RS::ARRAY_FORMAT_INDEX;
942 const int *iarr = idx.ptr();
943 for (int i = 0; i < is; i++) {
944 r_index->push_back(iarr[i]);
945 }
946 }
947 }
948
949 void SurfaceTool::create_from_arrays(const Array &p_arrays, Mesh::PrimitiveType p_primitive_type) {
950 clear();
951 primitive = p_primitive_type;
952 _create_list_from_arrays(p_arrays, &vertex_array, &index_array, format);
953
954 for (int j = 0; j < RS::ARRAY_CUSTOM_COUNT; j++) {
955 if (format & custom_mask[j]) {
956 last_custom_format[j] = (CustomFormat)((format >> custom_shift[j]) & RS::ARRAY_FORMAT_CUSTOM_MASK);
957 }
958 }
959 }
960
961 void SurfaceTool::create_from_triangle_arrays(const Array &p_arrays) {
962 create_from_arrays(p_arrays, Mesh::PRIMITIVE_TRIANGLES);
963 }
964
965 void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
966 ERR_FAIL_COND_MSG(p_existing.is_null(), "First argument in SurfaceTool::create_from() must be a valid object of type Mesh");
967
968 clear();
969 primitive = p_existing->surface_get_primitive_type(p_surface);
970 _create_list(p_existing, p_surface, &vertex_array, &index_array, format);
971 material = p_existing->surface_get_material(p_surface);
972
973 for (int j = 0; j < RS::ARRAY_CUSTOM_COUNT; j++) {
974 if (format & custom_mask[j]) {
975 last_custom_format[j] = (CustomFormat)((format >> custom_shift[j]) & RS::ARRAY_FORMAT_CUSTOM_MASK);
976 }
977 }
978 }
979
980 void SurfaceTool::create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name) {
981 ERR_FAIL_COND_MSG(p_existing.is_null(), "First argument in SurfaceTool::create_from_blend_shape() must be a valid object of type Mesh");
982
983 clear();
984 primitive = p_existing->surface_get_primitive_type(p_surface);
985 Array arr = p_existing->surface_get_blend_shape_arrays(p_surface);
986 Array blend_shape_names;
987 int32_t shape_idx = -1;
988 for (int32_t i = 0; i < p_existing->get_blend_shape_count(); i++) {
989 String name = p_existing->get_blend_shape_name(i);
990 if (name == p_blend_shape_name) {
991 shape_idx = i;
992 break;
993 }
994 }
995 ERR_FAIL_COND(shape_idx == -1);
996 ERR_FAIL_COND(shape_idx >= arr.size());
997 Array blendshape_mesh_arrays = arr[shape_idx];
998 ERR_FAIL_COND(blendshape_mesh_arrays.size() != RS::ARRAY_MAX);
999
1000 Array source_mesh_arrays = p_existing->surface_get_arrays(p_surface);
1001 ERR_FAIL_COND(source_mesh_arrays.size() != RS::ARRAY_MAX);
1002
1003 // Copy BlendShape vertex data over while keeping e.g. bones, weights, index from existing mesh intact.
1004 source_mesh_arrays[RS::ARRAY_VERTEX] = blendshape_mesh_arrays[RS::ARRAY_VERTEX];
1005 source_mesh_arrays[RS::ARRAY_NORMAL] = blendshape_mesh_arrays[RS::ARRAY_NORMAL];
1006 source_mesh_arrays[RS::ARRAY_TANGENT] = blendshape_mesh_arrays[RS::ARRAY_TANGENT];
1007
1008 _create_list_from_arrays(source_mesh_arrays, &vertex_array, &index_array, format);
1009
1010 material = p_existing->surface_get_material(p_surface);
1011 format = p_existing->surface_get_format(p_surface);
1012
1013 for (int j = 0; j < RS::ARRAY_CUSTOM_COUNT; j++) {
1014 if (format & custom_mask[j]) {
1015 last_custom_format[j] = (CustomFormat)((format >> custom_shift[j]) & RS::ARRAY_FORMAT_CUSTOM_MASK);
1016 }
1017 }
1018 }
1019
1020 void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform) {
1021 ERR_FAIL_COND_MSG(p_existing.is_null(), "First argument in SurfaceTool::append_from() must be a valid object of type Mesh");
1022
1023 if (vertex_array.is_empty()) {
1024 primitive = p_existing->surface_get_primitive_type(p_surface);
1025 format = 0;
1026 }
1027
1028 uint64_t nformat = 0;
1029 LocalVector<Vertex> nvertices;
1030 LocalVector<int> nindices;
1031 _create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
1032 format |= nformat;
1033
1034 for (int j = 0; j < RS::ARRAY_CUSTOM_COUNT; j++) {
1035 if (format & custom_mask[j]) {
1036 CustomFormat new_format = (CustomFormat)((format >> custom_shift[j]) & RS::ARRAY_FORMAT_CUSTOM_MASK);
1037 last_custom_format[j] = new_format;
1038 }
1039 }
1040 int vfrom = vertex_array.size();
1041
1042 for (Vertex &v : nvertices) {
1043 v.vertex = p_xform.xform(v.vertex);
1044 if (nformat & RS::ARRAY_FORMAT_NORMAL) {
1045 v.normal = p_xform.basis.xform(v.normal);
1046 }
1047 if (nformat & RS::ARRAY_FORMAT_TANGENT) {
1048 v.tangent = p_xform.basis.xform(v.tangent);
1049 v.binormal = p_xform.basis.xform(v.binormal);
1050 }
1051
1052 vertex_array.push_back(v);
1053 }
1054
1055 for (const int &index : nindices) {
1056 int dst_index = index + vfrom;
1057 index_array.push_back(dst_index);
1058 }
1059 if (index_array.size() % 3) {
1060 WARN_PRINT("SurfaceTool: Index array not a multiple of 3.");
1061 }
1062 }
1063
1064 //mikktspace callbacks
1065 namespace {
1066 struct TangentGenerationContextUserData {
1067 LocalVector<SurfaceTool::Vertex> *vertices;
1068 LocalVector<int> *indices;
1069 };
1070 } // namespace
1071
1072 int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
1073 TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
1074
1075 if (triangle_data.indices->size() > 0) {
1076 return triangle_data.indices->size() / 3;
1077 } else {
1078 return triangle_data.vertices->size() / 3;
1079 }
1080 }
1081
1082 int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
1083 return 3; //always 3
1084 }
1085
1086 void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
1087 TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
1088 Vector3 v;
1089 if (triangle_data.indices->size() > 0) {
1090 uint32_t index = triangle_data.indices->operator[](iFace * 3 + iVert);
1091 if (index < triangle_data.vertices->size()) {
1092 v = triangle_data.vertices->operator[](index).vertex;
1093 }
1094 } else {
1095 v = triangle_data.vertices->operator[](iFace * 3 + iVert).vertex;
1096 }
1097
1098 fvPosOut[0] = v.x;
1099 fvPosOut[1] = v.y;
1100 fvPosOut[2] = v.z;
1101 }
1102
1103 void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
1104 TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
1105 Vector3 v;
1106 if (triangle_data.indices->size() > 0) {
1107 uint32_t index = triangle_data.indices->operator[](iFace * 3 + iVert);
1108 if (index < triangle_data.vertices->size()) {
1109 v = triangle_data.vertices->operator[](index).normal;
1110 }
1111 } else {
1112 v = triangle_data.vertices->operator[](iFace * 3 + iVert).normal;
1113 }
1114
1115 fvNormOut[0] = v.x;
1116 fvNormOut[1] = v.y;
1117 fvNormOut[2] = v.z;
1118 }
1119
1120 void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
1121 TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
1122 Vector2 v;
1123 if (triangle_data.indices->size() > 0) {
1124 uint32_t index = triangle_data.indices->operator[](iFace * 3 + iVert);
1125 if (index < triangle_data.vertices->size()) {
1126 v = triangle_data.vertices->operator[](index).uv;
1127 }
1128 } else {
1129 v = triangle_data.vertices->operator[](iFace * 3 + iVert).uv;
1130 }
1131
1132 fvTexcOut[0] = v.x;
1133 fvTexcOut[1] = v.y;
1134 }
1135
1136 void SurfaceTool::mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
1137 const tbool bIsOrientationPreserving, const int iFace, const int iVert) {
1138 TangentGenerationContextUserData &triangle_data = *reinterpret_cast<TangentGenerationContextUserData *>(pContext->m_pUserData);
1139 Vertex *vtx = nullptr;
1140 if (triangle_data.indices->size() > 0) {
1141 uint32_t index = triangle_data.indices->operator[](iFace * 3 + iVert);
1142 if (index < triangle_data.vertices->size()) {
1143 vtx = &triangle_data.vertices->operator[](index);
1144 }
1145 } else {
1146 vtx = &triangle_data.vertices->operator[](iFace * 3 + iVert);
1147 }
1148
1149 if (vtx != nullptr) {
1150 vtx->tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
1151 vtx->binormal = Vector3(-fvBiTangent[0], -fvBiTangent[1], -fvBiTangent[2]); // for some reason these are reversed, something with the coordinate system in Godot
1152 }
1153 }
1154
1155 void SurfaceTool::generate_tangents() {
1156 ERR_FAIL_COND_MSG(!(format & Mesh::ARRAY_FORMAT_TEX_UV), "UVs are required to generate tangents.");
1157 ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
1158
1159 SMikkTSpaceInterface mkif;
1160 mkif.m_getNormal = mikktGetNormal;
1161 mkif.m_getNumFaces = mikktGetNumFaces;
1162 mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
1163 mkif.m_getPosition = mikktGetPosition;
1164 mkif.m_getTexCoord = mikktGetTexCoord;
1165 mkif.m_setTSpace = mikktSetTSpaceDefault;
1166 mkif.m_setTSpaceBasic = nullptr;
1167
1168 SMikkTSpaceContext msc;
1169 msc.m_pInterface = &mkif;
1170
1171 TangentGenerationContextUserData triangle_data;
1172 triangle_data.vertices = &vertex_array;
1173 for (Vertex &vertex : vertex_array) {
1174 vertex.binormal = Vector3();
1175 vertex.tangent = Vector3();
1176 }
1177 triangle_data.indices = &index_array;
1178 msc.m_pUserData = &triangle_data;
1179
1180 bool res = genTangSpaceDefault(&msc);
1181
1182 ERR_FAIL_COND(!res);
1183 format |= Mesh::ARRAY_FORMAT_TANGENT;
1184 }
1185
1186 void SurfaceTool::generate_normals(bool p_flip) {
1187 ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
1188
1189 bool was_indexed = index_array.size();
1190
1191 deindex();
1192
1193 ERR_FAIL_COND((vertex_array.size() % 3) != 0);
1194
1195 AHashMap<SmoothGroupVertex, Vector3, SmoothGroupVertexHasher> smooth_hash = vertex_array.size();
1196
1197 for (uint32_t vi = 0; vi < vertex_array.size(); vi += 3) {
1198 Vertex *v = &vertex_array[vi];
1199
1200 Vector3 normal;
1201 if (!p_flip) {
1202 normal = Plane(v[0].vertex, v[1].vertex, v[2].vertex).normal;
1203 } else {
1204 normal = Plane(v[2].vertex, v[1].vertex, v[0].vertex).normal;
1205 }
1206
1207 for (int i = 0; i < 3; i++) {
1208 // Add face normal to smooth vertex influence if vertex is member of a smoothing group
1209 if (v[i].smooth_group != UINT32_MAX) {
1210 Vector3 *lv = smooth_hash.getptr(v[i]);
1211 if (!lv) {
1212 smooth_hash.insert_new(v[i], normal);
1213 } else {
1214 (*lv) += normal;
1215 }
1216 } else {
1217 v[i].normal = normal;
1218 }
1219 }
1220 }
1221
1222 for (Vertex &vertex : vertex_array) {
1223 if (vertex.smooth_group != UINT32_MAX) {
1224 Vector3 *lv = smooth_hash.getptr(vertex);
1225 if (!lv) {
1226 vertex.normal = Vector3();
1227 } else {
1228 vertex.normal = lv->normalized();
1229 }
1230 }
1231 }
1232
1233 format |= Mesh::ARRAY_FORMAT_NORMAL;
1234
1235 if (was_indexed) {
1236 index();
1237 }
1238 }
1239
1240 void SurfaceTool::set_material(const Ref<Material> &p_material) {
1241 material = p_material;
1242 }
1243
1244 Ref<Material> SurfaceTool::get_material() const {
1245 return material;
1246 }
1247
1248 void SurfaceTool::clear() {
1249 begun = false;
1250 primitive = Mesh::PRIMITIVE_LINES;
1251 format = 0;
1252 last_bones.clear();
1253 last_weights.clear();
1254 index_array.clear();
1255 vertex_array.clear();
1256 material.unref();
1257 last_smooth_group = 0;
1258 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
1259 last_custom_format[i] = CUSTOM_MAX;
1260 }
1261 skin_weights = SKIN_4_WEIGHTS;
1262 }
1263
1264 void SurfaceTool::set_skin_weight_count(SkinWeightCount p_weights) {
1265 ERR_FAIL_COND(begun);
1266 skin_weights = p_weights;
1267 }
1268 SurfaceTool::SkinWeightCount SurfaceTool::get_skin_weight_count() const {
1269 return skin_weights;
1270 }
1271
1272 void SurfaceTool::set_custom_format(int p_channel_index, CustomFormat p_format) {
1273 ERR_FAIL_INDEX(p_channel_index, RS::ARRAY_CUSTOM_COUNT);
1274 ERR_FAIL_COND(!begun);
1275 ERR_FAIL_INDEX(p_format, CUSTOM_MAX + 1);
1276 last_custom_format[p_channel_index] = p_format;
1277 }
1278
1279 Mesh::PrimitiveType SurfaceTool::get_primitive_type() const {
1280 return primitive;
1281 }
1282 SurfaceTool::CustomFormat SurfaceTool::get_custom_format(int p_channel_index) const {
1283 ERR_FAIL_INDEX_V(p_channel_index, RS::ARRAY_CUSTOM_COUNT, CUSTOM_MAX);
1284 return last_custom_format[p_channel_index];
1285 }
1286 void SurfaceTool::optimize_indices_for_cache() {
1287 ERR_FAIL_NULL(optimize_vertex_cache_func);
1288 ERR_FAIL_COND(index_array.is_empty());
1289 ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
1290 ERR_FAIL_COND(index_array.size() % 3 != 0);
1291
1292 LocalVector old_index_array = index_array;
1293 memset(index_array.ptr(), 0, index_array.size() * sizeof(int));
1294 optimize_vertex_cache_func((unsigned int *)index_array.ptr(), (unsigned int *)old_index_array.ptr(), old_index_array.size(), vertex_array.size());
1295 }
1296
1297 AABB SurfaceTool::get_aabb() const {
1298 ERR_FAIL_COND_V(vertex_array.is_empty(), AABB());
1299
1300 AABB aabb;
1301 for (uint32_t i = 0; i < vertex_array.size(); i++) {
1302 if (i == 0) {
1303 aabb.position = vertex_array[i].vertex;
1304 } else {
1305 aabb.expand_to(vertex_array[i].vertex);
1306 }
1307 }
1308
1309 return aabb;
1310 }
1311 Vector<int> SurfaceTool::generate_lod(float p_threshold, int p_target_index_count) {
1312 WARN_DEPRECATED_MSG(R"*(The "SurfaceTool.generate_lod()" method is deprecated. Consider using "ImporterMesh.generate_lods()" instead.)*");
1313
1314 Vector<int> lod;
1315
1316 ERR_FAIL_NULL_V(simplify_func, lod);
1317 ERR_FAIL_COND_V(p_target_index_count < 0, lod);
1318 ERR_FAIL_COND_V(vertex_array.is_empty(), lod);
1319 ERR_FAIL_COND_V(index_array.is_empty(), lod);
1320 ERR_FAIL_COND_V(index_array.size() % 3 != 0, lod);
1321 ERR_FAIL_COND_V(index_array.size() < (unsigned int)p_target_index_count, lod);
1322
1323 lod.resize(index_array.size());
1324 LocalVector<float> vertices; //uses floats
1325 vertices.resize(vertex_array.size() * 3);
1326 for (uint32_t i = 0; i < vertex_array.size(); i++) {
1327 vertices[i * 3 + 0] = vertex_array[i].vertex.x;
1328 vertices[i * 3 + 1] = vertex_array[i].vertex.y;
1329 vertices[i * 3 + 2] = vertex_array[i].vertex.z;
1330 }
1331
1332 float error;
1333 const int simplify_options = SIMPLIFY_LOCK_BORDER;
1334 uint32_t index_count = simplify_func((unsigned int *)lod.ptrw(), (unsigned int *)index_array.ptr(), index_array.size(), vertices.ptr(), vertex_array.size(), sizeof(float) * 3, p_target_index_count, p_threshold, simplify_options, &error);
1335 ERR_FAIL_COND_V(index_count == 0, lod);
1336 lod.resize(index_count);
1337
1338 return lod;
1339 }
1340
1341 void SurfaceTool::_bind_methods() {
1342 ClassDB::bind_method(D_METHOD("set_skin_weight_count", "count"), &SurfaceTool::set_skin_weight_count);
1343 ClassDB::bind_method(D_METHOD("get_skin_weight_count"), &SurfaceTool::get_skin_weight_count);
1344
1345 ClassDB::bind_method(D_METHOD("set_custom_format", "channel_index", "format"), &SurfaceTool::set_custom_format);
1346 ClassDB::bind_method(D_METHOD("get_custom_format", "channel_index"), &SurfaceTool::get_custom_format);
1347
1348 ClassDB::bind_method(D_METHOD("begin", "primitive"), &SurfaceTool::begin);
1349
1350 ClassDB::bind_method(D_METHOD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
1351 ClassDB::bind_method(D_METHOD("set_color", "color"), &SurfaceTool::set_color);
1352 ClassDB::bind_method(D_METHOD("set_normal", "normal"), &SurfaceTool::set_normal);
1353 ClassDB::bind_method(D_METHOD("set_tangent", "tangent"), &SurfaceTool::set_tangent);
1354 ClassDB::bind_method(D_METHOD("set_uv", "uv"), &SurfaceTool::set_uv);
1355 ClassDB::bind_method(D_METHOD("set_uv2", "uv2"), &SurfaceTool::set_uv2);
1356 ClassDB::bind_method(D_METHOD("set_bones", "bones"), &SurfaceTool::set_bones);
1357 ClassDB::bind_method(D_METHOD("set_weights", "weights"), &SurfaceTool::set_weights);
1358 ClassDB::bind_method(D_METHOD("set_custom", "channel_index", "custom_color"), &SurfaceTool::set_custom);
1359 ClassDB::bind_method(D_METHOD("set_smooth_group", "index"), &SurfaceTool::set_smooth_group);
1360
1361 ClassDB::bind_method(D_METHOD("add_triangle_fan", "vertices", "uvs", "colors", "uv2s", "normals", "tangents"), &SurfaceTool::_add_triangle_fan, DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Color>()), DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Vector3>()), DEFVAL(TypedArray<Plane>()));
1362
1363 ClassDB::bind_method(D_METHOD("add_index", "index"), &SurfaceTool::add_index);
1364
1365 ClassDB::bind_method(D_METHOD("index"), &SurfaceTool::index);
1366 ClassDB::bind_method(D_METHOD("deindex"), &SurfaceTool::deindex);
1367 ClassDB::bind_method(D_METHOD("generate_normals", "flip"), &SurfaceTool::generate_normals, DEFVAL(false));
1368 ClassDB::bind_method(D_METHOD("generate_tangents"), &SurfaceTool::generate_tangents);
1369
1370 ClassDB::bind_method(D_METHOD("optimize_indices_for_cache"), &SurfaceTool::optimize_indices_for_cache);
1371
1372 ClassDB::bind_method(D_METHOD("get_aabb"), &SurfaceTool::get_aabb);
1373 ClassDB::bind_method(D_METHOD("generate_lod", "nd_threshold", "target_index_count"), &SurfaceTool::generate_lod, DEFVAL(3));
1374
1375 ClassDB::bind_method(D_METHOD("set_material", "material"), &SurfaceTool::set_material);
1376 ClassDB::bind_method(D_METHOD("get_primitive_type"), &SurfaceTool::get_primitive_type);
1377
1378 ClassDB::bind_method(D_METHOD("clear"), &SurfaceTool::clear);
1379
1380 ClassDB::bind_method(D_METHOD("create_from", "existing", "surface"), &SurfaceTool::create_from);
1381 ClassDB::bind_method(D_METHOD("create_from_arrays", "arrays", "primitive_type"), &SurfaceTool::create_from_arrays, DEFVAL(Mesh::PRIMITIVE_TRIANGLES));
1382 ClassDB::bind_method(D_METHOD("create_from_blend_shape", "existing", "surface", "blend_shape"), &SurfaceTool::create_from_blend_shape);
1383 ClassDB::bind_method(D_METHOD("append_from", "existing", "surface", "transform"), &SurfaceTool::append_from);
1384 ClassDB::bind_method(D_METHOD("commit", "existing", "flags"), &SurfaceTool::commit, DEFVAL(Variant()), DEFVAL(0));
1385 ClassDB::bind_method(D_METHOD("commit_to_arrays"), &SurfaceTool::commit_to_arrays);
1386
1387 BIND_ENUM_CONSTANT(CUSTOM_RGBA8_UNORM);
1388 BIND_ENUM_CONSTANT(CUSTOM_RGBA8_SNORM);
1389 BIND_ENUM_CONSTANT(CUSTOM_RG_HALF);
1390 BIND_ENUM_CONSTANT(CUSTOM_RGBA_HALF);
1391 BIND_ENUM_CONSTANT(CUSTOM_R_FLOAT);
1392 BIND_ENUM_CONSTANT(CUSTOM_RG_FLOAT);
1393 BIND_ENUM_CONSTANT(CUSTOM_RGB_FLOAT);
1394 BIND_ENUM_CONSTANT(CUSTOM_RGBA_FLOAT);
1395 BIND_ENUM_CONSTANT(CUSTOM_MAX);
1396 BIND_ENUM_CONSTANT(SKIN_4_WEIGHTS);
1397 BIND_ENUM_CONSTANT(SKIN_8_WEIGHTS);
1398 }
1399
1400 SurfaceTool::SurfaceTool() {
1401 for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
1402 last_custom_format[i] = CUSTOM_MAX;
1403 }
1404 }