Lux URP Uber Lit Pass.hlsl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef UNIVERSAL_FORWARD_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_FORWARD_LIT_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. struct Attributes
  5. {
  6. float4 positionOS : POSITION;
  7. float3 normalOS : NORMAL;
  8. float4 tangentOS : TANGENT;
  9. float2 texcoord : TEXCOORD0;
  10. float2 lightmapUV : TEXCOORD1;
  11. UNITY_VERTEX_INPUT_INSTANCE_ID
  12. };
  13. struct Varyings
  14. {
  15. float2 uv : TEXCOORD0;
  16. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  17. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  18. float3 positionWS : TEXCOORD2;
  19. #endif
  20. #if defined (_NORMALMAP) || defined(_PARALLAX) || defined (_BENTNORMAL)
  21. float3 normalWS : TEXCOORD3;
  22. float3 viewDirWS : TEXCOORD4;
  23. float4 tangentWS : TEXCOORD5;
  24. #else
  25. float3 normalWS : TEXCOORD3;
  26. float3 viewDirWS : TEXCOORD4;
  27. #endif
  28. half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
  29. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  30. float4 shadowCoord : TEXCOORD7;
  31. #endif
  32. /* not needed as we use positionCS
  33. #if SHADOWS_SCREEN
  34. #if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
  35. #if defined(_FADING_ON)
  36. float4 screenCoord : TEXCOORD8;
  37. #endif
  38. #endif
  39. #else
  40. #if defined(_FADING_ON)
  41. float4 screenCoord : TEXCOORD8;
  42. #endif
  43. #endif
  44. */
  45. float4 positionCS : SV_POSITION;
  46. UNITY_VERTEX_INPUT_INSTANCE_ID
  47. UNITY_VERTEX_OUTPUT_STEREO
  48. };
  49. void InitializeInputData(Varyings input, float3 bitangentWS, half3 viewDirWS, half3 normalTS, out InputData inputData)
  50. {
  51. inputData = (InputData)0;
  52. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  53. inputData.positionWS = input.positionWS;
  54. #endif
  55. #if defined(_NORMALMAP) || defined(_SAMPLENORMAL)
  56. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangentWS.xyz, input.normalWS.xyz));
  57. #else
  58. inputData.normalWS = input.normalWS.xyz;
  59. #endif
  60. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  61. inputData.viewDirectionWS = viewDirWS;
  62. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  63. inputData.shadowCoord = input.shadowCoord;
  64. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  65. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  66. #else
  67. inputData.shadowCoord = float4(0, 0, 0, 0);
  68. #endif
  69. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  70. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  71. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  72. //inputData.normalizedScreenSpaceUV = input.positionCS.xy;
  73. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  74. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // Vertex and Fragment functions //
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Used in Standard (Physically Based) shader
  80. Varyings LitPassVertexUber(Attributes input)
  81. {
  82. Varyings output = (Varyings)0;
  83. UNITY_SETUP_INSTANCE_ID(input);
  84. UNITY_TRANSFER_INSTANCE_ID(input, output);
  85. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  86. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  87. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  88. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  89. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  90. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  91. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  92. #if defined (_NORMALMAP) || defined(_PARALLAX) || defined (_BENTNORMAL)
  93. output.normalWS = normalInput.normalWS;
  94. float sign = input.tangentOS.w * GetOddNegativeScale();
  95. output.tangentWS = float4(normalInput.tangentWS, sign);
  96. #else
  97. output.normalWS = normalInput.normalWS; //NormalizeNormalPerVertex(normalInput.normalWS);
  98. #endif
  99. output.viewDirWS = viewDirWS;
  100. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  101. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  102. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  103. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  104. output.positionWS = vertexInput.positionWS;
  105. #endif
  106. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  107. output.shadowCoord = GetShadowCoord(vertexInput);
  108. #endif
  109. output.positionCS = vertexInput.positionCS;
  110. return output;
  111. }
  112. #endif