Blender V5.3
bake.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
51
52#include <climits>
53#include <cstring>
54
55#include "BLI_string_ref.hh"
56#include "MEM_guardedalloc.h"
57
58#include "BLI_index_range.hh"
59#include "BLI_math_geom_c.hh"
60#include "BLI_math_matrix_c.hh"
61#include "BLI_math_vector_c.hh"
62#include "BLI_task.hh"
63
64#include "BKE_attribute.hh"
65#include "BKE_bvhutils.hh"
66#include "BKE_customdata.hh"
67#include "BKE_image.hh"
68#include "BKE_lib_id.hh"
69#include "BKE_mesh.hh"
70#include "BKE_mesh_runtime.hh"
71#include "BKE_mesh_tangent.hh"
72#include "BKE_node.hh"
73
74#include "IMB_imbuf.hh"
75#include "IMB_imbuf_types.hh"
76
77#include "RE_bake.h"
78#include "RE_texture_margin.h"
79
80/* local include */
81#include "zbuf.h"
82
83namespace blender {
84
93
95 const float *positions[3];
96 const float *vert_normals[3];
98 const float *loop_normal[3];
99 float normal[3]; /* for flat faces */
101};
102
103static void store_bake_pixel(void *handle, int x, int y, float u, float v)
104{
105 BakeDataZSpan *bd = static_cast<BakeDataZSpan *>(handle);
106 BakePixel *pixel;
107
108 const int width = bd->bk_image->width;
109 const size_t offset = bd->bk_image->offset;
110 const int i = offset + y * width + x;
111
112 pixel = &bd->pixel_array[i];
113 pixel->primitive_id = bd->primitive_id;
114
115 /* At this point object_id is always 0, since this function runs for the
116 * low-poly mesh only. The object_id lookup indices are set afterwards. */
117
118 copy_v2_fl2(pixel->uv, u, v);
119
120 pixel->du_dx = bd->du_dx;
121 pixel->du_dy = bd->du_dy;
122 pixel->dv_dx = bd->dv_dx;
123 pixel->dv_dy = bd->dv_dy;
124 pixel->object_id = 0;
125 pixel->seed = i;
126}
127
128void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
129{
130 size_t i;
131 if (!mask) {
132 return;
133 }
134
135 /* only extend to pixels outside the mask area */
136 for (i = 0; i < pixels_num; i++) {
137 const BakePixel *px = &pixel_array[i];
138
139 if (px->primitive_id != -1 && !px->is_margin) {
141 }
142 }
143}
144
146 char *mask,
147 const int margin,
148 const char margin_type,
149 const Mesh *mesh,
150 const StringRef uv_layer,
151 const float uv_offset[2])
152{
153 /* margin */
154 switch (margin_type) {
156 RE_generate_texturemargin_adjacentfaces(ibuf, mask, margin, mesh, uv_layer, uv_offset, true);
157 break;
158 default:
159 /* fall through */
160 case R_BAKE_EXTEND:
161 IMB_filter_extend(ibuf, mask, margin);
162 break;
163 }
164
165 if (!ibuf->can_contain_alpha()) {
166 /* clear alpha added by filtering */
167 IMB_rectfill_alpha(ibuf, 1.0f);
168 }
169}
170
179 TriTessFace *triangles_cage,
180 const float mat_low[4][4],
181 const float mat_cage[4][4],
182 int primitive_id,
183 float u,
184 float v,
185 float r_co[3],
186 float r_dir[3])
187{
188 float data[2][3][3];
189 float coord[2][3];
190 float dir[3];
191 int i;
192
193 TriTessFace *triangle[2];
194
195 triangle[0] = &triangles_low[primitive_id];
196 triangle[1] = &triangles_cage[primitive_id];
197
198 for (i = 0; i < 2; i++) {
199 copy_v3_v3(data[i][0], triangle[i]->positions[0]);
200 copy_v3_v3(data[i][1], triangle[i]->positions[1]);
201 copy_v3_v3(data[i][2], triangle[i]->positions[2]);
202 interp_barycentric_tri_v3(data[i], u, v, coord[i]);
203 }
204
205 /* convert from local to world space */
206 mul_m4_v3(mat_low, coord[0]);
207 mul_m4_v3(mat_cage, coord[1]);
208
209 sub_v3_v3v3(dir, coord[0], coord[1]);
210 normalize_v3(dir);
211
212 copy_v3_v3(r_co, coord[1]);
213 copy_v3_v3(r_dir, dir);
214}
215
222 const float mat[4][4],
223 const float imat[4][4],
224 int primitive_id,
225 float u,
226 float v,
227 float cage_extrusion,
228 float r_co[3],
229 float r_dir[3],
230 const bool is_cage)
231{
232 float data[3][3];
233 float coord[3];
234 float dir[3];
235 float cage[3];
236 bool is_smooth;
237
238 TriTessFace *triangle = &triangles[primitive_id];
239 is_smooth = triangle->is_smooth || is_cage;
240
241 copy_v3_v3(data[0], triangle->positions[0]);
242 copy_v3_v3(data[1], triangle->positions[1]);
243 copy_v3_v3(data[2], triangle->positions[2]);
244
245 interp_barycentric_tri_v3(data, u, v, coord);
246
247 if (is_smooth) {
248 copy_v3_v3(data[0], triangle->vert_normals[0]);
249 copy_v3_v3(data[1], triangle->vert_normals[1]);
250 copy_v3_v3(data[2], triangle->vert_normals[2]);
251
253 normalize_v3(dir);
254 }
255 else {
256 copy_v3_v3(dir, triangle->normal);
257 }
258
259 mul_v3_v3fl(cage, dir, cage_extrusion);
260 add_v3_v3(coord, cage);
261
262 normalize_v3(dir);
263 negate_v3(dir);
264
265 /* convert from local to world space */
266 mul_m4_v3(mat, coord);
267 mul_transposed_mat3_m4_v3(imat, dir);
268 normalize_v3(dir);
269
270 copy_v3_v3(r_co, coord);
271 copy_v3_v3(r_dir, dir);
272}
273
274static void barycentric_differentials_from_position(const float co[3],
275 const float v1[3],
276 const float v2[3],
277 const float v3[3],
278 const float dxco[3],
279 const float dyco[3],
280 const float facenor[3],
281 const bool differentials,
282 float *u,
283 float *v,
284 float *dx_u,
285 float *dx_v,
286 float *dy_u,
287 float *dy_v)
288{
289 /* find most stable axis to project */
290 int axis1, axis2;
291 axis_dominant_v3(&axis1, &axis2, facenor);
292
293 /* compute u,v and derivatives */
294 float t00 = v3[axis1] - v1[axis1];
295 float t01 = v3[axis2] - v1[axis2];
296 float t10 = v3[axis1] - v2[axis1];
297 float t11 = v3[axis2] - v2[axis2];
298
299 float detsh = (t00 * t11 - t10 * t01);
300 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
301 t00 *= detsh;
302 t01 *= detsh;
303 t10 *= detsh;
304 t11 *= detsh;
305
306 *u = (v3[axis1] - co[axis1]) * t11 - (v3[axis2] - co[axis2]) * t10;
307 *v = (v3[axis2] - co[axis2]) * t00 - (v3[axis1] - co[axis1]) * t01;
308 if (differentials) {
309 *dx_u = dxco[axis1] * t11 - dxco[axis2] * t10;
310 *dx_v = dxco[axis2] * t00 - dxco[axis1] * t01;
311 *dy_u = dyco[axis1] * t11 - dyco[axis2] * t10;
312 *dy_v = dyco[axis2] * t00 - dyco[axis1] * t01;
313 }
314}
315
319static bool cast_ray_highpoly(const bke::bvh::Tree **treeData,
320 TriTessFace *triangle_low,
321 TriTessFace *triangles[],
322 BakePixel *pixel_array_low,
323 BakePixel *pixel_array,
324 const float mat_low[4][4],
325 BakeHighPolyData *highpoly,
326 const int highpoly_num,
327 MutableSpan<std::optional<bke::bvh::RayHit>> hits,
328 const float co[3],
329 const float dir[3],
330 const int pixel_id,
331 const float max_ray_distance)
332{
333 int hit_mesh = -1;
334 float hit_distance_squared = max_ray_distance * max_ray_distance;
335 if (hit_distance_squared == 0.0f) {
336 /* No ray distance set, use maximum. */
337 hit_distance_squared = FLT_MAX;
338 }
339
340 for (int i = 0; i < highpoly_num; i++) {
341 float co_high[3], dir_high[3];
342
343 /* Transform the ray from the world space to the `highpoly` space. */
344 mul_v3_m4v3(co_high, highpoly[i].imat, co);
345
346 /* rotates */
347 mul_v3_mat3_m4v3(dir_high, highpoly[i].imat, dir);
348 normalize_v3(dir_high);
349
350 /* cast ray */
351 /* TODO: we should use FLT_MAX here, but sweep-sphere code isn't prepared for that. */
352 const bke::bvh::Ray ray(co_high, dir_high, BVH_RAYCAST_DIST_MAX);
353 if (treeData[i]) {
354 hits[i] = treeData[i]->ray_intersect(ray);
355 }
356
357 if (hits[i]) {
358 /* distance comparison in world space */
359 float hit_world[3];
360 mul_v3_m4v3(hit_world, highpoly[i].obmat, hits[i]->position(ray));
361 float distance_squared = len_squared_v3v3(hit_world, co);
362
363 if (distance_squared < hit_distance_squared) {
364 hit_mesh = i;
365 hit_distance_squared = distance_squared;
366 }
367 }
368 }
369
370 if (hit_mesh != -1) {
371 int primitive_id_high = hits[hit_mesh]->index;
372 TriTessFace *triangle_high = &triangles[hit_mesh][primitive_id_high];
373 BakePixel *pixel_low = &pixel_array_low[pixel_id];
374 BakePixel *pixel_high = &pixel_array[pixel_id];
375
376 pixel_high->primitive_id = primitive_id_high;
377 pixel_high->object_id = hit_mesh;
378 pixel_high->seed = pixel_id;
379 pixel_high->is_margin = pixel_low->is_margin;
380
381 /* ray direction in high poly object space */
382 float co_high[3], dir_high[3];
383 mul_v3_m4v3(co_high, highpoly[hit_mesh].imat, co);
384 mul_v3_mat3_m4v3(dir_high, highpoly[hit_mesh].imat, dir);
385 normalize_v3(dir_high);
386
387 /* compute position differentials on low poly object */
388 float duco_low[3], dvco_low[3], dxco[3], dyco[3];
389 sub_v3_v3v3(duco_low, triangle_low->positions[0], triangle_low->positions[2]);
390 sub_v3_v3v3(dvco_low, triangle_low->positions[1], triangle_low->positions[2]);
391
392 mul_v3_v3fl(dxco, duco_low, pixel_low->du_dx);
393 madd_v3_v3fl(dxco, dvco_low, pixel_low->dv_dx);
394 mul_v3_v3fl(dyco, duco_low, pixel_low->du_dy);
395 madd_v3_v3fl(dyco, dvco_low, pixel_low->dv_dy);
396
397 /* transform from low poly to high poly object space */
398 mul_mat3_m4_v3(mat_low, dxco);
399 mul_mat3_m4_v3(mat_low, dyco);
400 mul_mat3_m4_v3(highpoly[hit_mesh].imat, dxco);
401 mul_mat3_m4_v3(highpoly[hit_mesh].imat, dyco);
402
403 /* transfer position differentials */
404 float tmp[3];
405 mul_v3_v3fl(tmp, dir_high, 1.0f / dot_v3v3(dir_high, triangle_high->normal));
406 madd_v3_v3fl(dxco, tmp, -dot_v3v3(dxco, triangle_high->normal));
407 madd_v3_v3fl(dyco, tmp, -dot_v3v3(dyco, triangle_high->normal));
408
409 /* compute barycentric differentials from position differentials */
410 const bke::bvh::Ray ray(co_high, dir_high);
412 triangle_high->positions[0],
413 triangle_high->positions[1],
414 triangle_high->positions[2],
415 dxco,
416 dyco,
417 triangle_high->normal,
418 true,
419 &pixel_high->uv[0],
420 &pixel_high->uv[1],
421 &pixel_high->du_dx,
422 &pixel_high->dv_dx,
423 &pixel_high->du_dy,
424 &pixel_high->dv_dy);
425
426 /* verify we have valid uvs */
427 BLI_assert(pixel_high->uv[0] >= -1e-3f && pixel_high->uv[1] >= -1e-3f &&
428 pixel_high->uv[0] + pixel_high->uv[1] <= 1.0f + 1e-3f);
429 }
430 else {
431 pixel_array[pixel_id].primitive_id = -1;
432 pixel_array[pixel_id].object_id = -1;
433 pixel_array[pixel_id].seed = 0;
434 pixel_array[pixel_id].is_margin = false;
435 }
436
437 return hit_mesh != -1;
438}
439
444static TriTessFace *mesh_calc_tri_tessface(Mesh *mesh, bool tangent, Mesh *mesh_eval)
445{
446 int i;
447
448 const int tottri = poly_to_tri_count(mesh->faces_num, mesh->corners_num);
449 TriTessFace *triangles;
450
451 /* calculate normal for each face only once */
452 uint mpoly_prev = UINT_MAX;
453 float3 no;
454
455 const Span<float3> positions = mesh->vert_positions();
456 const OffsetIndices faces = mesh->faces();
457 const Span<int> corner_verts = mesh->corner_verts();
458 const bke::AttributeAccessor attributes = mesh->attributes();
459 const VArray<bool> sharp_faces =
460 attributes.lookup_or_default<bool>("sharp_face", bke::AttrDomain::Face, false).varray;
461
462 int3 *corner_tris = MEM_new_array_uninitialized<int3>(tottri, __func__);
463 triangles = MEM_new_array_zeroed<TriTessFace>(tottri, __func__);
464
465 const bool calculate_normal = BKE_mesh_face_normals_are_dirty(mesh);
466 Span<float3> precomputed_normals;
467 if (!calculate_normal) {
468 precomputed_normals = mesh->face_normals();
469 }
470
471 if (!precomputed_normals.is_empty()) {
473 positions, faces, corner_verts, precomputed_normals, {corner_tris, tottri});
474 }
475 else {
476 bke::mesh::corner_tris_calc(positions, faces, corner_verts, {corner_tris, tottri});
477 }
478
479 Array<float4> tspace;
480 Span<float3> corner_normals;
481 if (tangent) {
482 const StringRef active_uv_map = mesh_eval->active_uv_map_name();
483 const VArraySpan uv_map = *attributes.lookup<float2>(active_uv_map, bke::AttrDomain::Corner);
485 faces,
486 corner_verts,
487 {corner_tris, tottri},
488 mesh->corner_tri_faces(),
489 VArraySpan(sharp_faces),
490 mesh->vert_normals(),
491 mesh->face_normals(),
492 mesh->corner_normals(),
493 {uv_map});
494 tspace = std::move(result[0]);
495
496 corner_normals = mesh_eval->corner_normals();
497 }
498
499 const Span<float3> vert_normals = mesh->vert_normals();
500 const Span<int> tri_faces = mesh->corner_tri_faces();
501 for (i = 0; i < tottri; i++) {
502 const int3 &tri = corner_tris[i];
503 const int face_i = tri_faces[i];
504
505 triangles[i].positions[0] = positions[corner_verts[tri[0]]];
506 triangles[i].positions[1] = positions[corner_verts[tri[1]]];
507 triangles[i].positions[2] = positions[corner_verts[tri[2]]];
508 triangles[i].vert_normals[0] = vert_normals[corner_verts[tri[0]]];
509 triangles[i].vert_normals[1] = vert_normals[corner_verts[tri[1]]];
510 triangles[i].vert_normals[2] = vert_normals[corner_verts[tri[2]]];
511 triangles[i].is_smooth = !sharp_faces[face_i];
512
513 if (tangent) {
514 triangles[i].tspace[0] = tspace[tri[0]];
515 triangles[i].tspace[1] = tspace[tri[1]];
516 triangles[i].tspace[2] = tspace[tri[2]];
517 }
518
519 if (!corner_normals.is_empty()) {
520 triangles[i].loop_normal[0] = corner_normals[tri[0]];
521 triangles[i].loop_normal[1] = corner_normals[tri[1]];
522 triangles[i].loop_normal[2] = corner_normals[tri[2]];
523 }
524
525 if (calculate_normal) {
526 if (face_i != mpoly_prev) {
527 no = bke::mesh::face_normal_calc(positions, corner_verts.slice(faces[face_i]));
528 mpoly_prev = face_i;
529 }
530 copy_v3_v3(triangles[i].normal, no);
531 }
532 else {
533 copy_v3_v3(triangles[i].normal, precomputed_normals[face_i]);
534 }
535 }
536
537 MEM_delete(corner_tris);
538
539 return triangles;
540}
541
543 BakePixel pixel_array_from[],
544 BakePixel pixel_array_to[],
545 BakeHighPolyData highpoly[],
546 const int highpoly_num,
547 const size_t pixels_num,
548 const bool is_custom_cage,
549 const float cage_extrusion,
550 const float max_ray_distance,
551 const float mat_low[4][4],
552 const float mat_cage[4][4],
553 Mesh *me_cage)
554{
555 float imat_low[4][4];
556 bool is_cage = me_cage != nullptr;
557 bool result = true;
558
559 Mesh *me_eval_low = nullptr;
560 Mesh **me_highpoly;
561
562 /* NOTE: all coordinates are in local space. */
563 TriTessFace *tris_low = nullptr;
564 TriTessFace *tris_cage = nullptr;
565 TriTessFace **tris_high;
566
567 /* Assume all low-poly tessfaces can be quads. */
568 tris_high = MEM_new_array_zeroed<TriTessFace *>(highpoly_num, "MVerts Highpoly Mesh Array");
569
570 /* Assume all high-poly tessfaces are triangles. */
571 me_highpoly = MEM_new_array_uninitialized<Mesh *>(highpoly_num, "Highpoly Derived Meshes");
572 Array<const bke::bvh::Tree *> treeData(highpoly_num, nullptr);
573
574 if (!is_cage) {
575 me_eval_low = BKE_mesh_copy_for_eval(*me_low);
576 tris_low = mesh_calc_tri_tessface(me_low, true, me_eval_low);
577 }
578 else if (is_custom_cage) {
579 tris_low = mesh_calc_tri_tessface(me_low, false, nullptr);
580 tris_cage = mesh_calc_tri_tessface(me_cage, false, nullptr);
581 }
582 else {
583 tris_cage = mesh_calc_tri_tessface(me_cage, false, nullptr);
584 }
585
586 invert_m4_m4(imat_low, mat_low);
587
588 for (int i = 0; i < highpoly_num; i++) {
589 tris_high[i] = mesh_calc_tri_tessface(highpoly[i].mesh, false, nullptr);
590
591 me_highpoly[i] = highpoly[i].mesh;
592
593 if (BKE_mesh_runtime_corner_tris_len(me_highpoly[i]) != 0) {
594 treeData[i] = &me_highpoly[i]->bvh_tris();
595 }
596 }
597
598 threading::parallel_for(IndexRange(pixels_num), 1024, [&](const IndexRange range) {
599 Array<std::optional<bke::bvh::RayHit>> hits(highpoly_num);
600 for (const IndexRange::Iterator::value_type i : range) {
601 int primitive_id = pixel_array_from[i].primitive_id;
602
603 if (primitive_id == -1) {
604 pixel_array_to[i].primitive_id = -1;
605 pixel_array_to[i].is_margin = false;
606 continue;
607 }
608
609 const float u = pixel_array_from[i].uv[0];
610 const float v = pixel_array_from[i].uv[1];
611 float co[3];
612 float dir[3];
613 TriTessFace *tri_low;
614
615 /* calculate from low poly mesh cage */
616 if (is_custom_cage) {
618 tris_low, tris_cage, mat_low, mat_cage, primitive_id, u, v, co, dir);
619 tri_low = &tris_cage[primitive_id];
620 }
621 else if (is_cage) {
623 tris_cage, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, true);
624 tri_low = &tris_cage[primitive_id];
625 }
626 else {
628 tris_low, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, false);
629 tri_low = &tris_low[primitive_id];
630 }
631
632 /* cast ray */
633 if (!cast_ray_highpoly(treeData.data(),
634 tri_low,
635 tris_high,
636 pixel_array_from,
637 pixel_array_to,
638 mat_low,
639 highpoly,
640 highpoly_num,
641 hits,
642 co,
643 dir,
644 i,
646 {
647 /* if it fails mask out the original pixel array */
648 pixel_array_from[i].primitive_id = -1;
649 }
650 }
651 });
652
653 /* garbage collection */
654 for (int i = 0; i < highpoly_num; i++) {
655 if (tris_high[i]) {
656 MEM_delete(tris_high[i]);
657 }
658 }
659
660 MEM_delete(tris_high);
661 MEM_delete(me_highpoly);
662
663 if (me_eval_low) {
664 BKE_id_free(nullptr, me_eval_low);
665 }
666 if (tris_low) {
667 MEM_delete(tris_low);
668 }
669 if (tris_cage) {
670 MEM_delete(tris_cage);
671 }
672
673 return result;
674}
675
677 const float *uv1,
678 const float *uv2,
679 const float *uv3)
680{
681 float A;
682
683 /* assumes dPdu = P1 - P3 and dPdv = P2 - P3 */
684 A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
685
686 if (fabsf(A) > FLT_EPSILON) {
687 A = 1.0f / A;
688
689 bd->du_dx = (uv2[1] - uv3[1]) * A;
690 bd->dv_dx = (uv3[1] - uv1[1]) * A;
691
692 bd->du_dy = (uv3[0] - uv2[0]) * A;
693 bd->dv_dy = (uv1[0] - uv3[0]) * A;
694 }
695 else {
696 bd->du_dx = bd->du_dy = 0.0f;
697 bd->dv_dx = bd->dv_dy = 0.0f;
698 }
699}
700
702 const BakeTargets *targets,
703 int margin)
704{
705 /* Bake neighboring pixels with the same primitive, so denoising gets a reasonable
706 * pixel value rather than a sharp transition that causes artifacts. */
707 for (int image_id = 0; image_id < targets->images_num; image_id++) {
708 BakeImage *image = &targets->images[image_id];
709
710 const int width = image->width;
711 const int height = image->height;
712 const size_t pixels_num = size_t(width) * size_t(height);
713 const size_t image_offset = image->offset;
714
715 BakePixel *bk_pixels = pixel_array + image_offset;
716
717 Array<char> mask(pixels_num, 0);
718 RE_bake_mask_fill(bk_pixels, pixels_num, mask.data());
719
720 for (int r = 0; r < margin; r++) {
721 for (int y = 0; y < height; y++) {
722 for (int x = 0; x < width; x++) {
723 const size_t px_index = size_t(x) + size_t(y) * width;
724 size_t copy_index;
725
726 if (mask[px_index] != FILTER_MASK_NULL) {
727 continue;
728 }
729
730 if (y != 0 && mask[px_index - width] == FILTER_MASK_USED) {
731 copy_index = px_index - width;
732 }
733 else if (y != height - 1 && mask[px_index + width] == FILTER_MASK_USED) {
734 copy_index = px_index + width;
735 }
736 else if (x != 0 && mask[px_index - 1] == FILTER_MASK_USED) {
737 copy_index = px_index - 1;
738 }
739 else if (x != width - 1 && mask[px_index + 1] == FILTER_MASK_USED) {
740 copy_index = px_index + 1;
741 }
742 else {
743 continue;
744 }
745
746 mask[px_index] = FILTER_MASK_MARGIN;
747 bk_pixels[px_index] = bk_pixels[copy_index];
748 bk_pixels[px_index].seed = px_index + image_offset;
749 bk_pixels[px_index].is_margin = true;
750 }
751 }
752
753 for (int y = 0; y < height; y++) {
754 for (int x = 0; x < width; x++) {
755 const size_t px_index = size_t(x) + size_t(y) * width;
756
757 if (mask[px_index] == FILTER_MASK_MARGIN) {
758 mask[px_index] = FILTER_MASK_USED;
759 }
760 }
761 }
762 }
763 }
764}
765
767 BakePixel pixel_array[],
768 const size_t pixels_num,
769 const BakeTargets *targets,
770 const StringRef uv_layer,
771 int margin)
772{
773 const bke::AttributeAccessor attributes = mesh->attributes();
774 VArraySpan<float2> uv_map;
775 if (uv_layer.is_empty()) {
776 const StringRef active_layer_name = mesh->active_uv_map_name();
777 uv_map = *attributes.lookup<float2>(active_layer_name, bke::AttrDomain::Corner);
778 }
779 else {
780 uv_map = *attributes.lookup<float2>(uv_layer, bke::AttrDomain::Corner);
781 }
782
783 if (uv_map.is_empty()) {
784 return;
785 }
786
787 BakeDataZSpan bd;
788 bd.pixel_array = pixel_array;
789 bd.zspan = MEM_new_array_zeroed<ZSpan>(targets->images_num, "bake zspan");
790
791 /* initialize all pixel arrays so we know which ones are 'blank' */
792 for (int i = 0; i < pixels_num; i++) {
793 pixel_array[i].primitive_id = -1;
794 pixel_array[i].object_id = 0;
795 pixel_array[i].is_margin = false;
796 }
797
798 for (int i = 0; i < targets->images_num; i++) {
799 zbuf_alloc_span(&bd.zspan[i], targets->images[i].width, targets->images[i].height);
800 }
801
802 const int tottri = poly_to_tri_count(mesh->faces_num, mesh->corners_num);
803 int3 *corner_tris = MEM_new_array_uninitialized<int3>(size_t(tottri), __func__);
804
806 mesh->vert_positions(), mesh->faces(), mesh->corner_verts(), {corner_tris, tottri});
807
808 const Span<int> tri_faces = mesh->corner_tri_faces();
809 const VArraySpan material_indices = *attributes.lookup<int>("material_index",
811
812 const int materials_num = targets->materials_num;
813
814 for (int fill = 0; fill < 2; fill++) {
815 for (int i = 0; i < tottri; i++) {
816 const int3 &tri = corner_tris[i];
817 const int face_i = tri_faces[i];
818
819 bd.primitive_id = i;
820
821 /* Find images matching this material. */
822 const int material_index = (!material_indices.is_empty() && materials_num) ?
823 clamp_i(material_indices[face_i], 0, materials_num - 1) :
824 0;
825 Image *image = targets->material_to_image[material_index];
826 for (int image_id = 0; image_id < targets->images_num; image_id++) {
827 BakeImage *bk_image = &targets->images[image_id];
828 if (bk_image->image != image) {
829 continue;
830 }
831
832 /* Compute triangle vertex UV coordinates. */
833 float vec[3][2];
834 for (int a = 0; a < 3; a++) {
835 const float2 &uv = uv_map[tri[a]];
836
837 vec[a][0] = (uv[0] - bk_image->uv_offset[0]) * float(bk_image->width);
838 vec[a][1] = (uv[1] - bk_image->uv_offset[1]) * float(bk_image->height);
839 }
840
841 /* Rasterize triangle. */
842 bd.bk_image = bk_image;
843 bake_differentials(&bd, vec[0], vec[1], vec[2]);
844
845 if (!fill) {
847 static_cast<void *>(&bd),
848 vec[0],
849 vec[1],
850 vec[2],
852 }
853 else {
854 zspan_rasterize_triangle(&bd.zspan[image_id],
855 static_cast<void *>(&bd),
856 vec[0],
857 vec[1],
858 vec[2],
860 }
861 }
862 }
863 }
864
865 for (int i = 0; i < targets->images_num; i++) {
866 zbuf_free_span(&bd.zspan[i]);
867 }
868
869 if (margin > 0) {
870 /* 4 pixels should be enough for denoiser to distinguish rendered
871 * pixels from the background. */
872 bake_pixels_add_denoise_margin(pixel_array, targets, std::min(margin, 4));
873 }
874
875 MEM_delete(corner_tris);
876 MEM_delete(bd.zspan);
877}
878
879/* ******************** NORMALS ************************ */
880
881static void normal_compress(float out[3],
882 const float in[3],
883 const eBakeNormalSwizzle normal_swizzle[3])
884{
885 const int swizzle_index[6] = {
886 0, /* R_BAKE_POSX */
887 1, /* R_BAKE_POSY */
888 2, /* R_BAKE_POSZ */
889 0, /* R_BAKE_NEGX */
890 1, /* R_BAKE_NEGY */
891 2, /* R_BAKE_NEGZ */
892 };
893 const float swizzle_sign[6] = {
894 +1.0f, /* R_BAKE_POSX */
895 +1.0f, /* R_BAKE_POSY */
896 +1.0f, /* R_BAKE_POSZ */
897 -1.0f, /* R_BAKE_NEGX */
898 -1.0f, /* R_BAKE_NEGY */
899 -1.0f, /* R_BAKE_NEGZ */
900 };
901
902 int i;
903
904 for (i = 0; i < 3; i++) {
905 int index;
906 float sign;
907
908 sign = swizzle_sign[normal_swizzle[i]];
909 index = swizzle_index[normal_swizzle[i]];
910
911 /*
912 * There is a small 1e-5f bias for precision issues. otherwise
913 * we randomly get 127 or 128 for neutral colors in tangent maps.
914 * we choose 128 because it is the convention flat color. *
915 */
916
917 out[i] = sign * in[index] / 2.0f + 0.5f + 1e-5f;
918 }
919}
920
922 const size_t pixels_num,
923 const int depth,
924 float result[],
925 Mesh *mesh,
926 const eBakeNormalSwizzle normal_swizzle[3],
927 const float mat[4][4])
928{
929 size_t i;
930
931 TriTessFace *triangles;
932
933 Mesh *mesh_eval = BKE_mesh_copy_for_eval(*mesh);
934
935 triangles = mesh_calc_tri_tessface(mesh, true, mesh_eval);
936
937 BLI_assert(pixels_num >= 3);
938
939 for (i = 0; i < pixels_num; i++) {
940 TriTessFace *triangle;
941 float tangents[3][3];
942 float normals[3][3];
943 float signs[3];
944 int j;
945
946 float tangent[3];
947 float normal[3];
948 float binormal[3];
949 float sign;
950 float u, v, w;
951
952 float tsm[3][3]; /* tangent space matrix */
953 float itsm[3][3];
954
955 size_t offset;
956 float nor[3]; /* texture normal */
957
958 bool is_smooth;
959
960 int primitive_id = pixel_array[i].primitive_id;
961
962 offset = i * depth;
963
964 if (primitive_id == -1) {
965 if (depth == 4) {
966 copy_v4_fl4(&result[offset], 0.5f, 0.5f, 1.0f, 1.0f);
967 }
968 else {
969 copy_v3_fl3(&result[offset], 0.5f, 0.5f, 1.0f);
970 }
971 continue;
972 }
973
974 triangle = &triangles[primitive_id];
975 is_smooth = triangle->is_smooth;
976
977 for (j = 0; j < 3; j++) {
978 const float4 *ts;
979
980 if (is_smooth) {
981 if (triangle->loop_normal[j]) {
982 copy_v3_v3(normals[j], triangle->loop_normal[j]);
983 }
984 else {
985 copy_v3_v3(normals[j], triangle->vert_normals[j]);
986 }
987 }
988
989 ts = &triangle->tspace[j];
990 copy_v3_v3(tangents[j], ts->xyz());
991 signs[j] = ts->w;
992 }
993
994 u = pixel_array[i].uv[0];
995 v = pixel_array[i].uv[1];
996 w = 1.0f - u - v;
997
998 /* normal */
999 if (is_smooth) {
1000 interp_barycentric_tri_v3(normals, u, v, normal);
1001 }
1002 else {
1003 copy_v3_v3(normal, triangle->normal);
1004 }
1005
1006 /* tangent */
1007 interp_barycentric_tri_v3(tangents, u, v, tangent);
1008
1009 /* sign */
1010 /* The sign is the same at all face vertices for any non degenerate face.
1011 * Just in case we clamp the interpolated value though. */
1012 sign = (signs[0] * u + signs[1] * v + signs[2] * w) < 0 ? (-1.0f) : 1.0f;
1013
1014 /* binormal */
1015 /* `B = sign * cross(N, T)` */
1016 cross_v3_v3v3(binormal, normal, tangent);
1017 mul_v3_fl(binormal, sign);
1018
1019 /* populate tangent space matrix */
1020 copy_v3_v3(tsm[0], tangent);
1021 copy_v3_v3(tsm[1], binormal);
1022 copy_v3_v3(tsm[2], normal);
1023
1024 /* texture values */
1025 copy_v3_v3(nor, &result[offset]);
1026
1027 /* converts from world space to local space */
1028 mul_transposed_mat3_m4_v3(mat, nor);
1029
1030 invert_m3_m3(itsm, tsm);
1031 mul_m3_v3(itsm, nor);
1032 normalize_v3(nor);
1033
1034 /* save back the values */
1035 normal_compress(&result[offset], nor, normal_swizzle);
1036 }
1037
1038 /* garbage collection */
1039 MEM_delete(triangles);
1040
1041 if (mesh_eval) {
1042 BKE_id_free(nullptr, mesh_eval);
1043 }
1044}
1045
1047 const size_t pixels_num,
1048 const int depth,
1049 float result[],
1050 Object *ob,
1051 const eBakeNormalSwizzle normal_swizzle[3])
1052{
1053 size_t i;
1054 float iobmat[4][4];
1055
1056 invert_m4_m4(iobmat, ob->object_to_world().ptr());
1057
1058 for (i = 0; i < pixels_num; i++) {
1059 size_t offset;
1060 float nor[3];
1061
1062 if (pixel_array[i].primitive_id == -1) {
1063 continue;
1064 }
1065
1066 offset = i * depth;
1067 copy_v3_v3(nor, &result[offset]);
1068
1069 /* rotates only without translation */
1070 mul_mat3_m4_v3(iobmat, nor);
1071 normalize_v3(nor);
1072
1073 /* save back the values */
1074 normal_compress(&result[offset], nor, normal_swizzle);
1075 }
1076}
1077
1079 const size_t pixels_num,
1080 const int depth,
1081 float result[],
1082 const eBakeNormalSwizzle normal_swizzle[3])
1083{
1084 size_t i;
1085
1086 for (i = 0; i < pixels_num; i++) {
1087 size_t offset;
1088 float nor[3];
1089
1090 if (pixel_array[i].primitive_id == -1) {
1091 continue;
1092 }
1093
1094 offset = i * depth;
1095 copy_v3_v3(nor, &result[offset]);
1096
1097 /* save back the values */
1098 normal_compress(&result[offset], nor, normal_swizzle);
1099 }
1100}
1101
1102void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
1103{
1104 ImBuf *ibuf;
1105 void *lock;
1106
1107 const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1108 const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1109 const float nor_alpha[4] = {0.5f, 0.5f, 1.0f, 0.0f};
1110 const float nor_solid[4] = {0.5f, 0.5f, 1.0f, 1.0f};
1111
1112 ibuf = BKE_image_acquire_ibuf(image, nullptr, &lock);
1113 BLI_assert(ibuf);
1114
1115 if (is_tangent) {
1116 IMB_rectfill(ibuf, ibuf->can_contain_alpha() ? nor_alpha : nor_solid);
1117 }
1118 else {
1119 IMB_rectfill(ibuf, ibuf->can_contain_alpha() ? vec_alpha : vec_solid);
1120 }
1121
1123}
1124
1125/* ************************************************************* */
1126
1127int RE_pass_depth(const eScenePassType pass_type)
1128{
1129 /* IMB_buffer_byte_from_float assumes 4 channels
1130 * making it work for now - XXX */
1131 return 4;
1132
1133 switch (pass_type) {
1134 case SCE_PASS_DEPTH:
1135 case SCE_PASS_AO:
1136 case SCE_PASS_MIST: {
1137 return 1;
1138 }
1139 case SCE_PASS_UV: {
1140 return 2;
1141 }
1142 case SCE_PASS_COMBINED:
1143 case SCE_PASS_SHADOW:
1144 case SCE_PASS_POSITION:
1145 case SCE_PASS_NORMAL:
1146 case SCE_PASS_VECTOR:
1147 case SCE_PASS_INDEXOB: /* XXX double check */
1148 case SCE_PASS_EMIT:
1150 case SCE_PASS_INDEXMA:
1163 default: {
1164 return 3;
1165 }
1166 }
1167}
1168
1169} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define BVH_RAYCAST_DIST_MAX
#define FILTER_MASK_USED
Definition IMB_imbuf.hh:347
#define FILTER_MASK_MARGIN
Definition IMB_imbuf.hh:346
#define FILTER_MASK_NULL
Definition IMB_imbuf.hh:345
Read Blender memory allocation and freeing.
volatile int lock
#define A
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const T * data() const
Definition BLI_array.hh:323
constexpr IndexRange()=default
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr bool is_empty() const
Definition BLI_span.hh:264
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
std::optional< RayHit > ray_intersect(const Ray &ray) const
Definition bvh.cc:439
constexpr bool is_empty() const
constexpr bool is_empty() const
#define FLT_MAX
#define FLT_EPSILON
#define position
constexpr T sign(T) RET
float distance_squared(VecT a, VecT b)
#define UINT_MAX
Definition hash_md5.cc:46
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
Array< Array< float4 > > calc_uv_tangents(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > corner_tri_faces, Span< bool > sharp_faces, Span< float3 > vert_normals, Span< float3 > face_normals, Span< float3 > corner_normals, Span< Span< float2 > > uv_maps)
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
void corner_tris_calc_with_normals(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< float3 > face_normals, MutableSpan< int3 > corner_tris)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
static TriTessFace * mesh_calc_tri_tessface(Mesh *mesh, bool tangent, Mesh *mesh_eval)
Definition bake.cc:444
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
Definition bake.cc:1102
void mul_m3_v3(const float M[3][3], float r[3])
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
Definition rectop.cc:1309
constexpr float max_ray_distance
static void bake_pixels_add_denoise_margin(BakePixel pixel_array[], const BakeTargets *targets, int margin)
Definition bake.cc:701
static bool cast_ray_highpoly(const bke::bvh::Tree **treeData, TriTessFace *triangle_low, TriTessFace *triangles[], BakePixel *pixel_array_low, BakePixel *pixel_array, const float mat_low[4][4], BakeHighPolyData *highpoly, const int highpoly_num, MutableSpan< std::optional< bke::bvh::RayHit > > hits, const float co[3], const float dir[3], const int pixel_id, const float max_ray_distance)
Definition bake.cc:319
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ R_BAKE_ADJACENT_FACES
void zspan_rasterize_conservative_wireframe(ZSpan *zspan, void *handle, const float *v1, const float *v2, const float *v3, void(*func)(void *, int, int, float, float))
Definition zbuf.cc:330
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
int RE_pass_depth(const eScenePassType pass_type)
Definition bake.cc:1127
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void zbuf_alloc_span(ZSpan *zspan, int rectx, int recty)
Definition zbuf.cc:33
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf, char *mask, const int margin, const Mesh *mesh, StringRef uv_layer, const float uv_offset[2], const bool conservative)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
static void store_bake_pixel(void *handle, int x, int y, float u, float v)
Definition bake.cc:103
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool RE_bake_pixels_populate_from_objects(Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int highpoly_num, const size_t pixels_num, const bool is_custom_cage, const float cage_extrusion, const float max_ray_distance, const float mat_low[4][4], const float mat_cage[4][4], Mesh *me_cage)
Definition bake.cc:542
MINLINE void mul_v3_fl(float r[3], float f)
void zbuf_free_span(ZSpan *zspan)
Definition zbuf.cc:44
static void calc_point_from_barycentric_extrusion(TriTessFace *triangles, const float mat[4][4], const float imat[4][4], int primitive_id, float u, float v, float cage_extrusion, float r_co[3], float r_dir[3], const bool is_cage)
Definition bake.cc:221
VecBase< float, 4 > float4
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
@ SCE_PASS_SUBSURFACE_INDIRECT
@ SCE_PASS_GLOSSY_COLOR
@ SCE_PASS_TRANSM_COLOR
@ SCE_PASS_GLOSSY_DIRECT
@ SCE_PASS_GLOSSY_INDIRECT
@ SCE_PASS_TRANSM_INDIRECT
@ SCE_PASS_DIFFUSE_COLOR
@ SCE_PASS_TRANSM_DIRECT
@ SCE_PASS_DIFFUSE_INDIRECT
@ SCE_PASS_DIFFUSE_DIRECT
@ SCE_PASS_SUBSURFACE_COLOR
@ SCE_PASS_SUBSURFACE_DIRECT
void RE_bake_pixels_populate(Mesh *mesh, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const StringRef uv_layer, int margin)
Definition bake.cc:766
void RE_bake_normal_world_to_tangent(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Mesh *mesh, const eBakeNormalSwizzle normal_swizzle[3], const float mat[4][4])
Definition bake.cc:921
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
BMesh const char void * data
void RE_bake_normal_world_to_object(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Object *ob, const eBakeNormalSwizzle normal_swizzle[3])
Definition bake.cc:1046
MINLINE void negate_v3(float r[3])
static void barycentric_differentials_from_position(const float co[3], const float v1[3], const float v2[3], const float v3[3], const float dxco[3], const float dyco[3], const float facenor[3], const bool differentials, float *u, float *v, float *dx_u, float *dx_v, float *dy_u, float *dy_v)
Definition bake.cc:274
int BKE_mesh_runtime_corner_tris_len(const Mesh *mesh)
MINLINE void copy_v2_fl2(float v[2], float x, float y)
void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
Definition bake.cc:128
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
void BKE_id_free(Main *bmain, void *idv)
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
static void bake_differentials(BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
Definition bake.cc:676
void RE_bake_normal_world_to_world(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], const eBakeNormalSwizzle normal_swizzle[3])
Definition bake.cc:1078
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
void interp_barycentric_tri_v3(float data[3][3], float u, float v, float res[3])
static void normal_compress(float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
Definition bake.cc:881
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
Definition filter.cc:203
void zspan_rasterize_triangle(ZSpan *zspan, void *handle, const float *v1_in, const float *v2_in, const float *v3_in, void(*func)(void *, int, int, float, float))
Definition zbuf.cc:160
VecBase< float, 3 > float3
void RE_bake_margin(ImBuf *ibuf, char *mask, const int margin, const char margin_type, const Mesh *mesh, const StringRef uv_layer, const float uv_offset[2])
Definition bake.cc:145
MINLINE int clamp_i(int value, int min, int max)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void calc_point_from_barycentric_cage(TriTessFace *triangles_low, TriTessFace *triangles_cage, const float mat_low[4][4], const float mat_cage[4][4], int primitive_id, float u, float v, float r_co[3], float r_dir[3])
Definition bake.cc:178
bool BKE_mesh_face_normals_are_dirty(const Mesh *mesh)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
#define fabsf
BakePixel * pixel_array
Definition bake.cc:86
BakeImage * bk_image
Definition bake.cc:88
struct Mesh * mesh
Definition RE_bake.h:67
float uv_offset[2]
Definition RE_bake.h:27
struct Image * image
Definition RE_bake.h:25
struct Image ** material_to_image
Definition RE_bake.h:42
BakeImage * images
Definition RE_bake.h:38
bool can_contain_alpha() const
float4 tspace[3]
Definition bake.cc:97
const float * vert_normals[3]
Definition bake.cc:96
const float * loop_normal[3]
Definition bake.cc:98
float normal[3]
Definition bake.cc:99
const float * positions[3]
Definition bake.cc:95