Documentation
¶
Overview ¶
Package models provides 3D model loading and representation for Trophy.
Index ¶
- type Face
- type GLTFLoader
- type Material
- type Mesh
- func LoadGLB(path string) (*Mesh, error)
- func LoadGLBFromFS(fsys fs.FS, path string) (*Mesh, error)
- func LoadGLBWithTexture(path string) (*Mesh, image.Image, error)
- func LoadGLBWithTextureFromFS(fsys fs.FS, path string) (*Mesh, image.Image, error)
- func LoadGLTFWithTextures(path string) (*Mesh, map[int][]byte, error)
- func LoadGLTFWithTexturesFromFS(fsys fs.FS, path string) (*Mesh, map[int][]byte, error)
- func LoadOBJ(path string) (*Mesh, error)
- func LoadOBJFromFS(fsys fs.FS, path string) (*Mesh, error)
- func LoadOBJSmooth(path string) (*Mesh, error)
- func LoadSTL(path string) (*Mesh, error)
- func LoadSTLClean(path string) (*Mesh, error)
- func LoadSTLFromFS(fsys fs.FS, path string) (*Mesh, error)
- func LoadSTLSmooth(path string) (*Mesh, error)
- func NewMesh(name string) *Mesh
- func (m *Mesh) CalculateBounds()
- func (m *Mesh) CalculateNormals()
- func (m *Mesh) CalculateSmoothNormals()
- func (m *Mesh) Center() math3d.Vec3
- func (m *Mesh) CleanMesh() int
- func (m *Mesh) Clone() *Mesh
- func (m *Mesh) DeduplicateFaces() int
- func (m *Mesh) GetBounds() (minV, maxV math3d.Vec3)
- func (m *Mesh) GetFace(i int) [3]int
- func (m *Mesh) GetFaceMaterial(i int) int
- func (m *Mesh) GetMaterial(i int) *Material
- func (m *Mesh) GetVertex(i int) (pos, normal math3d.Vec3, uv math3d.Vec2)
- func (m *Mesh) MaterialCount() int
- func (m *Mesh) RemoveDegenerateFaces() int
- func (m *Mesh) RemoveInternalFaces() int
- func (m *Mesh) RemoveUnreferencedVertices()
- func (m *Mesh) Size() math3d.Vec3
- func (m *Mesh) Transform(mat math3d.Mat4)
- func (m *Mesh) TriangleCount() int
- func (m *Mesh) VertexCount() int
- type MeshVertex
- type OBJLoader
- type STLLoader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Face ¶
type Face struct {
V [3]int // Indices into Mesh.Vertices
Material int // Index into Mesh.Materials (-1 for no material)
}
Face represents a triangle face with vertex indices and material reference.
type GLTFLoader ¶
GLTFLoader loads GLTF/GLB files into Mesh format.
func NewGLTFLoader ¶
func NewGLTFLoader() *GLTFLoader
NewGLTFLoader creates a new GLTF loader with default options.
func (*GLTFLoader) Load ¶
func (l *GLTFLoader) Load(path string) (*Mesh, error)
Load loads a GLTF or GLB file and returns a Mesh.
func (*GLTFLoader) LoadFromFS ¶ added in v1.5.0
LoadFromFS loads a GLTF or GLB file from an fs.FS and returns a Mesh.
type Material ¶
type Material struct {
Name string
BaseColor [4]float64 // RGBA in 0-1 range
Metallic float64 // 0 = dielectric, 1 = metal
Roughness float64 // 0 = smooth, 1 = rough
BaseMap image.Image // Optional base color texture
HasTexture bool
}
Material represents a PBR material from GLTF.
type Mesh ¶
type Mesh struct {
Name string
Vertices []MeshVertex
Faces []Face
Materials []Material
// Bounding box (calculated on load)
BoundsMin math3d.Vec3
BoundsMax math3d.Vec3
}
Mesh represents a 3D mesh with vertices, faces, and materials.
func LoadGLBFromFS ¶ added in v1.5.0
LoadGLBFromFS loads a binary GLTF (.glb) file from an fs.FS.
func LoadGLBWithTexture ¶
LoadGLBWithTexture loads a GLB file and returns the mesh plus the first embedded texture. Returns (mesh, texture image, error). Texture may be nil if none embedded.
func LoadGLBWithTextureFromFS ¶ added in v1.5.0
LoadGLBWithTextureFromFS loads a GLB file from an fs.FS and returns the mesh plus the first embedded texture. Returns (mesh, texture image, error). Texture may be nil if none embedded.
func LoadGLTFWithTextures ¶
LoadGLTFWithTextures loads a GLTF file and extracts embedded textures. Returns the mesh and a map of image index to texture data.
func LoadGLTFWithTexturesFromFS ¶ added in v1.5.0
LoadGLTFWithTexturesFromFS loads a GLTF file from an fs.FS and extracts textures. Returns the mesh and a map of image index to texture data.
func LoadOBJFromFS ¶ added in v1.5.0
LoadOBJFromFS loads an OBJ file from a filesystem interface.
func LoadOBJSmooth ¶
LoadOBJSmooth loads an OBJ file with smooth normals.
func LoadSTLClean ¶
LoadSTLClean loads an STL file and cleans the mesh. This removes degenerate faces, duplicate faces, and internal geometry.
func LoadSTLFromFS ¶ added in v1.5.0
LoadSTLFromFS loads an STL file from a filesystem interface.
func LoadSTLSmooth ¶
LoadSTLSmooth loads an STL file with smooth normals.
func (*Mesh) CalculateBounds ¶
func (m *Mesh) CalculateBounds()
CalculateBounds computes the axis-aligned bounding box.
func (*Mesh) CalculateNormals ¶
func (m *Mesh) CalculateNormals()
CalculateNormals computes face normals and assigns them to vertices. This is a simple flat-shading approach; for smooth shading, normals should be averaged per-vertex.
func (*Mesh) CalculateSmoothNormals ¶
func (m *Mesh) CalculateSmoothNormals()
CalculateSmoothNormals computes averaged normals for smooth shading.
func (*Mesh) CleanMesh ¶
CleanMesh performs all mesh cleanup operations: 1. Remove degenerate faces (zero area) 2. Remove internal faces (coplanar opposing pairs) - must come before dedup! 3. Remove duplicate faces 4. Remove unreferenced vertices Returns the total number of faces removed.
func (*Mesh) DeduplicateFaces ¶
DeduplicateFaces removes duplicate faces from the mesh. Two faces are considered duplicates if they have the same three vertices (regardless of winding order). When duplicates are found, only the first occurrence is kept. Returns the number of faces removed.
func (*Mesh) GetBounds ¶
GetBounds returns the axis-aligned bounding box. Implements render.BoundedMeshRenderer interface.
func (*Mesh) GetFace ¶
GetFace returns the vertex indices for face i. Implements render.MeshRenderer interface.
func (*Mesh) GetFaceMaterial ¶
GetFaceMaterial returns the material index for face i. Returns -1 if no material assigned.
func (*Mesh) GetMaterial ¶
GetMaterial returns the material at index i. Returns nil if index is out of bounds or -1.
func (*Mesh) GetVertex ¶
GetVertex returns the position, normal, and UV for vertex i. Implements render.MeshRenderer interface.
func (*Mesh) MaterialCount ¶
MaterialCount returns the number of materials.
func (*Mesh) RemoveDegenerateFaces ¶
RemoveDegenerateFaces removes faces with zero or near-zero area. Returns the number of faces removed.
func (*Mesh) RemoveInternalFaces ¶
RemoveInternalFaces removes pairs of coplanar faces that face opposite directions. These typically occur at the boundaries of merged/combined meshes where internal geometry should be removed. For each pair of faces sharing the same vertices but with opposite normals, both faces are removed. Returns the number of faces removed.
func (*Mesh) RemoveUnreferencedVertices ¶
func (m *Mesh) RemoveUnreferencedVertices()
RemoveUnreferencedVertices removes vertices that are not referenced by any face. This compacts the vertex array and updates face indices accordingly.
func (*Mesh) TriangleCount ¶
TriangleCount returns the number of triangles.
func (*Mesh) VertexCount ¶
VertexCount returns the number of vertices.
type MeshVertex ¶
MeshVertex holds all vertex attributes.
type OBJLoader ¶
type OBJLoader struct {
// Options
CalculateNormals bool // If true, calculate normals if not provided
SmoothNormals bool // If true, use smooth shading (averaged normals)
}
OBJLoader loads Wavefront OBJ files.
func NewOBJLoader ¶
func NewOBJLoader() *OBJLoader
NewOBJLoader creates a new OBJ loader with default settings.
type STLLoader ¶
type STLLoader struct {
// Options
SmoothNormals bool // If true, average normals per-vertex for smooth shading
NoDedupe bool // If true, don't deduplicate vertices (each triangle gets its own)
CleanMesh bool // If true, clean mesh after loading (remove degenerate/duplicate/internal faces)
MergeTolerance float64 // Tolerance for vertex merging (default 1e-6, 0 = exact match)
}
STLLoader loads STL (stereolithography) files in both ASCII and binary formats.
func NewSTLLoader ¶
func NewSTLLoader() *STLLoader
NewSTLLoader creates a new STL loader with default settings.
func (*STLLoader) Load ¶
Load parses STL from a reader. Note: This reads the entire content into memory to detect format.