• 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
1
Question by rustyruss1985 · Aug 28, 2018 at 04:50 PM · shaderalphatransparentopacitytransparent shader

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

alt text

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;
         
         
 
     }
fade-problems.png (206.3 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 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.

Comment
Add comment · Show 3 · 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 Bunny83 · Aug 28, 2018 at 06:12 PM 0
Share

Implementing a BSP tree might be the best solution when you do the manual sorting. Though this is all but trivial.

avatar image Bunny83 · Aug 28, 2018 at 06:22 PM 0
Share

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

avatar image rustyruss1985 · Aug 28, 2018 at 06:41 PM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

142 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 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

Will Semi Transparent mesh with several folds of geometry sort correctly? 1 Answer

Why my 2d character would not be displayed correctly at the very beginning? 0 Answers

A shader with self-illumination and alpha control - doesn't exist? 1 Answer

Opaque to Transparent animation effect on a textured object (Lerpz)? 1 Answer

shader for tree leaves + lightmap 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges