model

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: BSD-3-Clause Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package model is the plain Go data of a 3D model that a loader reads from a file (SPEC §27.9): meshes, materials, images and a tree of nodes with float64 transforms. It has no GPU and no window, so a test can check it as it is. The loaders are in model/stl and model/obj, and viewport3d.Upload makes the meshes, the materials and the items of a scene from it.

Index

Constants

View Source
const TagModelReadFailed = "ModelReadFailed"

TagModelReadFailed is the tag of the error of a loader that could not read its input: an I/O error of the reader or the file system, or a context that was done. The cause is wrapped, so errors.Is(err, context.Canceled) works; the metadata has the offset or the line that was reached, and the file name when there is one.

Flutter: no counterpart

Variables

View Source
var DefaultColor = [4]float32{0.78, 0.8, 0.82, 1}

DefaultColor is the color of a material that has none: an opaque light gray.

Flutter: no counterpart

Functions

func DetectImageType

func DetectImageType(name string, data []byte) string

DetectImageType returns the media type of an image from the extension of its name, or from its first bytes when the name does not say: "image/png", "image/jpeg", "image/gif", "image/bmp", "image/webp", or "" when it is none of them.

Flutter: no counterpart

func ReadAll

func ReadAll(ctx context.Context, r io.Reader) ([]byte, error)

ReadAll reads the whole input, as the loaders do, looking at the context between reads of a megabyte. It fails with ModelReadFailed for an error of the reader or a done context ("offset" is how many bytes were read).

Flutter: no counterpart

Types

type AlphaMode

type AlphaMode uint8

AlphaMode is how the alpha of a material is used.

Flutter: no counterpart

const (
	// AlphaOpaque ignores the alpha; the zero value.
	AlphaOpaque AlphaMode = iota
	// AlphaMask draws a pixel whose alpha is at least the cutoff and none otherwise.
	AlphaMask
	// AlphaBlend blends by the alpha.
	AlphaBlend
)

type Image

type Image struct {
	// Name is the name in the file, or empty.
	Name string
	// URI is where the image was read from: the path in the file system of the loader, or empty.
	URI string
	// Type is the media type, such as "image/png" or "image/jpeg"; empty when the file name and the data do not tell.
	Type string
	// Data are the bytes of the file. Upload decodes them (ui.InstantiateImageCodec) when it makes a texture.
	Data []byte
}

Image is an image as the file gave it, encoded.

Flutter: no counterpart

type Material

type Material struct {
	// Name is the name in the file, or empty.
	Name string
	// BaseColor is the color, red, green, blue and alpha from 0 to 1, not premultiplied. The zero value (all four 0) stands for the
	// default, an opaque light gray; use Color to read it.
	BaseColor [4]float32
	// BaseColorImage is the image that the color is multiplied by, or nil.
	BaseColorImage *Image
	// Specular is the color of the highlight (the Ks of an MTL file) and Shininess its exponent (the Ns).
	Specular  [3]float32
	Shininess float32
	// Metallic and Roughness are the metallic-roughness values of a physically based material (from glTF, §28.5), from 0 to 1.
	Metallic, Roughness float32
	// Emissive is the color that the material gives out.
	Emissive [3]float32
	// AlphaMode and AlphaCutoff say how the alpha is used.
	AlphaMode   AlphaMode
	AlphaCutoff float32
	// DoubleSided draws the back faces too.
	DoubleSided bool
}

Material is the look of a mesh.

Flutter: no counterpart

func (*Material) Color

func (m *Material) Color() [4]float32

Color is the base color, or the default when the material has none (all four numbers 0).

Flutter: no counterpart

type Mesh

type Mesh struct {
	// Name is the name in the file, or empty.
	Name string
	// Primitive is what the indices make.
	Primitive Primitive
	// Positions are x, y and z of each vertex in float32. A loader that was asked for more digits than float32 has (Float64 in its
	// options) and found them in the file leaves Positions nil and fills Positions64 instead: exactly one of the two is set.
	Positions []float32
	// Positions64 are the positions in float64, when the loader was asked for them and the file has more digits than float32 does.
	Positions64 []float64
	// Normals are x, y and z of each vertex, of length 1; nil when the file has none (the renderer shades flat).
	Normals []float32
	// TexCoords are u and v of each vertex; nil when the file has none.
	TexCoords []float32
	// Colors are red, green, blue and alpha of each vertex, not premultiplied; nil when the file has none.
	Colors []byte
	// Indices are the vertices of the primitives: 3, 2 or 1 for each, by Primitive.
	Indices []uint32
	// Material is the material of the mesh, or nil for the default.
	Material *Material
}

