gltf

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: Unlicense Imports: 10 Imported by: 0

Documentation

Overview

Package gltf 实现了加载glTF格式3D模型的数据结构和方法

Index

Constants

View Source
const (
	ChunkJSON = 0x4E4F534A
	ChunkBIN  = 0x004E4942
)
View Source
const (
	POINTS         = 0
	LINES          = 1
	LINE_LOOP      = 2
	LINE_STRIP     = 3
	TRIANGLES      = 4
	TRIANGLE_STRIP = 5
	TRIANGLE_FAN   = 6
)
View Source
const (
	NEAREST                = 9728
	LINEAR                 = 9729
	NEAREST_MIPMAP_NEAREST = 9984
	LINEAR_MIPMAP_NEAREST  = 9985
	NEAREST_MIPMAP_LINEAR  = 9986
	LINEAR_MIPMAP_LINEAR   = 9987
)
View Source
const (
	CLAMP_TO_EDGE   = 33071
	MIRRORED_REPEAT = 33648
	REPEAT          = 10497
)

Variables

This section is empty.

Functions

func ReadGLB

func ReadGLB(r io.Reader) (json, bin []byte, err error)

把GLB格式解码为JSON和BIN两个块. 考虑到3D软件都是运行在大内存的电脑里, 我们一次把所有内容都读进来.

func WriteGLB

func WriteGLB(w io.Writer, json, bin []byte) (err error)

把JSON和BIN两个块组合成GLB格式写入文件

Types

type Accessor

type Accessor struct {
	BufferView    uint32
	ByteOffset    uint32
	ComponentType ComponentType
	Count         uint32
	Max           []float32
	Min           []float32
	Type          DataType

	Sparse *struct {
		Count   uint32
		Indices struct {
			BufferView    uint32
			ByteOffset    uint32
			ComponentType ComponentType
		}
		Values struct {
			BufferView uint32
			ByteOffset uint32
		}
	}
}

func (Accessor) NumElemBytes

func (a Accessor) NumElemBytes() uint32

func (Accessor) NumTotalBytes

func (a Accessor) NumTotalBytes() uint32

type Animation

type Animation struct {
	Channels    []AnimationChannel
	AniSamplers []AnimationSampler `json:"Samplers"`
	Name        string
}

type AnimationChannel

type AnimationChannel struct {
	AniSampler uint32 `json:"Sampler"` // 指向所属Animation的Samplers, 不是数据的Sampler
	Target     AnimationTarget
}

type AnimationSampler

type AnimationSampler struct {
	KeyTimes      uint32 `json:"Input"`  // glTF的Input是关键帧时间点
	KeyValues     uint32 `json:"Output"` // glTF的Output是关键帧的各参数值
	Interpolation string // "LINEAR" "STEP" "CUBICSPLINE", ""=="LINEAR"
}

type AnimationTarget

type AnimationTarget struct {
	Node uint32
	Path string //    "translation"  "rotation"  "scale" "weights"
}

type Asset

type Asset struct {
	Version   string
	Generator string
	Copyright string
}

type Attributes

type Attributes struct {
	POSITION   *uint32
	NORMAL     *uint32
	TANGENT    *uint32
	TEXCOORD_0 *uint32
	TEXCOORD_1 *uint32
	TEXCOORD_2 *uint32
	TEXCOORD_3 *uint32
	COLOR_0    *uint32
	COLOR_1    *uint32
	COLOR_2    *uint32
	COLOR_3    *uint32
	JOINTS_0   *uint32
	JOINTS_1   *uint32
	JOINTS_2   *uint32
	JOINTS_3   *uint32
	WEIGHTS_0  *uint32
	WEIGHTS_1  *uint32
	WEIGHTS_2  *uint32
	WEIGHTS_3  *uint32
}

type Buffer

type Buffer struct {
	URI        string
	ByteLength uint32
	Name       string
	// contains filtered or unexported fields
}

func (Buffer) IsExternal

func (b Buffer) IsExternal() bool

type BufferView

type BufferView struct {
	Buffer     uint32
	ByteLength uint32
	ByteOffset uint32  // default 0
	ByteStride *uint32 // nil or  4 to 252
	Target     *uint32 // 34962 ARRAY_BUFFER, 34963 ELEMENT_ARRAY_BUFFER
	Name       string
}

type Camera

type Camera struct {
	Name        string
	Type        string
	Perspective *struct {
		AspectRatio float32
		Yfov        float32
		Zfar        float32
		Znear       float32
	}
	Orthographic *struct {
		Xmag  float32
		Ymag  float32
		Zfar  float32
		Znear float32
	}
}

type ComponentType

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

func (ComponentType) MarshalJSON

func (t ComponentType) MarshalJSON() ([]byte, error)

func (ComponentType) NumBytes

func (t ComponentType) NumBytes() uint32

func (ComponentType) String

func (t ComponentType) String() string

func (*ComponentType) UnmarshalJSON

func (t *ComponentType) UnmarshalJSON(b []byte) error

type DataType

type DataType uint32
const (
	SCALAR DataType = iota
	VEC2
	VEC3
	VEC4
	MAT2
	MAT3
	MAT4
)

func (DataType) MarshalJSON

func (a DataType) MarshalJSON() ([]byte, error)

func (DataType) NumBytes

func (a DataType) NumBytes() uint32

func (DataType) String

func (a DataType) String() string

func (*DataType) UnmarshalJSON

func (a *DataType) UnmarshalJSON(b []byte) error

type GLTF

