Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BakedLitForwardPass.hlsl 5.7KB

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