Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Larper · Jan 03, 2021 at 12:30 PM · webgltransparencyopacitytransparent shaderopaque

Transparent rendering mode not working in WebGL

I am making a 3D puzzle video game and I wish to show a translucent copy of the puzzle piece on its original position when the puzzle piece is near its original position. In other words, to show the player that they are on the right track.


I have done so and it is working fine, here's a 4 second quick gif for demonstration.


However, it does not work when I deploy the game to WebGL. The copy of the object which should be translucent simply isn't.


Here is the function which I use to create a translucent copy of the object. I found the code in this forum thread.

Basically what I am doing is instantiating a copy, deleting uncecessary components, manually changing the material settings to make the object use the Transparent rendering mode, changing the opacity to 0, and then disabling the renderer because for some reason the object is not fully transparent when opacity is set to 0.

 GameObject CreateHighlight(GameObject gameObject)
     {
         GameObject highlight = Instantiate(gameObject);
         Destroy(highlight.GetComponent<Rigidbody>());
         Destroy(highlight.GetComponent<MeshCollider>());
         Destroy(highlight.GetComponent<StayInside>());
         Destroy(highlight.GetComponent<ObjectControl>());
 
         // Change render mode to Transparent
         Material material = highlight.GetComponent<Renderer>().material;
         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
         material.SetInt("_ZWrite", 0);
         material.DisableKeyword("_ALPHATEST_ON");
         material.DisableKeyword("_ALPHABLEND_ON");
         material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
         material.renderQueue = 3000;
         // Change alpha to 0
         highlight.GetComponent<MeshRenderer>().material.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
 
         // Stop rendering the object because setting alpha to 0 does not make the object fully transparent
         highlight.GetComponent<MeshRenderer>().enabled = false;
 
         return highlight;
     }



I have stumbled upon this Reddit thread and the guy had the same problem as I did. He said he switched the rendering path from Deferred to Forward. I found somewhere one is supposed to change that setting in the MainCamera, so I did. Mine was set on Use Graphics Settings so I explicitly set it to Forward, but nothing changed.

Forward rendering path

unity-cr7qb0ioci.png (48.7 kB)
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
1
Best Answer

Answer by Larper · Jan 09, 2021 at 11:22 AM

OctangularPRISM Redditor saved the day on Reddit.


What he suggested was creating a new material, making it transparent and then in the code instantiating that material and applying it to the highlighted object instead of changing the rendering mode to Transparent as I tried.


Here are the steps in more detail:


  1. I first created public Material matTransparentMat; as a public variable in my ObjectControl script. This means an additional menu to add a Material was created on all the objects that have the ObjectControl script.


  2. Then, in the project window I created a new Material and made it transparent. You just go Right Click -> Create -> Material, and then choose the material and in the inspector press where it says Opaque and choose Transparent in the drop down menu, and then click the white color and change the alpha from 255 to 0.


  3. Then, drag and drop that Material to the Mat Transparent Mat public variable in the editor.


  4. Now, I changed the code which was changing the rendering mode to Transparent, to /u/OctangularPRISM's code which will instantiate a new material and apply it to the copy of the object.

       public Material matTransparentMat;
         
         GameObject CreateHighlight(GameObject GO)
         {
         GameObject highlight = Instantiate(GO);
         Destroy(highlight.GetComponent<Rigidbody>());
         Destroy(highlight.GetComponent<MeshCollider>());
         Destroy(highlight.GetComponent<StayInside>());
         Destroy(highlight.GetComponent<ObjectControl>());
         
         Renderer targetRend = highlight.GetComponent<Renderer>();
         Renderer srcRend = GO.GetComponent<Renderer>();
         Material newMat = Instantiate(matTransparentMat);
         newMat.mainTexture = srcRend.material.mainTexture;
         targetRend.material = newMat;
         return highlight;
         }
    
    

  5. Done!

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

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

116 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

Related Questions

can see inside object that is supposed to be fully opaque 0 Answers

is there a fast alternative to "Queue" = "Transparent" ? 0 Answers

Character with hair (mobile) 0 Answers

Mesh transparency halfway cutoff script 0 Answers

How to make my brown circle transparent so i can see the green ground? 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