model

package
v1.17.7 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 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, §28.5): meshes, materials, textures, images and a tree of nodes with float64 transforms, and for the formats that have them skins, morph targets, animations, cameras, lights and scenes (as data: viewport3d plays them). It has no GPU and no window, so a test can check it as it is. The loaders are in model/stl, model/obj and model/gltf, 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 Animation

type Animation struct {
	// Name is the name in the file, or empty.
	Name string
	// Channels are the changes.
	Channels []AnimationChannel
}

Animation is a set of channels that play together.

Flutter: no counterpart

func (*Animation) Duration

func (a *Animation) Duration() float32

Duration is the time of the last keyframe of the channels, in seconds; 0 for an animation with no keyframes.

Flutter: no counterpart

type AnimationChannel

type AnimationChannel struct {
	// Node is the node that is changed.
	Node *Node
	// Path is what is changed.
	Path AnimationPath
	// Interpolation is how the values are blended.
	Interpolation Interpolation
	// Times are the seconds of the keyframes, strictly increasing.
	Times []float32
	// Values are the keyframes: Width values for each time, or three times as many for a cubic spline (in-tangent, value,
	// out-tangent of each time). The channels of one sampler of the file share the same Times.
	Values []float32
}

AnimationChannel changes one thing of one node over time.

Flutter: no counterpart

func (*AnimationChannel) Width

func (c *AnimationChannel) Width() int

Width is how many values a keyframe has: 3 for a translation or a scale, 4 for a rotation, and for the weights the number of morph targets, which is told by the length of Values and Times.

Flutter: no counterpart

type AnimationPath

type AnimationPath uint8

AnimationPath is what an animation channel changes of its node.

Flutter: no counterpart

const (
	// PathTranslation changes the translation: three values at each time (x, y, z).
	PathTranslation AnimationPath = iota
	// PathRotation changes the rotation: four values at each time, a quaternion (x, y, z, w).
	PathRotation
	// PathScale changes the scale: three values at each time.
	PathScale
	// PathWeights changes the weights of the morph targets: as many values at each time as the node's mesh has targets.
	PathWeights
)

func (AnimationPath) String

func (p AnimationPath) String() string

String is the name of the path, as glTF writes it.

Flutter: no counterpart

type Camera

type Camera struct {
	// Name is the name in the file, or empty.
	Name string
	// Orthographic is true for a parallel projection and false for a perspective one.
	Orthographic bool
	// YFov is the vertical angle of view in radians and AspectRatio the width over the height, or 0 for the aspect of the view;
	// for a perspective camera.
	YFov, AspectRatio float64
	// XMag and YMag are the half width and half height of the view of an orthographic camera.
	XMag, YMag float64
	// ZNear and ZFar are the planes; ZFar is 0 for a perspective camera with an infinite far plane.
	ZNear, ZFar float64
}

Camera is a camera that a node carries.

Flutter: no counterpart

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 Interpolation

type Interpolation uint8

Interpolation is how an animation goes from a value to the next.

Flutter: no counterpart

const (
	// InterpolationLinear blends the two values (a spherical blend for a rotation); the zero value.
	InterpolationLinear Interpolation = iota
	// InterpolationStep keeps a value until the next time.
	InterpolationStep
	// InterpolationCubicSpline is a cubic Hermite spline: each time has three values, an in-tangent, the value and an out-tangent.
	InterpolationCubicSpline
)

func (Interpolation) String

func (i Interpolation) String() string

String is the name of the interpolation, as glTF writes it.

Flutter: no counterpart

type Light

type Light struct {
	// Name is the name in the file, or empty.
	Name string
	// Type is the kind of the light.
	Type LightType
	// Color is red, green and blue, from 0 to 1, linear; (1, 1, 1) when the file does not say.
	Color [3]float32
	// Intensity is in lux for a directional light and in candela for the others; 1 when the file does not say.
	Intensity float32
	// Range is the distance at which the light has gone, or 0 for no limit (a point or spot light only).
	Range float32
	// InnerConeAngle and OuterConeAngle are the angles in radians of a spot light where it starts to fade and where it ends:
	// 0 and a quarter of pi when the file does not say.
	InnerConeAngle, OuterConeAngle float32
}

Light is a punctual light that a node carries (KHR_lights_punctual).

Flutter: no counterpart

type LightType

type LightType uint8

LightType is the kind of a light of KHR_lights_punctual.

Flutter: no counterpart

const (
	// LightDirectional shines along the -z of its node, from infinitely far; the zero value.
	LightDirectional LightType = iota
	// LightPoint shines in all directions from its node.
	LightPoint
	// LightSpot shines in a cone along the -z of its node.
	LightSpot
)

func (LightType) String

func (t LightType) String() string

