gltf icon indicating copy to clipboard operation
gltf copied to clipboard

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

Open myaaaaaaaaa opened this issue 1 year ago • 2 comments

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~

attrs := struct {
	Position  [][3]float `gltf:"required"`
	Normal    [][3]float
}{}

~Array-of-Pointer-to-Structures example~

// 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 {
	// ...
}

myaaaaaaaaa avatar Aug 09 '23 16:08 myaaaaaaaaa

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.

qmuntal avatar Aug 10 '23 15:08 qmuntal

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.

myaaaaaaaaa avatar Aug 10 '23 18:08 myaaaaaaaaa