type GLTF struct {
	Asset       Asset
	Nodes       []*Node
	Scenes      []*Scene
	Scene       *uint32
	Accessors   []*Accessor
	Meshes      []*Mesh
	Skins       []*Skin
	Textures    []*Texture
	Images      []*Image
	Samplers    []*Sampler
	Materials   []*Material
	Cameras     []*Camera
	BufferViews []*BufferView
	Buffers     []*Buffer
	Animations  []*Animation

	ExtensionsUsed     []string
	ExtensionsRequired []string

	BIN []byte `json:"-"`
}

func Decode

func Decode(jsonChunk, binChunk []byte) (p *GLTF, err error)

把数据块解码成结构体. 如果没有二进制数据块, binChunk可以为空.

func ReadDecodeGLB

func ReadDecodeGLB(r io.Reader) (p *GLTF, err error)

func (*GLTF) ReadAccessor

func (p *GLTF) ReadAccessor(i uint32) (data []byte, stride uint32, a *Accessor, err error)

func (*GLTF) ReadBuffer

func (p *GLTF) ReadBuffer(i uint32) (data []byte, mime string, err error)

func (*GLTF) ReadBufferView

func (p *GLTF) ReadBufferView(i uint32) (data []byte, mime string, bv *BufferView, err error)

func (*GLTF) ReadImage

func (p *GLTF) ReadImage(i uint32) (data []byte, mime string, img *Image, err error)

type Image

type Image struct {
	URI        string
	BufferView *uint32
	MimeType   string
	Name       string
}

type KHR_materials_clearcoat

type KHR_materials_clearcoat struct {
	ClearcoatFactor           float32
	ClearcoatTexture          *TextureInfo
	ClearcoatRoughnessFactor  float32
	ClearcoatRoughnessTexture *TextureInfo
	ClearcoatNormalTexture    *NormalTextureInfo
}

type KHR_materials_pbrSpecularGlossiness

type KHR_materials_pbrSpecularGlossiness struct {
	DiffuseFactor             *[4]float32
	SpecularFactor            *[3]float32
	DiffuseTexture            TextureInfo
	GlossinessFactor          *float32
	SpecularGlossinessTexture TextureInfo
}

type KHR_materials_unlit

type KHR_materials_unlit struct {
}

type Material

type Material struct {
	Name string

	PbrMetallicRoughness *struct {
		BaseColorFactor  *[4]float32 // default 1
		BaseColorTexture *TextureInfo

		MetallicFactor           *float32 // default 1
		RoughnessFactor          *float32 // default 1
		MetallicRoughnessTexture *TextureInfo
	}

	NormalTexture *NormalTextureInfo

	OcclusionTexture *OcclusionTextureInfo

	EmissiveFactor  [3]float32 // default [0 0 0]
	EmissiveTexture *TextureInfo

	AlphaMode   string
	AlphaCutoff *float32 // default 0.5
	DoubleSided bool

	Extensions struct {
		KHR_materials_pbrSpecularGlossiness *KHR_materials_pbrSpecularGlossiness
		KHR_materials_clearcoat             *KHR_materials_clearcoat
		KHR_materials_unlit                 *KHR_materials_unlit
	}
}

type Matrix

type Matrix [16]float32

type Mesh

type Mesh struct {
	Primitives []Primitive
	Weights    []float32
	Name       string
}

type MorphTarget

type MorphTarget struct {
	NORMAL   *uint32
	POSITION *uint32
	TANGENT  *uint32
}

顶点变形动画的目标.

type Node

type Node struct {
	Name        string
	Mesh        *uint32
	Skin        *uint32
	Children    []uint32
	Camera      *uint32
	Translation *Translation
	Scale       *Scale
	Rotation    *Rotation
	Matrix      *Matrix
}

type NormalTextureInfo

type NormalTextureInfo struct {
	Index    uint32
	TexCoord uint32
	Scale    *float32 // default 1
}

type OcclusionTextureInfo

type OcclusionTextureInfo struct {
	Index    uint32
	TexCoord uint32
	Strength *float32 // default 1
}

type Primitive

type Primitive struct {
	Attributes Attributes    // 顶点属性
	Indices    *uint32       // 顶点索引
	Material   *uint32       // 材质
	Mode       *uint32       // 图元类型 default 4 TRIANGLES
	Targets    []MorphTarget // 顶点变形动画的目标
}

几何图元

type Rotation

type Rotation [4]float32

type Sampler

type Sampler struct {
	MagFilter uint32
	MinFilter uint32
	WrapS     uint32
	WrapT     uint32
}

type Scale

type Scale [3]float32

type Scene

type Scene struct {
	Name  string
	Nodes []uint32
}

type Skin

type Skin struct {
	Name                string
	InverseBindMatrices *uint32
	Joints              []uint32

	// 骨骼的公共根节点LCA(非必填).
	// glTF文档里虽然没明确指出骨骼的范围, 我们理解为当前Skin的骨骼的LCA.
	Skeleton *uint32
}

皮肤. glTF的皮肤可以理解为Bind Pose, 不是通常意义上的皮肤. 它通常和Mesh配套使用, 把Mesh绑到骨骼上. 当然它也可以不绑Mesh, Bind Pose本身可独立使用, 比如用来画骨骼. 皮肤里最有意义的数据是IBM数据, IBM和Bind Pose其实是一回事, 知道骨骼姿势就能算出IBM, 反之亦然.

type Texture

type Texture struct {
	Name    string
	Sampler *uint32 // When undefined, a sampler with repeat wrapping and auto filtering should be used
	Source  *uint32 // When undefined, it is expected that an extension or other mechanism will supply an alternate texture source
}

type TextureInfo

type TextureInfo struct {
	Index    uint32
	TexCoord uint32
}

type Translation

type Translation [3]float32

Jump to

Keyboard shortcuts

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