String is the name of the type, as glTF writes it.

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. A loader that gives BaseColorTexture sets this to its image
	// too, for the renderers that do not read textures with samplers.
	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
	// PBR is true for a material of the metallic-roughness model (from glTF, §28.5), which a loader fills in full: Metallic and
	// Roughness, the factors, and the textures. It is false for the Phong materials of the other loaders, which have Specular and
	// Shininess.
	PBR bool
	// Metallic and Roughness are the metallic-roughness values of a physically based material, from 0 to 1. glTF's defaults are 1
	// and 1, which its loader sets: the zero values here are values like the others.
	Metallic, Roughness float32
	// Emissive is the color that the material gives out, with the strength of KHR_materials_emissive_strength multiplied in (so it
	// may be above 1).
	Emissive [3]float32
	// Unlit says that the material is not lit: its color is the pixel's (KHR_materials_unlit).
	Unlit bool
	// BaseColorTexture multiplies BaseColor (and the vertex colors) in sRGB, and MetallicRoughnessTexture has the roughness in its
	// green channel and the metalness in its blue one, multiplied by Roughness and Metallic. NormalTexture has tangent space
	// normals, scaled by NormalScale; OcclusionTexture has the occlusion in its red channel, blended by OcclusionStrength; and
	// EmissiveTexture multiplies Emissive in sRGB. Each is nil for none.
	BaseColorTexture, MetallicRoughnessTexture, NormalTexture, OcclusionTexture, EmissiveTexture *TextureRef
	// NormalScale is the scale of the x and y of the normals of NormalTexture, and OcclusionStrength the strength of the
	// occlusion; glTF's defaults are 1 and 1, which its loader sets.
	NormalScale, OcclusionStrength 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; a PBR material always has its own).

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
	// Tangents are x, y, z and w of each vertex: the direction of the u axis of the texture and the sign of the bitangent (w is 1 or
	// -1); nil when the file has none (a normal texture then needs them made).
	Tangents []float32
	// TexCoords are u and v of each vertex; nil when the file has none.
	TexCoords []float32
	// TexCoords1 are the second set of u and v of each vertex (glTF's TEXCOORD_1); nil when the file has none.
	TexCoords1 []float32
	// Colors are red, green, blue and alpha of each vertex, not premultiplied; nil when the file has none.
	Colors []byte
	// Joints are the four joints of each vertex that move it, as positions in Skin.Joints of the node that has the mesh (glTF's
	// JOINTS_0), and Weights the four weights of those joints (WEIGHTS_0), which add up to 1; both nil for a mesh that is not
	// skinned. Further sets of four (JOINTS_1) are left out by the loaders, with a note.
	Joints  []uint16
	Weights []float32
	// MorphTargets are the shapes that the mesh moves towards; MorphWeights are the weights that they have at rest, one for each
	// (nil is all 0). A node's MorphWeights replace them.
	MorphTargets []MorphTarget
	MorphWeights []float32
	// 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: the nodes of the default scene.
	Nodes []*Node
	// Meshes, Materials, Textures and Images are every one of the model, each once; the nodes and the meshes point to them. A mesh of
	// a glTF file is one Mesh for each of its primitives.
	Meshes    []*Mesh
	Materials []*Material
	Textures  []*Texture
	Images    []*Image
	// Skins, Cameras and Lights are every one of the model, and Animations are its animations (glTF, §28.5).
	Skins      []*Skin
	Cameras    []*Camera
	Lights     []*Light
	Animations []*Animation
	// Scenes are the scenes of the file, and DefaultScene the one that Nodes is; nil when the file has none (Nodes are then its nodes
	// that no node has as a child).
	Scenes       []*Scene
	DefaultScene *Scene
	// 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 MorphTarget

type MorphTarget struct {
	// Name is the name in the file (mesh extras "targetNames"), or empty.
	Name string
	// Positions are the changes of x, y and z of each vertex, or nil for none.
	Positions []float32
	// Normals are the changes of the normals, x, y and z of each vertex, or nil for none.
	Normals []float32
	// Tangents are the changes of the x, y and z of the tangents, or nil for none.
	Tangents []float32
}

MorphTarget is a shape that a mesh can be moved towards: the change of each vertex at a weight of 1, added to the vertex with the weight of the target.

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
	// TRS is the transform as a translation, a rotation and a scale when the file gave it so (it is what animations change), and
	// Transform is its matrix; nil for a node whose file gave a matrix (animations do not change those).
	TRS *TRS
	// Skin is the skeleton that moves the vertices of the node's meshes, or nil.
	Skin *Skin
	// Camera and Light are the camera and the light that the node carries, or nil. They look along the -z of the node.
	Camera *Camera
	Light  *Light
	// MorphWeights are the weights of the morph targets of the node's meshes, or nil to use those of each mesh.
	MorphWeights []float32
}

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 Sampler

type Sampler struct {
	// Mag is the filter when the texture is enlarged, and Min when it is reduced.
	Mag, Min SamplerFilter
	// Mipmap is how the levels of the mipmaps are blended when Min asks for them (a glTF minFilter that has "mipmap" in its name):
	// FilterUnspecified for no mipmaps, FilterNearest to take the nearest level and FilterLinear to blend two.
	Mipmap SamplerFilter
	// WrapU and WrapV are how the texture is read outside of 0 to 1.
	WrapU, WrapV Wrap
}

Sampler says how a texture is read.

