Varyings.hlsl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #if (SHADERPASS == SHADERPASS_SHADOWCASTER)
  2. // 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
  3. // For Directional lights, _LightDirection is used when applying shadow Normal Bias.
  4. // 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.
  5. #ifndef HAVE_VFX_MODIFICATION
  6. float3 _LightDirection;
  7. #else
  8. //_LightDirection is already defined in com.unity.render-pipelines.universal\Runtime\VFXGraph\Shaders\VFXCommon.hlsl
  9. #endif
  10. float3 _LightPosition;
  11. #endif
  12. #ifdef VARYINGS_NEED_PREVIOUS_POSITION_CS
  13. bool IsSmoothRotation(float3 prevAxis1, float3 prevAxis2, float3 currAxis1, float3 currAxis2)
  14. {
  15. float angleThreshold = 0.984f; // cos(10 degrees)
  16. float2 angleDot = float2(dot(normalize(prevAxis1), normalize(currAxis1)), dot(normalize(prevAxis2), normalize(currAxis2)));
  17. return all(angleDot > angleThreshold);
  18. }
  19. #endif
  20. #if defined(FEATURES_GRAPH_VERTEX)
  21. #if defined(HAVE_VFX_MODIFICATION)
  22. VertexDescription BuildVertexDescription(Attributes input, AttributesElement element)
  23. {
  24. GraphProperties properties;
  25. ZERO_INITIALIZE(GraphProperties, properties);
  26. // Fetch the vertex graph properties for the particle instance.
  27. GetElementVertexProperties(element, properties);
  28. // Evaluate Vertex Graph
  29. VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
  30. VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs, properties);
  31. return vertexDescription;
  32. }
  33. #else
  34. VertexDescription BuildVertexDescription(Attributes input)
  35. {
  36. // Evaluate Vertex Graph
  37. VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
  38. VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs);
  39. return vertexDescription;
  40. }
  41. #endif
  42. #endif
  43. Varyings BuildVaryings(Attributes input)
  44. {
  45. Varyings output = (Varyings)0;
  46. #if defined(HAVE_VFX_MODIFICATION)
  47. AttributesElement element;
  48. ZERO_INITIALIZE(AttributesElement, element);
  49. if (!GetMeshAndElementIndex(input, element))
  50. return output; // Culled index.
  51. if (!GetInterpolatorAndElementData(output, element))
  52. return output; // Dead particle.
  53. SetupVFXMatrices(element, output);
  54. #endif
  55. UNITY_SETUP_INSTANCE_ID(input);
  56. UNITY_TRANSFER_INSTANCE_ID(input, output);
  57. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  58. #if defined(FEATURES_GRAPH_VERTEX)
  59. #if defined(HAVE_VFX_MODIFICATION)
  60. VertexDescription vertexDescription = BuildVertexDescription(input, element);
  61. #else
  62. VertexDescription vertexDescription = BuildVertexDescription(input);
  63. #endif
  64. #if defined(CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC)
  65. CustomInterpolatorPassThroughFunc(output, vertexDescription);
  66. #endif
  67. // Assign modified vertex attributes
  68. input.positionOS = vertexDescription.Position;
  69. #if defined(VARYINGS_NEED_NORMAL_WS)
  70. input.normalOS = vertexDescription.Normal;
  71. #endif //FEATURES_GRAPH_NORMAL
  72. #if defined(VARYINGS_NEED_TANGENT_WS)
  73. input.tangentOS.xyz = vertexDescription.Tangent.xyz;
  74. #endif //FEATURES GRAPH TANGENT
  75. #endif //FEATURES_GRAPH_VERTEX
  76. // TODO: Avoid path via VertexPositionInputs (Universal)
  77. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  78. // Returns the camera relative position (if enabled)
  79. float3 positionWS = TransformObjectToWorld(input.positionOS);
  80. #ifdef ATTRIBUTES_NEED_NORMAL
  81. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  82. #else
  83. // Required to compile ApplyVertexModification that doesn't use normal.
  84. float3 normalWS = float3(0.0, 0.0, 0.0);
  85. #endif
  86. #ifdef ATTRIBUTES_NEED_TANGENT
  87. float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
  88. #endif
  89. // TODO: Change to inline ifdef
  90. // Do vertex modification in camera relative space (if enabled)
  91. #if defined(HAVE_VERTEX_MODIFICATION)
  92. ApplyVertexModification(input, normalWS, positionWS, _TimeParameters.xyz);
  93. #endif
  94. #ifdef VARYINGS_NEED_POSITION_WS
  95. output.positionWS = positionWS;
  96. #endif
  97. #ifdef VARYINGS_NEED_NORMAL_WS
  98. output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal()
  99. #endif
  100. #ifdef VARYINGS_NEED_TANGENT_WS
  101. output.tangentWS = tangentWS; // normalized in TransformObjectToWorldDir()
  102. #endif
  103. #if (SHADERPASS == SHADERPASS_SHADOWCASTER)
  104. // Define shadow pass specific clip position for Universal
  105. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  106. float3 lightDirectionWS = normalize(_LightPosition - positionWS);
  107. #else
  108. float3 lightDirectionWS = _LightDirection;
  109. #endif
  110. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
  111. #if UNITY_REVERSED_Z
  112. output.positionCS.z = min(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
  113. #else
  114. output.positionCS.z = max(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
  115. #endif
  116. #elif (SHADERPASS == SHADERPASS_META)
  117. output.positionCS = UnityMetaVertexPosition(input.positionOS, input.uv1, input.uv2, unity_LightmapST, unity_DynamicLightmapST);
  118. #else
  119. output.positionCS = TransformWorldToHClip(positionWS);
  120. #endif
  121. #if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
  122. output.texCoord0 = input.uv0;
  123. #endif
  124. #ifdef EDITOR_VISUALIZATION
  125. float2 VizUV = 0;
  126. float4 LightCoord = 0;
  127. UnityEditorVizData(input.positionOS, input.uv0, input.uv1, input.uv2, VizUV, LightCoord);
  128. #endif
  129. #if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1)
  130. #ifdef EDITOR_VISUALIZATION
  131. output.texCoord1 = float4(VizUV, 0, 0);
  132. #else
  133. output.texCoord1 = input.uv1;
  134. #endif
  135. #endif
  136. #if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2)
  137. #ifdef EDITOR_VISUALIZATION
  138. output.texCoord2 = LightCoord;
  139. #else
  140. output.texCoord2 = input.uv2;
  141. #endif
  142. #endif
  143. #if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3)
  144. output.texCoord3 = input.uv3;
  145. #endif
  146. #if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR)
  147. output.color = input.color;
  148. #endif
  149. #ifdef VARYINGS_NEED_VIEWDIRECTION_WS
  150. // Need the unnormalized direction here as otherwise interpolation is incorrect.
  151. // It is normalized after interpolation in the fragment shader.
  152. output.viewDirectionWS = GetWorldSpaceViewDir(positionWS);
  153. #endif
  154. #ifdef VARYINGS_NEED_SCREENPOSITION
  155. output.screenPosition = vertexInput.positionNDC;
  156. #endif
  157. #if (SHADERPASS == SHADERPASS_FORWARD) || (SHADERPASS == SHADERPASS_GBUFFER)
  158. OUTPUT_LIGHTMAP_UV(input.uv1, unity_LightmapST, output.staticLightmapUV);
  159. #if defined(DYNAMICLIGHTMAP_ON)
  160. output.dynamicLightmapUV.xy = input.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  161. #endif
  162. OUTPUT_SH(normalWS, output.sh);
  163. #endif
  164. #ifdef VARYINGS_NEED_FOG_AND_VERTEX_LIGHT
  165. half fogFactor = 0;
  166. #if !defined(_FOG_FRAGMENT)
  167. fogFactor = ComputeFogFactor(output.positionCS.z);
  168. #endif
  169. half3 vertexLight = VertexLighting(positionWS, normalWS);
  170. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  171. #endif
  172. #ifdef VARYINGS_NEED_CURRENT_POSITION_CS
  173. float3 curWS = TransformObjectToWorld(input.positionOS.xyz);
  174. output.curPositionCS = TransformWorldToHClip(curWS);
  175. #endif
  176. #ifdef VARYINGS_NEED_PREVIOUS_POSITION_CS
  177. if (unity_MotionVectorsParams.y == 0.0)
  178. {
  179. output.prevPositionCS = float4(0.0, 0.0, 0.0, 1.0);
  180. }
  181. else
  182. {
  183. bool hasDeformation = unity_MotionVectorsParams.x > 0.0;
  184. float3 effectivePositionOS = (hasDeformation ? input.uv4.xyz : input.positionOS.xyz);
  185. float3 previousWS = TransformPreviousObjectToWorld(effectivePositionOS);
  186. float4x4 previousOTW = GetPrevObjectToWorldMatrix();
  187. float4x4 currentOTW = GetObjectToWorldMatrix();
  188. if (!IsSmoothRotation(previousOTW._11_21_31, previousOTW._12_22_32, currentOTW._11_21_31, currentOTW._12_22_32))
  189. {
  190. output.prevPositionCS = output.curPositionCS;
  191. }
  192. else
  193. {
  194. output.prevPositionCS = TransformWorldToPrevHClip(previousWS);
  195. }
  196. }
  197. #endif
  198. #if defined(VARYINGS_NEED_SHADOW_COORD) && defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  199. output.shadowCoord = GetShadowCoord(vertexInput);
  200. #endif
  201. return output;
  202. }
  203. SurfaceDescription BuildSurfaceDescription(Varyings varyings)
  204. {
  205. SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(varyings);
  206. #if defined(HAVE_VFX_MODIFICATION)
  207. GraphProperties properties;
  208. ZERO_INITIALIZE(GraphProperties, properties);
  209. GetElementPixelProperties(surfaceDescriptionInputs, properties);
  210. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs, properties);
  211. #else
  212. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
  213. #endif
  214. return surfaceDescription;
  215. }