Access Rendering mode var on Standard Shader via scripting

Hey Guys,

So basically I have been searching (without no luck) how to access the rendering mode in the standard shader; this is purely to be able to classify the type of standard shader is being used.

I know how to read all the properties, TexEnv Color, Range, and even if a texture is a flat texture type (TexDim2D) but I haven’t been able to work out by myself what’s the property that defines the Rendering Mode in the standard shader :s

I’m attaching an image to show exactly what I’m looking for.

Thanks a lot :slight_smile:

1895126--122043--Screen Shot 2014-12-22 at 09.42.48.png

1 Like

Guy I even have the properties for the standard shader and as I said there is no ref to a rendering mode (I would guess the rendering mode is an enum)… so … any idea? :slight_smile:

Shader "Standard"
{
    Properties
    {
        [LM_Albedo] [LM_Transparency] _Color("Color", Color) = (1,1,1,1)  
        [LM_MasterTilingOffset] [LM_Albedo] _MainTex("Albedo", 2D) = "white" {}
      
        [LM_TransparencyCutOff] _AlphaTestRef("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

        [LM_Glossiness] _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.0
        [LM_Metallic] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
        [LM_Metallic] [LM_Glossiness] _MetallicGlossMap("Metallic", 2D) = "white" {}

        _BumpScale("Scale", Float) = 1.0
        [LM_NormalMap] _BumpMap("Normal Map", 2D) = "bump" {}

        _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
        _ParallaxMap ("Height Map", 2D) = "black" {}

        _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
        _OcclusionMap("Occlusion", 2D) = "white" {}

        [LM_Emission] _EmissionColor("Color", Color) = (0,0,0)
        [LM_Emission] _EmissionMap("Emission", 2D) = "white" {}
      
        _DetailMask("Detail Mask", 2D) = "white" {}

        _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
        _DetailNormalMapScale("Scale", Float) = 1.0
        _DetailNormalMap("Normal Map", 2D) = "bump" {}

        [KeywordEnum(UV1, UV2)] _UVSec ("UV Set for secondary textures", Float) = 0

        // UI-only data
        [KeywordEnum(None, Realtime, Baked)]  _Lightmapping ("GI", Int) = 1
        [HideInInspector] _EmissionScaleUI("Scale", Float) = 1.0
        [HideInInspector] _EmissionColorUI("Color", Color) = (0,0,0)
        [HideInInspector] _EmissionColorWithMapUI("Color", Color) = (1,1,1)

        // Blending state
        [HideInInspector] _Mode ("__mode", Float) = 0.0
        [HideInInspector] _SrcBlend ("__src", Float) = 1.0
        [HideInInspector] _DstBlend ("__dst", Float) = 0.0
        [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    }

Near the bottom of the Standard shader there is a reference to an editor script for the UI. That’s what controls the “Rendering Mode”.

ah I see it!, thanks a lot! :). I guess is this one (at the very end of the standard shader tab).

    FallBack "VertexLit"
    CustomEditor "StandardShaderGUI"
}

Now unfortunately I haven’t been able to see any API call to that script :/, any idea on how to call it to ask?, is this internal?

I mean, I know its included in the standard shaders (the StandardShaderGUI.cs) but my question is, how do I access the blendmode for each shader ? :S

 public enum BlendMode
    {
        Opaque,
        Cutout,
        Transparent
    }

thanks a lot guys!

Guys, found the solution! <3 thanks!.

The Rendering mode in the standard shader that refers to either Opaque, Cutout or transparent is mapped via the StandardShaderGUI to a float property called _Mode

so its a matter of getting the float like this:

Material mat;//this is the material we want to know its rendering mode
Debug.Log(mat.GetFloat("_Mode"));
//and depending on the number it outputs (0, 1, 2) it maps to Opaque, Cutout and transparent

Again :slight_smile: thanks!

9 Likes

Thanks for this! I’m trying to change a material from opaque to transparent at run-time. By using something like:

Material mat = GetComponentInChildren<SkinnedMeshRenderer>().materials[0];
        mat.SetFloat("_Mode", 2.0f);

it changes the rendering mode in the inspector, but in-game, it’s still opaque! If I manually change it in the inspector instead of in the code, it works as expected. Does anyone know if you have to refresh the shader or something along those lines? Thanks.

2 Likes

ah, something like that similarly happened to me when I wanted to change some values in materials.

Try out using sharedMaterials instead of materials :)!, and if that doesnt work for you try out loading the Material from the project view (if possible)

Hi Guys,

I am trying to set the render mode at runtime as like Discord, using sharedMaterials didn’t help. Has anyone found a work around? Thanks so much! I can actually see the values in the inspector change, but the material won’t show the change!

Hey @HarpSeal1 did you tried loading the material from the project view? (and then accessing the sharedMaterial), that should solve your issue me thinks…

if it doesnt let me know I try some things here and check what can I come up with :wink:

Hi Jmunozar, Thanks for the reply. I’ve tried using sharedMaterials and assigning material from project view. It seems the alpha value in albedo color isn’t updating in the viewport, but the Inspector value is correct. Once i move the alpha value in the inspector even 1 it will update. Thanks for you help!

Ah! I found the problem. Use:

Material.SetColor("_Color", yourColor);

I was trying to use:

Material.color = yourColor;

Strange, maybe i should file a bug?

AHH its a bug! I already reported it in the last Unity5 Beta!, and they said they will fix it in the new beta; if you click on the material in the project view it should be fixed :).

STILL… do you have a small video you can share so I can see if its the same bug? :stuck_out_tongue:

1 Like

Yeah it must be a bug, it works fine on some runs, then breaks on others until I edit it in the Inspector. Of course, here is a video. I hope there is a new beta soon :confused:

1911886–123319–ScreenCapture_1-7-2015 1.32.53 PM.zip (1.01 MB)

Hey @HarpSeal1 indeed it looks like a bug, it should get the alpha from the very beginning, report it as a bug with repro steps :slight_smile:

I got the same problem.
I hope Unity can fix this bug early.

This seems to me to still be broken in 5.0.0f1. I’m attempting to put a material with the standard shader into “Fade” mode from script and this is what I’m doing so far:

    Material m;
    // [...]
    m.SetFloat("_Mode", 2);
    m.EnableKeyword("_ALPHABLEND_ON");
    m.renderQueue = 3000;
    m.SetColor("_Color", new Color(1, 1, 1, 0.5f));

Should that work? As others in this thread mentioned, if I inspect the material it seems to be in the right mode and with the correct alpha set on the albedo, but it doesn’t actually fade in the game view unless I tweak the the alpha in the inspector.

Has anyone else figured out a way of doing this reliably?

2 Likes

Hey @JJC1138

wow, it seems it hasnt been fixed then, even on RC1 :/…

I think the best way is to still send a bug report with a small video showing step by step on how to repro this annoying bug :/.

Unfortunately I havent found a way to overcome this annoying thing :confused: even updating the project view doesnt help :confused:

1 Like

@jmunozar Ah thanks for the reply! I will file a bug report.

no problem :), hopefully it gets fixed by RC2! :confused:

Eek, I did a bit more digging and it turns out this isn’t actually a Unity bug at all. I just wasn’t setting all the necessary parameters on the shader. If you download the Built-in Shaders from the beta page you’ll find there’s a script Editor/StandardShaderGUI.cs, and that has a method SetupMaterialWithBlendMode(Material, BlendMode) that shows how to set it up properly. To switch to fade mode you do:

                material.SetFloat("_Mode", 2);
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
27 Likes