Varyings.hlsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #if defined(SHADERPASS_SHADOWCASTER)
  2. float3 _LightDirection;
  3. #endif
  4. Varyings BuildVaryings(Attributes input)
  5. {
  6. Varyings output = (Varyings)0;
  7. UNITY_SETUP_INSTANCE_ID(input);
  8. UNITY_TRANSFER_INSTANCE_ID(input, output);
  9. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  10. #if defined(FEATURES_GRAPH_VERTEX)
  11. // Evaluate Vertex Graph
  12. VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input);
  13. VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs);
  14. // Assign modified vertex attributes
  15. input.positionOS = vertexDescription.VertexPosition;
  16. #if defined(VARYINGS_NEED_NORMAL_WS)
  17. input.normalOS = vertexDescription.VertexNormal;
  18. #endif //FEATURES_GRAPH_NORMAL
  19. #if defined(VARYINGS_NEED_TANGENT_WS)
  20. input.tangentOS.xyz = vertexDescription.VertexTangent.xyz;
  21. #endif //FEATURES GRAPH TANGENT
  22. #endif //FEATURES_GRAPH_VERTEX
  23. // TODO: Avoid path via VertexPositionInputs (Universal)
  24. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  25. // Returns the camera relative position (if enabled)
  26. float3 positionWS = TransformObjectToWorld(input.positionOS);
  27. #ifdef ATTRIBUTES_NEED_NORMAL
  28. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  29. #else
  30. // Required to compile ApplyVertexModification that doesn't use normal.
  31. float3 normalWS = float3(0.0, 0.0, 0.0);
  32. #endif
  33. #ifdef ATTRIBUTES_NEED_TANGENT
  34. float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
  35. #endif
  36. // TODO: Change to inline ifdef
  37. // Do vertex modification in camera relative space (if enabled)
  38. #if defined(HAVE_VERTEX_MODIFICATION)
  39. ApplyVertexModification(input, normalWS, positionWS, _TimeParameters.xyz);
  40. #endif
  41. #ifdef VARYINGS_NEED_POSITION_WS
  42. output.positionWS = positionWS;
  43. #endif
  44. #ifdef VARYINGS_NEED_NORMAL_WS
  45. output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal()
  46. #endif
  47. #ifdef VARYINGS_NEED_TANGENT_WS
  48. output.tangentWS = tangentWS; // normalized in TransformObjectToWorldDir()
  49. #endif
  50. #if defined(SHADERPASS_SHADOWCASTER)
  51. // Define shadow pass specific clip position for Universal
  52. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  53. #if UNITY_REVERSED_Z
  54. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  55. #else
  56. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  57. #endif
  58. #elif defined(SHADERPASS_META)
  59. output.positionCS = MetaVertexPosition(float4(input.positionOS, 0), input.uv1, input.uv2, unity_LightmapST, unity_DynamicLightmapST);
  60. #else
  61. output.positionCS = TransformWorldToHClip(positionWS);
  62. #endif
  63. #if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
  64. output.texCoord0 = input.uv0;
  65. #endif
  66. #if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1)
  67. output.texCoord1 = input.uv1;
  68. #endif
  69. #if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2)
  70. output.texCoord2 = input.uv2;
  71. #endif
  72. #if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3)
  73. output.texCoord3 = input.uv3;
  74. #endif
  75. #if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR)
  76. output.color = input.color;
  77. #endif
  78. #ifdef VARYINGS_NEED_VIEWDIRECTION_WS
  79. output.viewDirectionWS = _WorldSpaceCameraPos.xyz - positionWS;
  80. #endif
  81. #ifdef VARYINGS_NEED_SCREENPOSITION
  82. output.screenPosition = ComputeScreenPos(output.positionCS, _ProjectionParams.x);
  83. #endif
  84. #if defined(SHADERPASS_FORWARD)
  85. OUTPUT_LIGHTMAP_UV(input.uv1, unity_LightmapST, output.lightmapUV);
  86. OUTPUT_SH(normalWS, output.sh);
  87. #endif
  88. #ifdef VARYINGS_NEED_FOG_AND_VERTEX_LIGHT
  89. half3 vertexLight = VertexLighting(positionWS, normalWS);
  90. half fogFactor = ComputeFogFactor(output.positionCS.z);
  91. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  92. #endif
  93. #ifdef _MAIN_LIGHT_SHADOWS
  94. output.shadowCoord = GetShadowCoord(vertexInput);
  95. #endif
  96. return output;
  97. }