- Home /
Depth of Field Ignoring Particles
Hello!
I have been trying to use Unity's built-in Depth of Field image effect in conjunction with Particle Systems. The problem is, the Depth of Field doesn't seem to factor in particles in a non-mesh rendering mode. Instead, it will be blurred based on the distance of the object behind it (as shown in the picture below).
I have tried many different types of shaders, including opaque shaders, and adding ZWrite On to existing particle shaders, with no luck whatsoever.
My question is: is there a way to make this work? And if not, is there perhaps an alternative (preferrably free) to either the particle system, or DoF effect that will make it work?
I am using Unity 5.2.1
Answer by Johannski · Dec 13, 2016 at 02:02 PM
For anyone who still has this problem: As for Unity 5.5 it is not possible to write into the depth buffer for transparent objects. The depth buffer is used for quite some post processing effects so in order to get the particles to render the correct depth they need to be cutout or opaque shaders instead. Here is a simple example of such a shader:
Shader "Supyrb/Particles/Cutout" {
Properties{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
// Not used, but important for fallback to render the correct depth buffer
[HideInInspector] _Color("Main Color", Color) = (1,1,1,1)
}
SubShader{
Tags{ "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 color: COLOR;
};
void surf(Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"
}
Answer by ZacGarby · Sep 26, 2015 at 09:22 PM
Couldn't you just make it render the particles as a mesh?
It is possible to render the particles as a mesh, as long as I use an opaque shader, otherwise I get the same problems (and see$$anonymous$$gly less performance).