Search Unity

deferred renders, particles, and lighting issue

Discussion in 'Shaders' started by Damocles, Mar 23, 2013.

  1. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    I think I may be trying the impossible here - I'm trying to get pixel-lit particles that are transparent and soft-edged, but the camera is deferred rendering path.

    I have made some headway (slow and painful headway) but the end result is very odd. Look and see:
    $FBbjOdO.jpg

    There are a few issues here:

    The lighting seems to be taken from the deferred light prepass, not the forward add.
    The edges are not soft.
    These particles are high-opacity when viewed using Particles/VertexLit Blended. So the alpha seems to be getting messed up too (although this might just be the way it looks due to the lighting).

    I've been banging my head against this for ages now and have hit a dead end. Am I wasting my time, or have I misunderstood how to get forward lighting into the shader?

    The shader code:
    Code (csharp):
    1. Shader "Particles/VertexLit Soft2"
    2. {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Particle Texture", 2D) = "white" {}
    6.         _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    7.     }
    8. SubShader {
    9.         Pass {
    10.             Tags {
    11.                 "LightMode" = "ForwardAdd"
    12.                 "RenderType"="Transparent"
    13.                 "Queue"="Transparent"
    14.             }
    15.             Fog { Color(0,0,0,0) }
    16.             Cull Off
    17.             ZWrite Off
    18.             Blend SrcAlpha OneMinusSrcAlpha
    19.  
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma multi_compile_fwdadd_fullshadows
    24.             #include "UnityCG.cginc"
    25.             #include "AutoLight.cginc"
    26.            
    27.             sampler2D _MainTex;
    28.             float _InvFade;
    29.             float4 _Color;
    30.             uniform sampler2D _CameraDepthTexture;
    31.  
    32.             struct v2f {
    33.                 float4 pos : SV_POSITION;
    34.                 LIGHTING_COORDS(0,1)
    35.                 float4 tc0 : TEXCOORD0;
    36.                 float4 tc1 : TEXCOORD1;
    37.             };
    38.  
    39.             v2f vert (appdata_full v) {            
    40.                 v2f o;
    41.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    42.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    43.                
    44.                 #ifdef SOFTPARTICLES_ON
    45.                 o.tc1 = ComputeScreenPos (mul(UNITY_MATRIX_MVP, v.vertex));  
    46.                 COMPUTE_EYEDEPTH(o.tc1);
    47.                 #endif
    48.                
    49.                 return o;
    50.             }
    51.  
    52.             float4 frag (v2f i) : COLOR {
    53.            
    54.                 half4 tex = tex2D(_MainTex, i.tc0.xy);
    55.                 float4 c;
    56.                 c.rgb = tex.rgb * (LIGHT_ATTENUATION(i)*4);
    57.                
    58.                 #ifdef SOFTPARTICLES_ON
    59.                 float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.tc1))));
    60.                 tex.a *= saturate (_InvFade * (sceneZ - i.tc1.z));
    61.                 #endif
    62.                
    63.                 c.a = tex.a * _Color.a;
    64.                
    65.                 return c;
    66.             }
    67.             ENDCG
    68.         } //Pass
    69.     } //SubShader
    70.     FallBack "Particles/VertexLit Blended" //note: for passes: ForwardBase, ShadowCaster, ShadowCollector
    71. }
    72.  
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    It's possible, I have pixel lit particles working (without normal maps at the moment) but I don't have the code on me at the moment.
     
  3. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    Farfarer - any chance I could get a peek at your code? I've googled till my eyes are sore, and still haven't found where I'm going wrong.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
  5. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    Thanks for the code. I can see it's a huge undertaking getting lit particles into Unity.

    Sadly, the code you have interferes with other shader code of mine. And when I try to fix one part to make them work together, something else breaks. I think I'm about ready to say screw it and just have mediocre particles. It's not like anyone is going to complain that the particles aren't amazing.
     
  6. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    I think a forwardbase pass is needed if you want your forwardadd pass to work
     
  7. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    The key point is that you can't get the world space particle position in a shader without the camera script to generate _Camera2World.

    And all of the other lighting stuff relies on that (attenuation, shadows, light direction, etc) which is why I've had to duplicate a lot of code and put in the extra helper macros that use correct values.

    Lulucifer; You don't need a forwardbase pass to use forwardadd.
     
  8. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    Not very sure,but my test does result that,Maybe you should do one more test to deny that
     
  9. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    Holy thread resurrection batman!

    So I decided I wasn't happy with mediocre particles after all, and have returned to working on this. I have lit particles working (mostly based off of Farfarer's code, big thanks for that!). The only remaining issue is getting soft particles to work in it. I am completely stumped here - it should be virtually the same as normal soft particles. I compute the screen position, then the depth, then sample the camera depth to get the fade amount.

    Yet it does not work. I tried changing it so that instead of fading it would color the results red, but it had no effect. So it seems the depth computations are either being ignored, or are not working at all. I checked, and soft particles are definitely on, so I can only assume something is wrong with how I'm determining the soft fade factor.

    Does anyone have any ideas where I'm going wrong?

    The code on paste bin (it's a bit big to paste here)
     
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Resurecting old thread......
    did anyone manage to get this shaders work?
    I got compiler crashed everytime i try to import these shader.
     
  11. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    I could never get both lit particles and soft particles to work at the same time. In the end I gave up because it wasn't vital that I have both, it was just a nice thing to have. So I ended up using hard pixel-lit in some cases, and soft vertex-lit in others.
     
  12. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
    noticed that on my project with one camera in forward and one camera in deferred when i fire my projectiles i do not see them ( particles) and after the screen freeze, i have to rescale my editor to unfreeze. if i set the second camera to forward and disable HDR, there is no bug.
     
  13. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    A two year necro post, by any chance? :D

    That aside... ;) what do you think is causing the screen to freeze? o_O there are some issues with the new deferred renderer working properly atm, although that shouldn't affect particles. Try turning the soft particles factor up if all else fails! ;)