jkuhlmann / cgltf

:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
MIT License
1.46k stars 136 forks source link

Add support for KHR_materials_variants #132

Closed zeux closed 3 years ago

zeux commented 3 years ago

KHR_materials_variants adds two arrays: a top-level array of asset variants, where each variant can have a name and arbitrary extra info, and a per-primitive mappings array that establishes a map between variants and materials.

Specification: https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_variants/README.md

Since modeling arrays-of-arrays is awkward wrt memory management, this change represents mappings as a flat array of material-variant pairs. When parsing we unpack the source data into that representation.

variant is stored as an index; while we could store a pointer as well, for runtime representation it seems best to preserve the indexing scheme. We could change this in the future if need be...

In addition to adding support for this extension, this change also fixes a couple of issues with cgltf_write, namely ability to write non-nullterminated extras (fixing crashes in test_write for assets with extras) and KHR_lights_punctual handling in cases when no node refers to an existing light.

jkuhlmann commented 3 years ago

Great work (as always) - thanks!

zeux commented 3 years ago

Oh, looks like I forgot a couple of error checks for edge conditions; I'm going to submit a followup PR after I do a fuzzing run to see if anything else is amiss during parsing in general.