Flutter: no counterpart

type SamplerFilter

type SamplerFilter uint8

SamplerFilter is how a texture is read between its pixels.

Flutter: no counterpart

const (
	// FilterUnspecified lets the renderer choose (linear); the zero value.
	FilterUnspecified SamplerFilter = iota
	// FilterNearest takes the nearest pixel.
	FilterNearest
	// FilterLinear blends the pixels around.
	FilterLinear
)

type Scene

type Scene struct {
	// Name is the name in the file, or empty.
	Name string
	// Nodes are the roots of the scene.
	Nodes []*Node
}

Scene is a set of root nodes.

Flutter: no counterpart

type Skin

type Skin struct {
	// Name is the name in the file, or empty.
	Name string
	// Joints are the nodes that move the vertices; the JOINTS attribute of a mesh counts in this list.
	Joints []*Node
	// InverseBindMatrices take a vertex from the coordinates of the mesh to those of each joint when the skeleton is at rest; one
	// for each joint (the identity when the file has none).
	InverseBindMatrices []vecmath.Mat4
	// Skeleton is the root of the joints, or nil.
	Skeleton *Node
}

Skin is the skeleton of the meshes of the nodes that use it.

Flutter: no counterpart

type TRS

type TRS struct {
	Translation vecmath.Vec3
	Rotation    vecmath.Quat
	Scale       vecmath.Vec3
}

TRS is the transform of a node as a translation, a rotation and a scale, which is what an animation changes.

Flutter: no counterpart

func IdentityTRS

func IdentityTRS() TRS

IdentityTRS is the transform that does nothing.

Flutter: no counterpart

func (TRS) Matrix

func (t TRS) Matrix() vecmath.Mat4

Matrix is the matrix of the transform: scale, then rotation, then translation.

Flutter: no counterpart

type Texture

type Texture struct {
	// Name is the name in the file, or empty.
	Name string
	// Image is the image, or nil when the file gives it in a form that the loader does not read.
	Image *Image
	// Sampler says how the image is read.
	Sampler Sampler
}

Texture is an image with a sampler.

Flutter: no counterpart

type TextureRef

type TextureRef struct {
	// Texture is the texture.
	Texture *Texture
	// TexCoord is the set of texture coordinates that it reads: 0 for Mesh.TexCoords and 1 for Mesh.TexCoords1.
	TexCoord int
	// Transform is the KHR_texture_transform of the use, or nil for none. A transform that gives another texture coordinate set
	// has set TexCoord.
	Transform *TextureTransform
}

TextureRef is the use of a texture by a material.

Flutter: no counterpart

type TextureTransform

type TextureTransform struct {
	// Offset is added to the coordinates after the rotation.
	Offset [2]float32
	// Rotation is the angle in radians, counterclockwise in the image.
	Rotation float32
	// Scale multiplies the coordinates first.
	Scale [2]float32
}

TextureTransform is the transform of the texture coordinates of KHR_texture_transform: the coordinates are scaled, rotated and then moved.

Flutter: no counterpart

func (TextureTransform) Apply

func (t TextureTransform) Apply(u, v float32) (float32, float32)

Apply is the transform of the coordinates (u, v).

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

type Wrap

type Wrap uint8

Wrap is how a texture is read outside of 0 to 1.

Flutter: no counterpart

const (
	// WrapRepeat repeats the texture; the zero value, and glTF's default.
	WrapRepeat Wrap = iota
	// WrapMirroredRepeat repeats it, every other time mirrored.
	WrapMirroredRepeat
	// WrapClampToEdge repeats the pixels at the edge.
	WrapClampToEdge
)

Directories

Path Synopsis
Package gltf reads glTF 2.0 files, the text kind (.gltf, with its buffers and images in files or in data: URIs) and the binary kind (.glb), into a model.Model (SPEC §28.5).
Package gltf reads glTF 2.0 files, the text kind (.gltf, with its buffers and images in files or in data: URIs) and the binary kind (.glb), into a model.Model (SPEC §28.5).
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).
poly
Package poly cuts the polygons of a model file into triangles (a fan when the polygon is convex, ear clipping when it is not); the loaders of OBJ and PLY share it.
Package poly cuts the polygons of a model file into triangles (a fan when the polygon is convex, ear clipping when it is not); the loaders of OBJ and PLY share it.
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 ply reads PLY files, the ASCII kind and the binary kind in both byte orders (SPEC §28.5), into a model.Model.
Package ply reads PLY files, the ASCII kind and the binary kind in both byte orders (SPEC §28.5), into a model.Model.
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.
Package threemf reads 3MF files (SPEC §28.5) into a model.Model: the core specification of the 3D Manufacturing Format, a zip archive whose model part (3D/3dmodel.model, found through the package's relationships) is XML, and the base materials and colors of its materials extension.
Package threemf reads 3MF files (SPEC §28.5) into a model.Model: the core specification of the 3D Manufacturing Format, a zip archive whose model part (3D/3dmodel.model, found through the package's relationships) is XML, and the base materials and colors of its materials extension.

Jump to

Keyboard shortcuts

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