110 const int i = offset +
y * width +
x;
136 for (i = 0; i < pixels_num; i++) {
148 const char margin_type,
151 const float uv_offset[2])
154 switch (margin_type) {
180 const float mat_low[4][4],
181 const float mat_cage[4][4],
195 triangle[0] = &triangles_low[primitive_id];
196 triangle[1] = &triangles_cage[primitive_id];
198 for (i = 0; i < 2; i++) {
222 const float mat[4][4],
223 const float imat[4][4],
227 float cage_extrusion,
239 is_smooth = triangle->
is_smooth || is_cage;
280 const float facenor[3],
281 const bool differentials,
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];
299 float detsh = (t00 * t11 - t10 * t01);
300 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
306 *u = (v3[axis1] - co[axis1]) * t11 - (v3[axis2] - co[axis2]) * t10;
307 *
v = (v3[axis2] - co[axis2]) * t00 - (v3[axis1] - co[axis1]) * t01;
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;
324 const float mat_low[4][4],
326 const int highpoly_num,
335 if (hit_distance_squared == 0.0f) {
337 hit_distance_squared =
FLT_MAX;
340 for (
int i = 0; i < highpoly_num; i++) {
341 float co_high[3], dir_high[3];
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];
378 pixel_high->
seed = pixel_id;
382 float co_high[3], dir_high[3];
388 float duco_low[3], dvco_low[3], dxco[3], dyco[3];
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);
433 pixel_array[pixel_id].
seed = 0;
437 return hit_mesh != -1;
467 if (!calculate_normal) {
468 precomputed_normals =
mesh->face_normals();
471 if (!precomputed_normals.
is_empty()) {
473 positions, faces, corner_verts, precomputed_normals, {corner_tris, tottri});
482 const StringRef active_uv_map = mesh_eval->active_uv_map_name();
487 {corner_tris, tottri},
488 mesh->corner_tri_faces(),
490 mesh->vert_normals(),
491 mesh->face_normals(),
492 mesh->corner_normals(),
494 tspace = std::move(
result[0]);
496 corner_normals = mesh_eval->corner_normals();
501 for (i = 0; i < tottri; i++) {
502 const int3 &tri = corner_tris[i];
503 const int face_i = tri_faces[i];
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];
514 triangles[i].
tspace[0] = tspace[tri[0]];
515 triangles[i].
tspace[1] = tspace[tri[1]];
516 triangles[i].
tspace[2] = tspace[tri[2]];
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]];
525 if (calculate_normal) {
526 if (face_i != mpoly_prev) {
533 copy_v3_v3(triangles[i].normal, precomputed_normals[face_i]);
537 MEM_delete(corner_tris);
546 const int highpoly_num,
547 const size_t pixels_num,
548 const bool is_custom_cage,
549 const float cage_extrusion,
551 const float mat_low[4][4],
552 const float mat_cage[4][4],
555 float imat_low[4][4];
556 bool is_cage = me_cage !=
nullptr;
559 Mesh *me_eval_low =
nullptr;
578 else if (is_custom_cage) {
588 for (
int i = 0; i < highpoly_num; i++) {
591 me_highpoly[i] = highpoly[i].
mesh;
594 treeData[i] = &me_highpoly[i]->bvh_tris();
603 if (primitive_id == -1) {
609 const float u = pixel_array_from[i].
uv[0];
610 const float v = pixel_array_from[i].
uv[1];
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];
623 tris_cage, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
true);
624 tri_low = &tris_cage[primitive_id];
628 tris_low, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
false);
629 tri_low = &tris_low[primitive_id];
654 for (
int i = 0; i < highpoly_num; i++) {
656 MEM_delete(tris_high[i]);
660 MEM_delete(tris_high);
661 MEM_delete(me_highpoly);
667 MEM_delete(tris_low);
670 MEM_delete(tris_cage);
684 A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
689 bd->
du_dx = (uv2[1] - uv3[1]) *
A;
690 bd->
dv_dx = (uv3[1] - uv1[1]) *
A;
692 bd->
du_dy = (uv3[0] - uv2[0]) *
A;
693 bd->
dv_dy = (uv1[0] - uv3[0]) *
A;
707 for (
int image_id = 0; image_id < targets->
images_num; image_id++) {
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;
715 BakePixel *bk_pixels = pixel_array + image_offset;
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;
731 copy_index = px_index - width;
734 copy_index = px_index + width;
737 copy_index = px_index - 1;
740 copy_index = px_index + 1;
747 bk_pixels[px_index] = bk_pixels[copy_index];
748 bk_pixels[px_index].
seed = px_index + image_offset;
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;
768 const size_t pixels_num,
776 const StringRef active_layer_name =
mesh->active_uv_map_name();
792 for (
int i = 0; i < pixels_num; i++) {
798 for (
int i = 0; i < targets->
images_num; i++) {
806 mesh->vert_positions(),
mesh->faces(),
mesh->corner_verts(), {corner_tris, tottri});
809 const VArraySpan material_indices = *attributes.
lookup<
int>(
"material_index",
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];
822 const int material_index = (!material_indices.
is_empty() && materials_num) ?
823 clamp_i(material_indices[face_i], 0, materials_num - 1) :
826 for (
int image_id = 0; image_id < targets->
images_num; image_id++) {
834 for (
int a = 0; a < 3; a++) {
835 const float2 &uv = uv_map[tri[a]];
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);
847 static_cast<void *
>(&bd),
855 static_cast<void *
>(&bd),
865 for (
int i = 0; i < targets->
images_num; i++) {
875 MEM_delete(corner_tris);
876 MEM_delete(bd.
zspan);
885 const int swizzle_index[6] = {
893 const float swizzle_sign[6] = {
904 for (i = 0; i < 3; i++) {
908 sign = swizzle_sign[normal_swizzle[i]];
909 index = swizzle_index[normal_swizzle[i]];
922 const size_t pixels_num,
927 const float mat[4][4])
939 for (i = 0; i < pixels_num; i++) {
941 float tangents[3][3];
964 if (primitive_id == -1) {
974 triangle = &triangles[primitive_id];
977 for (j = 0; j < 3; j++) {
989 ts = &triangle->
tspace[j];
994 u = pixel_array[i].
uv[0];
995 v = pixel_array[i].
uv[1];
1012 sign = (signs[0] * u + signs[1] *
v + signs[2] *
w) < 0 ? (-1.0f) : 1.0f;
1039 MEM_delete(triangles);
1047 const size_t pixels_num,
1058 for (i = 0; i < pixels_num; i++) {
1062 if (pixel_array[i].primitive_id == -1) {
1079 const size_t pixels_num,
1086 for (i = 0; i < pixels_num; i++) {
1090 if (pixel_array[i].primitive_id == -1) {
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};
1133 switch (pass_type) {
CustomData interface, see also DNA_customdata_types.h.
#define BVH_RAYCAST_DIST_MAX
#define FILTER_MASK_MARGIN
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr IndexRange()=default
constexpr Span slice(int64_t start, int64_t size) const
constexpr bool is_empty() const
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
constexpr bool is_empty() const
constexpr bool is_empty() const
float distance_squared(VecT a, VecT b)
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))
static TriTessFace * mesh_calc_tri_tessface(Mesh *mesh, bool tangent, Mesh *mesh_eval)
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)
void mul_m3_v3(const float M[3][3], float r[3])
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
constexpr float max_ray_distance
static void bake_pixels_add_denoise_margin(BakePixel pixel_array[], const BakeTargets *targets, int margin)
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)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
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))
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
int RE_pass_depth(const eScenePassType pass_type)
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)
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])
static void store_bake_pixel(void *handle, int x, int y, float u, float v)
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)
MINLINE void mul_v3_fl(float r[3], float f)
void zbuf_free_span(ZSpan *zspan)
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)
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_INDIRECT
@ SCE_PASS_TRANSM_INDIRECT
@ 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)
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])
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])
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)
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)
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)
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])
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])
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
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))
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])
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])
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)
struct Image ** material_to_image
bool can_contain_alpha() const
const float * vert_normals[3]
const float * loop_normal[3]
const float * positions[3]