Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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
9
Question by markblank05 · Jul 12, 2015 at 11:58 AM · shader

Change material rendering mode in runtime

Hi, I'm trying to change the rendering mode of material to transparent when my player is near the object. Here is what I got so far.

 using UnityEngine;
 using System.Collections;
 
 public class ChangeAlpha : MonoBehaviour
 {
     public Renderer targetObject;
     public float alpha = 0.25f;
 
     Color newColor;
 
     void Awake ()
     {
         targetObject = targetObject.GetComponent<Renderer> ();
     }
 
     void OnTriggerStay (Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             newColor = targetObject.material.color;
             targetObject.material.SetFloat("_Mode", 3f);
             newColor.a = alpha;
             targetObject.material.color = newColor;
         }
     }
 
     void OnTriggerExit (Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             newColor.a = 1f;
             targetObject.material.SetFloat("_Mode", 0f);
             targetObject.material.color = newColor;
         }
     }
 }
 

This code is not working, but when I change the material rendering mode to transparent before run time and remove the material.setfloat, it was working. but I want the material to be opaque when not trigger. So how can I do this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
50

Answer by bellicapax · Nov 02, 2016 at 05:44 PM

I'm sorry to necro this answer, but I just wanted to clarify in case anyone else came across this question.

  1. This answer assumes that you are using the Unity Standard shader.

  2. If you compile and view the Standard shader code, you can see at the very end that it uses a CustomEditor of type "StandardShaderGUI"

  3. If you peek at that code (which others have done), you'll see that it does more than just change the "_Mode" of the shader.

Here is a helper class that can perform the actions of the StandardShaderGUI at runtime assuming the Material you pass in is using the Unity Standard shader:

 using UnityEngine;
 
 public static class StandardShaderUtils
 {
     public enum BlendMode
     {
         Opaque,
         Cutout,
         Fade,
         Transparent
     }
 
     public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
     {
         switch (blendMode)
         {
             case BlendMode.Opaque:
                 standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                 standardShaderMaterial.SetInt("_ZWrite", 1);
                 standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 standardShaderMaterial.renderQueue = -1;
                 break;
             case BlendMode.Cutout:
                 standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                 standardShaderMaterial.SetInt("_ZWrite", 1);
                 standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 standardShaderMaterial.renderQueue = 2450;
                 break;
             case BlendMode.Fade:
                 standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                 standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                 standardShaderMaterial.SetInt("_ZWrite", 0);
                 standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                 standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 standardShaderMaterial.renderQueue = 3000;
                 break;
             case BlendMode.Transparent:
                 standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                 standardShaderMaterial.SetInt("_ZWrite", 0);
                 standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                 standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                 standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                 standardShaderMaterial.renderQueue = 3000;
                 break;
         }
 
     }
 }
 

The other important thing to note is that while this will always work in the Editor, you will want to make sure that you have a shader variant that uses the blendmode you want referenced somewhere so that when you build, it will work. You can see if Unity has automatically grabbed the shader variant you need by going to Project Settings->Graphics and at the bottom under Shader Preloading click "Save to asset". You need a variant under Standard that has _ALPHAPREMULTIPLY_ON defined for the "Transparent" mode to work (which you can see from the above script) as well as all the other defines that the shader is using.

If you don't see a variant you need listed, you can run your program, make all the changes to the shader, and click this button to save a new ShaderVariantCollection as it tracks variants at runtime. You can then add this collection the the Preloaded Shaders array and it should be added to the build as far as I can tell (haven't tested currently).

Comment
Add comment · Show 8 · 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 Smireles · Nov 06, 2016 at 01:18 AM 1
Share

I'd give you more upvotes, but alas I can only do it once. /bow thanks a lot for this.

avatar image silentslack · Sep 19, 2017 at 01:22 PM 0
Share

I had issues with this until I added standardShader$$anonymous$$aterial.SetFloat("_$$anonymous$$ode", 2);

avatar image JPhilipp · Apr 13, 2018 at 04:27 PM 0
Share

Great answer! To complete this, you would also want to set e.g. standardShader$$anonymous$$aterial.SetOverrideTag("RenderType", "Fade"), and possibly the _$$anonymous$$ode too, as some other modules -- including Unity's new native PostProcessing2 -- sometimes test against these, leading to unwanted results if not also changed.

avatar image Glurth · Apr 26, 2019 at 04:28 PM 0
Share

I use standardShader$$anonymous$$aterial.SetInt("_SrcBlend", int)UnityEngine.Rendering.Blend$$anonymous$$ode.SrcAlpha)(as opposed to Blend$$anonymous$$ode.One) for the Transparent blend-mode.

avatar image 8bitgoose · Mar 01 at 05:04 PM 2
Share

Like the other comments said, you need to add the following

material.SetFloat("_Mode", X); where X -> 0 opaque, 1 Cutout, 2 Fade, 3 Transparent

If you don't set this, then the unity editor will reset it back to opaque when you look at it in the inspector

avatar image LegendaryRylex 8bitgoose · 8 hours ago 0
Share

What an absolute LEGEND! Thank you so much.

Show more comments
avatar image
1

Answer by chris3331 · Dec 21, 2015 at 04:38 PM

I guess you are missing the keywords to activate the rendering mode! Have a look here:

http://answers.unity3d.com/questions/1016155/standard-material-shader-ignoring-setfloat-propert.html

and here...

http://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html

Hope that helps!!

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 BlackOpsBen · Jun 03, 2020 at 03:09 AM 0
Share

Where do you put/access/use the keywords? I've been desperately searching for days with no luck, and every answer just points to that documentation which I can't seem to get to work.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Shader error: Material doesn't have stencil properties ?? 3 Answers

Billboard trees glow white in dark scene. 2 Answers

Can someone help me to upgrade a shader to URP? 0 Answers

Without normal map material 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