Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
2
Question by PlauineB · Mar 04, 2019 at 03:15 PM · lightingshadersmaterialstransparency

Change surface type with LWRP

Hi everyone,

I'm using LWRP and I would like to make some parts of my model become slightly transparent. In the editor, all I have to do is to change Surface Type from opaque to transparent and then the alpha of Albedo to 50 for example.

Now, I want to do the same thing in code. Is there a way to do this ? If yes, which is it ? Please let me know if you need more info!

Thanks a lot for your help :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Red-Works · Apr 08, 2019 at 03:10 AM

This question has a long answer, unfortunately. I can not find a shortcut for this. So I search deep into LWRP scripts and found this solution and write this code. It worked for me for now, I hope it works for you.

 using UnityEngine;
 using System;
 
 public class DisplayController : MonoBehaviour
 {
     public enum SurfaceType
     {
         Opaque,
         Transparent
     }

 public enum BlendMode
 {
     Alpha,
     Premultiply,
     Additive,
     Multiply
 }
 
 public Material wallMaterial;
 

 public void ChangeWallTransparency(bool transparent)
 {
     if (transparent)
     {
         wallMaterial.SetFloat("_Surface", (float)SurfaceType.Transparent);
         wallMaterial.SetFloat("_Blend", (float)BlendMode.Alpha);
     }
     else
     {
         wallMaterial.SetFloat("_Surface", (float)SurfaceType.Opaque);
     }
     SetupMaterialBlendMode(wallMaterial);
 }

 void SetupMaterialBlendMode(Material material)
 {
     if (material == null)
         throw new ArgumentNullException("material");

     bool alphaClip = material.GetFloat("_AlphaClip") == 1;
     if (alphaClip)
         material.EnableKeyword("_ALPHATEST_ON");
     else
         material.DisableKeyword("_ALPHATEST_ON");

     SurfaceType surfaceType = (SurfaceType)material.GetFloat("_Surface");
     if (surfaceType == 0)
     {
         material.SetOverrideTag("RenderType", "");
         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
         material.SetInt("_ZWrite", 1);
         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
         material.renderQueue = -1;
         material.SetShaderPassEnabled("ShadowCaster", true);
     }
     else
     {
         BlendMode blendMode = (BlendMode)material.GetFloat("_Blend");
         switch (blendMode)
         {
             case BlendMode.Alpha:
                 material.SetOverrideTag("RenderType", "Transparent");
                 material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                 material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                 material.SetInt("_ZWrite", 0);
                 material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                 material.SetShaderPassEnabled("ShadowCaster", false);
                 break;
             case BlendMode.Premultiply:
                 material.SetOverrideTag("RenderType", "Transparent");
                 material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                 material.SetInt("_ZWrite", 0);
                 material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                 material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                 material.SetShaderPassEnabled("ShadowCaster", false);
                 break;
             case BlendMode.Additive:
                 material.SetOverrideTag("RenderType", "Transparent");
                 material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
                 material.SetInt("_ZWrite", 0);
                 material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                 material.SetShaderPassEnabled("ShadowCaster", false);
                 break;
             case BlendMode.Multiply:
                 material.SetOverrideTag("RenderType", "Transparent");
                 material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.DstColor);
                 material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                 material.SetInt("_ZWrite", 0);
                 material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                 material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
                 material.SetShaderPassEnabled("ShadowCaster", false);
                 break;
         }
     }
 }
 }



Comment
Add comment · Show 3 · 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 PlauineB · Apr 02, 2019 at 11:39 AM 0
Share

Hi @polyworks,

Thanks a lot for your answer ! I don't have time to try it right now but I will get back to you when I test it.

Again thank you for taking the time

avatar image nareshbishtasus · Jun 23, 2019 at 08:09 PM 0
Share

Good found it usefull.. Thanks Great answer

avatar image SteffenItterheim · Jan 01 at 02:17 PM 1
Share

Allow me to say it with a famous quote: "Noooooooooo ..." -Darth Vader

Look for the answer by Undume further down. His or hers is the right way to do it in just 3 lines of code!

Though the above will probably work, it's just way too much code and only going to spell trouble if the Material/Shader doesn't have the properties that the code requires.

avatar image
3

Answer by Undume · Feb 26, 2021 at 08:42 PM

You can't change in runtime the surface type to transparent in urp/lwrp, when you do it from the inspector during play mode, the editor is cheating so you can play with it and after do it properly.

The right thing to do is have a material with the transparency already set up. Then you copy the properties from the opaque to the transparent with Material.CopyPropertiesFromMaterial and pass the transparent reference to the render.

 public class Item : Monobehaviour
 {
        public Material transparentMaterial;
        
        private void SetModeTransparent()
        {
               MeshRenderer renderer = GetComponent<MeshRenderer>();
               Material originalMat = renderer.sharedMaterial;
               Material materialTrans = new Material(transparentMaterial);
               materialTrans.CopyPropertiesFromMaterial(originalMat);
               renderer.sharedMaterial = materialTrans;
        }
 }
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 SteffenItterheim · Jan 01 at 02:17 PM 0
Share

THIS should be the accepted answer!

avatar image
0

Answer by PlauineB · Mar 06, 2019 at 08:51 AM

Anyone ? :)

Comment
Add comment · 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
0

Answer by venomtail · Apr 02, 2019 at 12:01 AM

How did you get your question to be approved? I've been waiting for ages but nothing. How long did it take?

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 PlauineB · Apr 02, 2019 at 11:37 AM 0
Share

No idea sorry...

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

216 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does this button in the material preview window do? (lighting and shader troubles) 0 Answers

Same material looks different in different projects 2 Answers

Receive Shadow Mesh Renderer - Weird Effect 0 Answers

Problem with HDR (I think?) 0 Answers

disabling terrain diffuse lighting in surface shader 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