-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathParticle.hlsl
283 lines (249 loc) · 8.04 KB
/
Particle.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "Common/FrameBuffer.hlsli"
#include "Common/VR.hlsli"
struct VS_INPUT
{
float4 Position : POSITION0;
#if !defined(ENVCUBE)
float4 Normal : NORMAL0;
#endif
float4 TexCoord0 : TEXCOORD0;
#if defined(ENVCUBE)
float4
#else
int4
#endif
TexCoord1 : TEXCOORD1;
#if defined(VR)
uint InstanceID : SV_INSTANCEID;
#endif // VR
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION0;
float4 Color : COLOR0;
float2 TexCoord0 : TEXCOORD0;
#if defined(ENVCUBE)
float4 PrecipitationOcclusionTexCoord : TEXCOORD1;
#endif
#if defined(VR)
float ClipDistance : SV_ClipDistance0; // o11
float CullDistance : SV_CullDistance0; // p11
uint EyeIndex : EYEIDX0;
#endif // VR
};
#ifdef VSHADER
cbuffer PerTechnique : register(b0)
{
float2 ScaleAdjust : packoffset(c0);
};
cbuffer PerGeometry : register(b2)
{
# if !defined(VR)
row_major float4x4 WorldViewProj[1]; // 0
row_major float4x4 WorldView[1]; // 4
# else
row_major float4x4 WorldViewProj[2]; // 0
row_major float4x4 WorldView[2]; // 8
# endif
# if defined(ENVCUBE)
row_major float4x4 PrecipitationOcclusionWorldViewProj; // 8, 16
# endif
float4 fVars0; // 8, 16 ENVCUBE 12, 20
float4 fVars1; // 9, 17 ENVCUBE 13, 21
float4 fVars2; // 10, 18 ENVCUBE 14, 22
float4 fVars3; // 11, 19 ENVCUBE 15, 23
float4 fVars4; // 12, 20 ENVCUBE 16, 24
float4 Color1; // 13, 21 ENVCUBE 17, 25
float4 Color2; // 14, 22 ENVCUBE 18, 26
float4 Color3; // 15, 23 ENVCUBE 19, 27
float4 Velocity; // 16, 24 ENVCUBE 20, 28
float4 Acceleration; // 17, 25 ENVCUBE 21, 29
float4 Wind; // 18, 26 ENVCUBE 22, 30
}
float2x2 GetRotationMatrix(float angle)
{
float sine, cosine;
sincos(angle, sine, cosine);
return float2x2(float2(cosine, -sine), float2(sine, cosine));
}
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT vsout;
uint eyeIndex = Stereo::GetEyeIndexVS(
# if defined(VR)
input.InstanceID
# endif
);
# if defined(ENVCUBE)
# if defined(RAIN)
float2 positionOffset = input.TexCoord1.xy;
# else
float2x2 rotationMatrix = GetRotationMatrix(fVars0.w);
float2 positionOffset = mul(rotationMatrix, input.TexCoord1.xy) * ScaleAdjust + mul(rotationMatrix, input.TexCoord1.zw);
# endif
float3 normalizedPosition = (fVars0.xyz + input.Position.xyz) / fVars2.xxx;
normalizedPosition = normalizedPosition >= -normalizedPosition ? frac(abs(normalizedPosition)) :
-frac(abs(normalizedPosition));
float4 msPosition;
msPosition.xyz = normalizedPosition * fVars2.xxx + (-(fVars2.x * 0.5).xxx + fVars1.xyz);
msPosition.w = 1;
float4 viewPosition = mul(WorldViewProj[eyeIndex], msPosition);
# if defined(RAIN)
float4 adjustedMsPosition = msPosition - float4(Velocity.xyz, 0);
float positionBlendParam = 0.5 * (1 + input.TexCoord1.y);
float4 adjustedViewPosition = mul(WorldViewProj[eyeIndex], adjustedMsPosition);
float4 finalViewPosition = lerp(adjustedViewPosition, viewPosition, positionBlendParam);
# else
float4 finalViewPosition = viewPosition;
# endif
vsout.Position.xy = positionOffset + finalViewPosition.xy;
vsout.Position.zw = finalViewPosition.zw;
vsout.Color.xyz = 1.0.xxx;
vsout.Color.w = fVars1.w;
vsout.TexCoord0.xy = input.TexCoord0.xy;
float4 precipitationOcclusionTexCoord = mul(PrecipitationOcclusionWorldViewProj, msPosition);
precipitationOcclusionTexCoord.y = -precipitationOcclusionTexCoord.y;
vsout.PrecipitationOcclusionTexCoord = precipitationOcclusionTexCoord;
# else
float tmp2 = input.Normal.w * input.Position.w;
float tmp1 = tmp2 / fVars0.y;
float uvScale1, uvScale2, tmp3, tmp4;
if (tmp1 > fVars2.w) {
uvScale1 = fVars2.y;
uvScale2 = 0;
tmp3 = fVars2.w;
tmp4 = 1;
} else if (tmp1 > fVars2.z) {
uvScale1 = fVars2.x;
uvScale2 = fVars2.y;
tmp3 = fVars2.z;
tmp4 = fVars2.w;
} else {
uvScale1 = 0;
uvScale2 = fVars2.x;
tmp3 = 0;
tmp4 = fVars2.z;
}
float uvScaleParam = (tmp1 - tmp3) / (tmp4 - tmp3);
float uvScale = lerp(uvScale1, uvScale2, uvScaleParam);
vsout.TexCoord0.xy = fVars4.xy * input.TexCoord1.xy;
float2 uv1 = (input.TexCoord1.zw * 2.0.xx - 1.0.xx) * uvScale;
float uvAngle = input.TexCoord0.y * input.Position.w + input.TexCoord0.x;
float2x2 rotationMatrix = GetRotationMatrix(uvAngle);
float2 positionOffset = mul(rotationMatrix, uv1);
float4 msPosition;
msPosition.xyz = -fVars3.xyz +
(((input.Normal.xyz * fVars0.www + Acceleration.xyz) * (tmp2 * tmp2)) * 0.5 +
(((fVars0.zzz * input.Normal.xyz) * input.TexCoord0.zzz +
(normalize(-Wind.xyz + input.Position.xyz) * Wind.www + Velocity.xyz)) *
tmp2 +
input.Position.xyz));
msPosition.w = 1;
float4 viewPosition = mul(WorldViewProj[eyeIndex], msPosition);
vsout.Position.xy = positionOffset * ScaleAdjust + viewPosition.xy;
vsout.Position.zw = viewPosition.zw;
float4 color1, color2;
float colorTmp1, colorTmp2;
if (tmp1 > fVars1.z) {
color1 = Color3.xyzw;
color2 = float4(Color3.xyz, 0);
colorTmp1 = fVars1.z;
colorTmp2 = 1;
} else if (tmp1 > fVars1.y) {
color1 = Color2.xyzw;
color2 = Color3.xyzw;
colorTmp1 = fVars1.y;
colorTmp2 = fVars1.z;
} else if (tmp1 > fVars1.x) {
color1 = Color1.xyzw;
color2 = Color2.xyzw;
colorTmp1 = fVars1.x;
colorTmp2 = fVars1.y;
} else {
color1 = float4(Color1.xyz, 0);
color2 = Color1.xyzw;
colorTmp1 = 0;
colorTmp2 = fVars1.x;
}
float colorParam = (tmp1 - colorTmp1) / (colorTmp2 - colorTmp1);
float4 color = lerp(color1, color2, colorParam);
vsout.Color.w = fVars3.w * color.w;
vsout.Color.xyz = color.xyz;
# endif
# ifdef VR
vsout.EyeIndex = eyeIndex;
Stereo::VR_OUTPUT VRout = Stereo::GetVRVSOutput(vsout.Position, eyeIndex);
vsout.Position = VRout.VRPosition;
vsout.ClipDistance.x = VRout.ClipDistance;
vsout.CullDistance.x = VRout.CullDistance;
# endif // VR
return vsout;
}
#endif
typedef VS_OUTPUT PS_INPUT;
struct PS_OUTPUT
{
float4 Color : SV_Target0;
float4 Normal : SV_Target1;
};
#ifdef PSHADER
SamplerState SampSourceTexture : register(s0);
# if defined(GRAYSCALE_TO_COLOR) || defined(GRAYSCALE_TO_ALPHA)
SamplerState SampGrayscaleTexture : register(s1);
# endif
# if defined(ENVCUBE)
SamplerState SampPrecipitationOcclusionTexture : register(s2);
SamplerState SampUnderwaterMask : register(s3);
# endif
Texture2D<float4> TexSourceTexture : register(t0);
# if defined(GRAYSCALE_TO_COLOR) || defined(GRAYSCALE_TO_ALPHA)
Texture2D<float4> TexGrayscaleTexture : register(t1);
# endif
# if defined(ENVCUBE)
Texture2D<float4> TexPrecipitationOcclusionTexture : register(t2);
Texture2D<float4> TexUnderwaterMask : register(t3);
# endif
cbuffer PerGeometry : register(b2)
{
float ColorScale : packoffset(c0);
float3 TextureSize : packoffset(c1);
};
PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
# if !defined(VR)
uint eyeIndex = 0;
# else
uint eyeIndex = input.EyeIndex;
# endif // !VR
# if defined(ENVCUBE)
float2 precipitationOcclusionUV = (input.PrecipitationOcclusionTexCoord.xy * 0.5 + 0.5) * TextureSize.x;
# ifdef VR
precipitationOcclusionUV *= FrameBuffer::DynamicResolutionParams1.x; // only difference in VR
# endif
float precipitationOcclusion = -input.PrecipitationOcclusionTexCoord.z + TexPrecipitationOcclusionTexture.Load(float3(precipitationOcclusionUV, 0)).x;
float2 underwaterMaskUv = TextureSize.yz * input.Position.xy;
float underwaterMask = TexUnderwaterMask.Sample(SampUnderwaterMask, underwaterMaskUv).x;
if (precipitationOcclusion - underwaterMask < 0) {
discard;
}
# endif
float4 sourceColor = TexSourceTexture.Sample(SampSourceTexture, input.TexCoord0);
float4 baseColor = input.Color * sourceColor;
# if defined(GRAYSCALE_TO_COLOR)
float3 grayScaleColor =
TexGrayscaleTexture.Sample(SampGrayscaleTexture, float2(sourceColor.y, input.Color.x)).xyz;
baseColor.xyz = grayScaleColor;
# endif
# if defined(GRAYSCALE_TO_ALPHA)
float grayScaleAlpha =
TexGrayscaleTexture.Sample(SampGrayscaleTexture, float2(sourceColor.w, input.Color.w)).w;
baseColor.w = grayScaleAlpha;
# endif
psout.Color.xyz = ColorScale * baseColor.xyz;
psout.Color.w = baseColor.w;
psout.Normal.w = baseColor.w;
psout.Normal.xyz = float3(0, 1, 0);
return psout;
}
#endif