Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Transparency question

Discussion in 'Shaders' started by nug700, May 18, 2019 at 6:02 AM.

  1. nug700

    nug700

    Joined:
    Nov 19, 2012
    Posts:
    23
    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:
    6,469
    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. nug700

    nug700

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

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,469
    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. nug700

    nug700

    Joined:
    Nov 19, 2012
    Posts:
    23
    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 at 9:52 PM
  6. bgolus

    bgolus

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