UnlitForwardPass.hlsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef URP_UNLIT_FORWARD_PASS_INCLUDED
  2. #define URP_UNLIT_FORWARD_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Unlit.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  5. struct Attributes
  6. {
  7. float4 positionOS : POSITION;
  8. float2 uv : TEXCOORD0;
  9. #if defined(DEBUG_DISPLAY)
  10. float3 normalOS : NORMAL;
  11. float4 tangentOS : TANGENT;
  12. #endif
  13. UNITY_VERTEX_INPUT_INSTANCE_ID
  14. };
  15. struct Varyings
  16. {
  17. float2 uv : TEXCOORD0;
  18. float fogCoord : TEXCOORD1;
  19. float4 positionCS : SV_POSITION;
  20. #if defined(DEBUG_DISPLAY)
  21. float3 positionWS : TEXCOORD2;
  22. float3 normalWS : TEXCOORD3;
  23. float3 viewDirWS : TEXCOORD4;
  24. #endif
  25. UNITY_VERTEX_INPUT_INSTANCE_ID
  26. UNITY_VERTEX_OUTPUT_STEREO
  27. };
  28. void InitializeInputData(Varyings input, out InputData inputData)
  29. {
  30. inputData = (InputData)0;
  31. #if defined(DEBUG_DISPLAY)
  32. inputData.positionWS = input.positionWS;
  33. inputData.normalWS = input.normalWS;
  34. inputData.viewDirectionWS = input.viewDirWS;
  35. #else
  36. inputData.positionWS = float3(0, 0, 0);
  37. inputData.normalWS = half3(0, 0, 1);
  38. inputData.viewDirectionWS = half3(0, 0, 1);
  39. #endif
  40. inputData.shadowCoord = 0;
  41. inputData.fogCoord = 0;
  42. inputData.vertexLighting = half3(0, 0, 0);
  43. inputData.bakedGI = half3(0, 0, 0);
  44. inputData.normalizedScreenSpaceUV = 0;
  45. inputData.shadowMask = half4(1, 1, 1, 1);
  46. }
  47. Varyings UnlitPassVertex(Attributes input)
  48. {
  49. Varyings output = (Varyings)0;
  50. UNITY_SETUP_INSTANCE_ID(input);
  51. UNITY_TRANSFER_INSTANCE_ID(input, output);
  52. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  53. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  54. output.positionCS = vertexInput.positionCS;
  55. output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
  56. #if defined(_FOG_FRAGMENT)
  57. output.fogCoord = vertexInput.positionVS.z;
  58. #else
  59. output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
  60. #endif
  61. #if defined(DEBUG_DISPLAY)
  62. // normalWS and tangentWS already normalize.
  63. // this is required to avoid skewing the direction during interpolation
  64. // also required for per-vertex lighting and SH evaluation
  65. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  66. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  67. // already normalized from normal transform to WS.
  68. output.positionWS = vertexInput.positionWS;
  69. output.normalWS = normalInput.normalWS;
  70. output.viewDirWS = viewDirWS;
  71. #endif
  72. return output;
  73. }
  74. half4 UnlitPassFragment(Varyings input) : SV_Target
  75. {
  76. UNITY_SETUP_INSTANCE_ID(input);
  77. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  78. half2 uv = input.uv;
  79. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  80. half3 color = texColor.rgb * _BaseColor.rgb;
  81. half alpha = texColor.a * _BaseColor.a;
  82. AlphaDiscard(alpha, _Cutoff);
  83. #if defined(_ALPHAPREMULTIPLY_ON)
  84. color *= alpha;
  85. #endif
  86. InputData inputData;
  87. InitializeInputData(input, inputData);
  88. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
  89. #ifdef _DBUFFER
  90. ApplyDecalToBaseColor(input.positionCS, color);
  91. #endif
  92. #if defined(_FOG_FRAGMENT)
  93. #if (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  94. float viewZ = -input.fogCoord;
  95. float nearToFarZ = max(viewZ - _ProjectionParams.y, 0);
  96. half fogFactor = ComputeFogFactorZ0ToFar(nearToFarZ);
  97. #else
  98. half fogFactor = 0;
  99. #endif
  100. #else
  101. half fogFactor = input.fogCoord;
  102. #endif
  103. half4 finalColor = UniversalFragmentUnlit(inputData, color, alpha);
  104. #if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
  105. float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  106. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
  107. finalColor.rgb *= aoFactor.directAmbientOcclusion;
  108. #endif
  109. finalColor.rgb = MixFog(finalColor.rgb, fogFactor);
  110. return finalColor;
  111. }
  112. #endif