Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Varyings.hlsl 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if defined(FEATURES_GRAPH_VERTEX)
  13. #if defined(HAVE_VFX_MODIFICATION)
  14. VertexDescription BuildVertexDescription(Attributes input, AttributesElement element, out GraphProperties properties)
  15. {
  16. ZERO_INITIALIZE(GraphProperties, properties);
  17. // Fetch the vertex graph properties for the particle instance.
  18. GetElementVertexProperties(element, properties);
  19. // Evaluate Vertex Graph
  20. VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
  21. VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs, properties);
  22. return vertexDescription;
  23. }
  24. #else
  25. VertexDescription BuildVertexDescription(Attributes input)
  26. {
  27. // Evaluate Vertex Graph
  28. VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
  29. VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs);
  30. return vertexDescription;
  31. }
  32. #endif
  33. #endif
  34. #if (SHADERPASS == SHADERPASS_MOTION_VECTORS)
  35. // We want to gather some internal data from the BuildVaryings call to
  36. // avoid rereading and recalculating these values again in the ShaderGraph motion vector pass
  37. struct MotionVectorPassOutput
  38. {
  39. float3 positionOS;
  40. float3 positionWS;
  41. #if defined(FEATURES_GRAPH_VERTEX_MOTION_VECTOR_OUTPUT)
  42. float3 motionVector;
  43. #endif
  44. #if defined(HAVE_VFX_MODIFICATION)
  45. float3 vfxParticlePositionOS;
  46. AttributesElement vfxElementAttributes;
  47. GraphProperties vfxGraphProperties;
  48. #endif
  49. };
  50. #endif
  51. Varyings BuildVaryings(Attributes input
  52. #if (SHADERPASS == SHADERPASS_MOTION_VECTORS)
  53. , inout MotionVectorPassOutput motionVectorOutput
  54. #endif
  55. )
  56. {
  57. Varyings output = (Varyings)0;
  58. UNITY_SETUP_INSTANCE_ID(input);
  59. UNITY_TRANSFER_INSTANCE_ID(input, output);
  60. #if defined(HAVE_VFX_MODIFICATION)
  61. AttributesElement element;
  62. ZERO_INITIALIZE(AttributesElement, element);
  63. if (!GetMeshAndElementIndex(input, element))
  64. return output; // Culled index.
  65. #if UNITY_ANY_INSTANCING_ENABLED
  66. output.instanceID = input.instanceID; //Transfer instanceID again because we modify it in GetMeshAndElementIndex
  67. #endif
  68. if (!GetInterpolatorAndElementData(input, output, element))
  69. return output; // Dead particle.
  70. SetupVFXMatrices(element, output);
  71. #if (SHADERPASS == SHADERPASS_MOTION_VECTORS)
  72. motionVectorOutput.vfxParticlePositionOS = input.positionOS;
  73. #endif
  74. #endif
  75. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  76. #if defined(FEATURES_GRAPH_VERTEX)
  77. #if defined(HAVE_VFX_MODIFICATION)
  78. GraphProperties properties;
  79. VertexDescription vertexDescription = BuildVertexDescription(input, element, properties);
  80. #else
  81. VertexDescription vertexDescription = BuildVertexDescription(input);
  82. #endif
  83. #if defined(CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC)
  84. CustomInterpolatorPassThroughFunc(output, vertexDescription);
  85. #endif
  86. // Assign modified vertex attributes
  87. input.positionOS = vertexDescription.Position;
  88. #if defined(ATTRIBUTES_NEED_NORMAL) && defined(FEATURES_GRAPH_VERTEX_NORMAL_OUTPUT)
  89. input.normalOS = vertexDescription.Normal;
  90. #endif //FEATURES_GRAPH_NORMAL
  91. #if defined(ATTRIBUTES_NEED_TANGENT) && defined(FEATURES_GRAPH_VERTEX_TANGENT_OUTPUT)
  92. input.tangentOS.xyz = vertexDescription.Tangent.xyz;
  93. #endif //FEATURES GRAPH TANGENT
  94. #endif //FEATURES_GRAPH_VERTEX
  95. // TODO: Avoid path via VertexPositionInputs (Universal)
  96. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  97. // Returns the camera relative position (if enabled)
  98. float3 positionWS = TransformObjectToWorld(input.positionOS);
  99. #if (SHADERPASS == SHADERPASS_MOTION_VECTORS)
  100. motionVectorOutput.positionOS = input.positionOS;
  101. motionVectorOutput.positionWS = positionWS;
  102. #if defined(FEATURES_GRAPH_VERTEX_MOTION_VECTOR_OUTPUT)
  103. motionVectorOutput.motionVector = vertexDescription.MotionVector;
  104. #endif
  105. #if defined(HAVE_VFX_MODIFICATION)
  106. motionVectorOutput.vfxElementAttributes = element;
  107. motionVectorOutput.vfxGraphProperties = properties;
  108. #endif
  109. #endif
  110. #ifdef ATTRIBUTES_NEED_NORMAL
  111. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  112. #else
  113. // Required to compile ApplyVertexModification that doesn't use normal.
  114. float3 normalWS = float3(0.0, 0.0, 0.0);
  115. #endif
  116. #ifdef ATTRIBUTES_NEED_TANGENT
  117. float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
  118. #endif
  119. // TODO: Change to inline ifdef
  120. // Do vertex modification in camera relative space (if enabled)
  121. #if defined(HAVE_VERTEX_MODIFICATION)
  122. ApplyVertexModification(input, normalWS, positionWS, _TimeParameters.xyz);
  123. #endif
  124. #ifdef VARYINGS_NEED_POSITION_WS
  125. output.positionWS = positionWS;
  126. #endif
  127. #ifdef VARYINGS_NEED_NORMAL_WS
  128. output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal()
  129. #endif
  130. #ifdef VARYINGS_NEED_TANGENT_WS
  131. output.tangentWS = tangentWS; // normalized in TransformObjectToWorldDir()
  132. #endif
  133. #if (SHADERPASS == SHADERPASS_SHADOWCASTER)
  134. // Define shadow pass specific clip position for Universal
  135. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  136. float3 lightDirectionWS = normalize(_LightPosition - positionWS);
  137. #else
  138. float3 lightDirectionWS = _LightDirection;
  139. #endif
  140. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
  141. #if UNITY_REVERSED_Z
  142. output.positionCS.z = min(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
  143. #else
  144. output.positionCS.z = max(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
  145. #endif
  146. #elif (SHADERPASS == SHADERPASS_META)
  147. output.positionCS = UnityMetaVertexPosition(input.positionOS, input.uv1, input.uv2, unity_LightmapST, unity_DynamicLightmapST);
  148. #else
  149. output.positionCS = TransformWorldToHClip(positionWS);
  150. #endif
  151. #if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
  152. output.texCoord0 = input.uv0;
  153. #endif
  154. #ifdef EDITOR_VISUALIZATION
  155. float2 VizUV = 0;
  156. float4 LightCoord = 0;
  157. UnityEditorVizData(input.positionOS, input.uv0, input.uv1, input.uv2, VizUV, LightCoord);
  158. #endif
  159. #if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1)
  160. #ifdef EDITOR_VISUALIZATION
  161. output.texCoord1 = float4(VizUV, 0, 0);
  162. #else
  163. output.texCoord1 = input.uv1;
  164. #endif
  165. #endif
  166. #if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2)
  167. #ifdef EDITOR_VISUALIZATION
  168. output.texCoord2 = LightCoord;
  169. #else
  170. output.texCoord2 = input.uv2;
  171. #endif
  172. #endif
  173. #if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3)
  174. output.texCoord3 = input.uv3;
  175. #endif
  176. #if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR)
  177. output.color = input.color;
  178. #endif
  179. #if (defined(VARYINGS_NEED_INSTANCEID) || defined(VARYINGS_DS_NEED_INSTANCEID)) && !UNITY_ANY_INSTANCING_ENABLED
  180. output.instanceID = input.instanceID;
  181. #endif
  182. #ifdef VARYINGS_NEED_SCREENPOSITION
  183. output.screenPosition = vertexInput.positionNDC;
  184. #endif
  185. #if (SHADERPASS == SHADERPASS_FORWARD) || (SHADERPASS == SHADERPASS_GBUFFER)
  186. OUTPUT_LIGHTMAP_UV(input.uv1, unity_LightmapST, output.staticLightmapUV);
  187. #if defined(DYNAMICLIGHTMAP_ON)
  188. output.dynamicLightmapUV.xy = input.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  189. #endif
  190. OUTPUT_SH4(vertexInput.positionWS, normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.sh, output.probeOcclusion);
  191. #endif
  192. #ifdef VARYINGS_NEED_FOG_AND_VERTEX_LIGHT
  193. half fogFactor = 0;
  194. #if !defined(_FOG_FRAGMENT)
  195. fogFactor = ComputeFogFactor(output.positionCS.z);
  196. #endif
  197. half3 vertexLight = VertexLighting(positionWS, normalWS);
  198. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  199. #endif
  200. #if defined(VARYINGS_NEED_SHADOW_COORD) && defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  201. output.shadowCoord = GetShadowCoord(vertexInput);
  202. #endif
  203. #if defined(VARYINGS_NEED_SIX_WAY_DIFFUSE_GI_DATA)
  204. GatherDiffuseGIData(vertexInput.positionWS, normalWS.xyz, tangentWS.xyz, output.diffuseGIData0, output.diffuseGIData1, output.diffuseGIData2);
  205. #endif
  206. return output;
  207. }
  208. SurfaceDescription BuildSurfaceDescription(Varyings varyings)
  209. {
  210. SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(varyings);
  211. #if defined(HAVE_VFX_MODIFICATION)
  212. GraphProperties properties;
  213. ZERO_INITIALIZE(GraphProperties, properties);
  214. GetElementPixelProperties(surfaceDescriptionInputs, properties);
  215. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs, properties);
  216. #else
  217. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
  218. #endif
  219. return surfaceDescription;
  220. }