Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by punsho · Nov 27, 2018 at 09:45 PM · shadersmaterialsshader programming

Turning standard materials from opaque to transparent and back in runtime

I have made a system that detects all objects that are in-between the camera and gives them transparent properties when they are not gives back their normal ones, it works like a charm in the editor but when I build the project transparency stops working. Here's the script that turns them transparent:

 foreach (var mat in a.renderer.materials)
 {
         mat.SetFloat("_Mode", 3);
     
         mat.SetColor("_Color", new Color(mat.color.r, mat.color.g, mat.color.b, 0.3f));
         mat.renderQueue = 3000;
         mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
         mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
         mat.SetInt("_ZWrite", 0);
         mat.DisableKeyword("_ALPHATEST_ON");
         mat.EnableKeyword("_ALPHABLEND_ON");
         mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
 }
 

and this one turns them back into opaque:

 foreach (var mat in a.renderer.materials)
 {
         mat.SetFloat("_Mode", 0);
         mat.SetColor("_Color", new Color(mat.color.r, mat.color.g, mat.color.b, 1f));
     
         mat.renderQueue = 2000;
         mat.SetInt("_SrcBlend", 1);
         mat.SetInt("_DstBlend", 0);
         mat.SetInt("_ZWrite", 1);
         mat.EnableKeyword("_ALPHATEST_ON");
         mat.DisableKeyword("_ALPHABLEND_ON");
         mat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
 }

I'm not familiar with materials and shaders so all help will be appreciated

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
0

Answer by Cornelis-de-Jager · Nov 27, 2018 at 09:56 PM

Hi @punsho , to achieve that what you will need to do is have all your materials have the Transparency on the shader. Next step is to access each material like you do. Then its simply changing the alpha value in the colour property

 foreach (var mat in a.renderer.materials)
  {
     mat.color.a = 0.5; // 50 % transparency
     mat.color.a = 1; // 100% transparency
 }

var other : GameObject; other.renderer.material.color.a = 0.5; // 50 % transparent other.renderer.material.color.a = 1.0; // fully opaque

Comment
Add comment · Show 2 · 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 punsho · Nov 27, 2018 at 10:02 PM 0
Share

no that doesn't work properly, I have already tried that and I know that transparent materials with alpha value of 1 render incorrectly

avatar image punsho · Nov 28, 2018 at 08:11 AM 0
Share

this is how it looks when opaque alt text this is how it works when transparent with an alpha value of one 1 alt text

screen-shot-2018-11-28-at-100820.png (194.1 kB)
screen-shot-2018-11-28-at-100922.png (160.1 kB)

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

109 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

Related Questions

Shader for distorting a texture as if it were on a sphere 3 Answers

Adding object to scene causes problem with other objects material/shader 0 Answers

How do I make a water shader that has depth. 1 Answer

See backside of a transparent emissive shader? 0 Answers

Night Vision shader is interacting weird between the level's mesh and the spotlight 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