deferred renders, particles, and lighting issue

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:
1198758--47828--$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:

Shader "Particles/VertexLit Soft2"
{
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Particle Texture", 2D) = "white" {}
		_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
	}
SubShader {
		Pass {
			Tags { 
				"LightMode" = "ForwardAdd"
				"RenderType"="Transparent"
				"Queue"="Transparent"
			}
			Fog { Color(0,0,0,0) }
			Cull Off
			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha 
 
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdadd_fullshadows
			#include "UnityCG.cginc"
			#include "AutoLight.cginc"
			
			sampler2D _MainTex;
			float _InvFade;
			float4 _Color;
			uniform sampler2D _CameraDepthTexture;
 
			struct v2f {
				float4 pos : SV_POSITION;
				LIGHTING_COORDS(0,1)
				float4 tc0 : TEXCOORD0;
				float4 tc1 : TEXCOORD1;
			};
 
			v2f vert (appdata_full v) {				
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				TRANSFER_VERTEX_TO_FRAGMENT(o); 
				
				#ifdef SOFTPARTICLES_ON
				o.tc1 = ComputeScreenPos (mul(UNITY_MATRIX_MVP, v.vertex));  
				COMPUTE_EYEDEPTH(o.tc1);
				#endif
				
				return o;
			}
 
			float4 frag (v2f i) : COLOR {
			
				half4 tex = tex2D(_MainTex, i.tc0.xy);
				float4 c;
				c.rgb = tex.rgb * (LIGHT_ATTENUATION(i)*4);
				
				#ifdef SOFTPARTICLES_ON
				float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.tc1))));
				tex.a *= saturate (_InvFade * (sceneZ - i.tc1.z)); 
				#endif 
				
				c.a = tex.a * _Color.a;
				
				return c;
			}
			ENDCG
		} //Pass
	} //SubShader
	FallBack "Particles/VertexLit Blended" //note: for passes: ForwardBase, ShadowCaster, ShadowCollector
}

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.

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.

Yeah, I’ll post it up on Monday. I think this is a working version of my code, but I’ve fiddled with it since then…

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.

I think a forwardbase pass is needed if you want your forwardadd pass to work

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.

Not very sure,but my test does result that,Maybe you should do one more test to deny that

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)

Resurecting old thread…
did anyone manage to get this shaders work?
I got compiler crashed everytime i try to import these shader.

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.

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.

A two year necro post, by any chance? :smile:

That aside… :wink: what do you think is causing the screen to freeze? :eyes: 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! :wink: