Skip to content

Commit 9c2ed2e

Browse files
authored
[2021.2] Particle Shaders: Adding Depth & Depth Normals passes (#3141)
* Adding Depth & Depth Normals to Particle Shaders * Changes based on comments * Minor tweak * More minor tweaks * Fixing some keywords in the particle shaders * Fixing a shader error with redefinition of variable [Cancel Old CI]
1 parent a9d439f commit 9c2ed2e

11 files changed

+506
-8
lines changed

com.unity.render-pipelines.universal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2121
- Move Assets/Create/Rendering/Universal Render Pipeline/Forward Renderer to Assets/Create/Rendering/URP Forward Renderer
2222
- Removing unused temporary depth buffers for Depth of Field and Panini Projection.
2323
- Optimized the Bokeh Depth of Field shader on mobile by using half precision floats.
24+
- Added Depth and DepthNormals passes to particles shaders.
2425

2526
### Fixed
2627
- Fixed an issue where ShadowCaster2D was generating garbage when running in the editor. [case 1304158](https://issuetracker.unity3d.com/product/unity/issues/guid/1304158/)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#ifndef UNIVERSAL_PARTICLES_LIT_DEPTH_NORMALS_PASS_INCLUDED
2+
#define UNIVERSAL_PARTICLES_LIT_DEPTH_NORMALS_PASS_INCLUDED
3+
4+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
5+
6+
VaryingsDepthNormalsParticle DepthNormalsVertex(AttributesDepthNormalsParticle input)
7+
{
8+
VaryingsDepthNormalsParticle output = (VaryingsDepthNormalsParticle)0;
9+
UNITY_SETUP_INSTANCE_ID(input);
10+
UNITY_TRANSFER_INSTANCE_ID(input, output);
11+
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
12+
13+
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
14+
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, input.tangent);
15+
16+
half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
17+
#if !SHADER_HINT_NICE_QUALITY
18+
viewDirWS = SafeNormalize(viewDirWS);
19+
#endif
20+
21+
#if defined(_NORMALMAP)
22+
output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
23+
output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
24+
output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
25+
#else
26+
output.normalWS = normalInput.normalWS;
27+
output.viewDirWS = viewDirWS;
28+
#endif
29+
30+
output.clipPos = vertexInput.positionCS;
31+
32+
#if defined(_ALPHATEST_ON)
33+
output.color = GetParticleColor(input.color);
34+
#endif
35+
36+
#if defined(_ALPHATEST_ON) || defined(_NORMALMAP)
37+
#if defined(_FLIPBOOKBLENDING_ON)
38+
#if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
39+
GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
40+
#else
41+
GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
42+
#endif
43+
#else
44+
GetParticleTexcoords(output.texcoord, input.texcoords.xy);
45+
#endif
46+
#endif
47+
48+
return output;
49+
}
50+
51+
half4 DepthNormalsFragment(VaryingsDepthNormalsParticle input) : SV_TARGET
52+
{
53+
UNITY_SETUP_INSTANCE_ID(input);
54+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
55+
56+
// Inputs...
57+
#if defined(_ALPHATEST_ON) || defined(_NORMALMAP)
58+
float2 uv = input.texcoord;
59+
60+
#if defined(_FLIPBOOKBLENDING_ON)
61+
float3 blendUv = input.texcoord2AndBlend;
62+
#else
63+
float3 blendUv = float3(0,0,0);
64+
#endif
65+
#endif
66+
67+
// Check if we need to discard...
68+
#if defined(_ALPHATEST_ON)
69+
half4 vertexColor = input.color;
70+
half4 baseColor = _BaseColor;
71+
half4 albedo = BlendTexture(TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap), uv, blendUv) * baseColor;
72+
73+
half4 colorAddSubDiff = half4(0, 0, 0, 0);
74+
#if defined(_COLORADDSUBDIFF_ON)
75+
colorAddSubDiff = _BaseColorAddSubDiff;
76+
#endif
77+
78+
albedo = MixParticleColor(albedo, vertexColor, colorAddSubDiff);
79+
AlphaDiscard(albedo.a, _Cutoff);
80+
#endif
81+
82+
// Normals...
83+
#ifdef _NORMALMAP
84+
half3 normalTS = SampleNormalTS(uv, blendUv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
85+
float3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
86+
#else
87+
float3 normalWS = input.normalWS;
88+
#endif
89+
90+
// Output...
91+
#if defined(_GBUFFER_NORMALS_OCT)
92+
float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on Nintendo Switch.
93+
float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
94+
half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
95+
return half4(packedNormalWS, 0.0);
96+
#else
97+
return half4(NormalizeNormalPerPixel(normalWS), 0.0);
98+
#endif
99+
}
100+
101+
#endif // UNIVERSAL_PARTICLES_LIT_DEPTH_NORMALS_PASS_INCLUDED

com.unity.render-pipelines.universal/Shaders/Particles/ParticlesDepthNormalsPass.hlsl.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef UNIVERSAL_PARTICLES_LIT_DEPTH_ONLY_PASS_INCLUDED
2+
#define UNIVERSAL_PARTICLES_LIT_DEPTH_ONLY_PASS_INCLUDED
3+
4+
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
5+
6+
VaryingsDepthOnlyParticle DepthOnlyVertex(AttributesDepthOnlyParticle input)
7+
{
8+
VaryingsDepthOnlyParticle output = (VaryingsDepthOnlyParticle)0;
9+
UNITY_SETUP_INSTANCE_ID(input);
10+
UNITY_TRANSFER_INSTANCE_ID(input, output);
11+
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
12+
13+
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
14+
output.clipPos = vertexInput.positionCS;
15+
16+
#if defined(_ALPHATEST_ON)
17+
output.color = GetParticleColor(input.color);
18+
19+
#if defined(_FLIPBOOKBLENDING_ON)
20+
#if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
21+
GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
22+
#else
23+
GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
24+
#endif
25+
#else
26+
GetParticleTexcoords(output.texcoord, input.texcoords.xy);
27+
#endif
28+
#endif
29+
30+
return output;
31+
}
32+
33+
half4 DepthOnlyFragment(VaryingsDepthOnlyParticle input) : SV_TARGET
34+
{
35+
UNITY_SETUP_INSTANCE_ID(input);
36+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
37+
38+
// Check if we need to discard...
39+
#if defined(_ALPHATEST_ON)
40+
float2 uv = input.texcoord;
41+
half4 vertexColor = input.color;
42+
half4 baseColor = _BaseColor;
43+
44+
#if defined(_FLIPBOOKBLENDING_ON)
45+
float3 blendUv = input.texcoord2AndBlend;
46+
#else
47+
float3 blendUv = float3(0,0,0);
48+
#endif
49+
50+
half4 albedo = BlendTexture(TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap), uv, blendUv) * baseColor;
51+
half4 colorAddSubDiff = half4(0, 0, 0, 0);
52+
#if defined (_COLORADDSUBDIFF_ON)
53+
colorAddSubDiff = _BaseColorAddSubDiff;
54+
#endif
55+
56+
albedo = MixParticleColor(albedo, vertexColor, colorAddSubDiff);
57+
AlphaDiscard(albedo.a, _Cutoff);
58+
#endif
59+
60+
return 0;
61+
}
62+
63+
#endif // UNIVERSAL_PARTICLES_LIT_DEPTH_ONLY_PASS_INCLUDED

com.unity.render-pipelines.universal/Shaders/Particles/ParticlesDepthOnlyPass.hlsl.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.render-pipelines.universal/Shaders/Particles/ParticlesInput.hlsl

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66
struct AttributesParticle
77
{
8-
float4 vertex : POSITION;
9-
half4 color : COLOR;
8+
float4 vertex : POSITION;
9+
half4 color : COLOR;
1010

1111
#if defined(_FLIPBOOKBLENDING_ON) && !defined(UNITY_PARTICLE_INSTANCING_ENABLED)
12-
float4 texcoords : TEXCOORD0;
13-
float texcoordBlend : TEXCOORD1;
12+
float4 texcoords : TEXCOORD0;
13+
float texcoordBlend : TEXCOORD1;
1414
#else
15-
float2 texcoords : TEXCOORD0;
15+
float2 texcoords : TEXCOORD0;
1616
#endif
1717

1818
#if !defined(PARTICLES_EDITOR_META_PASS)
19-
float3 normal : NORMAL;
20-
float4 tangent : TANGENT;
19+
float3 normal : NORMAL;
20+
float4 tangent : TANGENT;
2121
#endif
2222
UNITY_VERTEX_INPUT_INSTANCE_ID
2323
};
@@ -59,4 +59,91 @@ struct VaryingsParticle
5959
UNITY_VERTEX_OUTPUT_STEREO
6060
};
6161

