Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Transparent rendering mode not working in WebGL

Discussion in 'Web' started by Larper, Jan 4, 2021.

  1. Larper

    Larper

    Joined:
    Jun 17, 2015
    Posts:
    4
    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 unnecessary 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.

    Code (CSharp):
    1. GameObject CreateHighlight(GameObject gameObject)
    2.         {
    3.             GameObject highlight = Instantiate(gameObject);
    4.             Destroy(highlight.GetComponent<Rigidbody>());
    5.             Destroy(highlight.GetComponent<MeshCollider>());
    6.             Destroy(highlight.GetComponent<StayInside>());
    7.             Destroy(highlight.GetComponent<ObjectControl>());
    8.  
    9.             // Change render mode to Transparent
    10.             Material material = highlight.GetComponent<Renderer>().material;
    11.             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    12.             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    13.             material.SetInt("_ZWrite", 0);
    14.             material.DisableKeyword("_ALPHATEST_ON");
    15.             material.DisableKeyword("_ALPHABLEND_ON");
    16.             material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    17.             material.renderQueue = 3000;
    18.             // Change alpha to 0
    19.             highlight.GetComponent<MeshRenderer>().material.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
    20.  
    21.             // Stop rendering the object because setting alpha to 0 does not make the object fully transparent
    22.             highlight.GetComponent<MeshRenderer>().enabled = false;
    23.  
    24.             return highlight;
    25.         }
    ----------
    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.

     
  2. Larper

    Larper

    Joined:
    Jun 17, 2015
    Posts:
    4
    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.

    Code (CSharp):
    1.        public Material matTransparentMat;
    2.      
    3.         GameObject CreateHighlight(GameObject GO)
    4.         {
    5.         GameObject highlight = Instantiate(GO);
    6.         Destroy(highlight.GetComponent<Rigidbody>());
    7.         Destroy(highlight.GetComponent<MeshCollider>());
    8.         Destroy(highlight.GetComponent<StayInside>());
    9.         Destroy(highlight.GetComponent<ObjectControl>());
    10.      
    11.         Renderer targetRend = highlight.GetComponent<Renderer>();
    12.         Renderer srcRend = GO.GetComponent<Renderer>();
    13.         Material newMat = Instantiate(matTransparentMat);
    14.         newMat.mainTexture = srcRend.material.mainTexture;
    15.         targetRend.material = newMat;
    16.         return highlight;
    17.         }

    5. Done!