Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to change RenderingMode and Set Alpha using Material Property Blocks

Discussion in 'Scripting' started by ConorArup, Aug 30, 2018.

  1. ConorArup

    ConorArup

    Joined:
    Feb 15, 2018
    Posts:
    17
    I'm using the standard shader but want to change the RenderingMode from 'Opaque' to 'Transparent/Fade' and set the Alpha value from 255 to 100 during runtime. Is this possible using MaterialPropertyBlocks?

    I managed to set the RenderingMode during runtime using this:

    Code (CSharp):
    1. // Create MaterialPropertyBlock
    2. MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
    3. gameObjectMeshRenderer.GetPropertyBlock(materialPropertyBlock);
    4.  
    5. // Change rendering type to 'Fade'
    6. materialPropertyBlock.SetFloat("_Mode", 2);
    7.  
    8. // Set colour alpha
    9. Color32 materialColour = belowSeaLevelModels[i].material.color;
    10. materialColour.a = 100;
    11. materialPropertyBlock.SetColor("_Color", materialColour);
    12.  
    13. // Set altered materialPropertyBlock values
    14. gameObjectMeshRenderer.SetPropertyBlock(materialPropertyBlock);
    But, while it changed the enum state on the material in the inspector, nothing changed on the actual game object. Changing the colour works, but again the alpha doesn't show because the rendering mode isn't being changed during runtime.

    Been looking for a couple hours now and can't seem to find a definitive simple solution to something that should be easy (at least in my eyes).
     
    Last edited: Aug 30, 2018
  2. ConorArup

    ConorArup

    Joined:
    Feb 15, 2018
    Posts:
    17
    Apparently, to set via code I'll need to set all of these properties:
    Code (CSharp):
    1.  Material m = new Material(Shader.Find("Standard"));
    2. m.SetFloat("_Mode", 2);
    3. m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    4. m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    5. m.SetInt("_ZWrite", 0);
    6. m.DisableKeyword("_ALPHATEST_ON");
    7. m.EnableKeyword("_ALPHABLEND_ON");
    8. m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    9. m.renderQueue = 3000;
    I'm not sure, however, how to do this through the MaterialPropertyBlock...
     
  3. MikeWise

    MikeWise

    Joined:
    Dec 27, 2017
    Posts:
    14
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    This is because changing some of these options requires using another shader variant. Material.EnableKeyword does this for you on a material basis, and is not available on a MaterialPropertBlock, because again, requires another shader.
    In the Editor the material inspector does .EnableKeyword behind the scenes.
    I would just swap between two materials with the correct shader setup and then use propertyblocks to change the other settings.