Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
3 captures
11 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hotdug · Jul 27, 2015 at 09:20 PM · shadertexturematerialbuggraphics

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bentheburrito · Jun 04, 2017 at 01:30 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accidentally Clicked Refresh Assets and This Happened. How Do I Fix It? 0 Answers

How can I prevent lights overlapping in intensity? 1 Answer

Tracing in Unity3D 2 Answers

use RenderTexture on a sphere correctly? 1 Answer

Problem using multiple materials 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges