MaterialPropertyBlock not setting lerp interpolation value inside shader

Hiya,

I’m pretty sure there is somehing wrong with my way of doing it, but I can’t figure it out. I’m trying to lerp between two textures, setting interpolation value using MaterialPropertyBlock and it seems like its just not working for me.

Shader pseudo

Properties {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _MainTexFinal ("Albedo Final(RGB)", 2D) = "white" {}
        _InterpolationValue("Interpolation value", Range(0,1)) = 0.0
    }
SubShader {
        Tags { "RenderType"="Opaque" }
        CGPROGRAM
        sampler2D _MainTex;
        sampler2D _MainTexMaxVelo;
        half _InterpolationValue;

void surf(){
        fixed4 c = lerp(tex2D (_MainTex, IN.uv_MainTex), tex2D(_MainTexFinal , IN.uv_MainTex), _InterpolationValue);
       o.Albedo = c.rgb;
}
        ENDCG
}

C# script used to change value

var rends = GetComponentsInChildren<MeshRenderer>();
mBlock = new MaterialPropertyBlock();

var ratio = 0.6f;

for (int i = 0; i < rends.Length; i++){
                rends[i].GetPropertyBlock(mBlock);
                mBlock.SetFloat("_InterpolationValue", ratio);
                rends[i].SetPropertyBlock(mBlock);
}

_InterpolationValue works from shader inspector or if I use global Material.SetFloat(“_InterpolationValue”, ratio), but not from MaterialPropertyBlock.

I’ve read posts from @bgolus that MaterialPropertyBlock can change only inside CGPROGRAM which seems to be the case. So I’m kinda stuck here. Anyone?

Are you ever setting the material property blocks on these renderers from another script? If you are, are you using the per-material index version of SetPropertyBlock()? i.e.: If you’re ever doing renderer.SetPropertyBlock(matBlock, index), then renderer.SetPropertyBlock(matBlock) on those same renderers will cease to do anything since the per-index property block takes precedence. You’d need to update the per material index material property block to make it work.

Otherwise, I’m not seeing any obvious error.

2 Likes

I’m sitting with my jaw dropped. I can’t belive how precise you are. Indeed, I’ve been using multi submeshes and then decided to go single mesh and left setter with index 0.

Sincerely want to thank you for contribution to gamedev community, and for saving me hours of research. I’m very impressed.

2 Likes