Brak opisu
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.

UnlitForwardPass.hlsl 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if defined(LOD_FADE_CROSSFADE)
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  7. #endif
  8. struct Attributes
  9. {
  10. float4 positionOS : POSITION;
  11. float2 uv : TEXCOORD0;
  12. #if defined(DEBUG_DISPLAY)
  13. float3 normalOS : NORMAL;
  14. float4 tangentOS : TANGENT;
  15. #endif
  16. UNITY_VERTEX_INPUT_INSTANCE_ID
  17. };
  18. struct Varyings
  19. {
  20. float2 uv : TEXCOORD0;
  21. float fogCoord : TEXCOORD1;
  22. float4 positionCS : SV_POSITION;
  23. #if defined(DEBUG_DISPLAY)
  24. float3 positionWS : TEXCOORD2;
  25. float3 normalWS : TEXCOORD3;
  26. float3 viewDirWS : TEXCOORD4;
  27. #endif
  28. UNITY_VERTEX_INPUT_INSTANCE_ID
  29. UNITY_VERTEX_OUTPUT_STEREO
  30. };
  31. void InitializeInputData(Varyings input, out InputData inputData)
  32. {
  33. inputData = (InputData)0;
  34. #if defined(DEBUG_DISPLAY)
  35. inputData.positionWS = input.positionWS;
  36. inputData.positionCS = input.positionCS;
  37. inputData.normalWS = input.normalWS;
  38. inputData.viewDirectionWS = input.viewDirWS;
  39. #else
  40. inputData.positionWS = float3(0, 0, 0);
  41. inputData.normalWS = half3(0, 0, 1);
  42. inputData.viewDirectionWS = half3(0, 0, 1);
  43. #endif
  44. inputData.shadowCoord = 0;
  45. inputData.fogCoord = 0;
  46. inputData.vertexLighting = half3(0, 0, 0);
  47. inputData.bakedGI = half3(0, 0, 0);
  48. inputData.normalizedScreenSpaceUV = 0;
  49. inputData.shadowMask = half4(1, 1, 1, 1);
  50. }
  51. Varyings UnlitPassVertex(Attributes input)
  52. {
  53. Varyings output = (Varyings)0;
  54. UNITY_SETUP_INSTANCE_ID(input);
  55. UNITY_TRANSFER_INSTANCE_ID(input, output);
  56. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  57. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  58. output.positionCS = vertexInput.positionCS;
  59. output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
  60. #if defined(_FOG_FRAGMENT)
  61. output.fogCoord = vertexInput.positionVS.z;
  62. #else
  63. output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
  64. #endif
  65. #if defined(DEBUG_DISPLAY)
  66. // normalWS and tangentWS already normalize.
  67. // this is required to avoid skewing the direction during interpolation
  68. // also required for per-vertex lighting and SH evaluation
  69. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  70. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  71. // already normalized from normal transform to WS.
  72. output.positionWS = vertexInput.positionWS;
  73. output.normalWS = normalInput.normalWS;
  74. output.viewDirWS = viewDirWS;
  75. #endif
  76. return output;
  77. }
  78. void UnlitPassFragment(
  79. Varyings input
  80. , out half4 outColor : SV_Target0
  81. #ifdef _WRITE_RENDERING_LAYERS
  82. , out float4 outRenderingLayers : SV_Target1
  83. #endif
  84. )
  85. {
  86. UNITY_SETUP_INSTANCE_ID(input);
  87. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  88. half2 uv = input.uv;
  89. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  90. half3 color = texColor.rgb * _BaseColor.rgb;
  91. half alpha = texColor.a * _BaseColor.a;
  92. alpha = AlphaDiscard(alpha, _Cutoff);
  93. color = AlphaModulate(color, alpha);
  94. #ifdef LOD_FADE_CROSSFADE
  95. LODFadeCrossFade(input.positionCS);
  96. #endif
  97. InputData inputData;
  98. InitializeInputData(input, inputData);
  99. SETUP_DEBUG_TEXTURE_DATA(inputData, UNDO_TRANSFORM_TEX(input.uv, _BaseMap));
  100. #ifdef _DBUFFER
  101. ApplyDecalToBaseColor(input.positionCS, color);
  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. #if defined(_FOG_FRAGMENT)
  110. #if (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  111. float viewZ = -input.fogCoord;
  112. float nearToFarZ = max(viewZ - _ProjectionParams.y, 0);
  113. half fogFactor = ComputeFogFactorZ0ToFar(nearToFarZ);
  114. #else
  115. half fogFactor = 0;
  116. #endif
  117. #else
  118. half fogFactor = input.fogCoord;
  119. #endif
  120. finalColor.rgb = MixFog(finalColor.rgb, fogFactor);
  121. finalColor.a = OutputAlpha(finalColor.a, IsSurfaceTypeTransparent(_Surface));
  122. outColor = finalColor;
  123. #ifdef _WRITE_RENDERING_LAYERS
  124. uint renderingLayers = GetMeshRenderingLayer();
  125. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  126. #endif
  127. }
  128. #endif