Blender V5.3
scene/mesh.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <algorithm>
6
7#include "bvh/build.h"
8#include "bvh/bvh.h"
9
10#include "device/device.h"
11
12#include "scene/attribute.h"
13#include "scene/mesh.h"
14#include "scene/object.h"
15#include "scene/scene.h"
16#include "scene/shader_graph.h"
17
18#include "subd/split.h"
19
20#include "util/log.h"
21#include "util/set.h"
22
23#include "mikktspace.hh"
24
26
27/* Tangent Space */
28
45
47 {
48 return mesh->num_triangles();
49 }
50
51 int GetNumVerticesOfFace(const int /*face_num*/)
52 {
53 return 3;
54 }
55
56 int CornerIndex(const int face_num, const int vert_num)
57 {
58 return face_num * 3 + vert_num;
59 }
60
61 int VertexIndex(const int face_num, const int vert_num)
62 {
63 const int corner = CornerIndex(face_num, vert_num);
64 return mesh->get_triangles()[corner];
65 }
66
67 mikk::float3 GetPosition(const int face_num, const int vert_num)
68 {
69 const float3 vP = float3(position[VertexIndex(face_num, vert_num)]);
70 return mikk::float3(vP.x, vP.y, vP.z);
71 }
72
73 mikk::float3 GetTexCoord(const int face_num, const int vert_num)
74 {
75 /* TODO: Check whether introducing a template boolean in order to
76 * turn this into a constexpr is worth it. */
77 if (has_uv()) {
78 const int corner_index = CornerIndex(face_num, vert_num);
79 const float2 tfuv = uv[corner_index];
80 return mikk::float3(tfuv.x, tfuv.y, 1.0f);
81 }
82 /* revert to vertex position */
83 const float3 vP = float3(position[VertexIndex(face_num, vert_num)]);
84 const float2 uv = map_to_sphere(vP);
85 return mikk::float3(uv.x, uv.y, 1.0f);
86 }
87
88 mikk::float3 GetNormal(const int face_num, const int vert_num)
89 {
90 float3 vN;
91 if (mesh->get_smooth()[face_num]) {
92 vN = ((corner_normal) ? corner_normal[CornerIndex(face_num, vert_num)] :
93 vertex_normal[VertexIndex(face_num, vert_num)])
94 .decode();
95 }
96 else {
97 const Mesh::Triangle tri = mesh->get_triangle(face_num);
98 vN = tri.compute_normal(position);
99 }
100 return mikk::float3(vN.x, vN.y, vN.z);
101 }
102
103 void SetTangentSpace(const int face_num, const int vert_num, mikk::float3 T, bool orientation)
104 {
105 const int corner_index = CornerIndex(face_num, vert_num);
106 tangent[corner_index] = packed_float3(make_float3(T.x, T.y, T.z));
107 if (tangent_sign != nullptr) {
108 tangent_sign[corner_index] = orientation ? 1.0f : -1.0f;
109 }
110 }
111
112 bool has_uv() const
113 {
114 return uv != nullptr;
115 }
116
117 const Mesh *mesh;
119
122 const float2 *uv;
123
126};
127
128static void mikk_compute_tangents(Attribute *attr_uv,
129 Mesh *mesh,
130 const bool need_sign,
131 const AttributeStandard tangent_std,
132 const AttributeStandard tangent_sign_std,
133 const char *tangent_postfix,
134 const char *tangent_sign_postfix)
135{
136 /* Create tangent attributes. */
137 AttributeSet &attributes = mesh->attributes;
138
139 Attribute *attr_vN = attributes.find(ATTR_STD_VERTEX_NORMAL);
140 Attribute *attr_cN = attributes.find(ATTR_STD_CORNER_NORMAL);
141 if (attr_vN == nullptr && attr_cN == nullptr) {
142 /* no normals */
143 return;
144 }
145
146 const packed_normal *vertex_normal = attr_vN ? attr_vN->data<packed_normal>() : nullptr;
147 const packed_normal *corner_normal = attr_cN ? attr_cN->data<packed_normal>() : nullptr;
148 const float2 *uv = (attr_uv) ? attr_uv->data<float2>() : nullptr;
149
150 const ustring name = ustring((attr_uv) ? attr_uv->name.string() + tangent_postfix :
151 Attribute::standard_name(tangent_std));
152 Attribute *attr;
153 if (attr_uv == nullptr || attr_uv->std == ATTR_STD_UV) {
154 attr = attributes.add(tangent_std, name);
155 }
156 else {
157 attr = attributes.add(name, TypeVector, ATTR_ELEMENT_CORNER);
158 }
159 packed_float3 *tangent = attr->data_for_write<packed_float3>();
160 /* Create bitangent sign attribute. */
161 float *tangent_sign = nullptr;
162 if (need_sign) {
163 const ustring name_sign = ustring((attr_uv) ? attr_uv->name.string() + tangent_sign_postfix :
164 Attribute::standard_name(tangent_sign_std));
165 Attribute *attr_sign;
166 if (attr_uv == nullptr || attr_uv->std == ATTR_STD_UV) {
167 attr_sign = attributes.add(tangent_sign_std, name_sign);
168 }
169 else {
170 attr_sign = attributes.add(name_sign, TypeFloat, ATTR_ELEMENT_CORNER);
171 }
172 tangent_sign = attr_sign->data_for_write<float>();
173 }
174
175 MikkMeshWrapper userdata(mesh, vertex_normal, corner_normal, uv, tangent, tangent_sign);
176 /* Compute tangents. */
177 mikk::Mikktspace(userdata).genTangSpace();
178}
179
180/* Triangle */
181
183{
184 bounds.grow(verts[v[0]]);
185 bounds.grow(verts[v[1]]);
186 bounds.grow(verts[v[2]]);
187}
188
190 const size_t num_steps,
191 const float time,
192 float3 r_verts[3]) const
193{
194 /* Figure out which steps we need to fetch and their interpolation factor. */
195 const size_t max_step = num_steps - 1;
196 const size_t step = min((size_t)(time * max_step), max_step - 1);
197 const float t = time * max_step - step;
198 /* Fetch vertex coordinates. */
199 float3 curr_verts[3];
200 float3 next_verts[3];
201 verts_for_step(attr_P, step, curr_verts);
202 verts_for_step(attr_P, step + 1, next_verts);
203 /* Interpolate between steps. */
204 r_verts[0] = (1.0f - t) * curr_verts[0] + t * next_verts[0];
205 r_verts[1] = (1.0f - t) * curr_verts[1] + t * next_verts[1];
206 r_verts[2] = (1.0f - t) * curr_verts[2] + t * next_verts[2];
207}
208
210 const size_t step,
211 float3 r_verts[3]) const
212{
213 const packed_float3 *vert_step = attr_P->data_at_time_step<packed_float3>(
214 step, attr_P->num_motion_steps());
215 r_verts[0] = vert_step[v[0]];
216 r_verts[1] = vert_step[v[1]];
217 r_verts[2] = vert_step[v[2]];
218}
219
221{
222 const float3 v0 = verts[v[0]];
223 const float3 v1 = verts[v[1]];
224 const float3 v2 = verts[v[2]];
225 const float3 norm = cross(v1 - v0, v2 - v0);
226 const float normlen = len(norm);
227 if (normlen == 0.0f) {
228 return make_float3(1.0f, 0.0f, 0.0f);
229 }
230 return norm / normlen;
231}
232
233bool Mesh::Triangle::valid(const packed_float3 *verts) const
234{
235 return isfinite_safe(float3(verts[v[0]])) && isfinite_safe(float3(verts[v[1]])) &&
236 isfinite_safe(float3(verts[v[2]]));
237}
238
239/* SubdFace */
240
242{
243 const packed_float3 *verts =
245 const float3 v0 = verts[mesh->subd_face_corners[start_corner + 0]];
246 const float3 v1 = verts[mesh->subd_face_corners[start_corner + 1]];
247 const float3 v2 = verts[mesh->subd_face_corners[start_corner + 2]];
248
249 return safe_normalize(cross(v1 - v0, v2 - v0));
250}
251
252size_t Mesh::num_verts() const
253{
255 return attr ? attr->size : 0;
256}
257
258/* Mesh */
259
261{
262 NodeType *type = NodeType::add("mesh", create, NodeType::NONE, Geometry::get_node_base_type());
263
264 SOCKET_INT_ARRAY(triangles, "Triangles", array<int>());
265 SOCKET_INT_ARRAY(shader, "Shader", array<int>());
267
268 static NodeEnum subdivision_type_enum;
269 subdivision_type_enum.insert("none", SUBDIVISION_NONE);
270 subdivision_type_enum.insert("linear", SUBDIVISION_LINEAR);
271 subdivision_type_enum.insert("catmull_clark", SUBDIVISION_CATMULL_CLARK);
272 SOCKET_ENUM(subdivision_type, "Subdivision Type", subdivision_type_enum, SUBDIVISION_NONE);
273
274 static NodeEnum subdivision_boundary_interpolation_enum;
275 subdivision_boundary_interpolation_enum.insert("none", SUBDIVISION_BOUNDARY_NONE);
276 subdivision_boundary_interpolation_enum.insert("edge_only", SUBDIVISION_BOUNDARY_EDGE_ONLY);
277 subdivision_boundary_interpolation_enum.insert("edge_and_corner",
279 SOCKET_ENUM(subdivision_boundary_interpolation,
280 "Subdivision Boundary Interpolation",
281 subdivision_boundary_interpolation_enum,
283
284 static NodeEnum subdivision_fvar_interpolation_enum;
285 subdivision_fvar_interpolation_enum.insert("none", SUBDIVISION_FVAR_LINEAR_NONE);
286 subdivision_fvar_interpolation_enum.insert("corners_only", SUBDIVISION_FVAR_LINEAR_CORNERS_ONLY);
287 subdivision_fvar_interpolation_enum.insert("corners_plus1",
289 subdivision_fvar_interpolation_enum.insert("corners_plus2",
291 subdivision_fvar_interpolation_enum.insert("boundaries", SUBDIVISION_FVAR_LINEAR_BOUNDARIES);
292 subdivision_fvar_interpolation_enum.insert("all", SUBDIVISION_FVAR_LINEAR_ALL);
293 SOCKET_ENUM(subdivision_fvar_interpolation,
294 "Subdivision Face-Varying Interpolation",
295 subdivision_fvar_interpolation_enum,
297
298 SOCKET_INT_ARRAY(subd_vert_creases, "Subdivision Vertex Crease", array<int>());
300 subd_vert_creases_weight, "Subdivision Vertex Crease Weights", array<float>());
301 SOCKET_INT_ARRAY(subd_creases_edge, "Subdivision Crease Edges", array<int>());
302 SOCKET_FLOAT_ARRAY(subd_creases_weight, "Subdivision Crease Weights", array<float>());
303 SOCKET_INT_ARRAY(subd_face_corners, "Subdivision Face Corners", array<int>());
304 SOCKET_INT_ARRAY(subd_start_corner, "Subdivision Face Start Corner", array<int>());
305 SOCKET_INT_ARRAY(subd_num_corners, "Subdivision Face Corner Count", array<int>());
306 SOCKET_INT_ARRAY(subd_shader, "Subdivision Face Shader", array<int>());
307 SOCKET_BOOLEAN_ARRAY(subd_smooth, "Subdivision Face Smooth", array<bool>());
308 SOCKET_INT_ARRAY(subd_ptex_offset, "Subdivision Face PTex Offset", array<int>());
309
310 /* Subdivisions parameters */
311 static NodeEnum subd_adaptive_space_enum;
312 subd_adaptive_space_enum.insert("pixel", SUBDIVISION_ADAPTIVE_SPACE_PIXEL);
313 subd_adaptive_space_enum.insert("object", SUBDIVISION_ADAPTIVE_SPACE_OBJECT);
314
315 SOCKET_ENUM(subd_adaptive_space,
316 "Subdivision Adaptive Space",
317 subd_adaptive_space_enum,
319 SOCKET_FLOAT(subd_dicing_rate, "Subdivision Dicing Rate", 1.0f)
320 SOCKET_INT(subd_max_level, "Max Subdivision Level", 1);
321 SOCKET_TRANSFORM(subd_objecttoworld, "Subdivision Object Transform", transform_identity());
322
323 return type;
324}
325
327{
328 return (subdivision_type != SUBDIVISION_NONE) &&
329 (position_is_modified() || subd_dicing_rate_is_modified() ||
330 subd_adaptive_space_is_modified() || subd_objecttoworld_is_modified() ||
331 subd_max_level_is_modified());
332}
333
334Mesh::Mesh(const NodeType *node_type, Type geom_type_)
335 : Geometry(node_type, geom_type_), subd_attributes(this, ATTR_PRIM_SUBD)
336{
337 face_offset = 0;
338 corner_offset = 0;
339
340 num_subd_added_verts = 0;
341 num_subd_faces = 0;
342
343 subdivision_type = SUBDIVISION_NONE;
344
346}
347
348Mesh::Mesh() : Mesh(get_node_type(), Geometry::MESH) {}
349
354
355void Mesh::resize_mesh(const int numverts, const int numtris)
356{
358 attr_P->resize(numverts);
359 triangles.resize(numtris * 3);
360 shader.resize(numtris);
361 smooth.resize(numtris);
362
364}
365
366void Mesh::resize_subd_faces(const int numfaces, const int numcorners)
367{
368 subd_start_corner.resize(numfaces);
369 subd_num_corners.resize(numfaces);
370 subd_shader.resize(numfaces);
371 subd_smooth.resize(numfaces);
372 subd_ptex_offset.resize(numfaces);
373 subd_face_corners.resize(numcorners);
374 num_subd_faces = numfaces;
375
377}
378
379void Mesh::reserve_subd_creases(const size_t num_creases)
380{
381 subd_creases_edge.reserve(num_creases * 2);
382 subd_creases_weight.reserve(num_creases);
383}
384
386{
387 Geometry::clear(true);
388
389 num_subd_added_verts = 0;
390 num_subd_faces = 0;
391}
392
393void Mesh::clear(bool preserve_shaders, bool preserve_voxel_data)
394{
395 Geometry::clear(preserve_shaders);
396
397 /* clear all verts and triangles */
398 triangles.clear();
399 shader.clear();
400 smooth.clear();
401
402 subd_start_corner.clear();
403 subd_num_corners.clear();
404 subd_shader.clear();
405 subd_smooth.clear();
406 subd_ptex_offset.clear();
407 subd_face_corners.clear();
408
409 subd_creases_edge.clear();
410 subd_creases_weight.clear();
411
413 attributes.clear(preserve_voxel_data);
415
416 subdivision_type = SubdivisionType::SUBDIVISION_NONE;
417
419}
420
421void Mesh::clear(bool preserve_shaders)
422{
423 clear(preserve_shaders, false);
424}
425
427{
429 s.shader = subd_shader[index];
430 s.num_corners = subd_num_corners[index];
431 s.smooth = subd_smooth[index];
432 s.ptex_offset = subd_ptex_offset[index];
433 s.start_corner = subd_start_corner[index];
434 return s;
435}
436
437void Mesh::add_edge_crease(const int v0, const int v1, const float weight)
438{
439 subd_creases_edge.push_back_slow(v0);
440 subd_creases_edge.push_back_slow(v1);
441 subd_creases_weight.push_back_slow(weight);
442
443 tag_subd_creases_edge_modified();
444 tag_subd_creases_edge_modified();
445 tag_subd_creases_weight_modified();
446}
447
448void Mesh::add_vertex_crease(const int v, const float weight)
449{
450 subd_vert_creases.push_back_slow(v);
451 subd_vert_creases_weight.push_back_slow(weight);
452
453 tag_subd_vert_creases_modified();
454 tag_subd_vert_creases_weight_modified();
455}
456
458{
459 const int attr_step = motion_step + 1;
460 for (AttributeSet *attr_set : {&attributes, &subd_attributes}) {
461 Attribute *attr_P = attr_set->find(ATTR_STD_POSITION);
462 if (attr_P && attr_P->has_motion()) {
463 const packed_float3 *P = attr_P->data<packed_float3>();
464 std::copy_n(P, attr_P->size, attr_P->data_for_write<packed_float3>(attr_step));
465 }
466
467 Attribute *attr_N = attr_set->find(ATTR_STD_VERTEX_NORMAL);
468 if (attr_N && attr_N->has_motion()) {
469 const packed_normal *N = attr_N->data<packed_normal>();
470 std::copy_n(N, attr_N->size, attr_N->data_for_write<packed_normal>(attr_step));
471 }
472 }
473
475 if (attr_cN && attr_cN->has_motion()) {
476 const size_t numcorners = triangles.size();
477 const packed_normal *N = attr_cN->data<packed_normal>();
478 std::copy_n(N, numcorners, attr_cN->data_for_write<packed_normal>(attr_step));
479 }
480}
481
482void Mesh::get_uv_tiles(ustring map, unordered_set<int> &tiles)
483{
484 Attribute *attr;
485 Attribute *subd_attr;
486
487 if (map.empty()) {
489 subd_attr = subd_attributes.find(ATTR_STD_UV);
490 }
491 else {
492 attr = attributes.find(map);
493 subd_attr = subd_attributes.find(map);
494 }
495
496 if (attr) {
498 }
499 if (subd_attr) {
500 subd_attr->get_uv_tiles(this, ATTR_PRIM_SUBD, tiles);
501 }
502}
503
505{
507 const size_t verts_size = num_verts();
508 const packed_float3 *verts = get_position();
509
510 if (verts_size > 0) {
511 for (size_t i = 0; i < verts_size; i++) {
512 bnds.grow(verts[i]);
513 }
514
516 if (use_motion_blur && attr_P->has_motion()) {
517 for (int attr_step = 1; attr_step < attr_P->num_motion_steps(); attr_step++) {
518 const packed_float3 *vert_step = attr_P->data<packed_float3>(attr_step);
519 for (size_t i = 0; i < verts_size; i++) {
520 bnds.grow(vert_step[i]);
521 }
522 }
523 }
524
525 if (!bnds.valid()) {
526 bnds = BoundBox::empty;
527
528 /* skip nan or inf coordinates */
529 for (size_t i = 0; i < verts_size; i++) {
530 bnds.grow_safe(verts[i]);
531 }
532
533 if (use_motion_blur && attr_P->has_motion()) {
534 for (int attr_step = 1; attr_step < attr_P->num_motion_steps(); attr_step++) {
535 const packed_float3 *vert_step = attr_P->data<packed_float3>(attr_step);
536 for (size_t i = 0; i < verts_size; i++) {
537 bnds.grow_safe(vert_step[i]);
538 }
539 }
540 }
541 }
542 }
543
544 if (!bnds.valid()) {
545 /* empty mesh */
546 bnds.grow(zero_float3());
547 }
548
549 bounds = bnds;
550}
551
552void Mesh::apply_transform(const Transform &tfm, const bool apply_to_motion)
553{
555
556 /* apply to mesh vertices */
558 const size_t num_verts = this->num_verts();
559 for (size_t i = 0; i < num_verts; i++) {
560 verts[i] = transform_point(&tfm, verts[i]);
561 }
562
564
566 if (attr_vN) {
567 const Transform ntfm = transform_normal;
568 const size_t num_verts = this->num_verts();
570
571 for (size_t i = 0; i < num_verts; i++) {
572 vN[i] = packed_normal(normalize(transform_direction(&ntfm, vN[i].decode())));
573 }
574 }
575
577 if (attr_cN) {
578 const Transform ntfm = transform_normal;
579 const size_t num_corners = triangles.size();
581
582 for (size_t i = 0; i < num_corners; i++) {
583 cN[i] = packed_normal(normalize(transform_direction(&ntfm, cN[i].decode())));
584 }
585 }
586
588 if (attr_uN) {
589 const Transform ntfm = transform_normal;
590 const size_t size = attr_uN->buffer_size(this, ATTR_PRIM_GEOMETRY) / sizeof(packed_normal);
592
593 for (size_t i = 0; i < size; i++) {
594 uN[i] = packed_normal(normalize(transform_direction(&ntfm, uN[i].decode())));
595 }
596 }
597
598 if (apply_to_motion) {
600
601 if (attr_P->has_motion()) {
602 const size_t num_verts = this->num_verts();
603 for (int step = 1; step <= int(attr_P->motion.size()); step++) {
604 packed_float3 *vert_step = attr_P->data_for_write<packed_float3>(step);
605 for (size_t i = 0; i < num_verts; i++) {
606 vert_step[i] = transform_point(&tfm, vert_step[i]);
607 }
608 }
609 }
610
612
613 if (attr_mN && attr_mN->has_motion()) {
614 const Transform ntfm = transform_normal;
615 const size_t num_verts = this->num_verts();
616 for (int step = 1; step <= int(attr_mN->motion.size()); step++) {
617 packed_normal *normal_step = attr_mN->data_for_write<packed_normal>(step);
618 for (size_t i = 0; i < num_verts; i++) {
619 normal_step[i] = packed_normal(
620 normalize(transform_direction(&ntfm, normal_step[i].decode())));
621 }
622 }
623 }
624
626
627 if (attr_mcN && attr_mcN->has_motion()) {
628 const Transform ntfm = transform_normal;
629 const size_t nc = triangles.size();
630 for (int step = 1; step <= int(attr_mcN->motion.size()); step++) {
631 packed_normal *normal_step = attr_mcN->data_for_write<packed_normal>(step);
632 for (size_t i = 0; i < nc; i++) {
633 normal_step[i] = packed_normal(
634 normalize(transform_direction(&ntfm, normal_step[i].decode())));
635 }
636 }
637 }
638 }
639}
640
642{
644 if (attr_cN) {
645 /* Not needed if we already have corner normals overriding these.
646 * If there is motion blur without motion corner normals we can't
647 * render correctly, discard corner normals. */
648 if (has_motion_blur() && !attr_cN->has_motion()) {
650 }
651 else {
652 return;
653 }
654 }
655
656 const bool flip = transform_negative_scaled;
657 const size_t verts_size = num_verts();
658 const size_t triangles_size = num_triangles();
659
660 /* static vertex normals */
661 if (!attributes.find(ATTR_STD_VERTEX_NORMAL) && triangles_size) {
662 /* get attributes */
664
665 const packed_float3 *verts_ptr = get_position();
667
668 /* compute vertex normals */
669 vector<float3> vN_float(verts_size, zero_float3());
670
671 for (size_t i = 0; i < triangles_size; i++) {
672 const float3 fN = get_triangle(i).compute_normal(verts_ptr);
673 for (size_t j = 0; j < 3; j++) {
674 vN_float[get_triangle(i).v[j]] += fN;
675 }
676 }
677
678 if (flip) {
679 for (size_t i = 0; i < verts_size; i++) {
680 vN[i] = packed_normal(-normalize(vN_float[i]));
681 }
682 }
683 else {
684 for (size_t i = 0; i < verts_size; i++) {
685 vN[i] = packed_normal(normalize(vN_float[i]));
686 }
687 }
688 }
689
690 /* motion vertex normals */
693
694 if (has_motion_blur() && attr_P->has_motion() && !(attr_N && attr_N->has_motion()) &&
695 triangles_size)
696 {
697 if (!attr_N) {
699 }
700 attr_N->add_motion(this);
701
702 for (int attr_step = 1; attr_step < attr_P->num_motion_steps(); attr_step++) {
703 const packed_float3 *mP = attr_P->data<packed_float3>(attr_step);
704 packed_normal *mN = attr_N->data_for_write<packed_normal>(attr_step);
705
706 /* compute */
707 vector<float3> mN_float(verts_size, zero_float3());
708
709 for (size_t i = 0; i < triangles_size; i++) {
710 const Triangle tri = get_triangle(i);
711 const float3 fN = tri.compute_normal(mP);
712 for (size_t j = 0; j < 3; j++) {
713 mN_float[tri.v[j]] += fN;
714 }
715 }
716
717 if (flip) {
718 for (size_t i = 0; i < verts_size; i++) {
719 mN[i] = packed_normal(-normalize(mN_float[i]));
720 }
721 }
722 else {
723 for (size_t i = 0; i < verts_size; i++) {
724 mN[i] = packed_normal(normalize(mN_float[i]));
725 }
726 }
727 }
728 }
729
730 /* subd vertex normals */
732 /* get attributes */
735
736 /* compute vertex normals */
737 vector<float3> vN_float(verts_size, zero_float3());
738
739 for (size_t i = 0; i < get_num_subd_faces(); i++) {
740 const SubdFace face = get_subd_face(i);
741 const float3 fN = face.normal(this);
742
743 for (size_t j = 0; j < face.num_corners; j++) {
744 const size_t corner = subd_face_corners[face.start_corner + j];
745 vN_float[corner] += fN;
746 }
747 }
748
749 if (flip) {
750 for (size_t i = 0; i < verts_size; i++) {
751 vN[i] = packed_normal(-normalize(vN_float[i]));
752 }
753 }
754 else {
755 for (size_t i = 0; i < verts_size; i++) {
756 vN[i] = packed_normal(normalize(vN_float[i]));
757 }
758 }
759 }
760}
761
763{
766 {
767 /* Copy position to attribute. */
769
770 size_t size = attr->buffer_size(this, ATTR_PRIM_GEOMETRY) / sizeof(packed_float3);
771 std::copy_n(get_position(), size, attr->data_for_write<packed_float3>());
772 }
773
776 {
777 /* Copy corner or vertex normal to attribute, using the matching element type
778 * so the kernel reads and interpolates it correctly. */
780 if (!attr_N) {
782 }
783 if (attr_N) {
784 Attribute *attr = attributes.add(
786 TypeNormal,
787 attr_N->element);
789
790 size_t size = attr->buffer_size(this, ATTR_PRIM_GEOMETRY) / sizeof(packed_normal);
791 std::copy_n(attr_N->data<packed_normal>(), size, attr->data_for_write<packed_normal>());
792 }
793 }
794}
795
797{
798 if (!num_triangles() && !num_subd_faces) {
799 return;
800 }
801
802 AttributeSet &attrs = num_subd_faces ? subd_attributes : attributes;
803
804 /* apply generated attributes if needed or missing */
806 const Attribute *attr_P = attrs.find(ATTR_STD_POSITION);
807 if (attr_P) {
808 const size_t verts_size = attr_P->size;
809 const packed_float3 *verts = attr_P->data<packed_float3>();
810 Attribute *attr_generated = attrs.add(ATTR_STD_GENERATED);
811 packed_float3 *generated = attr_generated->data_for_write<packed_float3>();
812 for (size_t i = 0; i < verts_size; ++i) {
813 generated[i] = verts[i];
814 }
815 }
816 }
817}
818
819void Mesh::update_tangents(Scene *scene, bool undisplaced)
820{
821 if (!num_triangles()) {
822 return;
823 }
824
826
827 ccl::set<ustring> uv_maps;
828 Attribute *attr_std_uv = attributes.find(ATTR_STD_UV);
829
830 AttributeStandard tangent_std = (undisplaced) ? ATTR_STD_UV_TANGENT_UNDISPLACED :
832 AttributeStandard tangent_sign_std = (undisplaced) ? ATTR_STD_UV_TANGENT_SIGN_UNDISPLACED :
834 const char *tangent_postfix = (undisplaced) ? ".undisplaced_tangent" : ".tangent";
835 const char *tangent_sign_postfix = (undisplaced) ? ".undisplaced_tangent_sign" : ".tangent_sign";
836
837 /* Standard UVs. */
838 if ((need_attribute(scene, tangent_std) || need_attribute(scene, tangent_sign_std)) &&
839 !attributes.find(tangent_std))
840 {
841 mikk_compute_tangents(attr_std_uv,
842 this,
843 true,
844 tangent_std,
845 tangent_sign_std,
846 tangent_postfix,
847 tangent_sign_postfix); /* sign */
848 }
849
850 /* now generate for any other UVs requested */
851 for (Attribute &attr : attributes.attributes) {
852 if (!(attr.type == TypeFloat2 && attr.element == ATTR_ELEMENT_CORNER)) {
853 continue;
854 }
855
856 const ustring tangent_name = ustring(attr.name.string() + tangent_postfix);
857 const ustring tangent_sign_name = ustring(attr.name.string() + tangent_sign_postfix);
858
859 if ((need_attribute(scene, tangent_name) || need_attribute(scene, tangent_sign_name)) &&
860 !attributes.find(tangent_name))
861 {
863 this,
864 true,
865 tangent_std,
866 tangent_sign_std,
867 tangent_postfix,
868 tangent_sign_postfix); /* sign */
869 }
870 }
871}
872
873void Mesh::pack_shaders(Scene *scene, uint *tri_shader)
874{
875 uint shader_id = 0;
876 uint last_shader = -1;
877 bool last_smooth = false;
878
879 const size_t triangles_size = num_triangles();
880 const int *shader_ptr = shader.data();
881
882 /* Corner normals override the smooth flag, as the flatness is already
883 * encoded in the corner normals and we always interpolate them. */
884 const bool use_corner_normals = attributes.find(ATTR_STD_CORNER_NORMAL) != nullptr;
885 const bool *smooth_ptr = (use_corner_normals) ? nullptr : smooth.data();
886 const bool smooth_constant = (use_corner_normals) ? true : false;
887
888 for (size_t i = 0; i < triangles_size; i++) {
889 const int new_shader = shader_ptr ? shader_ptr[i] : INT_MAX;
890 const bool new_smooth = smooth_ptr ? smooth_ptr[i] : smooth_constant;
891
892 if (new_shader != last_shader || last_smooth != new_smooth) {
893 last_shader = new_shader;
894 last_smooth = new_smooth;
895 Shader *shader = (last_shader < used_shaders.size()) ?
896 static_cast<Shader *>(used_shaders[last_shader]) :
897 scene->default_surface;
898 shader_id = scene->shader_manager->get_shader_id(shader, last_smooth);
899 }
900
901 tri_shader[i] = shader_id;
902 }
903}
904
906{
907 const size_t triangles_size = num_triangles();
908 const int *p_tris = triangles.data();
909 int off = 0;
910 for (size_t i = 0; i < triangles_size; i++) {
911 tri_vindex[i] = make_packed_uint3(p_tris[off + 0], p_tris[off + 1], p_tris[off + 2]);
912 off += 3;
913 }
914}
915
917{
920 return use_motion_blur &&
921 (attr_P->has_motion() || (get_subdivision_type() != Mesh::SUBDIVISION_NONE &&
922 subd_attr_P && subd_attr_P->has_motion()));
923}
924
929
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
list< Attribute > attributes
Attribute * find(ustring name) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void remove(ustring name)
void clear(bool preserve_voxel_data=false)
AttributeElement element
vector< Buffer > motion
const T * data(const int step=0) const
void resize(Geometry *geom, AttributePrimitive prim)
T * data_for_write(const int step=0)
bool has_motion() const
TypeDesc type
void add_motion(const Geometry *geom)
void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set< int > &tiles) const
static const char * standard_name(AttributeStandard std)
size_t buffer_size(Geometry *geom, AttributePrimitive prim) const
AttributeStandard std
int num_motion_steps() const
const T * data_at_time_step(const int step, const int num_time_steps) const
__forceinline bool valid() const
Definition boundbox.h:127
__forceinline void grow_safe(const float3 &pt)
Definition boundbox.h:56
__forceinline void grow(const float3 &pt)
Definition boundbox.h:35
Transform transform_normal
int motion_step(const float time) const
BoundBox bounds
void tag_position_modified()
const packed_float3 * get_position() const
bool position_is_modified() const
AttributeSet attributes
Geometry(const NodeType *node_type, const Type type)
bool transform_negative_scaled
bool need_attribute(const Scene *scene, AttributeStandard std)
virtual void clear(bool preserve_shaders=false)
packed_float3 * get_position_for_write()
size_t get_num_subd_faces() const
Definition scene/mesh.h:221
void add_undisplaced(Scene *scene)
@ SUBDIVISION_ADAPTIVE_SPACE_OBJECT
Definition scene/mesh.h:136
@ SUBDIVISION_ADAPTIVE_SPACE_PIXEL
Definition scene/mesh.h:135
size_t face_offset
Definition scene/mesh.h:172
NODE_DECLARE size_t num_verts() const
void reserve_subd_creases(const size_t num_creases)
void compute_bounds() override
size_t corner_offset
Definition scene/mesh.h:173
void add_vertex_normals()
AttributeSet subd_attributes
Definition scene/mesh.h:169
void copy_center_to_motion_step(const int motion_step)
Mesh(const NodeType *node_type_, Type geom_type_)
void clear(bool preserve_shaders=false) override
void add_builtin_attributes()
bool has_motion_blur() const override
bool need_tessellation()
@ SUBDIVISION_FVAR_LINEAR_NONE
Definition scene/mesh.h:126
@ SUBDIVISION_FVAR_LINEAR_CORNERS_PLUS2
Definition scene/mesh.h:129
@ SUBDIVISION_FVAR_LINEAR_CORNERS_ONLY
Definition scene/mesh.h:127
@ SUBDIVISION_FVAR_LINEAR_ALL
Definition scene/mesh.h:131
@ SUBDIVISION_FVAR_LINEAR_BOUNDARIES
Definition scene/mesh.h:130
@ SUBDIVISION_FVAR_LINEAR_CORNERS_PLUS1
Definition scene/mesh.h:128
void add_vertex_crease(const int v, const float weight)
SubdFace get_subd_face(const size_t index) const
void pack_triangles(packed_uint3 *tri_vindex)
@ SUBDIVISION_NONE
Definition scene/mesh.h:114
@ SUBDIVISION_LINEAR
Definition scene/mesh.h:115
@ SUBDIVISION_CATMULL_CLARK
Definition scene/mesh.h:116
size_t num_triangles() const
Definition scene/mesh.h:73
void clear_non_sockets()
void resize_subd_faces(const int numfaces, const int numcorners)
void get_uv_tiles(ustring map, unordered_set< int > &tiles) override
void resize_mesh(const int numverts, const int numtris)
void update_generated(Scene *scene)
Triangle get_triangle(const size_t i) const
Definition scene/mesh.h:67
void pack_shaders(Scene *scene, uint *shader)
PrimitiveType primitive_type() const override
void add_edge_crease(const int v0, const int v1, const float weight)
void update_tangents(Scene *scene, bool undisplaced)
void apply_transform(const Transform &tfm, const bool apply_to_motion) override
@ SUBDIVISION_BOUNDARY_EDGE_ONLY
Definition scene/mesh.h:121
@ SUBDIVISION_BOUNDARY_EDGE_AND_CORNER
Definition scene/mesh.h:122
@ SUBDIVISION_BOUNDARY_NONE
Definition scene/mesh.h:120
Definition scene.h:133
Shader * default_surface
Definition scene.h:178
unique_ptr< ShaderManager > shader_manager
Definition scene.h:169
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define assert(assertion)
#define INT_MAX
#define smooth
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
VecT safe_normalize(VecT vector)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
PrimitiveType
@ PRIMITIVE_MOTION_TRIANGLE
@ PRIMITIVE_TRIANGLE
@ ATTR_ELEMENT_CORNER
@ ATTR_PRIM_SUBD
@ ATTR_PRIM_GEOMETRY
AttributeStandard
@ ATTR_STD_UV
@ ATTR_STD_VERTEX_NORMAL
@ ATTR_STD_UV_TANGENT
@ ATTR_STD_POSITION_UNDISPLACED
@ ATTR_STD_NORMAL_UNDISPLACED
@ ATTR_STD_UV_TANGENT_SIGN
@ ATTR_STD_UV_TANGENT_SIGN_UNDISPLACED
@ ATTR_STD_POSITION
@ ATTR_STD_CORNER_NORMAL
@ ATTR_STD_UV_TANGENT_UNDISPLACED
@ ATTR_STD_GENERATED
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual2 map_to_sphere(const dual3 co)
Definition math_dual.h:418
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
#define T
#define SOCKET_BOOLEAN_ARRAY(name, ui_name, default_value,...)
Definition node_type.h:262
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition node_type.h:219
#define SOCKET_INT(name, ui_name, default_value,...)
Definition node_type.h:213
#define SOCKET_FLOAT_ARRAY(name, ui_name, default_value,...)
Definition node_type.h:267
#define SOCKET_TRANSFORM(name, ui_name, default_value,...)
Definition node_type.h:233
#define SOCKET_INT_ARRAY(name, ui_name, default_value,...)
Definition node_type.h:265
#define NODE_DEFINE(structname)
Definition node_type.h:157
#define SOCKET_ENUM(name, ui_name, values, default_value,...)
Definition node_type.h:235
static void mikk_compute_tangents(Attribute *attr_uv, Mesh *mesh, const bool need_sign, const AttributeStandard tangent_std, const AttributeStandard tangent_sign_std, const char *tangent_postfix, const char *tangent_sign_postfix)
#define min(a, b)
Definition sort.cc:38
float3 normal(const Mesh *mesh) const
float3 compute_normal(const packed_float3 *verts) const
void verts_for_step(const Attribute *attr_P, const size_t step, float3 r_verts[3]) const
void bounds_grow(const packed_float3 *verts, BoundBox &bounds) const
void motion_verts(const Attribute *attr_P, const size_t num_steps, const float time, float3 r_verts[3]) const
bool valid(const packed_float3 *verts) const
mikk::float3 GetNormal(const int face_num, const int vert_num)
int VertexIndex(const int face_num, const int vert_num)
const float2 * uv
void SetTangentSpace(const int face_num, const int vert_num, mikk::float3 T, bool orientation)
int CornerIndex(const int face_num, const int vert_num)
int GetNumVerticesOfFace(const int)
const packed_float3 * position
const packed_normal * corner_normal
const Mesh * mesh
bool has_uv() const
MikkMeshWrapper(const Mesh *mesh, const packed_normal *vertex_normal, const packed_normal *corner_normal, const float2 *uv, packed_float3 *tangent, float *tangent_sign)
mikk::float3 GetPosition(const int face_num, const int vert_num)
mikk::float3 GetTexCoord(const int face_num, const int vert_num)
packed_float3 * tangent
const packed_normal * vertex_normal
void insert(const char *x, const int y)
Definition node_enum.h:21
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
const NodeType * type
Definition graph/node.h:179
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
packed_normal()=default
CCL_NAMESPACE_BEGIN Transform transform_transposed_inverse(const Transform &tfm)
Definition transform.cpp:15
ccl_device_inline Transform transform_identity()
Definition transform.h:355
ccl_device_inline packed_uint3 make_packed_uint3(const uint x, const uint y, uint z)
Definition types_uint3.h:85