62+
struct AttributesDepthOnlyParticle
63+
{
64+
float4 vertex : POSITION;
65+
66+
#if defined(_ALPHATEST_ON)
67+
half4 color : COLOR;
68+
69+
#if defined(_FLIPBOOKBLENDING_ON) && !defined(UNITY_PARTICLE_INSTANCING_ENABLED)
70+
float4 texcoords : TEXCOORD0;
71+
float texcoordBlend : TEXCOORD1;
72+
#else
73+
float2 texcoords : TEXCOORD0;
74+
#endif
75+
#endif
76+
UNITY_VERTEX_INPUT_INSTANCE_ID
77+
};
78+
79+
struct VaryingsDepthOnlyParticle
80+
{
81+
float4 clipPos : SV_POSITION;
82+
83+
#if defined(_ALPHATEST_ON)
84+
float2 texcoord : TEXCOORD0;
85+
half4 color : COLOR;
86+
87+
#if defined(_FLIPBOOKBLENDING_ON)
88+
float3 texcoord2AndBlend : TEXCOORD5;
89+
#endif
90+
#endif
91+
92+
UNITY_VERTEX_INPUT_INSTANCE_ID
93+
UNITY_VERTEX_OUTPUT_STEREO
94+
};
95+
96+
struct AttributesDepthNormalsParticle
97+
{
98+
float4 vertex : POSITION;
99+
100+
#if defined(_ALPHATEST_ON)
101+
half4 color : COLOR;
102+
#endif
103+
104+
#if defined(_ALPHATEST_ON) || defined(_NORMALMAP)
105+
#if defined(_FLIPBOOKBLENDING_ON) && !defined(UNITY_PARTICLE_INSTANCING_ENABLED)
106+
float4 texcoords : TEXCOORD0;
107+
float texcoordBlend : TEXCOORD1;
108+
#else
109+
float2 texcoords : TEXCOORD0;
110+
#endif
111+
#endif
112+
113+
float3 normal : NORMAL;
114+
float4 tangent : TANGENT;
115+
116+
UNITY_VERTEX_INPUT_INSTANCE_ID
117+
};
118+
119+
120+
struct VaryingsDepthNormalsParticle
121+
{
122+
float4 clipPos : SV_POSITION;
123+
124+
#if defined(_ALPHATEST_ON)
125+
half4 color : COLOR;
126+
#endif
127+
128+
#if defined(_ALPHATEST_ON) || defined(_NORMALMAP)
129+
float2 texcoord : TEXCOORD0;
130+
131+
#if defined(_FLIPBOOKBLENDING_ON)
132+
float3 texcoord2AndBlend : TEXCOORD5;
133+
#endif
134+
#endif
135+
136+
#if defined(_NORMALMAP)
137+
float4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
138+
float4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
139+
float4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
140+
#else
141+
float3 normalWS : TEXCOORD2;
142+
float3 viewDirWS : TEXCOORD3;
143+
#endif
144+
145+
UNITY_VERTEX_INPUT_INSTANCE_ID
146+
UNITY_VERTEX_OUTPUT_STEREO
147+
};
148+
62149
#endif // UNIVERSAL_PARTICLES_INPUT_INCLUDED

