-
Notifications
You must be signed in to change notification settings - Fork 832
/
Copy pathVFXCommon.hlsl
213 lines (179 loc) · 6.11 KB
/
VFXCommon.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/AmbientOcclusion.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl"
float3 _LightDirection;
#ifdef VFX_VARYING_PS_INPUTS
void VFXTransformPSInputs(inout VFX_VARYING_PS_INPUTS input) {}
float4 VFXApplyPreExposure(float4 color, float exposureWeight)
{
return color;
}
float4 VFXApplyPreExposure(float4 color, VFX_VARYING_PS_INPUTS input)
{
return color;
}
#endif
float4 VFXTransformFinalColor(float4 color)
{
return color;
}
float2 VFXGetNormalizedScreenSpaceUV(float4 clipPos)
{
return GetNormalizedScreenSpaceUV(clipPos);
}
void VFXEncodeMotionVector(float2 velocity, out float4 outBuffer)
{
//TODO : LWRP doesn't support motion vector & TAA yet
outBuffer = (float4)0.0f;
}
float4 VFXTransformPositionWorldToClip(float3 posWS)
{
return TransformWorldToHClip(posWS);
}
float4 VFXTransformPositionWorldToNonJitteredClip(float3 posWS)
{
//TODO : LWRP doesn't support motion vector & TAA yet
return VFXTransformPositionWorldToClip(posWS);
}
float4 VFXTransformPositionWorldToPreviousClip(float3 posWS)
{
//TODO : LWRP doesn't support motion vector & TAA yet
return VFXTransformPositionWorldToClip(posWS);
}
float4 VFXTransformPositionObjectToClip(float3 posOS)
{
float3 posWS = TransformObjectToWorld(posOS);
return VFXTransformPositionWorldToClip(posWS);
}
float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS)
{
//TODO : LWRP doesn't support motion vector & TAA yet
return VFXTransformPositionObjectToClip(posOS);
}
float4 VFXTransformPositionObjectToPreviousClip(float3 posOS)
{
//TODO : LWRP doesn't support motion vector & TAA yet
return VFXTransformPositionObjectToClip(posOS);
}
float3 VFXTransformPositionWorldToView(float3 posWS)
{
return TransformWorldToView(posWS);
}
float3 VFXTransformPositionWorldToCameraRelative(float3 posWS)
{
#if SHADEROPTIONS_CAMERA_RELATIVE_RENDERING
#error VFX Camera Relative rendering isn't supported in URP.
#endif
return posWS;
}
//Compatibility functions for the common ShaderGraph integration
float4x4 ApplyCameraTranslationToMatrix(float4x4 modelMatrix)
{
return modelMatrix;
}
float4x4 ApplyCameraTranslationToInverseMatrix(float4x4 inverseModelMatrix)
{
return inverseModelMatrix;
}
//End of compatibility functions
float4x4 VFXGetObjectToWorldMatrix()
{
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
return GetSGVFXUnityObjectToWorld();
#else
return GetObjectToWorldMatrix();
#endif
}
float4x4 VFXGetWorldToObjectMatrix()
{
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
return GetSGVFXUnityWorldToObject();
#else
return GetWorldToObjectMatrix();
#endif
}
float3x3 VFXGetWorldToViewRotMatrix()
{
return (float3x3)GetWorldToViewMatrix();
}
float3 VFXGetViewWorldPosition()
{
return GetCurrentViewPosition();
}
float4x4 VFXGetViewToWorldMatrix()
{
return UNITY_MATRIX_I_V;
}
#ifdef USING_STEREO_MATRICES
float3 GetWorldStereoOffset()
{
return float3(0.0f, 0.0f, 0.0f);
}
#endif
float VFXSampleDepth(float4 posSS)
{
float2 screenUV = GetNormalizedScreenSpaceUV(posSS.xy);
// In URP, the depth texture is optional and could be 4x4 white texture, Load isn't appropriate in that case.
//float depth = LoadSceneDepth(screenUV * _ScreenParams.xy);
float depth = SampleSceneDepth(screenUV);
return depth;
}
void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS)
{
posWS = ApplyShadowBias(posWS, normalWS, _LightDirection);
posCS = VFXTransformPositionWorldToClip(posWS);
}
void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS)
{
posWS = ApplyShadowBias(posWS, _LightDirection, _LightDirection);
posCS = VFXTransformPositionWorldToClip(posWS);
}
float4 VFXApplyAO(float4 color, float4 posCS)
{
#if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(posCS);
AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
color.rgb *= aoFactor.directAmbientOcclusion;
#endif
return color;
}
float4 VFXApplyFog(float4 color,float4 posCS,float3 posWS)
{
float4 fog = (float4)0;
fog.rgb = unity_FogColor.rgb;
float fogFactor = ComputeFogFactor(posCS.z * posCS.w);
fog.a = ComputeFogIntensity(fogFactor);
#if VFX_BLENDMODE_ALPHA || IS_OPAQUE_PARTICLE
color.rgb = lerp(fog.rgb, color.rgb, fog.a);
#elif VFX_BLENDMODE_ADD
color.rgb *= fog.a;
#elif VFX_BLENDMODE_PREMULTIPLY
color.rgb = lerp(fog.rgb * color.a, color.rgb, fog.a);
#endif
return color;
}
float3 VFXGetCameraWorldDirection()
{
return unity_CameraToWorld._m02_m12_m22;
}
#if defined(_GBUFFER_NORMALS_OCT)
#define VFXComputePixelOutputToNormalBuffer(i,normalWS,uvData,outNormalBuffer) \
{ \
float2 octNormalWS = PackNormalOctQuadEncode(normalWS); /* values between [-1, +1], must use fp32 on some platforms */ \
float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); /* values between [ 0, 1] */ \
half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); /* values between [ 0, 1] */ \
outNormalBuffer = float4(packedNormalWS, 0.0); \
}
#else
#define VFXComputePixelOutputToNormalBuffer(i,normalWS,uvData,outNormalBuffer) \
{ \
outNormalBuffer = float4(normalWS, 0.0); \
}
#endif