97 const float *positions[3];
112 const int i = offset + y * width +
x;
122 pixel->du_dx = bd->
du_dx;
123 pixel->du_dy = bd->
du_dy;
124 pixel->dv_dx = bd->
dv_dx;
125 pixel->dv_dy = bd->
dv_dy;
126 pixel->object_id = 0;
138 for (i = 0; i < pixels_num; i++) {
139 if (pixel_array[i].primitive_id != -1) {
148 const char margin_type,
150 char const *uv_layer,
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++) {
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]);
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],
329 const int tot_highpoly,
330 const float max_ray_distance)
334 float hit_distance_squared = max_ray_distance * max_ray_distance;
335 if (hit_distance_squared == 0.0f) {
337 hit_distance_squared =
FLT_MAX;
344 for (i = 0; i < tot_highpoly; i++) {
345 float co_high[3], dir_high[3];
359 if (treeData[i].
tree) {
369 if (hits[i].index != -1) {
372 mul_v3_m4v3(hit_world, highpoly[i].obmat, hits[i].co);
375 if (distance_squared < hit_distance_squared) {
377 hit_distance_squared = distance_squared;
382 if (hit_mesh != -1) {
383 int primitive_id_high = hits[hit_mesh].
index;
384 TriTessFace *triangle_high = &triangles[hit_mesh][primitive_id_high];
385 BakePixel *pixel_low = &pixel_array_low[pixel_id];
386 BakePixel *pixel_high = &pixel_array[pixel_id];
390 pixel_high->
seed = pixel_id;
398 float duco_low[3], dvco_low[3], dxco[3], dyco[3];
436 BLI_assert(pixel_high->
uv[0] >= -1e-3f && pixel_high->
uv[1] >= -1e-3f &&
437 pixel_high->
uv[0] + pixel_high->
uv[1] <= 1.0f + 1e-3f);
442 pixel_array[pixel_id].
seed = 0;
446 return hit_mesh != -1;
470 attributes.lookup_or_default<
bool>(
"sharp_face", bke::AttrDomain::Face,
false).varray;
473 MEM_mallocN(
sizeof(*corner_tris) * tottri, __func__));
478 if (!calculate_normal) {
479 precomputed_normals = mesh->face_normals();
482 if (!precomputed_normals.
is_empty()) {
484 positions, faces, corner_verts, precomputed_normals, {corner_tris, tottri});
490 const TSpace *tspace =
nullptr;
495 tspace =
static_cast<const TSpace *
>(
499 corner_normals = mesh_eval->corner_normals();
504 for (i = 0; i < tottri; i++) {
505 const int3 &tri = corner_tris[i];
506 const int face_i = tri_faces[i];
508 triangles[i].positions[0] = positions[corner_verts[tri[0]]];
509 triangles[i].positions[1] = positions[corner_verts[tri[1]]];
510 triangles[i].positions[2] = positions[corner_verts[tri[2]]];
511 triangles[i].vert_normals[0] = vert_normals[corner_verts[tri[0]]];
512 triangles[i].vert_normals[1] = vert_normals[corner_verts[tri[1]]];
513 triangles[i].vert_normals[2] = vert_normals[corner_verts[tri[2]]];
514 triangles[i].is_smooth = !sharp_faces[face_i];
517 triangles[i].tspace[0] = &tspace[tri[0]];
518 triangles[i].tspace[1] = &tspace[tri[1]];
519 triangles[i].tspace[2] = &tspace[tri[2]];
523 triangles[i].loop_normal[0] = corner_normals[tri[0]];
524 triangles[i].loop_normal[1] = corner_normals[tri[1]];
525 triangles[i].loop_normal[2] = corner_normals[tri[2]];
528 if (calculate_normal) {
529 if (face_i != mpoly_prev) {
536 copy_v3_v3(triangles[i].normal, precomputed_normals[face_i]);
549 const int tot_highpoly,
550 const size_t pixels_num,
551 const bool is_custom_cage,
552 const float cage_extrusion,
553 const float max_ray_distance,
554 const float mat_low[4][4],
555 const float mat_cage[4][4],
561 float imat_low[4][4];
562 bool is_cage = me_cage !=
nullptr;
565 Mesh *me_eval_low =
nullptr;
574 tris_high = MEM_cnew_array<TriTessFace *>(tot_highpoly,
"MVerts Highpoly Mesh Array");
577 me_highpoly =
static_cast<Mesh **
>(
578 MEM_mallocN(
sizeof(
Mesh *) * tot_highpoly,
"Highpoly Derived Meshes"));
585 else if (is_custom_cage) {
595 for (i = 0; i < tot_highpoly; i++) {
598 me_highpoly[i] = highpoly[i].
mesh;
604 if (treeData[i].
tree ==
nullptr) {
605 printf(
"Baking: out of memory while creating BHVTree for object \"%s\"\n",
606 highpoly[i].ob->id.name + 2);
613 for (i = 0; i < pixels_num; i++) {
620 if (primitive_id == -1) {
625 u = pixel_array_from[i].
uv[0];
626 v = pixel_array_from[i].
uv[1];
629 if (is_custom_cage) {
631 tris_low, tris_cage, mat_low, mat_cage, primitive_id, u,
v, co, dir);
632 tri_low = &tris_cage[primitive_id];
636 tris_cage, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
true);
637 tri_low = &tris_cage[primitive_id];
641 tris_low, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
false);
642 tri_low = &tris_low[primitive_id];
666 for (i = 0; i < tot_highpoly; i++) {
698 A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
700 if (
fabsf(A) > FLT_EPSILON) {
703 bd->
du_dx = (uv2[1] - uv3[1]) * A;
704 bd->
dv_dx = (uv3[1] - uv1[1]) * A;
706 bd->
du_dy = (uv3[0] - uv2[0]) * A;
707 bd->
dv_dy = (uv1[0] - uv3[0]) * A;
717 const size_t pixels_num,
719 const char *uv_layer)
722 const float(*mloopuv)[2];
723 if ((uv_layer ==
nullptr) || (uv_layer[0] ==
'\0')) {
724 mloopuv =
static_cast<const float(*)[2]
>(
729 mloopuv =
static_cast<const float(*)[2]
>(
733 if (mloopuv ==
nullptr) {
742 for (
int i = 0; i < pixels_num; i++) {
747 for (
int i = 0; i < targets->
images_num; i++) {
753 MEM_mallocN(
sizeof(*corner_tris) * tottri, __func__));
756 mesh->vert_positions(), mesh->faces(), mesh->corner_verts(), {corner_tris, tottri});
760 const VArraySpan material_indices = *attributes.lookup<
int>(
"material_index",
761 bke::AttrDomain::Face);
765 for (
int i = 0; i < tottri; i++) {
766 const int3 &tri = corner_tris[i];
767 const int face_i = tri_faces[i];
772 const int material_index = (!material_indices.
is_empty() && materials_num) ?
773 clamp_i(material_indices[face_i], 0, materials_num - 1) :
776 for (
int image_id = 0; image_id < targets->
images_num; image_id++) {
778 if (bk_image->
image != image) {
784 for (
int a = 0; a < 3; a++) {
785 const float *uv = mloopuv[tri[a]];
791 vec[a][0] = (uv[0] - bk_image->
uv_offset[0]) *
float(bk_image->
width) - (0.5f + 0.001f);
792 vec[a][1] = (uv[1] - bk_image->
uv_offset[1]) *
float(bk_image->
height) - (0.5f + 0.002f);
803 for (
int i = 0; i < targets->
images_num; i++) {
817 const int swizzle_index[6] = {
825 const float swizzle_sign[6] = {
836 for (i = 0; i < 3; i++) {
840 sign = swizzle_sign[normal_swizzle[i]];
841 index = swizzle_index[normal_swizzle[i]];
849 out[i] = sign * in[index] / 2.0f + 0.5f + 1e-5f;
854 const size_t pixels_num,
859 const float mat[4][4])
871 for (i = 0; i < pixels_num; i++) {
873 float tangents[3][3];
896 if (primitive_id == -1) {
898 copy_v4_fl4(&result[offset], 0.5f, 0.5f, 1.0f, 1.0f);
906 triangle = &triangles[primitive_id];
909 for (j = 0; j < 3; j++) {
926 u = pixel_array[i].
uv[0];
927 v = pixel_array[i].
uv[1];
944 sign = (signs[0] * u + signs[1] *
v + signs[2] *
w) < 0 ? (-1.0f) : 1.0f;
979 const size_t pixels_num,
990 for (i = 0; i < pixels_num; i++) {
994 if (pixel_array[i].primitive_id == -1) {
1011 const size_t pixels_num,
1018 for (i = 0; i < pixels_num; i++) {
1022 if (pixel_array[i].primitive_id == -1) {
1039 const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1040 const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1041 const float nor_alpha[4] = {0.5f, 0.5f, 1.0f, 0.0f};
1042 const float nor_solid[4] = {0.5f, 0.5f, 1.0f, 1.0f};
1065 switch (pass_type) {
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
BVHTree * BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data, const Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
@ BVHTREE_FROM_CORNER_TRIS
CustomData interface, see also DNA_customdata_types.h.
const void * CustomData_get_layer_n(const CustomData *data, eCustomDataType type, int n)
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, blender::StringRef name)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void BKE_id_free(Main *bmain, void *idv)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
bool BKE_mesh_face_normals_are_dirty(const Mesh *mesh)
int BKE_mesh_runtime_corner_tris_len(const Mesh *mesh)
void BKE_mesh_calc_loop_tangents(Mesh *mesh_eval, bool calc_active_tangent, const char(*tangent_names)[MAX_CUSTOMDATA_LAYER_NAME], int tangent_names_len)
#define BVH_RAYCAST_DIST_MAX
int BLI_bvhtree_ray_cast(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
MINLINE int clamp_i(int value, int min, int max)
void interp_barycentric_tri_v3(float data[3][3], float u, float v, float res[3])
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
void mul_m3_v3(const float M[3][3], float r[3])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_m4_v3(const float M[4][4], float r[3])
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v2_fl2(float v[2], float x, float y)
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
@ SCE_PASS_SUBSURFACE_INDIRECT
@ SCE_PASS_SUBSURFACE_COLOR
@ SCE_PASS_DIFFUSE_DIRECT
@ SCE_PASS_GLOSSY_INDIRECT
@ SCE_PASS_TRANSM_INDIRECT
@ SCE_PASS_DIFFUSE_INDIRECT
@ SCE_PASS_SUBSURFACE_DIRECT
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
void IMB_rectfill(ImBuf *drect, const float col[4])
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
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])
int RE_pass_depth(const eScenePassType pass_type)
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)
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])
static void store_bake_pixel(void *handle, int x, int y, float u, float v)
void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
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)
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])
void RE_bake_pixels_populate(Mesh *mesh, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const char *uv_layer)
static void normal_compress(float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
static bool cast_ray_highpoly(BVHTreeFromMesh *treeData, TriTessFace *triangle_low, TriTessFace *triangles[], BakePixel *pixel_array_low, BakePixel *pixel_array, const float mat_low[4][4], BakeHighPolyData *highpoly, const float co[3], const float dir[3], const int pixel_id, const int tot_highpoly, const float max_ray_distance)
void RE_bake_margin(ImBuf *ibuf, char *mask, const int margin, const char margin_type, const Mesh *mesh, char const *uv_layer, const float uv_offset[2])
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])
bool RE_bake_pixels_populate_from_objects(Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int tot_highpoly, 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)
static TriTessFace * mesh_calc_tri_tessface(Mesh *mesh, bool tangent, Mesh *mesh_eval)
void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
static void bake_differentials(BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
static void raycast_callback(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr Span slice(int64_t start, int64_t size) const
constexpr bool is_empty() const
draw_view in_light_buf[] float
void *(* MEM_mallocN)(size_t len, const char *str)
void MEM_freeN(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
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)
Frequency::GEOMETRY nor[]
struct Image ** material_to_image
const float * loop_normal[3]
const float * vert_normals[3]
const float * positions[3]
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf, char *mask, const int margin, const Mesh *mesh, char const *uv_layer, const float uv_offset[2])
void zspan_scanconvert(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void(*func)(void *, int, int, float, float))
void zbuf_alloc_span(ZSpan *zspan, int rectx, int recty)
void zbuf_free_span(ZSpan *zspan)