com.unity.render-pipelines.universal/Shaders/Particles/ParticlesLit.shader

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Shader "Universal Render Pipeline/Particles/Lit"
9191
// -------------------------------------
9292
// Particle Keywords
9393
#pragma shader_feature_local_fragment _ _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
94-
#pragma shader_feature_local_fragment _ALPHATEST_ON
94+
#pragma shader_feature_local_fragment _ _ALPHATEST_ON
9595
#pragma shader_feature_local_fragment _ _COLOROVERLAY_ON _COLORCOLOR_ON _COLORADDSUBDIFF_ON
9696
#pragma shader_feature_local _FLIPBOOKBLENDING_ON
9797
#pragma shader_feature_local _SOFTPARTICLES_ON
@@ -172,6 +172,69 @@ Shader "Universal Render Pipeline/Particles/Lit"
172172
ENDHLSL
173173
}
174174
// ------------------------------------------------------------------
175+
// Depth Only pass.
176+
Pass
177+
{
178+
Name "DepthOnly"
179+
Tags{"LightMode" = "DepthOnly"}
180+
181+
ZWrite On
182+
ColorMask 0
183+
Cull[_Cull]
184+
185+
HLSLPROGRAM
186+
#pragma target 2.0
187+
188+
// -------------------------------------
189+
// Material Keywords
190+
#pragma shader_feature_local _ _ALPHATEST_ON
191+
#pragma shader_feature_local _ _FLIPBOOKBLENDING_ON
192+
#pragma shader_feature_local_fragment _ _COLOROVERLAY_ON _COLORCOLOR_ON _COLORADDSUBDIFF_ON
193+
194+
// -------------------------------------
195+
// Unity defined keywords
196+
#pragma multi_compile_instancing
197+
#pragma instancing_options procedural:ParticleInstancingSetup
198+
199+
#pragma vertex DepthOnlyVertex
200+
#pragma fragment DepthOnlyFragment
201+
202+
#include "Packages/com.unity.render-pipelines.universal/Shaders/Particles/ParticlesLitInput.hlsl"
203+
#include "Packages/com.unity.render-pipelines.universal/Shaders/Particles/ParticlesDepthOnlyPass.hlsl"
204+
ENDHLSL
205+
}
206+
// This pass is used when drawing to a _CameraNormalsTexture texture
207+
Pass
208+
{
209+
Name "DepthNormals"
210+
Tags{"LightMode" = "DepthNormals"}
211+
212+
ZWrite On
213+
Cull[_Cull]
214+
215+
HLSLPROGRAM
216+
#pragma target 2.0
217+
218+
// -------------------------------------
219+
// Material Keywords
220+
#pragma shader_feature_local _ _NORMALMAP
221+
#pragma shader_feature_local _ _FLIPBOOKBLENDING_ON
222+
#pragma shader_feature_local _ _ALPHATEST_ON
223+
#pragma shader_feature_local_fragment _ _COLOROVERLAY_ON _COLORCOLOR_ON _COLORADDSUBDIFF_ON
224+
225+
// -------------------------------------
226+
// Unity defined keywords
227+
#pragma multi_compile_instancing
228+
#pragma instancing_options procedural:ParticleInstancingSetup
229+
230+
#pragma vertex DepthNormalsVertex
231+
#pragma fragment DepthNormalsFragment
232+
233+
#include "Packages/com.unity.render-pipelines.universal/Shaders/Particles/ParticlesLitInput.hlsl"
234+
#include "Packages/com.unity.render-pipelines.universal/Shaders/Particles/ParticlesDepthNormalsPass.hlsl"
235+
ENDHLSL
236+
}
237+
// ------------------------------------------------------------------
175238
// Scene view outline pass.
176239
Pass
177240
{

0 commit comments

Comments
 (0)