Search Unity

Making the Particle Additive Shader be affected by default Fog

Discussion in 'Shaders' started by NeatWolf, Sep 1, 2015.

  1. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
    Hi,

    I'm using the Particle/Additive (Soft) Shader from Unity 4.6.3, but it doesn't seem to be affected by the scene Fog:



    I have some path points there that shouldn't be so easily visible from this distance.

    Those are combined, relatively complex Shuriken FX with the abovementioned shader applied to all of them.

    Is it possible to make them blend with fog?

    Here's the original full shader I'm using:

    Code (csharp):
    1.  
    2. Shader "Particles/Additive (Soft)" {
    3. Properties {
    4.     _MainTex ("Particle Texture", 2D) = "white" {}
    5.     _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    6. }
    7.  
    8. Category {
    9.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    10.     Blend One OneMinusSrcColor
    11.     ColorMask RGB
    12.     Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    13.  
    14.     SubShader {
    15.         Pass {
    16.  
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #pragma multi_compile_particles
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             sampler2D _MainTex;
    25.             fixed4 _TintColor;
    26.    
    27.             struct appdata_t {
    28.                 float4 vertex : POSITION;
    29.                 fixed4 color : COLOR;
    30.                 float2 texcoord : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f {
    34.                 float4 vertex : SV_POSITION;
    35.                 fixed4 color : COLOR;
    36.                 float2 texcoord : TEXCOORD0;
    37.                 #ifdef SOFTPARTICLES_ON
    38.                 float4 projPos : TEXCOORD1;
    39.                 #endif
    40.             };
    41.  
    42.             float4 _MainTex_ST;
    43.    
    44.             v2f vert (appdata_t v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    48.                 #ifdef SOFTPARTICLES_ON
    49.                 o.projPos = ComputeScreenPos (o.vertex);
    50.                 COMPUTE_EYEDEPTH(o.projPos.z);
    51.                 #endif
    52.                 o.color = v.color;
    53.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    54.                 return o;
    55.             }
    56.  
    57.             sampler2D_float _CameraDepthTexture;
    58.             float _InvFade;
    59.    
    60.             fixed4 frag (v2f i) : SV_Target
    61.             {
    62.                 #ifdef SOFTPARTICLES_ON
    63.                 float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    64.                 float partZ = i.projPos.z;
    65.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    66.                 i.color.a *= fade;
    67.                 #endif
    68.        
    69.                 half4 prev = i.color * tex2D(_MainTex, i.texcoord);
    70.                 prev.rgb *= prev.a;
    71.                 return prev;
    72.             }
    73.             ENDCG
    74.         }
    75.     }
    76. }
    77. }
    78.  

    I removed the "Fog" part, but now the textures look opaque even where they're transparent. They look overbright and there are artifacts.


    What should I change to obtain the desired result?

    I'd like them to seamlessly blend with the scenery. Maybe I should make them fade with distance? Could you please help me with the code? I'm using Exp2 scene Fog.
    I also tried to make them fade with distance with this, but with no success:
    Code (csharp):
    1.  
    2. Shader "NeatWolf/Additive (Soft) Fog" {
    3. Properties {
    4.     _MainTex ("Particle Texture", 2D) = "white" {}
    5.     _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    6.     _MinVisDistance("Min Vis Distance", float) = 0.0
    7.     _MaxVisDistance("Max Vis Distance", float) = 200.0
    8. }
    9.  
    10. Category {
    11.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    12.     Blend One OneMinusSrcColor
    13.     ColorMask RGB
    14.     Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    15.  
    16.     SubShader {
    17.         Pass {
    18.        
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma multi_compile_particles
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             fixed4 _TintColor;
    28.             half _MinVisDistance;
    29.             half _MaxVisDistance;
    30.             //struct appdata_t {
    31.             //    float4 vertex : POSITION;
    32.             //    fixed4 color : COLOR;
    33.             //    float2 texcoord : TEXCOORD0;
    34.             //};
    35.  
    36.             struct v2f {
    37.                 float4 vertex : SV_POSITION;
    38.                 fixed4 color : COLOR;
    39.                 float2 texcoord : TEXCOORD0;
    40.                 #ifdef SOFTPARTICLES_ON
    41.                 float4 projPos : TEXCOORD1;
    42.                 #endif
    43.             };
    44.  
    45.             float4 _MainTex_ST;
    46.            
    47.             v2f vert (appdata_full v)
    48.             {
    49.                 v2f o;
    50.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    51.                 #ifdef SOFTPARTICLES_ON
    52.                 o.projPos = ComputeScreenPos (o.vertex);
    53.                 COMPUTE_EYEDEPTH(o.projPos.z);
    54.                 #endif
    55.                 o.color = v.color;
    56.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    57.                
    58.                 //distance falloff
    59.                 half3 viewDirW = _WorldSpaceCameraPos - mul((half4x4)_Object2World, v.vertex);
    60.                 half viewDist = length(viewDirW);
    61.                 half falloff = saturate((viewDist - _MinVisDistance) / (_MaxVisDistance - _MinVisDistance));
    62.                 o.color.a *= (1.0f - falloff);
    63.                
    64.                 return o;
    65.             }
    66.  
    67.             sampler2D_float _CameraDepthTexture;
    68.             float _InvFade;
    69.            
    70.             fixed4 frag (v2f i) : SV_Target
    71.             {
    72.                 #ifdef SOFTPARTICLES_ON
    73.                 float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    74.                 float partZ = i.projPos.z;
    75.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    76.                 i.color.a *= fade;
    77.                 #endif
    78.                
    79.                 half4 prev = i.color * tex2D(_MainTex, i.texcoord);
    80.                 prev.rgb *= prev.a;
    81.                 return prev;
    82.             }
    83.             ENDCG
    84.         }
    85.     }
    86. }
    87.  

    Thank you for your time! :)
     
    Last edited: Sep 1, 2015
  2. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    Your shader appears to be using

    "Blend One OneMinusSrcColor"

    Which seems a fairly odd blend mode? - and that explains why your alpha-fade has no effect, the blend mode doesn't use the source alpha.

    If you can use a more standard additive blend mode - "Blend SrcAlpha One" - then your alpha-fade-with-distance approach should work well?