Swapping materials with custom vertex attributes?

Is swapping materials with custom vertex attributes supported? After the painful mysteries found in this thread, I’d rather not do something that’s akin to tip-toeing around potential bugs in the engine.

More specifically, let’s say I have material A and material B. My sprites are all drawn with A, then I want to use render.enable_material("B") to draw them again, but with the new shaders that B offers.

Material A has three custom vertex attributes, whereas material B has four custom vertex attributes. Some of these attributes are unique to the material, others might be the same and should be shared across draw calls:

Material A
Attribute 1: color (UNIQUE)
Attribute 2: light (SHARED)
Attribute 3: normal (SHARED)

Material B
Attribute 1: normal (SHARED)
Attribute 2: light (SHARED)
Attribute 3: flavor (UNIQUE)
Attribute 4: enthusiasm (UNIQUE)

The other option would be to use multiple sprites on the same game object, where each sprite uses the same texture but a different material. I would like to take the render.enable_material() route if possible.

1 Like

That should work yes, what defold version are you using? I very recently fixed an issue related to this specific scenario

Okay, just making sure. I’m using version 1.9.3, always the latest.

I suppose the main issue I’m having is that custom vertex attributes don’t seem to be built into the vertex format if those attributes aren’t used in a shader. For example, if material A only carries the shared light and normal attributes so that those properties can be customized with go.set() before swapping to material B, then material B actually uses them in its fragment shader.

This scenario doesn’t seem to work since the sprite component is initially given material A, which doesn’t actualize those shared properties. Does that make sense?

Could you share a repro or a small project that I can look at? I’m fairly certain your scenario is supported since at least 1.9.3, but maybe there’s some case missing for whatever reason :thinking:

1 Like