Search Unity

MaterialPropertyBlock not setting lerp interpolation value inside shader

Discussion in 'Shaders' started by crudeMe, Sep 20, 2021.

  1. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    92
    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
    Code (CSharp):
    1. Properties {
    2.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    3.         _MainTexFinal ("Albedo Final(RGB)", 2D) = "white" {}
    4.         _InterpolationValue("Interpolation value", Range(0,1)) = 0.0
    5.     }
    6. SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         CGPROGRAM
    9.         sampler2D _MainTex;
    10.         sampler2D _MainTexMaxVelo;
    11.         half _InterpolationValue;
    12.  
    13. void surf(){
    14.         fixed4 c = lerp(tex2D (_MainTex, IN.uv_MainTex), tex2D(_MainTexFinal , IN.uv_MainTex), _InterpolationValue);
    15.        o.Albedo = c.rgb;
    16. }
    17.         ENDCG
    18. }
    19.  
    C# script used to change value
    Code (CSharp):
    1.  
    2. var rends = GetComponentsInChildren<MeshRenderer>();
    3. mBlock = new MaterialPropertyBlock();
    4.  
    5. var ratio = 0.6f;
    6.  
    7. for (int i = 0; i < rends.Length; i++){
    8.                 rends[i].GetPropertyBlock(mBlock);
    9.                 mBlock.SetFloat("_InterpolationValue", ratio);
    10.                 rends[i].SetPropertyBlock(mBlock);
    11. }
    _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?
     
    Last edited: Sep 20, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    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.
     
    esgnn likes this.
  3. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    92
    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.
     
    esgnn likes this.