Files
craft-libs/crates/mesh-ops

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, with validate, 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): w holds the bitangent sign. compute_tangents keeps 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_split instead 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 gets w = +1. The generated corner frames of a triangle with usable UV gradients share one handedness sign. With compute_tangents, a vertex shared by mirrored triangles keeps the first triangle's frame, so the stored signs of the other triangle can differ; use compute_tangents_split when 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 the compute_tangents result. Corners whose copy would need an index above u32::MAX keep the first frame. Tangents are always finite; a non-finite normal counts as +Y.
  • UV unwrapping. unwrap::auto_unwrap makes 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 in f64. 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-weighted f64 accumulation and normalization.
  • UV G-buffer. UvGBuffer records, for each texel of one material's UV space, the triangle and barycentrics under the texel center; surface_at turns them into a position, normal and tangent frame. UvGBuffer::new returns an error for a side of 0 or above MAX_SIDE (16,384), for more than MAX_TEXELS texels (8,192 × 8,192), when the triangles touch more than MAX_BAND_ENTRIES bands 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::cube and primitives::rounded_box return Result<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 returns GeomError::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 use f64 before the UVs and normals are stored as f32.
  • BVH. Bvh is 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. OrbitCamera and Ray give 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.