Mesh is the geometry of one material: vertices and the indices of the primitives that they make.

Flutter: no counterpart

func (*Mesh) Bounds

func (m *Mesh) Bounds() vecmath.Box3

Bounds is the box of the vertices that the indices use (the box of every vertex when there are no indices), in the mesh's own coordinates; empty for a mesh with none. An index outside the vertices is left out.

Flutter: no counterpart

func (*Mesh) Position

func (m *Mesh) Position(i int) vecmath.Vec3

Position is the position of a vertex in float64.

Flutter: no counterpart

func (*Mesh) PrimitiveCount

func (m *Mesh) PrimitiveCount() int

PrimitiveCount is the number of triangles, segments or points.

Flutter: no counterpart

func (*Mesh) VertexCount

func (m *Mesh) VertexCount() int

VertexCount is the number of vertices.

Flutter: no counterpart

type Model

type Model struct {
	// Name is the name in the file, or empty.
	Name string
	// Nodes are the top of the tree.
	Nodes []*Node
	// Meshes, Materials and Images are every one of the model, each once; the nodes and the meshes point to them.
	Meshes    []*Mesh
	Materials []*Material
	Images    []*Image
	// Units are the units of the positions, when the file says.
	Units Units
}

Model is what a file holds.

Flutter: no counterpart

func (*Model) Bounds

func (m *Model) Bounds() vecmath.Box3

Bounds is the box of the model in the coordinates of its top nodes, in float64: the boxes of the meshes of every node, moved by the transforms from the node up to the top. It is empty for a model with no geometry.

Flutter: no counterpart

func (*Model) TriangleCount

func (m *Model) TriangleCount() int

TriangleCount is the number of triangles of the meshes that the nodes use, a mesh that several nodes use counted for each.

Flutter: no counterpart

type Node

type Node struct {
	// Name is the name in the file, or empty.
	Name string
	// Transform takes the node's meshes to its parent's coordinates, in float64. The zero value is not a transform: loaders set
	// the identity (Mat4Identity); NewNode does too.
	Transform vecmath.Mat4
	// Meshes are drawn with the node's transform; several nodes may have the same mesh.
	Meshes []*Mesh
	// Children are nodes in the coordinates of this one.
	Children []*Node
}

Node is a part of the tree of a model: a transform, meshes and children.

Flutter: no counterpart

func NewNode

func NewNode(name string) *Node

NewNode makes a node with the identity transform.

Flutter: no counterpart

type Primitive

type Primitive uint8

Primitive is what the vertices of a mesh make.

Flutter: no counterpart

const (
	// PrimitiveTriangles is three indices for each triangle; the zero value.
	PrimitiveTriangles Primitive = iota
	// PrimitiveLines is two indices for each segment.
	PrimitiveLines
	// PrimitivePoints is one index for each point.
	PrimitivePoints
)

func (Primitive) String

func (p Primitive) String() string

String is the name of the primitive.

Flutter: no counterpart

type Units

type Units struct {
	// Name is "m", "mm", "in" and so on.
	Name string
	// MetersPerUnit is the length of a unit in meters; 0 when the file does not say.
	MetersPerUnit float64
}

Units says what a unit of the model is, when the file says.

Flutter: no counterpart

Directories

Path Synopsis
internal
parse
Package parse has the small text readers that the loaders of package model share: fields, integers and decimal numbers read from byte slices with no allocation (an OBJ file of a million triangles has some seven million of them).
Package parse has the small text readers that the loaders of package model share: fields, integers and decimal numbers read from byte slices with no allocation (an OBJ file of a million triangles has some seven million of them).
vtx
Package vtx is the table that welds equal vertices for the loaders of package model: a hash table from a key of three integers (the bits of a position, or its cell on a grid) to the number of the vertex that was first seen with it.
Package vtx is the table that welds equal vertices for the loaders of package model: a hash table from a key of three integers (the bits of a position, or its cell on a grid) to the number of the vertex that was first seen with it.
Package obj reads Wavefront OBJ files, with the subset of MTL that matters to a viewer, into a model.Model (SPEC §27.9).
Package obj reads Wavefront OBJ files, with the subset of MTL that matters to a viewer, into a model.Model (SPEC §27.9).
Package stl reads STL files, the binary and the ASCII kind (SPEC §27.9), into a model.Model.
Package stl reads STL files, the binary and the ASCII kind (SPEC §27.9), into a model.Model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL