BakedLitForwardPass.hlsl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  2. struct Attributes
  3. {
  4. float4 positionOS : POSITION;
  5. float2 uv : TEXCOORD0;
  6. float2 staticLightmapUV : TEXCOORD1;
  7. float3 normalOS : NORMAL;
  8. float4 tangentOS : TANGENT;
  9. UNITY_VERTEX_INPUT_INSTANCE_ID
  10. };
  11. struct Varyings
  12. {
  13. float4 positionCS : SV_POSITION;
  14. float3 uv0AndFogCoord : TEXCOORD0; // xy: uv0, z: fogCoord
  15. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 1);
  16. half3 normalWS : TEXCOORD2;
  17. #if defined(_NORMALMAP)
  18. half4 tangentWS : TEXCOORD3;
  19. #endif
  20. #if defined(DEBUG_DISPLAY)
  21. float3 positionWS : TEXCOORD4;
  22. float3 viewDirWS : TEXCOORD5;
  23. #endif
  24. UNITY_VERTEX_INPUT_INSTANCE_ID
  25. UNITY_VERTEX_OUTPUT_STEREO
  26. };
  27. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  28. {
  29. inputData = (InputData)0;
  30. #if defined(DEBUG_DISPLAY)
  31. inputData.positionWS = input.positionWS;
  32. inputData.viewDirectionWS = input.viewDirWS;
  33. #else
  34. inputData.positionWS = float3(0, 0, 0);
  35. inputData.viewDirectionWS = half3(0, 0, 1);
  36. #endif
  37. #if defined(_NORMALMAP)
  38. float sgn = input.tangentWS.w; // should be either +1 or -1
  39. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  40. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
  41. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  42. #else
  43. inputData.normalWS = input.normalWS;
  44. #endif
  45. inputData.shadowCoord = float4(0, 0, 0, 0);
  46. inputData.fogCoord = input.uv0AndFogCoord.z;
  47. inputData.vertexLighting = half3(0, 0, 0);
  48. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  49. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  50. inputData.shadowMask = half4(1, 1, 1, 1);
  51. #if defined(DEBUG_DISPLAY)
  52. #if defined(LIGHTMAP_ON)
  53. inputData.staticLightmapUV = input.staticLightmapUV;
  54. #else
  55. inputData.vertexSH = input.vertexSH;
  56. #endif
  57. #endif
  58. }
  59. Varyings BakedLitForwardPassVertex(Attributes input)
  60. {
  61. Varyings output;
  62. UNITY_SETUP_INSTANCE_ID(input);
  63. UNITY_TRANSFER_INSTANCE_ID(input, output);
  64. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  65. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  66. output.positionCS = vertexInput.positionCS;
  67. output.uv0AndFogCoord.xy = TRANSFORM_TEX(input.uv, _BaseMap);
  68. #if defined(_FOG_FRAGMENT)
  69. output.uv0AndFogCoord.z = vertexInput.positionVS.z;
  70. #else
  71. output.uv0AndFogCoord.z = ComputeFogFactor(vertexInput.positionCS.z);
  72. #endif
  73. // normalWS and tangentWS already normalize.
  74. // this is required to avoid skewing the direction during interpolation
  75. // also required for per-vertex SH evaluation
  76. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  77. output.normalWS = normalInput.normalWS;
  78. #if defined(_NORMALMAP)
  79. real sign = input.tangentOS.w * GetOddNegativeScale();
  80. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  81. #endif
  82. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  83. OUTPUT_SH(output.normalWS, output.vertexSH);
  84. #if defined(DEBUG_DISPLAY)
  85. output.positionWS = vertexInput.positionWS;
  86. output.viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  87. #endif
  88. return output;
  89. }
  90. half4 BakedLitForwardPassFragment(Varyings input) : SV_Target
  91. {
  92. UNITY_SETUP_INSTANCE_ID(input);
  93. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  94. half2 uv = input.uv0AndFogCoord.xy;
  95. #if defined(_NORMALMAP)
  96. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  97. #else
  98. half3 normalTS = half3(0, 0, 1);
  99. #endif
  100. InputData inputData;
  101. InitializeInputData(input, normalTS, inputData);
  102. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv0AndFogCoord.xy, _BaseMap);
  103. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  104. half3 color = texColor.rgb * _BaseColor.rgb;
  105. half alpha = texColor.a * _BaseColor.a;
  106. AlphaDiscard(alpha, _Cutoff);
  107. #ifdef _DBUFFER
  108. ApplyDecalToBaseColorAndNormal(input.positionCS, color, inputData.normalWS);
  109. #endif
  110. half4 finalColor = UniversalFragmentBakedLit(inputData, color, alpha, normalTS);
  111. finalColor.a = OutputAlpha(finalColor.a, _Surface);
  112. return finalColor;
  113. }