Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Canazza · Aug 19, 2013 at 02:45 PM · shadertransparent

Transparent Objects within Transparent Objects

I'll admit, I'm a complete novice when it comes to shaders and I may be in way over my head with this one.

I'm currently trying to write a shader that allows transparent objects to be placed within or intersecting other transparent objects and have each object, and its back-faces, visible, and to fade between opaque + coloured and transparent + Greyscale

So far I've created a shader that appears to fit the bill, except that it's not rendering consistently and I'm getting alot of flickering (potentially z-fighting) when rotating around the object.

In my example I have a Cube within a Cube within a Cube, it looks perfectly fine at some angles:

alt text

At other angles (even <1 degree change) it looks like this:

alt text

Here's the shader code incase it helps:

 Shader "GreyscaleTransition" {
     Properties {
     
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _BumpMap ("Bump (RGB)", 2D) = "black" {}
         _Brightness ("Brightness (Desaturated)", Range(-1,1)) = 0 
         _Fade ("Fade ",Range(0,1)) = 0
         _Alpha ("Alpha (Desaturated)", Range(0,1)) = 1
     }
     SubShader {
         Tags { 
             "RenderType"="Transparent"    
             "IgnoreProjector"="False"
             "Queue" = "Transparent" 
             
             }
         LOD 200
         Cull Off
         ZWrite on
         Blend SrcAlpha OneMinusSrcAlpha
         CGPROGRAM
         #pragma surface surf Lambert
 
         sampler2D _MainTex;
         sampler2D _BumpMap;
         fixed _Brightness;
         fixed _Fade;
         fixed _Alpha;
         
         struct Input {
             float2 uv_MainTex;
             float2 uv_BumpMap;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             fixed3 desat = dot(c.rgb, float3(0.3, 0.59, 0.11)) + _Brightness;
             fixed3 colour = c.rgb;
             o.Albedo = lerp(colour,desat,_Fade);
             o.Alpha =  lerp(c.a,_Alpha,_Fade);
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 


   

transparentshader_correct_1.png (47.6 kB)
transparentshader_incorrect_1.png (47.7 kB)
Comment
Add comment · Show 3
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 Benproductions1 · Aug 20, 2013 at 02:07 AM 0
Share

Transparent shaders can't have Z-fighting, because they don't write to the Z-buffer :)

avatar image Canazza · Aug 20, 2013 at 07:59 AM 0
Share

They do if you have ZWrite on

avatar image Benproductions1 · Aug 20, 2013 at 09:00 AM 0
Share

Sorry, I meant they can't have Z-fighting because they aren't ordered by the Z-buffer ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Aug 20, 2013 at 08:09 AM

You need a 2 pass shader to make that work for a single cube, the first pass writes the back faces and the second pass writes the front faces.

The problem otherwise is that Unity does not Z order the triangles being drawn and so back and front faces can overlap and be drawn in the wrong order.

There's another problem too - given the ordering you will always end up with one of the cubes not drawing in front of another one, because the whole object is rendered at once either it will write the furthest back faces and the front faces (stopping the inner cube from working) or the inner cube will overwrite the outer cube. To fix that you need to ensure that all of the cubes back faces are written first (late in the pipeline so that all other geometry is rendered) and then all of the front faces, not just for each object, but indeed for the whole collection.

Comment
Add comment · Show 1 · 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 whydoidoit · Aug 20, 2013 at 08:13 AM 0
Share

You can do that last bit by having the two subshaders use "Queue"="Transparent+1000" and "Queue"="Transparent+1001" for example

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

18 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

Related Questions

Shader for overlaying textures 1 Answer

Transparent & reflective shader 1 Answer

Transparency shader that also writes to Camera Depth Texture 0 Answers

Unity Tree transparency 1 Answer

weird shader problem (correct in editor, weird ingame) 2 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