Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

vertex original position block transparent fx from rendering

Discussion in 'Shaders' started by peeka, Aug 12, 2018.

  1. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    Hi I ran into this issue:

    A Opaque shader that moves vertex, and Transparent shader will be blocked by the original vertex if the Transparent shader is behind the Opaque object. Any idea if there is a way to fix this?


    When the Transparent shader is in the front it renders correctly, Opaque shader like the debris renders correctly when it's behind the vertex.



    (Update)

    This is the vertex shader:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/1 - Vertex Displacement/World Space"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.  
    9.         _Speed("Speed",Range(0.1,4)) = 1
    10.         _Amount("Amount", Range(0.1,10)) = 3
    11.         _Distance("Distance", Range( 0, 2 )) = 0.3
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.        
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.  
    41.             half _Speed;
    42.             half _Amount;
    43.             half _Distance;
    44.        
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.  
    49.                 // Takes the mesh's verts and turns it into a point in world space
    50.                 // this is the equivalent of Transform.TransformPoint on the scripting side
    51.                 float4 worldSpaceVertex = mul( unity_ObjectToWorld, v.vertex );
    52.  
    53.                 // Takes the x position of each vert as it was authored and moves it along the object's x-axis
    54.                 // The amount it's moved used a sine wave to have a nice wave shape to it
    55.                 // The vertex's y position is used otherwise all verts will be moved uniformly
    56.                 worldSpaceVertex.x += sin( _Time.y * _Speed + worldSpaceVertex.y * _Amount ) * _Distance;
    57.  
    58.                 // takes the new modified position of the vert in world space and then puts it back in local space
    59.                 v.vertex = mul( unity_WorldToObject, worldSpaceVertex );
    60.  
    61.                 // This line takes our mesh's vertices and turns them into screen positions that the fragment shader can fill in
    62.                 o.vertex = UnityObjectToClipPos(v.vertex);
    63.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    64.                 return o;
    65.             }
    66.        
    67.             fixed4 frag (v2f i) : SV_Target
    68.             {
    69.                 fixed4 col = tex2D(_MainTex, i.uv);
    70.                 return col;
    71.             }
    72.             ENDCG
    73.         }
    74.     }
    75. }
    76.  

    This is the transparent fx shader:

    Code (CSharp):
    1. Shader "AlphaBlended_test" {
    2. Properties {
    3.     _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    4.     _ColorStrength ("Color strength", Float) = 1.0
    5.     _MainTex ("Particle Texture", 2D) = "white" {}
    6.     _InvFade ("Soft Particles Factor", Float) = 0.5
    7. }
    8.  
    9. Category {
    10.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    11.     Blend SrcAlpha OneMinusSrcAlpha
    12.     Cull Off
    13.     ZWrite Off
    14.  
    15.     SubShader {
    16.         Pass {
    17.  
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma multi_compile_particles
    22.             #pragma fragmentoption ARB_precision_hint_fastest
    23.        
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             fixed4 _TintColor;
    28.             fixed4 _LightColor0;
    29.             fixed _ColorStrength;
    30.        
    31.             struct appdata_t {
    32.                 float4 vertex : POSITION;
    33.                 fixed4 color : COLOR;
    34.                 float2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             struct v2f {
    38.                 float4 vertex : POSITION;
    39.                 fixed4 color : COLOR;
    40.                 float2 texcoord : TEXCOORD0;            
    41.             };
    42.        
    43.             float4 _MainTex_ST;
    44.  
    45.             v2f vert (appdata_t v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);            
    49.                 o.color = v.color;
    50.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    51.                 return o;
    52.             }
    53.  
    54.        
    55.             half4 frag (v2f i) : COLOR
    56.             {
    57.            
    58.  
    59.                 fixed4 tex =  tex2D(_MainTex, i.texcoord);
    60.            
    61.                 half4 col = 2.0f * i.color * tex;
    62.                 col.rgb *= _TintColor.rgb;
    63.                 col.a = saturate(col.a * _TintColor.a);
    64.            
    65.            
    66.  
    67.                 return half4(col.rgb * _ColorStrength, col.a);
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
    73. }
     

    Attached Files:

    Last edited: Aug 12, 2018
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Without seeing the shader, no idea.
     
  3. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    Hi

    This is the vertex shader:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/1 - Vertex Displacement/World Space"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.  
    9.         _Speed("Speed",Range(0.1,4)) = 1
    10.         _Amount("Amount", Range(0.1,10)) = 3
    11.         _Distance("Distance", Range( 0, 2 )) = 0.3
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.          
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.  
    41.             half _Speed;
    42.             half _Amount;
    43.             half _Distance;
    44.          
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.  
    49.                 // Takes the mesh's verts and turns it into a point in world space
    50.                 // this is the equivalent of Transform.TransformPoint on the scripting side
    51.                 float4 worldSpaceVertex = mul( unity_ObjectToWorld, v.vertex );
    52.  
    53.                 // Takes the x position of each vert as it was authored and moves it along the object's x-axis
    54.                 // The amount it's moved used a sine wave to have a nice wave shape to it
    55.                 // The vertex's y position is used otherwise all verts will be moved uniformly
    56.                 worldSpaceVertex.x += sin( _Time.y * _Speed + worldSpaceVertex.y * _Amount ) * _Distance;
    57.  
    58.                 // takes the new modified position of the vert in world space and then puts it back in local space
    59.                 v.vertex = mul( unity_WorldToObject, worldSpaceVertex );
    60.  
    61.                 // This line takes our mesh's vertices and turns them into screen positions that the fragment shader can fill in
    62.                 o.vertex = UnityObjectToClipPos(v.vertex);
    63.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    64.                 return o;
    65.             }
    66.          
    67.             fixed4 frag (v2f i) : SV_Target
    68.             {
    69.                 fixed4 col = tex2D(_MainTex, i.uv);
    70.                 return col;
    71.             }
    72.             ENDCG
    73.         }
    74.     }
    75. }
    76.  

    This is the transparent fx shader:

    Code (CSharp):
    1. Shader "AlphaBlended_test" {
    2. Properties {
    3.     _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    4.     _ColorStrength ("Color strength", Float) = 1.0
    5.     _MainTex ("Particle Texture", 2D) = "white" {}
    6.     _InvFade ("Soft Particles Factor", Float) = 0.5
    7. }
    8.  
    9. Category {
    10.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    11.     Blend SrcAlpha OneMinusSrcAlpha
    12.     Cull Off
    13.     ZWrite Off
    14.  
    15.     SubShader {
    16.         Pass {
    17.  
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma multi_compile_particles
    22.             #pragma fragmentoption ARB_precision_hint_fastest
    23.          
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             fixed4 _TintColor;
    28.             fixed4 _LightColor0;
    29.             fixed _ColorStrength;
    30.          
    31.             struct appdata_t {
    32.                 float4 vertex : POSITION;
    33.                 fixed4 color : COLOR;
    34.                 float2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             struct v2f {
    38.                 float4 vertex : POSITION;
    39.                 fixed4 color : COLOR;
    40.                 float2 texcoord : TEXCOORD0;              
    41.             };
    42.          
    43.             float4 _MainTex_ST;
    44.  
    45.             v2f vert (appdata_t v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);            
    49.                 o.color = v.color;
    50.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    51.                 return o;
    52.             }
    53.  
    54.          
    55.             half4 frag (v2f i) : COLOR
    56.             {
    57.              
    58.  
    59.                 fixed4 tex =  tex2D(_MainTex, i.texcoord);
    60.              
    61.                 half4 col = 2.0f * i.color * tex;
    62.                 col.rgb *= _TintColor.rgb;
    63.                 col.a = saturate(col.a * _TintColor.a);
    64.              
    65.              
    66.  
    67.                 return half4(col.rgb * _ColorStrength, col.a);
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
    73. }
    Thanks for replying.
     
    Last edited: Aug 12, 2018
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    I see no reason why the vertex modified object should be causing any issues. I fact I suspect it’s not the problem at all and if you hide it the issue will still appear.

    The problem is likely because your alpha blend shader is using ZWrite On. It’s rare you want a transparency shader with that, and it’s also the default state for shaders so the line isn’t needed to enable it generally. You should be using ZWrite Off. I suspect the real bug is you have two transparent quads there and one is blocking the other.
     
  5. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    ahh sorry the ZWrite was off when the problem occurred, I was just messing around with the shader see if ZWrite On fixes it, but same problem.

    Also what do you mean by hide it? I tried disable the object and the problem would go away.
     
  6. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    I just did a simpler test, I create a cube with the transparent texture and the shader I posted applied to it, and made a sphere with vertex shader on it, notice the cube has the outline of the original sphere culling the transparent texture.

    I am on unity 2018.2.2f1
     

    Attached Files:

  7. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    the more I test, it actually doesn't always happen, sometimes it happen, trying to get a better repro now
     
  8. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    ok I figured out, I am using a projector to draw decal, and if there is a decal projected to the vertex modified mesh, it will block the view, if I remove the projector it return to normal. I am not planning on using projector on the vertex modified mesh. so I can ignore this issue now. thanks for replying.
     

    Attached Files:

  9. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    but for discussion sake.. what do you think happens here? is it because the projector's texture got apply to the mesh and it's the projector's transparent texture blocking the view?
     
  10. peeka

    peeka

    Joined:
    Dec 3, 2014
    Posts:
    113
    so my projector shader have ZWrite On , and when I set projector shader to ZWrite off, everything works even if projector is projecting to it.

    Thanks Bgolus!
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    It seems like you’ve figured it all out on your own!

    Just to add to this, any object that you apply vertex animation to won’t receive projectors properly, and should likely have the same “IgnoreProjector” tag as transparent shaders usually have. This is because the built in projectors work by simply rendering the mesh again with the projector’s shader, and that shader likely does not have any vertex animation.
     
    peeka likes this.