- Home /
Standard material shader ignoring SetFloat property "_Mode"?
In my code I create a texture and fill it with colors (some are 100% transparent, others are 100% opaque). I then create a material and put the texture in it. Finally a quad is created and the material is used on its renderer.
Color32[] colors = ... //length: 250000
Texture2D t = new Texture2D(500, 500, TextureFormat.ARGB32, false);
t.SetPixels32(colors);
t.Apply();
Material mat = new Material(Shader.Find("Standard"));
mat.SetFloat("_Mode", 2);
mat.mainTexture = t;
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Quad);
go.transform.localScale = new Vector3(500, 500, 1);
go.GetComponent<MeshRenderer>().material = mat;
The problem is that alpha is ignored, all colors are 100% opaque. If I select the quad while playing I can see the properties of the material in Unity's inspector. Shader is "Standard" and the Rendering Mode is indeed "Fade".
If I manually select "Fade" again from the drop down menu the transparent pixels in the texture immediately become transparent. If I set the inspector to Debug and manually go to Saved Properties > Floats > Element 10 (which is "_Mode") I can see that "Second" is indeed set to "2" (which means "Fade"). If I manually change this number to 1 ("Cutout") nothing happens. If I manually set it back to 2 nothing happens as well, all pixels remain opaque and alpha is still ignored.
If I set it to 3 (for "Transparent") nothing happens. If I then change the inspector back to "Normal" still nothing happens, except that the Material's Rendering Mode shows as "Transparent" in the Inspector. If I then click the drop down list and select the already-selected "Transparent" option again then the alpha values are respected and some colors in the texture becomes non-opaque.
Is this a bug or is something weird required when using SetFloat?
Because SetFloat("_Mode", 2); does not work on the Standard shader. It seems like we can't use the Standard shader as intended from script.
Answer by Hotdug · Jul 30, 2015 at 08:12 PM
I asked on the forum instead and got an answer:
http://forum.unity3d.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/
If anyone sees this I recommend everybody asks on the forum instead of on answers.unity3d.com in the future because this place is a desert compared to over there. In addition you don't need to wait for a moderator to approve your questions (or answers).
The depressing answer is:
Material m = new Material(Shader.Find("Standard"));
m.SetFloat("_Mode", 2);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
That's right, eight lines of code for something that should be one line. Unity should put a .ChangeProperty(string, value) method into the Material class that does this work for you if you hand it the right keyword (shaders would override ChangeProperty methods and listen to their own keywords and execute their own code depending on what is being given). Then just make a note of supported strings in the shader's documentation.
Considering how much confusion I've seen online from _Mode not working it would definitely be something that people would appreciate. Most seem to think, as I did, that it was a bug when chaging _Mode did nothing except updating the drop-down list in the inspector.
Wow, that's absurd! I've seen others say this code was "needed" for creating a material at runtime and adjusting its' settings. I kept thinking to myself: "All I need is to change the rendering mode to cutout -- surely I don't need all this". If i hadn't stumbled upon this I'd probably be spending another thirty $$anonymous$$utes to an hour trying to find a solution. Thanks!