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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Standard Material from code - transparency

Discussion in 'Scripting' started by Powdered_sugar, Apr 10, 2015.

  1. Powdered_sugar

    Powdered_sugar

    Joined:
    Aug 23, 2012
    Posts:
    64
    Good afternoon all,

    I'm trying to use code to generate a material at runtime using the new "Standard" shader. The material is created, and the color I want to give it is applied, and it's Rendering Mode is set to transparent:

    Code (CSharp):
    1. Material mat = new Material(Shader.Find("Standard"));
    2. mat.SetInt("_Mode", 3);
    3. mat.SetColor("_Color", new Color(0.0f, 1.0f, 1.0f, 0.5f));
    4. obj.GetComponent<Renderer>().material = mat;
    The problem I'm having is that when I actually start up the game the material has no transparency until I manually change one of its parameters. Is there some property I'm unaware of that needs to be set to give it transparency from the onset (beyond the rendering mode)?

    Also, does anyone know of a place where all of the new shader's properties are outlined or source code is available? I found _Mode and _Color through a couple other forum threads, but would love to see it all outlined.

    Thanks!
     
  2. rekabmot

    rekabmot

    Joined:
    Jul 28, 2014
    Posts:
    1
    I'm having the exact same issue - did you find an answer?
     
  3. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    I had similar issues before with custom Inspector scripts and the Standard shader is using one. The inspector loads data of the material or object and settings change accordingly on input in the Inspector. Try to set the color and transparency properties in Update() to see if that helps.
    Maybe there is something to call to force to update but I have no idea.

    Maybe that helps.
    Code (csharp):
    1. Properties
    2.    {
    3.      _Color("Color", Color) = (1,1,1,1)
    4.      _MainTex("Albedo", 2D) = "white" {}
    5.      
    6.      _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    7.  
    8.      _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    9. //MetallicWorkflow
    10.           [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    11.           _MetallicGlossMap("Metallic", 2D) = "white" {}
    12. //Specular Workflow
    13.           _SpecColor("Specular", Color) = (0.2,0.2,0.2)
    14.           _SpecGlossMap("Specular", 2D) = "white" {}
    15.  
    16.      _BumpScale("Scale", Float) = 1.0
    17.      _BumpMap("Normal Map", 2D) = "bump" {}
    18.  
    19.      _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    20.      _ParallaxMap ("Height Map", 2D) = "black" {}
    21.  
    22.      _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    23.      _OcclusionMap("Occlusion", 2D) = "white" {}
    24.  
    25.      _EmissionColor("Color", Color) = (0,0,0)
    26.      _EmissionMap("Emission", 2D) = "white" {}
    27.      
    28.      _DetailMask("Detail Mask", 2D) = "white" {}
    29.  
    30.      _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    31.      _DetailNormalMapScale("Scale", Float) = 1.0
    32.      _DetailNormalMap("Normal Map", 2D) = "bump" {}
    33.  
    34.      [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    35.  
    36.      // UI-only data
    37.      [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
    38.      [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)
    39.  
    40.      // Blending state
    41.      [HideInInspector] _Mode ("__mode", Float) = 0.0
    42.      [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    43.      [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    44.      [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    45.    }
     
  4. Powdered_sugar

    Powdered_sugar

    Joined:
    Aug 23, 2012
    Posts:
    64
    Thanks for the reply. I've actually decided that a solid color looks better, so I'm not going to be dealing with transparency this time. I'll have another set of objects later that will though, so I'll try out your suggestion.

    Also, thank you for that list of properties!
     
    Last edited: Apr 13, 2015
  5. CyberFox01

    CyberFox01

    Joined:
    Oct 5, 2012
    Posts:
    23
    I had the same issue. And I've had a lot of these issues and they're all related to EnableKeywords. But all the answers can be found in the StandardShaderGUI.cs

    Seems Transparency was a tad more complex but shamelessly copying the code i got it working

    StandardShaderGUI.cs:320
    Code (CSharp):
    1. static void SetupMaterialWithBlendMode(Material material , BlendMode blendMode)
    2. {
    3.    switch (blendMode)
    4.    {
    5.        case BlendMode.Opaque:
    6.            material.SetOverrideTag("RenderType", ""); //
    7.            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    8.            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    9.            material.SetInt("_ZWrite", 1);
    10.            material.DisableKeyword("_ALPHATEST_ON");
    11.            material.DisableKeyword("_ALPHABLEND_ON");
    12.            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    13.            material.renderQueue = -1;
    14.            break;
    15.        case BlendMode.Cutout:
    16.            material.SetOverrideTag("RenderType", "TransparentCutout");
    17.            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    18.            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    19.            material.SetInt("_ZWrite", 1);
    20.            material.EnableKeyword("_ALPHATEST_ON");
    21.            material.DisableKeyword("_ALPHABLEND_ON");
    22.            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    23.            material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
    24.            break;
    25.        case BlendMode.Fade:
    26.            material.SetOverrideTag("RenderType", "Transparent");
    27.            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    28.            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    29.            material.SetInt("_ZWrite", 0);
    30.            material.DisableKeyword("_ALPHATEST_ON");
    31.            material.EnableKeyword("_ALPHABLEND_ON");
    32.            material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    33.            material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
    34.            break;
    35.        case BlendMode.Transparent:
    36.            material.SetOverrideTag("RenderType", "Transparent");
    37.            material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    38.            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    39.            material.SetInt("_ZWrite", 0);
    40.            material.DisableKeyword("_ALPHATEST_ON");
    41.            material.DisableKeyword("_ALPHABLEND_ON");
    42.            material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    43.            material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
    44.            break;
    45.    }
    46. }
     

    Attached Files:

    Last edited: Mar 2, 2018
  6. CyberFox01

    CyberFox01

    Joined:
    Oct 5, 2012
    Posts:
    23
    Accidentally included the StandardShaderGUIcs twice but i can't edit it out of my post since the system keeps claiming my post is spam.