nicmar

I'm using a simple custom shader to tint a spine model. Then I use the code below to set a property of it.
It works for SpriteRenderers, but not on the MeshRenderer that spine uses.

The shader inspector says "MaterialPropertyBlock is used to modify these values", on the SpriteRenderer. Not sure if that makes any difference. Or isn't this supported? If so, how can I change the shader on the Spine object?
private static readonly int _weight = Shader.PropertyToID("_weight");
// ...

// Doesn't change the material
meshRenderer.material.SetFloat(_weight,1);

// Changes the sprite renderer material
spriteRenderer.material.SetFloat(_weight,1);
However, when i change the weight parameter of the shader's inspector on the object when the game is running, it changes.

Any ideas what's wrong here?

---

I found this code linked from a forum post about this topic:
https://gist.github.com/pharan/7a06e2a5239f3893d79b8852c249642f

And tried this code:
block = new MaterialPropertyBlock();
meshRenderer.SetPropertyBlock(block);
block.SetFloat(_weight,1);
Debug.Log("block.GetFloat = " + block.GetFloat(_weight));
The debug outputs 1, but the image and the shader still remains unchanged..

Also the shader on the spine meshrenderer doesn't say "MaterialPropertyBlock is used to modify these values" like it does on SpriteRenderer which seem to indicate i'm doing something wrong...
We're making SpaceChef with Spine. An action filled cooking adventure. We're coming soon to Kickstarter, sign up for our newsletter to be the first to get notified: www.spacechefgame.com. Follow our work on Twitter @BlueGooGames.
User avatar
nicmar
  • Posts: 148

Harald

nicmar wrote:And tried this code:
block = new MaterialPropertyBlock();
meshRenderer.SetPropertyBlock(block);
block.SetFloat(_weight,1);
Debug.Log("block.GetFloat = " + block.GetFloat(_weight));
The debug outputs 1, but the image and the shader still remains unchanged..
You are applying the property block, and then modify the float value after the (default unchanged) block was applied. Please note that order matters here :). Also you typically re-use your property block as shown in the MaterialPropertyBlockExample script in example scene Per Instance Material Properties.
User avatar
Harald

Harri
  • Posts: 4459

nicmar

Thanks a lot, that worked of course! :)
We're making SpaceChef with Spine. An action filled cooking adventure. We're coming soon to Kickstarter, sign up for our newsletter to be the first to get notified: www.spacechefgame.com. Follow our work on Twitter @BlueGooGames.
User avatar
nicmar
  • Posts: 148

Harald

Glad to hear, thanks for letting us know! :)
User avatar
Harald

Harri
  • Posts: 4459


Return to Unity