Repository navigation
Expand file tree
/
Copy pathVaryings.hlsl
More file actions
231 lines (198 loc) · 8.28 KB
/
Copy pathVaryings.hlsl
File metadata and controls
231 lines (198 loc) · 8.28 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
#if (SHADERPASS == SHADERPASS_SHADOWCASTER)
// Shadow Casting Light geometric parameters. These variables are used when applying the shadow Normal Bias and are set by UnityEngine.Rendering.Universal.ShadowUtils.SetupShadowCasterConstantBuffer in com.unity.render-pipelines.universal/Runtime/ShadowUtils.cs
// For Directional lights, _LightDirection is used when applying shadow Normal Bias.
// For Spot lights and Point lights, _LightPosition is used to compute the actual light direction because it is different at each shadow caster geometry vertex.
#ifndef HAVE_VFX_MODIFICATION
float3 _LightDirection;
#else
//_LightDirection is already defined in com.unity.render-pipelines.universal\Runtime\VFXGraph\Shaders\VFXCommon.hlsl
#endif
float3 _LightPosition;
#endif
#if defined(FEATURES_GRAPH_VERTEX)
#if defined(HAVE_VFX_MODIFICATION)
VertexDescription BuildVertexDescription(Attributes input, AttributesElement element)
{
GraphProperties properties;
ZERO_INITIALIZE(GraphProperties, properties);
// Fetch the vertex graph properties for the particle instance.
GetElementVertexProperties(element, properties);
// Evaluate Vertex Graph
VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs, properties);
return vertexDescription;
}
#else
VertexDescription BuildVertexDescription(Attributes input)
{
// Evaluate Vertex Graph
VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs);
return vertexDescription;
}
#endif
#endif
Varyings BuildVaryings(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
#if defined(HAVE_VFX_MODIFICATION)
AttributesElement element;
ZERO_INITIALIZE(AttributesElement, element);
if (!GetMeshAndElementIndex(input, element))
return output; // Culled index.
if (!GetInterpolatorAndElementData(output, element))
return output; // Dead particle.
SetupVFXMatrices(element, output);
#endif
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
#if defined(FEATURES_GRAPH_VERTEX)
#if defined(HAVE_VFX_MODIFICATION)
VertexDescription vertexDescription = BuildVertexDescription(input, element);
#else
VertexDescription vertexDescription = BuildVertexDescription(input);
#endif
#if defined(CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC)
CustomInterpolatorPassThroughFunc(output, vertexDescription);
#endif
// Assign modified vertex attributes
input.positionOS = vertexDescription.Position;
#if defined(VARYINGS_NEED_NORMAL_WS)
input.normalOS = vertexDescription.Normal;
#endif //FEATURES_GRAPH_NORMAL
#if defined(VARYINGS_NEED_TANGENT_WS)
input.tangentOS.xyz = vertexDescription.Tangent.xyz;
#endif //FEATURES GRAPH TANGENT
#endif //FEATURES_GRAPH_VERTEX
// TODO: Avoid path via VertexPositionInputs (Universal)
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
// Returns the camera relative position (if enabled)
float3 positionWS = TransformObjectToWorld(input.positionOS);
#ifdef ATTRIBUTES_NEED_NORMAL
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
#else
// Required to compile ApplyVertexModification that doesn't use normal.
float3 normalWS = float3(0.0, 0.0, 0.0);
#endif
#ifdef ATTRIBUTES_NEED_TANGENT
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
#endif
// TODO: Change to inline ifdef
// Do vertex modification in camera relative space (if enabled)
#if defined(HAVE_VERTEX_MODIFICATION)
ApplyVertexModification(input, normalWS, positionWS, _TimeParameters.xyz);
#endif
#ifdef VARYINGS_NEED_POSITION_WS
output.positionWS = positionWS;
#endif
#ifdef VARYINGS_NEED_NORMAL_WS
output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal()
#endif
#ifdef VARYINGS_NEED_TANGENT_WS
output.tangentWS = tangentWS; // normalized in TransformObjectToWorldDir()
#endif
#if (SHADERPASS == SHADERPASS_SHADOWCASTER)
// Define shadow pass specific clip position for Universal
#if _CASTING_PUNCTUAL_LIGHT_SHADOW
float3 lightDirectionWS = normalize(_LightPosition - positionWS);
#else
float3 lightDirectionWS = _LightDirection;
#endif
output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
#if UNITY_REVERSED_Z
output.positionCS.z = min(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
#else
output.positionCS.z = max(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
#endif
#elif (SHADERPASS == SHADERPASS_META)
output.positionCS = UnityMetaVertexPosition(input.positionOS, input.uv1, input.uv2, unity_LightmapST, unity_DynamicLightmapST);
#else
output.positionCS = TransformWorldToHClip(positionWS);
#endif
#if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
output.texCoord0 = input.uv0;
#endif
#ifdef EDITOR_VISUALIZATION
float2 VizUV = 0;
float4 LightCoord = 0;
UnityEditorVizData(input.positionOS, input.uv0, input.uv1, input.uv2, VizUV, LightCoord);
#endif
#if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1)
#ifdef EDITOR_VISUALIZATION
output.texCoord1 = float4(VizUV, 0, 0);
#else
output.texCoord1 = input.uv1;
#endif
#endif
#if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2)
#ifdef EDITOR_VISUALIZATION
output.texCoord2 = LightCoord;
#else
output.texCoord2 = input.uv2;
#endif
#endif
#if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3)
output.texCoord3 = input.uv3;
#endif
#if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR)
output.color = input.color;
#endif
#ifdef VARYINGS_NEED_VIEWDIRECTION_WS
// Need the unnormalized direction here as otherwise interpolation is incorrect.
// It is normalized after interpolation in the fragment shader.
output.viewDirectionWS = GetWorldSpaceViewDir(positionWS);
#endif
#ifdef VARYINGS_NEED_SCREENPOSITION
output.screenPosition = vertexInput.positionNDC;
#endif
#if (SHADERPASS == SHADERPASS_FORWARD) || (SHADERPASS == SHADERPASS_GBUFFER)
OUTPUT_LIGHTMAP_UV(input.uv1, unity_LightmapST, output.staticLightmapUV);
#if defined(DYNAMICLIGHTMAP_ON)
output.dynamicLightmapUV.xy = input.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
#endif
OUTPUT_SH(normalWS, output.sh);
#endif
#ifdef VARYINGS_NEED_FOG_AND_VERTEX_LIGHT
half fogFactor = 0;
#if !defined(_FOG_FRAGMENT)
fogFactor = ComputeFogFactor(output.positionCS.z);
#endif
half3 vertexLight = VertexLighting(positionWS, normalWS);
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
#endif
#ifdef VARYINGS_NEED_CURRENT_POSITION_CS
float3 curWS = TransformObjectToWorld(input.positionOS.xyz);
output.curPositionCS = TransformWorldToHClip(curWS);
#endif
#ifdef VARYINGS_NEED_PREVIOUS_POSITION_CS
if (unity_MotionVectorsParams.y == 0.0)
{
output.prevPositionCS = output.curPositionCS;
}
else
{
bool hasDeformation = unity_MotionVectorsParams.x > 0.0;
float3 effectivePositionOS = (hasDeformation ? input.uv4.xyz : input.positionOS.xyz);
float3 previousWS = TransformPreviousObjectToWorld(effectivePositionOS);
output.prevPositionCS = TransformWorldToPrevHClip(previousWS);
}
#endif
#if defined(VARYINGS_NEED_SHADOW_COORD) && defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = GetShadowCoord(vertexInput);
#endif
return output;
}
SurfaceDescription BuildSurfaceDescription(Varyings varyings)
{
SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(varyings);
#if defined(HAVE_VFX_MODIFICATION)
GraphProperties properties;
ZERO_INITIALIZE(GraphProperties, properties);
GetElementPixelProperties(surfaceDescriptionInputs, properties);
SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs, properties);
#else
SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
#endif
return surfaceDescription;
}