Search Unity

Adding Soft Particles to a Vertex Lit Particle Shader

Discussion in 'Shaders' started by x70x, May 18, 2013.

  1. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    49
    Is this possible?

    Here's my existing shader that I would like to add soft particles to:

    Code (csharp):
    1. Shader "Particles/VertexLit Blended2" {
    2. Properties {
    3.     _EmisColor ("Emissive Color", Color) = (.2,.2,.2,0)
    4.     _MainTex ("Particle Texture", 2D) = "white" {}
    5. }
    6.  
    7. SubShader {
    8.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    9.     Tags { "LightMode" = "Vertex" }
    10.     Fog {Mode Off}
    11.     Cull Off
    12.     Lighting On
    13.     Material { Emission [_EmisColor] }
    14.     ColorMaterial AmbientAndDiffuse
    15.     ZWrite Off
    16.     ColorMask RGB
    17.     Blend SrcAlpha OneMinusSrcAlpha
    18.     AlphaTest Greater .001
    19.     Pass {
    20.         SetTexture [_MainTex] { combine primary * texture }
    21.     }
    22. }
    23. }
    I know very little about shader programming, but the other shaders that already support soft particles do not give me the effect I'm looking for. The blending modes are way too extreme and the vertex shader above gives me exactly what I want (color and blending), except for the fact that all the particles intersect with the scene geometry. If anyone could point me in the right direction I would appreciate it. I'm looking for something similar to this: https://www.youtube.com/watch?v=ES0IY_e5Kd8.

     
  2. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    49
    Anyone have any ideas? Still not really sure what to do here.
     
  3. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    I doubt you can achieve that with a fixed function shader (why does everyone keep using them anyways?). Move on to a surface shader.. or even better, custom vert/frag shader
     
  4. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    49
    I still have not found any way to achieve this effect in Unity. I would even be willing to pay someone who could program a shader with vertex lighting and depth blending.
     
  5. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    You should write shader for particles.

    On vertex shader calculate depth of vertex.
    On pixel shader read depth and compare to depth from vertex shader.

    And lerp alpha to 0, if difference is smaller that some bias.


    Contact me if you want me to write that shader.
     
  6. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    The problem is with the lighting, it so hard to make them work with particles
     
  7. jura_wd

    jura_wd

    Joined:
    Jan 28, 2014
    Posts:
    32
    Hm, yeah. Looks like particles shader cannot access _WorldSpaceLightPos0 or something like that..
     
  8. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    49
    That's so strange. So how does the Vertex Lit particle shader get lighting information then? There are particle shaders with depth blending (soft particles) and there are particle shaders with vertex lighting... why can't you have both?
     
  9. Pawl

    Pawl

    Joined:
    Jun 23, 2013
    Posts:
    113
    I just wanted to say, I came across this thread looking for the same thing (a vertex-lit soft particle shader) and I was unable to find one.

    However depending on your scene, you might be able to get away with using the soft particle shader and simply modify the color & alpha value to fake a type of "global" lighting intensity. I've been using this with a directional sun light that has a basic day/night cycle and it doesn't look too bad.
     
  10. x70x

    x70x

    Joined:
    Aug 20, 2011
    Posts:
    49
    I have managed to do exactly as you have suggested. It works well enough to get the desired effect. My biggest issue was wanting to have my soft particles illuminated by a flashlight or other light sources, but oh well.
     
  11. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Try this not perfect tho,there's still some weird stuff happened
    Code (CSharp):
    1.  
    2. Shader "Rea/Particles/Lit-Alpha Blended " {
    3. Properties {
    4.    _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    5.    _MainTex ("Particle Texture", 2D) = "white" {}
    6.    _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    7. }
    8.  
    9. Category {
    10.    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"  }
    11.    Blend SrcAlpha OneMinusSrcAlpha
    12.    ColorMask RGB
    13.    Cull Off Lighting On ZWrite Off
    14.  
    15.    
    16.    // ---- Fragment program cards
    17.    SubShader {
    18.      Pass {
    19.  
    20.        CGPROGRAM
    21.        #pragma vertex vert
    22.        #pragma fragment frag
    23.        #pragma fragmentoption ARB_precision_hint_fastest
    24.        #pragma multi_compile_particles
    25.        
    26.        #include "UnityCG.cginc"
    27.  
    28.        sampler2D _MainTex;
    29.        float4 _TintColor;
    30.  
    31.        struct appdata_t {
    32.          float4 vertex : POSITION;
    33.          fixed4 color : COLOR;
    34.          float2 texcoord : TEXCOORD0;
    35.          float3 normal : NORMAL;
    36.        };
    37.  
    38.        struct v2f {
    39.          float4 pos : POSITION;
    40.          fixed4 color : COLOR;
    41.          float2 texcoord : TEXCOORD0;
    42.          float2 DisCoord : TEXCOORD1;
    43.  
    44.          float4 projPos : TEXCOORD2;
    45.  
    46.          float3 VL : TEXCOORD3;
    47.  
    48.          float3 normal :TEXCOORD4;
    49.          float3 ViewT : TEXCOORD5;
    50.  
    51.        };
    52.        
    53.        float4 _MainTex_ST;
    54.        float4 _Dist_ST;
    55.  
    56.        v2f vert (appdata_t v)
    57.        {
    58.          v2f o;
    59.          o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    60.  
    61.          o.projPos = ComputeScreenPos (o.pos);
    62.          COMPUTE_EYEDEPTH(o.projPos.z);
    63.  
    64.          o.color = v.color;
    65.          o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    66.          o.DisCoord = TRANSFORM_TEX(v.texcoord,_Dist);
    67.          o.normal = normalize(v.normal);
    68.          o.ViewT = normalize(ObjSpaceViewDir(v.vertex));
    69.          o.VL = ShadeVertexLights(v.vertex, dot(o.normal,o.ViewT));
    70.          return o;
    71.        }
    72.  
    73.        sampler2D _CameraDepthTexture;
    74.        float _InvFade;
    75.        
    76.        fixed4 frag (v2f i) : COLOR
    77.        {
    78.  
    79.          float4 tex = tex2D(_MainTex, i.texcoord);
    80.  
    81.          float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
    82.          float partZ = i.projPos.z;
    83.          float fade = saturate (_InvFade * (sceneZ-partZ));
    84.          i.color.a *= fade;
    85.  
    86.          float4 VertL = (dot(i.ViewT,i.VL));
    87.          return 2.0f * float4(i.VL,1.0) * i.color  * _TintColor * tex;
    88.  
    89.        }
    90.        ENDCG
    91.      }
    92.    }    
    93.    
    94.    // ---- Dual texture cards
    95.    SubShader {
    96.      Pass {
    97.        SetTexture [_MainTex] {
    98.          constantColor [_TintColor]
    99.          combine constant * primary
    100.        }
    101.        SetTexture [_MainTex] {
    102.          combine texture * previous DOUBLE
    103.        }
    104.      }
    105.    }
    106.    
    107.    // ---- Single texture cards (does not do color tint)
    108.    SubShader {
    109.      Pass {
    110.        SetTexture [_MainTex] {
    111.          combine texture * primary
    112.        }
    113.      }
    114.    }
    115. }
    116. }
    117.  
    118.  
     
    IgorAherne likes this.
  12. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    not to be the party pooper but you will have problems getting lights to work on particles, mainly because particle are dynamically generated to be just one mesh which will produce errors in the additional lights when particle stack on top of each other, they will get additive, you can still make 4 vertex lights to work or invent your own system to simulate surround lighting, i'm just alerting you with the incoming problems