Shader variants and MaterialPropertyBlocks

Hi,

I’m currently trying to figure out how to get per-instance MaterialPropertyBlocks settings to work with shader variants.
As I see it, MaterialPropertyBlocks has currently no method to set shader variants, e.g. like Enable/DisableKeyword.

What is the correct procedure to switch the shader variant in use, ideally per instance (in this case, a GameObject holding a controller MonoBehaviour)? Where are the pitfalls?
(E.g. cloning the material is going to lead to in-memory copies.)

Cheers and thanks for any insights.

7628020–949219–RedGreen.shader (1.58 KB)
7628020–949222–RedGreenController.cs (4.1 KB)

Additionally: what should I keep in mind when working with ComputeBuffers/GraphicsBuffers that are only available in a variant?

The correct procedure is to not use keywords / variants, because instancing doesn’t work between variants. Every instance in a GPU instanced draw needs to use exactly the same material, mesh, and shader variant.

The solution is to use if or switch statements in your shader checking against an instanced property.

Ok, but if I do use ifs/switches and have ComputeBuffers that I only set at runtime, the shader doesn’t show in the Editor and I get log spam like this:
Metal: Vertex shader missing buffer binding at index 4 (BonePositions / SpheroidNormal/Debug/SpheroidNormal)

This is the core of my issue. What’s the best way to handle this?

Attached the controller and shader code, currently working but leaking materials.

7630819–949705–SpheroidNormalController.cs (12.8 KB)
7630819–949708–SpheroidNormal.shader (5.1 KB)

Use [ExecuteInEditMode] to set the buffers in the editor?

Alternatively, when the game starts, set the keyword as a shader global, and unset it when the game stops.