Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SebastianToro · Oct 16, 2013 at 07:06 PM · shaderruntimetransparency

INDECIPHERABLE rendering order problem when modifying material property on Runtime

I want to change the material of an object at runtime. This material will use the shader “Transparent/VertexLit”. There are two different scenarios where this happens, on the first one the material will be assign without modifying any of its properties. On the second one, only of its properties is modified (“_Color”).

Next you’ll find the first case scenario. The new material is assigned through the editor to the variable transparentMaterial and then assigned to the renderer.material:

 newMat = new Material(transparentMaterial);
 newMat.CopyPropertiesFromMaterial(transparentMaterial);
 renderer.material = newMat;

The previous code, renders the following results:

alt text

As you may see on each image the result is the same, meaning, full transparency on each object.

For the second case, the code is the following:

 newMat = new Material(transparentMaterial);
 newMat.CopyPropertiesFromMaterial(transparentMaterial);
 newMat.SetColor("_Color", Color.red);
 renderer.material = newMat;

As you may see, there’s only one line that changes but the result is dramatically different from the previous one. Next are the results:

alt text

On the previous images, you can appreciate a only partial transparency. Let’s recall that the material is exactly the same on each case, but when you tried to modify a property of the material (in this case “_Color”) the results are awful. As you may see, on the left image, the middle stick is not transparent, even though it’s supposed to be. On the middle image you can appreciate full transparency only on the one of the vertical sticks, but the middle one still doesn’t show any transparency. On the image of the right, on the “3” side, you can’t see the object that is behind that part of the stick, but on the “3’ ” side, you are able to see the object behind.

When I was debugging this scenario, I realized that the order of the objects in which I changed the material would affect the transparencies. For example, if I only change the material of one stick (object), then I won’t be able to see anything through it. If after doing that I change the material of a second stick, I will be able to see through I will only see the first object which its material was changed. Through the second objects transparency I won’t be able to see the third stick. Add to that, when looking from the first one you still won’t see through it, even the one which its material just changed. When you change the material of the third object, the exact same thing happens. When you look through it, you’ll be able to see the first and second objects. When you look though the second one, you’ll only be able to see the first one (even though not the third one is now supposed to be transparent it does not appears behind the second one). When looking through the first one, you won’t be able to see anything behind it.

The previous description is the explanation of why only some objects look transparent on the second group of images. I’ve exhausted every resource I had to solve this, so now I’m hoping to solve this with your help. Thanks in advance.

Best regards,

Sebastián

pass.png (5.0 kB)
fail.png (6.7 kB)
Comment
Add comment · Show 1
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 SebastianToro · Oct 18, 2013 at 03:16 PM 0
Share

Come on guys! This must be something really easy for all the pros!

1 Reply

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

Answer by Loius · Oct 18, 2013 at 04:32 PM

You're setting the color to .Red, which is (1,0,0,1). The fourth argument is alpha (transparency); you want that at less-than-one.

You could do "newMat.SetColor("_Color", new Color(1f,0f,0f,0.5f));", or you could be amazing with extension methods which has saved me so much time:

 // goes in any subdirectory of the Plugins folder
 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 using UnityEditorInternal;
 #endif
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public static class UtilityExtensions {
     #region Color
     public static Color WithR(this Color c, float r) {return new Color(r,c.g,c.b,c.a);}
     public static Color WithG(this Color c, float g) {return new Color(c.r,g,c.b,c.a);}
     public static Color WithB(this Color c, float b) {return new Color(c.r,c.g,b,c.a);}
     public static Color WithA(this Color c, float a) {return new Color(c.r,c.g,c.b,a);}
     #endregion
 }

With that in place you can "newMat.SetColor("_Color", Color.red.WithA(0.5f));"

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 SebastianToro · Oct 18, 2013 at 06:57 PM 0
Share

You were right. That was my problem. The only line I had to change was on the inicialization. And that was all. Thanks a lot. private void Start() {

     Color transpColorAlpa = transparent$$anonymous$$aterial.GetColor("_Color");
     selectedColor = new Color(1,0,0,transpColorAlpa.a);        
 }

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

15 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

Related Questions

GameObject not receiving shadows 0 Answers

How to make trees in a terrain transparent in runtime? 0 Answers

Set shader's AlphaCutoff programmatically in HDRP 1 Answer

shader Transparency 1 Answer

Transparent/Specular Shader - I want alpha to control spec, not transparency 1 Answer


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