- Home /
transparent mode does not work at alpha=255
Hi, i'm trying to implement a fade for my surfaces, when alpha=255, i want the object to be fully opaque, when the alpha<255, i want the object to fade. however, when i use transparent or fade mode, the alpha=255 case looks weird (when using Transparent/Diffuse shader)
see below: LEFT: transparent or fade mode, alpha=255, shader = Transparent/Diffuse RIGHT: any mode, alpha=255, shader = VertexLit bottom: transparent or fade mode, alpha=50, shader=Transparent/Diffuse
i would like to be able to fade the object, while also maintaining full opacity at alpha=255, which i have been unable to do so far.
here is my code:
public void SetTransparent(string objectString, byte alpha)
{
GameObject pial = GameObject.Find(objectString);
MeshRenderer[] rendererArray = pial.GetComponentsInChildren<MeshRenderer>();
MeshRenderer renderer = rendererArray[0];
Material material = renderer.material;
//material.SetFloat("_Mode", 4f);
material.SetOverrideTag("RenderType", "Fade");
Shader shade = null;
shade = Shader.Find("Transparent/Diffuse");
material.shader = shade;
Color32 col = renderer.material.GetColor("_Color");
col.a = alpha;
col.r = 195;
col.g = 127;
col.b = 80;
renderer.material.SetColor("_Color", col);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
}
Answer by Bunny83 · Aug 28, 2018 at 06:07 PM
Transparency is a huge problem when you deal with just a single object. For transparency to work correctly things need to be drawn back to front. Unity does sort transparent objects based on their location (pivot) back to front. However the individual polygons within a single mesh are never sorted and usually rendered in the order they appear in the triangles array. Any transparent shader usually does not write to the depth buffer but it does perform a zTest (since opaque shaders are rendered first front to back to avoid overdraw).
If an object has a quite low alpha value the slight error due to wrong drawing order is almost neglectable. However the more opaque it gets the more visible the difference will be. At full opacity you can get very strange results as triangles which might be further away can overdraw those which are closer. Depending on the requirements you could use a stipple shader which basically renders the whole thing opaque with proper depth writing but simply omits more and more fragments (pixels) the more transparent the object should be.
So actually using alpha blending doesn't work if the object has self occluding triangles. You would need to sort your triangles based on the distance from the camera. If your camera view or the object rotation can change this would have to be redone each time you do so. This may be impractical for large meshes.. Though it could possibly be sped up by using some space partitioning.
So i don't think anybody has an easy solution for this problem. This issue is as old as computer graphics and still does not have an easy solution. More information can be found here.
If you just want to have the object being transparent without seeing other triangles from the same object you can use a two pass approach where you first just render the object into the depth buffer without writing color information and the second pass does actually render the transparent object. That way only the visible surface projection is visible but no triangles which are occluded. So only other objects which are behind the object you're rendering can be seen.
Implementing a BSP tree might be the best solution when you do the manual sorting. Though this is all but trivial.
As i said it highly depends on what you want to achieve. What is your actualy source data? Is it pointcloud data or actual $$anonymous$$RI slices? There are other ways to achieve volumetric rendering which doesn't suffer from the sorting issue as one basically renders the pointcloud directly back to front. An example can be found here
thanks for the response. ideally, i would like to gradually fade from my image in the top right, to the bottom image. so i don't think your visible surface projection only (last paragraph you wrote) will work for me.
i tried to use the stipple shader you linked to, but it doesn't seem to work. changing the transparency of my object does nothing using that shader, and it doesn't even render the colors properly (it renders everything white). in theory, however the stipple would work. i guess i can scrounge around for another stipple shader.
if worse comes to worse, i guess i will just have to switch shaders depending on the alpha value the user sets, for values=255, i will use the non-transparent shader, for values <255, i will use the transparent. it will give strange values around 200-254, but i guess i can live with that.
Your answer
Follow this Question
Related Questions
Z-priming alpha-per-vertex 0 Answers
Transparent terrain shader not working 2 Answers
Remove white edges on texture help 2 Answers
Object not effected by transparent object in front 1 Answer
Problem with transparent shader 0 Answers