Search code examples
gltf

Reading GLTF vertex color encoding


I'm trying to parse the BoxVertexColors GLTF 2.0 file from the official test sample set and store the information in my own data structures. I've got the position, normals, etc to work, but the result parsing the vertex color mesh attribute is weird.

The .gltf states the format is 5126 (=float) and VEC4, the starting offset is 0x288. When looking into the buffer.bin file, the content at 0x288 starts with

1D E9 06 30   7A 7A 25 3C   FE FF 7F 3F   51 D2 27 30

which doesn't look like floats to me, let alone an encoded color. I would have expected four floats representing RGBA. What am I missing ...?


Solution

  • What makes you think the data is incorrect?

      (0x3006E91D, 0X3C257A7A, 0x3F7FFFFE, 0x3727D251) =>
      (4.90802e-10, 0.0101, 1.0, 6.10531e-10) => (simplified)
      (0, 0.0101, 1.0, 0)
    

    Apart from the possibly nonsensical alpha value, this seems like a perfectly reasonable start position for what's clearly a computed range of colours spread across a cube.

    The alpha value is ultimately irrelevant, because the associated material does not specify a blendMode setting, which in turn means it defaults to OPAQUE, and thus transparency is ignored.

    Loading the model in https://gltf-viewer.donmccurdy.com/ and breakpointing confirms that the data is decoded as you'd expect:

    JavaScript Debugger