Search Unity

[Solved] Transparency question

Discussion in 'Shaders' started by JonRurka, May 18, 2019.

  1. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    The two triangles in the image below are part of the same mesh. What I would like, is for the transparency of the triangle in front to "overwrite" the triangle in the back in order to see the sphere behind. Essentially, where the two triangles clip will be completely transparent, but only when the triangle in front is transparent:



    Code (CSharp):
    1. Shader "Custom/TransparencyTest" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.         Tags { "Queue" = "Transparent" "RenderType" = "Transparent"  }
    7.         LOD 200
    8.  
    9.       ZWrite Off
    10.  
    11.         CGPROGRAM
    12.         // Physically based Standard lighting model, and enable shadows on all light types
    13.       #pragma surface surf Standard fullforwardshadows alpha:fade
    14.  
    15.         // Use shader model 3.0 target, to get nicer looking lighting
    16.         #pragma target 3.0
    17.  
    18.         sampler2D _MainTex;
    19.  
    20.         struct Input {
    21.             float2 uv_MainTex;
    22.          float4 color : COLOR;
    23.         };
    24.  
    25.         fixed4 _Color;
    26.  
    27.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    28.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    29.         // #pragma instancing_options assumeuniformscaling
    30.         UNITY_INSTANCING_BUFFER_START(Props)
    31.             // put more per-instance properties here
    32.         UNITY_INSTANCING_BUFFER_END(Props)
    33.  
    34.         void surf (Input IN, inout SurfaceOutputStandard o) {
    35.             // Albedo comes from a texture tinted by color
    36.             fixed4 c = _Color;
    37.             o.Albedo = c.rgb;
    38.          o.Alpha = IN.color.r;
    39.         }
    40.         ENDCG
    41.     }
    42.     FallBack "Diffuse"
    43. }
    44.  
    Code (CSharp):
    1. public class transparencyTest : MonoBehaviour {
    2.  
    3.     public float trans_1 = 0.5f;
    4.     public float trans_2 = 1;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         Vector3[] verts = new Vector3[]
    9.         {
    10.             new Vector3(0,0,0),
    11.             new Vector3(0.5f, 1, 0),
    12.             new Vector3(1, 0, 0),
    13.  
    14.             new Vector3(0,0, 1),
    15.             new Vector3(0.5f, 1, 1),
    16.             new Vector3(1, 0, 1),
    17.         };
    18.  
    19.         int[] tris = new int[] { 0, 1, 2, 3, 4, 5 };
    20.         Color[] data = new Color[]
    21.         {
    22.             new Color(trans_1,0, 0),
    23.             new Color(trans_1,0, 0),
    24.             new Color(trans_1,0, 0),
    25.  
    26.             new Color(trans_2,0, 0),
    27.             new Color(trans_2,0, 0),
    28.             new Color(trans_2,0, 0),
    29.         };
    30.         //uv = new Vector2[0];
    31.  
    32.         MeshFilter filter = GetComponent<MeshFilter>();
    33.  
    34.         Mesh mesh = new Mesh();
    35.  
    36.         mesh.vertices = verts;
    37.         mesh.colors = data;
    38.         mesh.triangles = tris;
    39.  
    40.         filter.sharedMesh = mesh;
    41.  
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update () {
    46.         Color[] data = new Color[]
    47.         {
    48.             new Color(trans_1,0, 0),
    49.             new Color(trans_1,0, 0),
    50.             new Color(trans_1,0, 0),
    51.  
    52.             new Color(trans_2,0, 0),
    53.             new Color(trans_2,0, 0),
    54.             new Color(trans_2,0, 0),
    55.         };
    56.  
    57.         MeshFilter filter = GetComponent<MeshFilter>();
    58.  
    59.         Mesh mesh = filter.sharedMesh;
    60.  
    61.         mesh.colors = data;
    62.  
    63.         filter.sharedMesh = mesh;
    64.     }
    65. }
    66.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If nothing else needs to render “behind” the triangles, you could use a z depth pre pass.

    Add this before the CGPROGRAM line:
    Code (CSharp):
    1. Pass {
    2.     ColorMask 0
    3.     ZWrite On
    4. }
     
  3. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    There would be other objects behind the triangles.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I should have elaborated on that more.

    By "behind" I mean if nothing else that is in the transparent queue and could potentially get rendered after the triangles when using the default back to front transparency sorting, like a particle effect that has a large bounds, needs to render behind the triangle...

    Try it and see if it works well for you.

    The alternative would require you use stencils and two separate meshes, along with forcing the closer triangle to render first via the shader/material queue or the renderer's sorting order. Or if the front shape really is always going to be a triangle you could do in-shader raytracing/raycasting and clip the visible triangle based on a virtual triangle.
     
  5. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    What I'm trying to do is hide cave walls. You see, the first triangle represents the backside of another triangle, flipped the opposite way. The second triangle represents the front side of a cave wall. I want the "backside" triangles to hide any front side triangles behind it by making them transparent.
     
    Last edited: May 21, 2019
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Try this then:
    Code (CSharp):
    1. Pass {
    2.     ColorMask 0
    3.     ZWrite On
    4.     Cull Front
    5. }
     
  7. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    Was just able to try this. doesn't seem to be working:

     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  9. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    Code (CSharp):
    1. Shader "Custom/TransparencyTest" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.     }
    5.     SubShader {
    6.         Tags { "Queue" = "Transparent" "RenderType" = "Transparent"  }
    7.         LOD 200
    8.  
    9.       Pass {
    10.          ColorMask 0
    11.          ZWrite On
    12.          Cull Front
    13.       }
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.       #pragma surface surf Standard fullforwardshadows alpha:fade
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.          float4 color : COLOR;
    27.         };
    28.  
    29.         fixed4 _Color;
    30.  
    31.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    32.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    33.         // #pragma instancing_options assumeuniformscaling
    34.         UNITY_INSTANCING_BUFFER_START(Props)
    35.             // put more per-instance properties here
    36.         UNITY_INSTANCING_BUFFER_END(Props)
    37.  
    38.         void surf (Input IN, inout SurfaceOutputStandard o) {
    39.             // Albedo comes from a texture tinted by color
    40.             fixed4 c = _Color;
    41.             o.Albedo = c.rgb;
    42.          o.Alpha = IN.color.r;
    43.         }
    44.         ENDCG
    45.     }
    46.     FallBack "Diffuse"
    47. }
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Curious, that shader looks correct to me, and in testing produce the results I expected:
    upload_2019-5-25_22-48-5.png
     
  11. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    Could you link the other project files please?
     
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I mean, I copied the shader you posted, slapped it on an inverted cone mesh I had from another project, done?
     
  13. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    Ah, now It's working. So it makes any triangle behind a back side of a triangle transparent. brilliant! Thanks.