Documentation
¶
Overview ¶
Package modeler implements helper methods to write common glTF entities (indices, positions, colors, ...) into buffers.
Example ¶
package main
import (
"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/modeler"
)
func main() {
doc := gltf.NewDocument()
positionAccessor := modeler.WritePosition(doc, [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}})
indicesAccessor := modeler.WriteIndices(doc, []uint16{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3})
colorIndices := modeler.WriteColor(doc, [][3]uint8{{50, 155, 255}, {0, 100, 200}, {255, 155, 50}, {155, 155, 155}, {25, 25, 25}})
doc.Meshes = []*gltf.Mesh{{
Name: "Pyramid",
Primitives: []*gltf.Primitive{
{
Indices: gltf.Index(indicesAccessor),
Attributes: gltf.PrimitiveAttributes{
gltf.POSITION: positionAccessor,
gltf.COLOR_0: colorIndices,
},
},
},
}}
doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)
if err := gltf.SaveBinary(doc, "./example.glb"); err != nil {
panic(err)
}
}
Output:
Index ¶
- func ReadAccessor(doc *gltf.Document, acr *gltf.Accessor, buffer []byte) (any, error)
- func ReadBufferView(doc *gltf.Document, bv *gltf.BufferView) ([]byte, error)
- func ReadColor(doc *gltf.Document, acr *gltf.Accessor, buffer [][4]uint8) ([][4]uint8, error)
- func ReadColor64(doc *gltf.Document, acr *gltf.Accessor, buffer [][4]uint16) ([][4]uint16, error)
- func ReadIndices(doc *gltf.Document, acr *gltf.Accessor, buffer []uint32) ([]uint32, error)
- func ReadInverseBindMatrices(doc *gltf.Document, acr *gltf.Accessor, buffer [][4][4]float32) ([][4][4]float32, error)
- func ReadJoints(doc *gltf.Document, acr *gltf.Accessor, buffer [][4]uint16) ([][4]uint16, error)
- func ReadNormal(doc *gltf.Document, acr *gltf.Accessor, buffer [][3]float32) ([][3]float32, error)
- func ReadPosition(doc *gltf.Document, acr *gltf.Accessor, buffer [][3]float32) ([][3]float32, error)
- func ReadTangent(doc *gltf.Document, acr *gltf.Accessor, buffer [][4]float32) ([][4]float32, error)
- func ReadTextureCoord(doc *gltf.Document, acr *gltf.Accessor, buffer [][2]float32) ([][2]float32, error)
- func ReadWeights(doc *gltf.Document, acr *gltf.Accessor, buffer [][4]float32) ([][4]float32, error)
- func WriteAccessor(doc *gltf.Document, target gltf.Target, data any) int
- func WriteAccessorsInterleaved(doc *gltf.Document, data ...any) ([]int, error)
- func WriteBufferView(doc *gltf.Document, target gltf.Target, data any) int
- func WriteBufferViewInterleaved(doc *gltf.Document, data ...any) (int, error)
- func WriteColor(doc *gltf.Document, data any) int
- func WriteImage(doc *gltf.Document, name string, mimeType string, r io.Reader) (int, error)
- func WriteIndices(doc *gltf.Document, data any) int
- func WriteInverseBindMatrices(doc *gltf.Document, data [][4][4]float32) int
- func WriteJoints(doc *gltf.Document, data any) int
- func WriteNormal(doc *gltf.Document, data [][3]float32) int
- func WritePosition(doc *gltf.Document, data [][3]float32) int
- func WritePrimitiveAttributes(doc *gltf.Document, attr ...PrimitiveAttribute) (gltf.PrimitiveAttributes, error)
- func WriteTangent(doc *gltf.Document, data [][4]float32) int
- func WriteTextureCoord(doc *gltf.Document, data any) int
- func WriteWeights(doc *gltf.Document, data any) int
- type PrimitiveAttribute
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadAccessor ¶ added in v0.17.0
ReadAccessor returns the data references by acr as an slice whose element types are the ones associated with acr.ComponentType and acr.Type.
If buffer is not nil, it will be used as backing slice.
ReadAccessor supports all types of accessors: non-interleaved, interleaved, sparse, without buffer views, ..., and any combinations of them.
ReadAccessor is safe to use even with malformed documents. If that happens it will return an error instead of panic.
func ReadBufferView ¶ added in v0.17.0
ReadBufferView returns the slice of bytes associated with the BufferView. The slice is a view of the buffer data, so it is not safe to modify it.
It is safe to use even with malformed documents. If that happens it will return an error instead of panic.
func ReadColor ¶ added in v0.17.0
ReadColor returns the data referenced by acr. If acr.ComponentType is other than Ubyte the data will be converted and normalized appropriately.
See ReadAccessor for more info.
func ReadColor64 ¶ added in v0.17.0
ReadColor returns the data referenced by acr. If acr.ComponentType is other than Ushort the data will be converted and normalized appropriately.
See ReadAccessor for more info.
func ReadIndices ¶ added in v0.17.0
ReadIndices returns the data referenced by acr. If acr.ComponentType is other than Uint the data will be converted appropriately.
See ReadAccessor for more info.
func ReadInverseBindMatrices ¶ added in v0.28.0
func ReadInverseBindMatrices(doc *gltf.Document, acr *gltf.Accessor, buffer [][4][4]float32) ([][4][4]float32, error)
ReadInverseBindMatrices returns the data referenced by acr.
See ReadAccessor for more info.
func ReadJoints ¶ added in v0.17.0
ReadJoints returns the data referenced by acr. If acr.ComponentType is other than Ushort the data will be converted and denormalized appropriately.
See ReadAccessor for more info.
func ReadNormal ¶ added in v0.17.0
ReadNormal returns the data referenced by acr.
See ReadAccessor for more info.
func ReadPosition ¶ added in v0.17.0
func ReadPosition(doc *gltf.Document, acr *gltf.Accessor, buffer [][3]float32) ([][3]float32, error)
ReadPosition returns the data referenced by acr.
See ReadAccessor for more info.
func ReadTangent ¶ added in v0.17.0
ReadTangent returns the data referenced by acr.
See ReadAccessor for more info.
func ReadTextureCoord ¶ added in v0.17.0
func ReadTextureCoord(doc *gltf.Document, acr *gltf.Accessor, buffer [][2]float32) ([][2]float32, error)
ReadTextureCoord returns the data referenced by acr. If acr.ComponentType is other than Float the data will be converted and denormalized appropriately.
See ReadAccessor for more info.
func ReadWeights ¶ added in v0.17.0
ReadWeights returns the data referenced by acr. If acr.ComponentType is other than Float the data will be converted and denormalized appropriately.
See ReadAccessor for more info.
func WriteAccessor ¶ added in v0.16.0
WriteAccessor adds a new Accessor to doc and fills the buffer with the data. Returns the index of the new accessor.
func WriteAccessorsInterleaved ¶ added in v0.17.3
WriteAccessorsInterleaved adds as many accessors as elements in data all pointing to the same interleaved buffer view and fills the buffer with the data. Returns an slice with the indices of the newly created accessors, with the same order as data or an error if the data elements don´t have all the same length.
Example ¶
package main
import (
"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/modeler"
)
func main() {
doc := gltf.NewDocument()
indices, _ := modeler.WriteAccessorsInterleaved(doc,
[][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}},
[][3]uint8{{50, 155, 255}, {0, 100, 200}, {255, 155, 50}, {155, 155, 155}, {25, 25, 25}},
)
indicesAccessor := modeler.WriteIndices(doc, []uint16{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3})
doc.Meshes = []*gltf.Mesh{{
Name: "Pyramid",
Primitives: []*gltf.Primitive{
{
Indices: gltf.Index(indicesAccessor),
Attributes: gltf.PrimitiveAttributes{
gltf.POSITION: indices[0],
gltf.COLOR_0: indices[1],
},
},
},
}}
doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)
if err := gltf.SaveBinary(doc, "./example.glb"); err != nil {
panic(err)
}
}
Output:
func WriteBufferView ¶ added in v0.16.0
WriteBufferView adds a new BufferView to doc and fills the buffer with the data. Returns the index of the new buffer view.
func WriteBufferViewInterleaved ¶ added in v0.17.3
WriteBufferViewInterleaved adds a new BufferView to doc and fills the buffer with one or more vertex attribute. If success it returns the index of the new buffer view. Returns the index of the new buffer view or an error if the data elements don´t have all the same length.
func WriteColor ¶ added in v0.16.0
WriteColor adds a new COLOR accessor to doc and fills the buffer with data. If success it returns the index of the new accessor.
func WriteImage ¶ added in v0.16.0
WriteImage adds a new image to doc and fills the buffer with the image data. If success it returns the index of the new image.
Example ¶
package main
import (
"bytes"
"os"
"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/modeler"
)
func main() {
img, err := os.ReadFile("../assets/gopher_high.png")
if err != nil {
panic(err)
}
doc := gltf.NewDocument()
indicesAccessor := modeler.WriteIndices(doc, []uint16{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3})
positionAccessor := modeler.WritePosition(doc, [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}})
textureAccessor := modeler.WriteTextureCoord(doc, [][2]float32{{0, 1}, {0.4, 1}, {0.4, 0}, {0.4, 1}, {0, 1}})
imageIdx, err := modeler.WriteImage(doc, "gopher", "image/png", bytes.NewReader(img))
if err != nil {
panic(err)
}
doc.Textures = append(doc.Textures, &gltf.Texture{Source: gltf.Index(imageIdx)})
doc.Materials = append(doc.Materials, &gltf.Material{
Name: "Texture",
PBRMetallicRoughness: &gltf.PBRMetallicRoughness{
BaseColorTexture: &gltf.TextureInfo{
Index: len(doc.Textures) - 1,
},
MetallicFactor: gltf.Float(0),
},
})
doc.Meshes = []*gltf.Mesh{{
Name: "Pyramid",
Primitives: []*gltf.Primitive{
{
Indices: gltf.Index(indicesAccessor),
Attributes: gltf.PrimitiveAttributes{
gltf.POSITION: positionAccessor,
gltf.TEXCOORD_0: textureAccessor,
},
Material: gltf.Index(len(doc.Materials) - 1),
},
},
}}
doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)
if err := gltf.SaveBinary(doc, "./example.glb"); err != nil {
panic(err)
}
}
Output:
func WriteIndices ¶ added in v0.16.0
WriteIndices adds a new INDICES accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WriteInverseBindMatrices ¶ added in v0.28.0
WriteInverseBindMatrices adds a new inverse bind matrices accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WriteJoints ¶ added in v0.16.0
WriteJoints adds a new JOINTS accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WriteNormal ¶ added in v0.16.0
WriteNormal adds a new NORMAL accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WritePosition ¶ added in v0.16.0
WritePosition adds a new POSITION accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WritePrimitiveAttributes ¶ added in v0.27.0
func WritePrimitiveAttributes(doc *gltf.Document, attr ...PrimitiveAttribute) (gltf.PrimitiveAttributes, error)
WritePrimitiveAttributes write all the primitives attributes to doc as interleaved data. Returns an attribute map that can be directly used as a primitive attributes.
Example ¶
package main
import (
"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/modeler"
)
func main() {
doc := gltf.NewDocument()
attrs, _ := modeler.WritePrimitiveAttributes(doc,
modeler.PrimitiveAttribute{Name: gltf.POSITION, Data: [][3]float32{{1, 2, 3}, {0, 0, -1}}},
modeler.PrimitiveAttribute{Name: gltf.NORMAL, Data: [][3]float32{{1, 2, 3}, {0, 0, -1}}},
modeler.PrimitiveAttribute{Name: gltf.TANGENT, Data: [][4]float32{{1, 2, 3, 4}, {1, 2, 3, 4}}},
modeler.PrimitiveAttribute{Name: gltf.TEXCOORD_0, Data: [][2]uint8{{0, 255}, {255, 0}}},
modeler.PrimitiveAttribute{Name: gltf.TEXCOORD_1, Data: [][2]float32{{1, 2}, {1, 2}}},
modeler.PrimitiveAttribute{Name: gltf.JOINTS_0, Data: [][4]uint8{{1, 2, 3, 4}, {1, 2, 3, 4}}},
modeler.PrimitiveAttribute{Name: gltf.WEIGHTS_0, Data: [][4]uint8{{1, 2, 3, 4}, {1, 2, 3, 4}}},
modeler.PrimitiveAttribute{Name: gltf.COLOR_0, Data: [][3]uint8{{255, 255, 255}, {0, 255, 0}}},
modeler.PrimitiveAttribute{Name: "COLOR_1", Data: [][3]uint8{{0, 0, 255}, {100, 200, 0}}},
modeler.PrimitiveAttribute{Name: "COLOR_2", Data: [][4]uint8{{23, 58, 188, 1}, {0, 155, 0, 0}}},
)
indicesAccessor := modeler.WriteIndices(doc, []uint16{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3})
doc.Meshes = []*gltf.Mesh{{
Name: "Pyramid",
Primitives: []*gltf.Primitive{
{
Indices: gltf.Index(indicesAccessor),
Attributes: attrs,
},
},
}}
doc.Nodes = []*gltf.Node{{Name: "Root", Mesh: gltf.Index(0)}}
doc.Scenes[0].Nodes = append(doc.Scenes[0].Nodes, 0)
if err := gltf.SaveBinary(doc, "./example.glb"); err != nil {
panic(err)
}
}
Output:
func WriteTangent ¶ added in v0.16.0
WriteTangent adds a new TANGENT accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
func WriteTextureCoord ¶ added in v0.16.0
WriteTextureCoord adds a new TEXTURECOORD accessor to doc and fills the last buffer with data. If success it returns the index of the new accessor.
Types ¶
type PrimitiveAttribute ¶ added in v0.27.0
PrimitiveAttribute holds the data referenced by a gltf.PrimitiveAttributes entry.