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.