gltf

package module
v0.0.0-...-fc029d2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 6 Imported by: ? modules, 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.

README

glTF

A simple file loader for glTF files in Go. This package is not intended to be used for manipulation or writing glTF models to disk.

The entry point is any of FromBytes, FromFile, or FromFilename. If using FromFilename (which is recomended), the default search path for referenced files is automatically set to the same directory as the file.

Once a file is loaded, calling Resolve will:

  1. load binary data into byte slices stored in each Buffer
  2. create sub-slice references to the bytes in each BufferView
  3. replace all indexed references with a pointer to the resolved object; for example, a scene with a single node index in the source file will hold a pointer to the actual node after calling Resolve. That node will also have been resolved, so that you can walk the tree directly to the buffer data:
    resolved := myGltf.Resolve(nil)
    var meshData []byte := resovled.Scene.Nodes[0].Mesh.Primitives.Attributes[gltf.POSITION].BufferView.Data

When using either of the first two forms, you will (likely) need to provide a search URI for referenced files, such as textures or binary vertex data. Resolve accepts a slice of strings to allow searching multiple paths.

Development Status

This package is working for loading of models and has partial support for cameras. It is not currently handling textures, animations, etc. Features are being implemented in conjunction with development of a glTF model viewer written in Go.

License

MIT license. See the LICENSE file.

SPDX-License-Identifier: MIT

Documentation

Index

Constants

