qmuntal / gltf

Go library for encoding glTF 2.0 files
https://www.khronos.org/gltf/
BSD 2-Clause "Simplified" License
242 stars 32 forks source link

Add reflection-based `gltf/modeler` APIs akin to `json.Marshal()`? #72

Closed myaaaaaaaaa closed 4 months ago

myaaaaaaaaa commented 1 year ago

Example pseudocode as follows:

Array-of-Structures example

attrs := []struct {
    Position                [3]float `gltf:"required"`
    TexCoord_0              [2]float   // No need for interface{}
    Color_0                 [3]uint8
    Color_1                 [4]float   // Additional colors supported seamlessly
    CustomMetallicRoughness [2]float   // Marshals/unmarshals as "_METALLICROUGHNESS"
    // Occlusion            float      // panics, custom attributes must be prefixed with "Custom"
}{}
~Structure-of-Arrays example~ ```go attrs := struct { Position [][3]float `gltf:"required"` Normal [][3]float }{} ```
~Array-of-Pointer-to-Structures example~ ```go // modeler.ReadAttributes() should internally allocate an Array-of-Structures // to which the returned Array-of-Pointer-to-Structures all point to, to reduce allocator/GC pressure attrs := []*struct { Position [3]float `gltf:"required"` Normal [3]float }{} ```


if err := modeler.ReadAttributes(doc, doc.Meshes[0].Primitives[0].Attributes, &attrs); err != nil {
    // ...
}

// Maybe an interleaved option?
if attributesMap, err := modeler.WriteAttributes(doc, attrs); err == nil {
    doc.Meshes[0].Primitives[0].Attributes = attributesMap
} else {
    // ...
}
qmuntal commented 1 year ago

Having a reflection-based API opens the door for an unbounded number of feature requests, as it would never be expressive enough to cover each niche use-case.

IMO reading and writing attributes is already pretty straightforward, maybe a little verbose due to error handling -which I suppose is why you have submitted this proposal- but that's on Go itself.

myaaaaaaaaa commented 1 year ago

In that case, how about reducing the scope to just Array-of-Structures (which requires reflection, and was the main motivator for this proposal)?

Structure-of-Arrays (which gltf/modeler's APIs are currently all based on) may have superior runtime performance, but it does make mesh processing more awkward.