GLTF export custom attributes?

I have exported a GLTF geometry in Blender with the 3 standard attributes:

  • Position
  • Normals
  • UVs

Now I’m trying to add a custom one-dimensional attribute called “size” in the [0, 1] range to do some custom GLSL shader stuff. Something akin to:

geometry.setAttribute( 'size', new THREE.BufferAttribute( sizeArray, 1 ) );

How can I export this custom attribute?

In Blender, I’ve used Vertex Weights (in a Vertex Group) to assign this one-dimensional attribute to each vertex, but I can’t find a way to export it.
(Red = 1, green = 0.5, blue = 0):

vertexWeights

I’ve tried selecting “Include > Custom Properties”, but that gives me just a single custom: 1 value, not a full BufferAttribute. Is there a way to achieve this with GLTF? Also, can THREE.GLTFLoader parse custom attributes?

THREE.GLTFLoader can parse custom attributes, but the Blender exporter can’t currently write them. See https://github.com/KhronosGroup/glTF-Blender-IO/pull/807 for a work-in-progress. You can test that with vertex colors, as documented there, but it doesn’t yet support vertex weights. That would probably be a good addition. Feedback welcome!

3 Likes

Bummer. I guess I could generate a second set of UVs instead and just use the U vector while ignoring the V. Thanks, Don!

One other idea, you could use vertex colors — gltf supports any number of vertex color sets per mesh. Probably blender can put your weights into vertex colors with the python api, or you could hand paint them. Main downside being that they’ll be sRGB->linear decoded at export, so values will change a little.

Correct me if I’m wrong, but wouldn’t vertex colors provide me with a vec3 attribute? I only need 1 dimension for “size”, so I’d be wasting 2 elements per vertex. Can vertex colors be exported in a single channel?

2 Likes

Correct — you’d have vec3 with vertex colors, vec2 with UVs, or float with the (currently not exportable) vertex group weights.

1 Like

After the war, but if someone is looking like me a way to export attributes from Blender.

There is a simple solution from current version by simply prefix the attribute name with an underscore in the attribute section of your mesh “_myAttribute”.

Then when exporting, go to Export GLTF → Mesh → Attribute

So all prefixed attributes with “_” will be exported with your geometry

Related documentation:

https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html#data-mesh

image


image

4 Likes

For anyone finding this even later, post above is very helpful, but be careful when using custom attributes that are color type while not exporting vertex colors, because then your custom attribute might not be exported!
So a configuration like this, where you want a custom attribute of color type (here _side_color) but aren’t using vertex colors:

image

Here _side_color actually will not get exported (not sure if always, but is happening now in Blender 3.6).

Why? My guess is since it is color type, it is automatically placed in Color Attributes category (left side of the pic), which in turn makes Blender think it is vertex colors (the ones meant to influence the color of the material). Thus when you select not to export Vertex Colors, it is omitted. However if you decide to export it by just selecting Vertex Colors option as well, then gltf treats it as material vertex colors resulting in them influencing material’s diffuse color. This can be wokred around by setting vertex colors to false on materials, but thats still unnecessary work.

The solution is to leave the export options as is (Custom Attributes YES, Vertex Colors NO) and create an empty Color Attribute before export and set it as Default Color Attribute Used For Rendering - the little camera icon next to the Color attribute name:

image

Hope this helps someone in case they find it. @donmccurdy is this the intended behavior or a bug?

Not sure if that’s intended or not … Blender’s custom attributes are pretty new to me, perhaps worth asking on the repository (GitHub - KhronosGroup/glTF-Blender-IO: Blender glTF 2.0 importer and exporter).