View Source
const (
	POSITION   AttributeKey = "POSITION"
	NORMAL                  = "NORMAL"
	TANGENT                 = "TANGENT"
	TEXCOORD_0              = "TEXCOORD_0"
	TEXCOORD_1              = "TEXCOORD_1"
	COLOR_0                 = "COLOR_0"
	JOINTS_0                = "JOINTS_0"
	WEIGHTS_0               = "WEIGHTS_0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Accessor

type Accessor struct {
	BufferView    uint              `json:"bufferView"`
	ByteOffset    uint              `json:"byteOffset"`
	ComponentType ComponentTypeEnum `json:"componentType"`
	Normalized    bool              `json:"normalized"`
	Count         int               `json:"count"`
	Type          AccessorTypeEnum  `json:"type"`
	Max           []float64         `json:"max"`
	Min           []float64         `json:"min"`
	Sparse        SparseAccessor    `json:"sparse"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

Accessor: see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#schema-reference-accessor

func (*Accessor) Stride

func (a *Accessor) Stride() int

Stride is a convenience function returning the number of bytes in each element as defined by this Accessor.

type AccessorTypeEnum

type AccessorTypeEnum string
const (
	SCALAR AccessorTypeEnum = "SCALAR"
	VEC2   AccessorTypeEnum = "VEC2"
	VEC3   AccessorTypeEnum = "VEC3"
	VEC4   AccessorTypeEnum = "VEC4"
	MAT2   AccessorTypeEnum = "MAT2"
	MAT3   AccessorTypeEnum = "MAT3"
	MAT4   AccessorTypeEnum = "MAT4"
)

func (AccessorTypeEnum) Count

func (ate AccessorTypeEnum) Count() int

Count returns the number of individual components, without regard to the byte size of that component, in each element as defined by an Accessor. This function will panic() if called on an enum value not defined by this module.

type Animation

type Animation struct {
	Channels []AnimationChannel `json:"channels"`
	Samplers []AnimationSampler `json:"samplers"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type AnimationChannel

type AnimationChannel struct {
	Sampler uint                   `json:"sampler"`
	Target  AnimationChannelTarget `json:"target"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type AnimationChannelTarget

type AnimationChannelTarget struct {
	Node *uint                      `json:"node,omitempty"`
	Path AnimationChannelTargetPath `json:"path"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type AnimationChannelTargetPath

type AnimationChannelTargetPath string
const (
	TRANSLATION AnimationChannelTargetPath = "translation"
	ROTATION    AnimationChannelTargetPath = "rotation"
	SCALE       AnimationChannelTargetPath = "scale"
	WEIGHTS     AnimationChannelTargetPath = "weights"
)

type AnimationSampler

type AnimationSampler struct {
	Input         int                           `json:"input"`
	Interpolation AnimationSamplerInterpolation `json:"interpolation,omitempty"`
	Output        int                           `json:"output"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type AnimationSamplerInterpolation

type AnimationSamplerInterpolation string
const (
	LINEAR       AnimationSamplerInterpolation = "linear"
	STEP         AnimationSamplerInterpolation = "step"
	CUBIC_SPLINE AnimationSamplerInterpolation = "cubicspline"
)

type Asset

type Asset struct {
	// Spec: A copyright message suitable for display to credit the content creator.
	Copyright string `json:"copyright,omitempty"`
	// Spec: Tool that generated this glTF model.  Useful for debugging.
	Generator string `json:"generator,omitempty"`
	// Spec: The glTF version in the form of `<major>.<minor>` that this asset targets.
	Version string `json:"version"`
	// The minimum glTF version in the form of `<major>.<minor>` that this asset targets. This property **MUST NOT** be greater than the asset version.
	MinVersion string `json:"minVersion,omitempty"`
}

Spec: Metadata about the glTF asset.

type AttributeKey

type AttributeKey string

type Buffer

type Buffer struct {
	Uri        string `json:"uri,omitempty"`
	ByteLength uint   `json:"byteLength"`

	Name       GlTFId `json:"name"`
	Extensions `json:"extensions"`
	Extras     `json:"extras"`
}

type BufferTargetEnum

type BufferTargetEnum int
const (
	ARRAY_BUFFER         BufferTargetEnum = 34962
	ELEMENT_ARRAY_BUFFER BufferTargetEnum = 34963
)

type BufferView

type BufferView struct {
	Buffer     uint             `json:"buffer"`
	ByteOffset uint             `json:"byteOffset"`
	ByteLength uint             `json:"byteLength"`
	ByteStride uint             `json:"byteStride"`
	Target     BufferTargetEnum `json:"target"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type Camera

type Camera struct {
	Orthographic CameraOrthographic `json:"orthographic,omitempty"`
	Perspective  CameraPerpsective  `json:"perspective,omitempty"`
	Type         CameraType         `json:"type"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type CameraOrthographic

type CameraOrthographic struct {
	Xmag  float32 `json:"xmag"`
	Ymag  float32 `json:"ymag"`
	Zfar  float32 `json:"zfar"`
	Znear float32 `json:"znear"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type CameraPerpsective

type CameraPerpsective struct {
	AspectRatio float32 `json:"aspectRatio"`
	Yfov        float32 `json:"yfov"`
	Zfar        float32 `json:"zfar"`
	Znear       float32 `json:"znear"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type CameraType

type CameraType string
const (
	PERSPECTIVE  CameraType = "perspective"
	ORTHOGRAPHIC CameraType = "orthographic"
)

type ComponentTypeEnum

type ComponentTypeEnum int
const (
	BYTE           ComponentTypeEnum = 5120
	UNSIGNED_BYTE  ComponentTypeEnum = 5121
	SHORT          ComponentTypeEnum = 5122
	UNSIGNED_SHORT ComponentTypeEnum = 5123
	UNSIGNED_INT   ComponentTypeEnum = 5125
	FLOAT          ComponentTypeEnum = 5126
)

func (ComponentTypeEnum) Size

func (c ComponentTypeEnum) Size() int

Size returns the byte size of the component specified by an Accessor. Note that the size of component types are determined by the glTF spec, not the machine that the file is being interpreted by. This function will panic() if called on an enum value not defined by this module.

type Extensions

type Extensions map[string]any

These are supposed to be generic maps

type Extras

type Extras map[string]any

type GlTF

type GlTF struct {
	// Asset is a required field
	Asset Asset `json:"asset"`

	ExtensionsUsed     []string `json:"extensionsUsed"`
	ExtensionsRequired []string `json:"extensionsRequired"`

	Accessors   []Accessor   `json:"accessors"`
	Animations  []Animation  `json:"animations"`
	Buffers     []Buffer     `json:"buffers"`
	BufferViews []BufferView `json:"bufferViews"`
	Cameras     []Camera     `json:"cameras"`
	Images      []Image      `json:"images"`
	Materials   []Material   `json:"materials"`
	Meshes      []Mesh       `json:"meshes"`
	Nodes       []Node       `json:"nodes"`
	Samplers    []Sampler    `json:"samplers"`
	Scene       *uint        `json:"scene,omitempty"` // Spec: Scene is an optional reference to the default scene for this asset, as an index in the Scenes array.
	Scenes      []Scene      `json:"scenes"`
	Skins       []Skin       `json:"skins"`
	Textures    []Texture    `json:"textures"`

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
	// contains filtered or unexported fields
}

Spec: The root object for a glTF asset.

func FromBytes

func FromBytes(data []byte) (*GlTF, error)

func FromFile

func FromFile(f *os.File) (*GlTF, error)

func FromFilename

func FromFilename(name string) (*GlTF, error)

func (*GlTF) Resolve

func (gltf *GlTF) Resolve(uriSearchPath []string) (*ResolvedGlTF, error)

Resolve processes a glTF structure and returns a ResolvedGlTF that can be used in a program without further processing. The returned value will have:

* All indexed resource references resolved as pointers to that resource. For example, an array of Node indices will be converted to pointers to the actual Nodes. * All binary resources will be loaded into their Buffer objects, using the provided uriSearchPaths for name resolution. Note that the binary load will not interpret or validate the data, it will simply be loaded as-is into the buffer. An error will be raised if the binary data provided is shorter than the Buffer's ByteLength. NOTE HOWEVER, that the data will still be loaded into the Buffer and zero padded. The any data at the URI beyond the Buffer's ByteLength will be ignored.

Paths will be searched in the order provided and the first matching file will be used. If the GlTF instance was loaded from a file object, or from a file name, and if uriSearchPath is empty, then that location will be searched by default.

type GlTFId

type GlTFId string

type Image

type Image map[string]any

type Material

type Material struct {
	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type Mesh

type Mesh struct {
	Primitives []Primitive `json:"primitives"`
	Weights    []float32   `json:"weights"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type ModeEnum

type ModeEnum int
const (
	POINTS ModeEnum = iota
	LINES
	LINE_LOOP
	LINE_STRIP
	TRIANGLES
	TRIANGLE_STRIP
	TRIANGLE_FAN
)

type Node

type Node struct {
	Camera      *int        `json:"camera"`
	Children    []uint      `json:"children"`
	Skin        *int        `json:"skin"`
	Matrix      [16]float32 `json:"matrix"`
	Mesh        *uint       `json:"mesh,omitempty"`
	Rotation    *vkm.Vec    `json:"rotation"`
	Scale       *vkm.Vec3   `json:"scale"`
	Translation *vkm.Vec3   `json:"translation"`
	Weights     []float32   `json:"weights"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type Primitive

type Primitive struct {
	Attributes map[AttributeKey]int `json:"attributes"`
	Indices    *uint                `json:"indices,omitempty"`
	// May be null, indicating "default" material
	Material *int      `json:"material,omitempty"`
	Mode     *ModeEnum `json:"mode,omitempty"`
	Targets  []string  `json:"targets"` // TODO

	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type ResolvedAccessor

type ResolvedAccessor struct {
	*Accessor
	BufferView *ResolvedBufferView
}

type ResolvedAnimation

type ResolvedAnimation struct {
	*Animation
	Channels []ResolvedAnimationChannel
	Samplers []ResolvedAnimationSampler
}

type ResolvedAnimationChannel

type ResolvedAnimationChannel struct {
	*AnimationChannel
	Sampler *ResolvedAnimationSampler
	Target  ResolvedAnimationChannelTarget
}

type ResolvedAnimationChannelTarget

type ResolvedAnimationChannelTarget struct {
	*AnimationChannelTarget
	Node *ResolvedNode
}

type ResolvedAnimationSampler

type ResolvedAnimationSampler struct {
	*AnimationSampler
	Input  *ResolvedAccessor
	Output *ResolvedAccessor
}

type ResolvedBuffer

type ResolvedBuffer struct {
	*Buffer
	Data []byte
}

type ResolvedBufferView

type ResolvedBufferView struct {
	*BufferView
	Buffer *ResolvedBuffer
	// Data in the BufferView is a subslice (i.e. shared memory) from the source buffer data
	Data []byte
}

type ResolvedCamera

type ResolvedCamera struct {
	*Camera
	ProjMatrix vkm.Mat
}

type ResolvedGlTF

type ResolvedGlTF struct {
	*GlTF

	Animations  []ResolvedAnimation
	Buffers     []ResolvedBuffer
	BufferViews []ResolvedBufferView
	Cameras     []ResolvedCamera
	Accessors   []ResolvedAccessor
	Materials   []ResolvedMaterial
	Meshes      []ResolvedMesh
	Nodes       []ResolvedNode

	Scene  *ResolvedScene
	Scenes []ResolvedScene
}

ResolvedGlTF is a GlTF with all indexed node references and binary data resolved and loaded. The source GlTF struct is included as a pointer, so all of the source data can be referenced as neccessary. Fields for resolved data types are named the same as the sources in a GlTF struct, masking the original data. The original data can still be accessed through the embedded GlTF pointer: ``` var resolved *ResolvedGlTF ... resolved.Nodes => slice of ResolvedNode resolved.GlTF.Nodes => slice of Node ```

type ResolvedMaterial

type ResolvedMaterial struct {
	*Material
}

type ResolvedMesh

type ResolvedMesh struct {
	*Mesh
	Primitives []ResolvedPrimitive
}

type ResolvedNode

type ResolvedNode struct {
	*Node
	Camera   *ResolvedCamera
	Children []*ResolvedNode
	Mesh     *ResolvedMesh
}

type ResolvedPrimitive

type ResolvedPrimitive struct {
	*Primitive
	Attributes map[AttributeKey]*ResolvedAccessor
	Indices    *ResolvedAccessor
	Material   *ResolvedMaterial
}

type ResolvedScene

type ResolvedScene struct {
	*Scene
	Nodes []*ResolvedNode
}

type Sampler

type Sampler map[string]any

type Scene

type Scene struct {
	Nodes []uint `json:"nodes"`

	Name       GlTFId `json:"name,omitempty"`
	Extensions `json:"extensions,omitempty"`
	Extras     `json:"extras,omitempty"`
}

type Skin

type Skin map[string]any

type SparseAccessor

type SparseAccessor map[string]any

type Texture

type Texture map[string]any

Jump to

Keyboard shortcuts

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