-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathVoxel.shader
More file actions
336 lines (259 loc) · 12.3 KB
/
Copy pathVoxel.shader
File metadata and controls
336 lines (259 loc) · 12.3 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
Shader "Voxels/Voxel"
{
Properties
{
_CoordinateScaling ("Coordinate Scaling", Float) = 0.15
_BlendOffset ("Blend Offset", Range(0, 0.33)) = 0.2
_BlendExponent ("Blend Exponent", Range(0.0, 8.0)) = 2.0
_BlendHeightStrength ("Blend Height Strength", Range(0.01, 0.99)) = 0.5
}
SubShader
{
Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Opaque" "Queue" = "Geometry" }
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial)
half _CoordinateScaling;
half _BlendOffset;
half _BlendExponent;
half _BlendHeightStrength;
CBUFFER_END
ENDHLSL
Pass
{
Name "ForwardLit"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma require geometry
// Material Keywords
#pragma shader_feature_local_fragment _SPECULAR_SETUP
// URP Keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT
#pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
#pragma multi_compile _ SHADOWS_SHADOWMASK
// Unity Keywords
#pragma multi_compile _ DIRLIGHTMAP_COMBINED
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile_fog
#pragma vertex LitPassVertex
#pragma geometry LitPassGeometry
#pragma fragment LitPassFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct VertexPassInput
{
float4 positionOS : POSITION;
float4 normalOS : NORMAL;
uint materialIndex : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
};
struct GeometryPassInput
{
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
half3 normalWS : TEXCOORD1;
uint materialIndex : TEXCOORD2;
DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 3);
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
half4 fogFactorAndVertexLight : TEXCOORD4;
#else
half fogFactor : TEXCOORD4;
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD5;
#endif
};
struct FragmentPassInput
{
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
half3 normalWS : TEXCOORD1;
uint3 materialIndices : TEXCOORD2;
half3 materialWeights : TEXCOORD3;
DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 4);
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
half4 fogFactorAndVertexLight : TEXCOORD5;
#else
half fogFactor : TEXCOORD5;
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD6;
#endif
};
float cosOfHalfSharpFeatureAngle;
TEXTURE2D_ARRAY(materialAlbedoTextures);
TEXTURE2D_ARRAY(materialNormalTextures);
TEXTURE2D_ARRAY(materialMOHSTextures);
SAMPLER(sampler_linear_repeat);
#include "Assets/Shaders/Voxels/Include/Triplanar.hlsl"
GeometryPassInput LitPassVertex(VertexPassInput input)
{
GeometryPassInput output;
VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS.xyz);
VertexNormalInputs normalInputs = GetVertexNormalInputs(input.normalOS.xyz);
output.positionCS = positionInputs.positionCS;
output.positionWS = positionInputs.positionWS;
half3 vertexLight = VertexLighting(positionInputs.positionWS, normalInputs.normalWS);
half fogFactor = ComputeFogFactor(positionInputs.positionCS.z);
output.normalWS = NormalizeNormalPerVertex(normalInputs.normalWS);
output.materialIndex = input.materialIndex;
OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
#else
output.fogFactor = fogFactor;
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = GetShadowCoord(positionInputs);
#endif
return output;
}
[maxvertexcount(3)]
void LitPassGeometry(triangle GeometryPassInput inputs[3], inout TriangleStream<FragmentPassInput> outputStream)
{
const half3x3 half3x3Identity = half3x3
(
1.0h, 0.0h, 0.0h,
0.0h, 1.0h, 0.0h,
0.0h, 0.0h, 1.0h
);
uint3 materialIndices = float3(inputs[0].materialIndex, inputs[1].materialIndex, inputs[2].materialIndex);
float3 faceNormalWS = normalize(cross(inputs[1].positionWS - inputs[0].positionWS, inputs[2].positionWS - inputs[0].positionWS));
for (uint index = 0; index < 3; index++)
{
GeometryPassInput input = inputs[index];
input.normalWS = dot(input.normalWS, faceNormalWS) <= cosOfHalfSharpFeatureAngle ? faceNormalWS : input.normalWS;
FragmentPassInput output;
output.positionCS = input.positionCS;
output.positionWS = input.positionWS;
output.normalWS = input.normalWS;
output.materialIndices = materialIndices;
output.materialWeights = half3x3Identity[index];
#if defined(LIGHTMAP_ON)
output.lightmapUV = input.lightmapUV;
#else
output.vertexSH = input.vertexSH;
#endif
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
output.fogFactorAndVertexLight = input.fogFactorAndVertexLight;
#else
output.fogFactor = input.fogFactor;
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = input.shadowCoord;
#endif
outputStream.Append(output);
}
outputStream.RestartStrip();
}
float3 GetMaterialBlendWeights(FragmentPassInput input, half3 heights)
{
float3 materialWeights = abs(input.materialWeights);
materialWeights = saturate(materialWeights - _BlendOffset);
materialWeights *= abs(lerp(1.0h, heights, _BlendHeightStrength));
materialWeights = pow(materialWeights, _BlendExponent);
materialWeights /= dot(materialWeights, 1.0h);
return materialWeights;
}
SurfaceData CreateSurfaceData(FragmentPassInput input)
{
SurfaceData surfaceData = (SurfaceData)0;
TriplanarData triplanarDatas[3];
for (uint index = 0; index < 3; index++)
{
triplanarDatas[index] = ApplyTriplanarTexturing
(
input.positionWS,
input.normalWS,
materialAlbedoTextures,
materialNormalTextures,
materialMOHSTextures,
sampler_linear_repeat,
input.materialIndices[index]
);
}
half3 heights = half3(triplanarDatas[0].height, triplanarDatas[1].height, triplanarDatas[2].height);
float3 materialWeights = GetMaterialBlendWeights(input, heights);
for (index = 0; index < 3; index++)
{
surfaceData.albedo += materialWeights[index] * triplanarDatas[index].albedo.rgb;
surfaceData.alpha += materialWeights[index] * triplanarDatas[index].albedo.a;
// Use SurfaceData's normalTS field to store our normalWS.
surfaceData.normalTS += materialWeights[index] * triplanarDatas[index].normalWS;
surfaceData.metallic += materialWeights[index] * triplanarDatas[index].metallic;
surfaceData.occlusion += materialWeights[index] * triplanarDatas[index].occlusion;
surfaceData.smoothness += materialWeights[index] * triplanarDatas[index].smoothness;
}
return surfaceData;
}
InputData CreateInputData(FragmentPassInput input, half3 normalWS)
{
InputData inputData = (InputData)0;
inputData.positionWS = input.positionWS;
inputData.normalWS = NormalizeNormalPerPixel(normalWS);
inputData.viewDirectionWS = SafeNormalize(GetWorldSpaceNormalizeViewDir(inputData.positionWS));
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
inputData.shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
#else
inputData.shadowCoord = float4(0.0f, 0.0f, 0.0f, 0.0f);
#endif
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
inputData.fogCoord = input.fogFactorAndVertexLight.x;
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
#else
inputData.fogCoord = input.fogFactor;
inputData.vertexLighting = half3(0.0h, 0.0h, 0.0h);
#endif
inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
return inputData;
}
half4 LitPassFragment(FragmentPassInput input) : SV_Target
{
SurfaceData surfaceData = CreateSurfaceData(input);
InputData inputData = CreateInputData(input, surfaceData.normalTS);
half4 color = UniversalFragmentPBR(inputData, surfaceData);
color.rgb = MixFog(color.rgb, inputData.fogCoord);
return color;
}
ENDHLSL
}
Pass
{
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
HLSLPROGRAM
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
#include "Assets/Shaders/Voxels/Include/ShadowCasterPass.hlsl"
ENDHLSL
}
Pass
{
Name "DepthOnly"
Tags { "LightMode" = "DepthOnly" }
HLSLPROGRAM
#pragma vertex DepthOnlyVertex
#pragma fragment DepthOnlyFragment
#include "Assets/Shaders/Voxels/Include/DepthOnlyPass.hlsl"
ENDHLSL
}
Pass
{
Name "DepthNormals"
Tags { "LightMode" = "DepthNormals" }
HLSLPROGRAM
#pragma vertex DepthNormalsVertex
#pragma fragment DepthNormalsFragment
#include "Assets/Shaders/Voxels/Include/DepthNormalsPass.hlsl"
ENDHLSL
}
}
}