meshMods.module
README.md
meshMods
Blender's DECIMATE, TRIANGULATE and WEIGHTED_NORMAL modifiers over meshBuf meshes (Blender space). Each function takes an MB Mesh and returns a NEW one, leaving the input untouched, with material slots (FM), smooth flags (SMF) and UV layers carried over. All per-element data stays in substrate stores (MB.words / MB.vec3s / MB.hash), never in mesh-sized Luau tables, and every scratch store is freed when the call ends, including when it fails. The modifier stack (blenderSession) uses it to evaluate these modifiers.
local Mods = require("~ZeroBlender.modules.meshMods")
local low = Mods.decimate(m, {decimate_type="COLLAPSE", ratio=0.5})
local tri = Mods.triangulate(m, {quad_method="BEAUTY", ngon_method="BEAUTY", min_vertices=4})
local wn = Mods.weightedNormal(m, {mode="FACE_AREA", weight=50, thresh=0.01}) -- wn.CNC: V3 per corner
local r = Mods.apply(m, "DECIMATE", values) -- dispatch by type
decimate(m, values)
COLLAPSE: triangulates, then does Garland-Heckbert quadric edge collapse until there are at mostratio * trianglesfaces. Boundary edges add perpendicular penalty planes (weight 100). A collapse is rejected when it would flip or degenerate a face normal, or when it would break the link condition (keeping the surface manifold). A binary heap in word stores with lazy invalidation orders the collapses. Withuse_collapse_triangulate=false, faces whose vertices never moved come back as the original quads and n-gons.use_symmetrykeeps vertices on the mirror plane on it.DISSOLVE(planar): adjacent faces join while their area-weighted normals stay withinangle_limit. Each region bounded by one simple loop becomes an n-gon, found by walking its boundary. Vertices left between two nearly collinear edges are dropped. Supportsdelimitcontaining "MATERIAL" anduse_dissolve_boundaries.UNSUBDIV: reverses grid subdivisioniterationstimes on all-quad meshes. Vertices are classified by 2-colouring the edge graph and then the quad diagonals. A mesh that is not a subdivided grid raisesNotImplementedError: DECIMATE UNSUBDIV ....
triangulate(m, values)
Faces with min_vertices or more corners are split. quad_method is one of BEAUTY, FIXED (0-2), FIXED_ALTERNATE (1-3), SHORTEST_DIAGONAL or LONGEST_DIAGONAL. ngon_method CLIP ear-clips (MB.triangulateFace), and BEAUTY ear-clips and then flips edges. Every output corner copies the UVs of its source corner exactly. keep_custom_normals carries CNC/CNV.
weightedNormal(m, values)
Follows MOD_weighted_normal.cc. Values are corner angle, face area, or both multiplied, depending on mode. Items (fans) receive them in descending order: num_set grows when a value differs from the current one by more than thresh, and the face normal is added with weight val / weight^num_set. Here weight is the modifier weight divided by 50 (32767 at 100, 1/32767 at 1, and (w-1)*25 when that is greater than 1). With keep_sharp, fans split at m.SHARP edges (a W per MB.edges(m) edge), at flat faces (m.SMF) and at boundaries. Otherwise there is one fan per vertex. use_face_influence reads m.FSTR (a W per face: -1 weak, 0 medium, 1 strong). The result is a copy of m with CNC, a V3 with one normal per corner.