[Bf-blender-cvs] [93efdd624b5] temp-vert-normals-cleanup: Merge branch 'master' into temp-vert-normals-cleanup
Hans Goudey
noreply at git.blender.org
Thu Dec 9 21:30:38 CET 2021
Commit: 93efdd624b57815607cb012d6da679c98120aaf8
Author: Hans Goudey
Date: Thu Dec 9 13:55:37 2021 -0600
Branches: temp-vert-normals-cleanup
https://developer.blender.org/rB93efdd624b57815607cb012d6da679c98120aaf8
Merge branch 'master' into temp-vert-normals-cleanup
===================================================================
===================================================================
diff --cc source/blender/blenkernel/BKE_mesh.h
index 123dc230aa5,1e7262248d5..0cdbab879a3
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@@ -300,42 -382,23 +394,46 @@@ void BKE_mesh_calc_normals_poly(const s
const struct MPoly *mpoly,
int mpoly_len,
float (*r_poly_normals)[3]);
-void BKE_mesh_calc_normals_poly_and_vertex(struct MVert *mvert,
- int mvert_len,
- const struct MLoop *mloop,
- int mloop_len,
- const struct MPoly *mpolys,
- int mpoly_len,
- float (*r_poly_normals)[3],
- float (*r_vert_normals)[3]);
-/**
- * \note this does not update the #CD_NORMAL layer,
- * but does update the normals in the #CD_MVERT layer.
+
+/**
+ * Calculate vertex and face normals, storing the result in custom data layers on the mesh.
+ *
+ * \note It is usually preferrable to calculate normals lazily with
+ * #BKE_mesh_ensure_vertex_normals, but some areas (perhaps unnecessarily)
+ * can also calculate them eagerly.
*/
void BKE_mesh_calc_normals(struct Mesh *me);
-void BKE_mesh_ensure_normals(struct Mesh *me);
+
+/**
+ * Check that a mesh with non-dirty normals has vertex and face custom data layers.
+ */
+void BKE_mesh_assert_normals_dirty_or_calculated(const struct Mesh *mesh);
+
+/**
+ * Make sure the vertex normal data layer exists and return it.
+ * Used for manually assigning vertex normals. Clears the dirty flag.
+ */
+float (*BKE_mesh_vertex_normals_for_write(struct Mesh *mesh))[3];
+
+/**
+ * Make sure the face normal data layer exists and return it.
+ * Used for manually assigning face normals. Clears the dirty flag.
+ */
+float (*BKE_mesh_face_normals_for_write(struct Mesh *mesh))[3];
+
+/**
+ * \warning May still return null if the mesh is empty.
+ */
+const float (*BKE_mesh_ensure_vertex_normals(const struct Mesh *mesh))[3];
+
+/**
+ * \warning May still return null if the mesh is empty.
+ */
+const float (*BKE_mesh_ensure_face_normals(const struct Mesh *mesh))[3];
++
+ /**
+ * Called after calculating all modifiers.
+ */
void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh);
void BKE_mesh_calc_normals_looptri(struct MVert *mverts,
int numVerts,
diff --cc source/blender/blenkernel/BKE_mesh_iterators.h
index 65c3124f0e3,83f0228dc76..a3f3851e787
--- a/source/blender/blenkernel/BKE_mesh_iterators.h
+++ b/source/blender/blenkernel/BKE_mesh_iterators.h
@@@ -31,11 -31,19 +31,17 @@@ typedef enum MeshForeachFlag
MESH_FOREACH_USE_NORMAL = (1 << 0),
} MeshForeachFlag;
-void BKE_mesh_foreach_mapped_vert(struct Mesh *mesh,
- void (*func)(void *userData,
- int index,
- const float co[3],
- const float no_f[3],
- const short no_s[3]),
- void *userData,
- MeshForeachFlag flag);
+void BKE_mesh_foreach_mapped_vert(
+ struct Mesh *mesh,
+ void (*func)(void *userData, int index, const float co[3], const float no[3]),
+ void *userData,
+ MeshForeachFlag flag);
++
+ /**
+ * Copied from #cdDM_foreachMappedEdge.
+ * \param tot_edges: Number of original edges. Used to avoid calling the callback with invalid
+ * edge indices.
+ */
void BKE_mesh_foreach_mapped_edge(
struct Mesh *mesh,
int tot_edges,
diff --cc source/blender/blenkernel/BKE_particle.h
index e2bbe99cb51,e3d59599ab3..53e74de5ac7
--- a/source/blender/blenkernel/BKE_particle.h
+++ b/source/blender/blenkernel/BKE_particle.h
@@@ -544,8 -599,10 +599,11 @@@ void psys_get_texture(struct ParticleSi
struct ParticleTexture *ptex,
int event,
float cfra);
+ /**
+ * Interpolate a location on a face based on face coordinates.
+ */
void psys_interpolate_face(struct MVert *mvert,
+ const float (*vert_normals)[3],
struct MFace *mface,
struct MTFace *tface,
float (*orcodata)[3],
diff --cc source/blender/blenkernel/intern/mesh.cc
index 23ed26f213c,5f8d4ccb734..427b49f0f0f
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@@ -1931,13 -1857,18 +1884,6 @@@ void BKE_mesh_vert_coords_apply_with_ma
BKE_mesh_normals_tag_dirty(mesh);
}
- /**
- * Compute 'split' (aka loop, or per face corner's) normals.
- *
- * \param r_lnors_spacearr: Allows to get computed loop normal space array.
- * That data, among other things, contains 'smooth fan' info, useful e.g.
- * to split geometry along sharp edges...
- */
-void BKE_mesh_vert_normals_apply(Mesh *mesh, const short (*vert_normals)[3])
-{
- /* This will just return the pointer if it wasn't a referenced layer. */
- MVert *mv = (MVert *)CustomData_duplicate_referenced_layer(
- &mesh->vdata, CD_MVERT, mesh->totvert);
- mesh->mvert = mv;
- for (int i = 0; i < mesh->totvert; i++, mv++) {
- copy_v3_v3_short(mv->no, vert_normals[i]);
- }
- mesh->runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
-}
-
void BKE_mesh_calc_normals_split_ex(Mesh *mesh, MLoopNorSpaceArray *r_lnors_spacearr)
{
float(*r_loopnors)[3];
diff --cc source/blender/blenkernel/intern/mesh_iterators.c
index c8c9b4cba2a,3b6afc1f47a..a6608b92f88
--- a/source/blender/blenkernel/intern/mesh_iterators.c
+++ b/source/blender/blenkernel/intern/mesh_iterators.c
@@@ -34,12 -34,14 +34,11 @@@
#include "MEM_guardedalloc.h"
- /* Copied from cdDM_foreachMappedVert */
-void BKE_mesh_foreach_mapped_vert(Mesh *mesh,
- void (*func)(void *userData,
- int index,
- const float co[3],
- const float no_f[3],
- const short no_s[3]),
- void *userData,
- MeshForeachFlag flag)
+void BKE_mesh_foreach_mapped_vert(
+ Mesh *mesh,
+ void (*func)(void *userData, int index, const float co[3], const float no[3]),
+ void *userData,
+ MeshForeachFlag flag)
{
if (mesh->edit_mesh != NULL) {
BMEditMesh *em = mesh->edit_mesh;
diff --cc source/blender/blenkernel/intern/mesh_normals.cc
index 195c31da012,da5b4ccc764..2d1c50cfe8d
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@@ -167,167 -167,155 +167,164 @@@ void BKE_mesh_calc_normals_poly(const M
* meshes can slow down high-poly meshes. For details on performance, see D11993.
* \{ */
-struct MeshCalcNormalsData_PolyAndVertex {
- /** Write into vertex normals #MVert.no. */
- MVert *mvert;
- const MLoop *mloop;
- const MPoly *mpoly;
-
- /** Polygon normal output. */
- float (*pnors)[3];
- /** Vertex normal output (may be freed, copied into #MVert.no). */
- float (*vnors)[3];
-};
-
-static void mesh_calc_normals_poly_and_vertex_accum_fn(
- void *__restrict userdata, const int pidx, const TaskParallelTLS *__restrict UNUSED(tls))
+static void mesh_calc_normals_poly_and_vertex(MutableSpan<MVert> mverts,
+ Span<MLoop> loops,
+ Span<MPoly> polys,
+ MutableSpan<float3> poly_normals,
+ MutableSpan<float3> vert_normals)
{
- const MeshCalcNormalsData_PolyAndVertex *data = (MeshCalcNormalsData_PolyAndVertex *)userdata;
- const MPoly *mp = &data->mpoly[pidx];
- const MLoop *ml = &data->mloop[mp->loopstart];
- const MVert *mverts = data->mvert;
- float(*vnors)[3] = data->vnors;
-
- float pnor_temp[3];
- float *pnor = data->pnors ? data->pnors[pidx] : pnor_temp;
+ vert_normals.fill(float3(0));
+
+ /* Compute poly normals, accumulating them into vertex normals. */
+ blender::threading::parallel_for(polys.index_range(), 1024, [&](IndexRange range) {
+ for (const int i : range) {
+ const MPoly &poly = polys[i];
+ Span<MLoop> ml = loops.slice(poly.loopstart, poly.totloop);
+
+ float3 &pnor = poly_normals[i];
+
+ const int i_end = poly.totloop - 1;
+
+ /* Polygon Normal and edge-vector. */
+ /* Inline version of #BKE_mesh_calc_poly_normal, also does edge-vectors. */
+ {
+ zero_v3(pnor);
+ /* Newell's Method */
+ const float *v_curr = mverts[ml[i_end].v].co;
+ for (int i_next = 0; i_next <= i_end; i_next++) {
+ const float *v_next = mverts[ml[i_next].v].co;
+ add_newell_cross_v3_v3v3(pnor, v_curr, v_next);
+ v_curr = v_next;
+ }
+ if (UNLIKELY(normalize_v3(pnor) == 0.0f)) {
+ pnor[2] = 1.0f; /* Other axes set to zero. */
+ }
+ }
- const int i_end = mp->totloop - 1;
+ /* Accumulate angle weighted face normal into the vertex normal. */
+ /* Inline version of #accumulate_vertex_normals_poly_v3. */
+ {
+ float edvec_prev[3], edvec_next[3], edvec_end[3];
+ const float *v_curr = mverts[ml[i_end].v].co;
+ sub_v3_v3v3(edvec_prev, mverts[ml[i_end - 1].v].co, v_curr);
+ normalize_v3(edvec_prev);
+ copy_v3_v3(edvec_end, edvec_prev);
+
+ for (int i_next = 0, i_curr = i_end; i_next <= i_end; i_curr = i_next++) {
+ const float *v_next = mverts[ml[i_next].v].co;
+
+ /* Skip an extra normalization by reusing the first calculated edge. */
+ if (i_next != i_end) {
+
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list