mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
craft-mesh-ops
Operations on triangle meshes, for apps that paint, bake or preview materials on 3D models. The
crate has no UI, does no file or network I/O and has no unsafe code. File formats stay in
craft-mesh.
Mesh. Positions, normals, tangents, UVs, vertex colors, indices, per-material submeshes and polygon-diagonal flags, withvalidate,append,subdivided,normalize_to, wireframe and UV-seam edges, and UV-tile helpers.- Normals and tangents. Smooth normals are area weighted. Tangents come from MikkTSpace, which
glTF 2.0 recommends for meshes without tangents
(https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html):
wholds the bitangent sign.compute_tangentskeeps the indices and the vertex count; a vertex whose triangles have different tangent frames (mirrored UVs) takes the frame of its first corner.compute_tangents_splitinstead splits such a vertex into one copy per frame. Faces with coincident corners stay in the mesh and reuse shared vertex frames when available. MikkTSpace omits those faces. A mesh with more than 2,048 noncollapsed triangles around one vertex, or with non-finite positions or UVs, or whose largest absolute position or UV component is nonzero and outside 1e-6 to 1e6, gets per-corner tangents from f64 UV gradients instead of MikkTSpace. An edge-connected region containing a face whose f32 UV gradients overflow or underflow uses that fallback. Both paths read the stored glTF UVs (v down) with v negated, so an unmirrored mapping getsw = +1. The generated corner frames of a triangle with usable UV gradients share one handedness sign. Withcompute_tangents, a vertex shared by mirrored triangles keeps the first triangle's frame, so the stored signs of the other triangle can differ; usecompute_tangents_splitwhen both sides of a mirrored seam need their own frames. It splits only a mesh whose indices are in range, whose index count is a multiple of 3 and whose colors are empty or one per vertex; any other mesh gets thecompute_tangentsresult. Corners whose copy would need an index aboveu32::MAXkeep the first frame. Tangents are always finite; a non-finite normal counts as +Y. - UV unwrapping.
unwrap::auto_unwrapmakes a non-overlapping layout per material, splitting vertices where charts meet. It checks the mesh against its size limits and its attribute data first and returns an error without changing the mesh when a check fails. Charts are projected and packed inf64. Padding uses the requested resolution, with a minimum resolution of 16. A layout that cannot fit returns an error without changing the mesh. Missing normals use area-weightedf64accumulation and normalization. - UV G-buffer.
UvGBufferrecords, for each texel of one material's UV space, the triangle and barycentrics under the texel center;surface_atturns them into a position, normal and tangent frame.UvGBuffer::newreturns an error for a side of 0 or aboveMAX_SIDE(16,384), for more thanMAX_TEXELStexels (8,192 × 8,192), when the triangles touch more thanMAX_BAND_ENTRIESbands of 32 rows in all, and when the memory cannot be reserved. - Primitives. Sphere, rounded box, cube, cylinder, torus and plane with UVs and tangents, and
the built-in scenes in
primitives::BUILTINS.primitives::cubeandprimitives::rounded_boxreturnResult<Mesh>. They reject non-finite or nonpositive dimensions and dimensions whose doubled half extents overflow. A box bevel must be finite and between zero and 95% of its smallest half extent; an invalid bevel returnsGeomError::Invalid. Sphere and torus seam endpoints have identical positions and retain distinct UV endpoints. Cylinder caps share exact rim positions with the side, and rounded-box faces share exact boundary positions. Lathe profile lengths and direction normalization usef64before the UVs and normals are stored asf32. - BVH.
Bvhis a binned SAH tree on all three axes with a median split when no split beats a leaf. It answers closest-hit, any-hit and filtered ray casts, nearest-point queries and box queries. - Camera.
OrbitCameraandRaygive an orbit view and picking rays.
GeomError::Invalid is the one error type. rayon parallelizes the UV G-buffer, and serde
serializes OrbitCamera; both are always on.
Origin
| Part | Source |
|---|---|
mesh.rs, bvh.rs, primitives.rs, gbuffer.rs, unwrap.rs, camera.rs and their tests |
TextureCraft crates/geom at 386630e |
Mesh::subdivided and subdivision_shares_edge_midpoints |
MaterialCraft crates/geom/src/mesh.rs at 3133062 |
MikkTSpace comes from the mikktspace crate; its notice is in ATTRIBUTION.md.
Migration
| App | Status |
|---|---|
| TextureCraft | texturecraft-geom is the source. The app switches to this crate in its own pull request. |
| MaterialCraft | materialcraft-geom is the source of subdivided. The app switches in its own pull request. |