Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Transparent Sorting & Stencils of a DrawMeshInstanced

Discussion in 'Shaders' started by FuzzyOnion, Jul 20, 2021.

  1. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    I'm pretty happy with these hand trails but I've been stuck for days on trying to find a workaround for these transparent sorting issues.


    From the research I've done it seems like it's basically unavoidable with a transparent shader but I figured I'd ask anyway.

    There's probably a way to have the shader be opaque but still have the sorting of the instanced meshes with the newer ones on top. The problem then is that these instanced meshes cannot "fade out" as they scale up.

    Any help is much appreciated.

    Here's the shader:
    Code (CSharp):
    1.  
    2.  
    3. Shader "Trails" {
    4. Properties {
    5.     _MainTex ("Texture", 2D) = "white" {}
    6.     _Color ("Color", Color) = (1,1,1,1)
    7.     _CutOff("Cut Off", Float) = 0.5
    8.     _Cull ("Cull", Int) = 2
    9. }
    10.     SubShader
    11.     {
    12.         Tags {  "RenderType"="Opaque" "RenderType" = "Transparent" }
    13.  
    14.         Pass
    15.         {
    16.             Stencil {
    17.                 Ref 2
    18.                 ReadMask 2
    19.      
    20.                 Comp NotEqual
    21.                 Pass Replace
    22.              
    23.              
    24.                     }
    25.  
    26.      
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.        
    29.  
    30.         ZWrite Off
    31.        
    32.      
    33.  
    34.          
    35.  
    36.             CGPROGRAM
    37.             #pragma vertex vert
    38.             #pragma fragment frag
    39.             #pragma multi_compile_instancing assumeuniformscaling nolightprobe nolodfade nolightmap
    40.             #pragma multi_compile _ TRAIL_ALPHACLIP
    41.             #include "UnityCG.cginc"
    42.  
    43.             struct appdata
    44.             {
    45.                 float4 vertex : POSITION;
    46.                 float2 uv     : TEXCOORD0;
    47.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    48.             };
    49.  
    50.             struct v2f
    51.             {
    52.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    53.                 float4 pos    : SV_POSITION;
    54.                 float2 uv     : TEXCOORD0;
    55.                 UNITY_VERTEX_OUTPUT_STEREO
    56.             };
    57.  
    58.               sampler2D _MainTex;
    59.               float4 _MainTex_ST;
    60.               fixed _CutOff;
    61.  
    62.      
    63.  
    64. UNITY_INSTANCING_BUFFER_START(Props)
    65.     UNITY_DEFINE_INSTANCED_PROP(fixed4, _Colors)
    66. #define _Colors_arr Props
    67. UNITY_INSTANCING_BUFFER_END(Props)
    68.  
    69.             v2f vert (appdata v)
    70.             {
    71.                 v2f o;
    72.                 UNITY_SETUP_INSTANCE_ID(v);
    73.          
    74.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
    75.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    76.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    77.          
    78.              
    79.                 o.pos = UnityObjectToClipPos(v.vertex);
    80.                 o.uv = mul(unity_ObjectToWorld, v.vertex).xyz;
    81.      
    82.                 return o;
    83.             }
    84.  
    85.    
    86.  
    87.  
    88.             fixed4 frag(v2f i) : SV_Target
    89.             {
    90.                
    91.                 UNITY_SETUP_INSTANCE_ID(i);
    92.                 float2 multy = float2(4, 4);
    93.                 fixed4 multy2 = UNITY_ACCESS_INSTANCED_PROP(_Colors_arr, _Colors);
    94.                 fixed4 col = tex2D(_MainTex, i.uv * multy + _Time*.2f );
    95.            
    96.                   col *= multy2.w;
    97.                 return col;
    98.             }
    99.             ENDCG
    100.         }
    101.  
    102.     }
    103. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,242
    You are correct that there is no real solution to this. Not anything that can do exactly what you want performantly at least. Fast and correct sorting of real time transparent surfaces is still an unsolved problem.

    For what you're doing you might want to look at implementing Weighted Blended OIT. This won't achieve the exact look you have above, but might get you something that's acceptably close.
    http://jcgt.org/published/0002/02/09/
     
  3. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    I forgot to thank you for the reply! Thank you and thank you for all the help you provide on these forums.