SimpleLitForwardPass.hlsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_SIMPLE_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. float3 posWS : TEXCOORD2; // xyz: posWS
  18. #ifdef _NORMALMAP
  19. float4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
  20. float4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
  21. float4 bitangent : TEXCOORD5; // xyz: bitangent, w: viewDir.z
  22. #else
  23. float3 normal : TEXCOORD3;
  24. float3 viewDir : TEXCOORD4;
  25. #endif
  26. half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
  27. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  28. float4 shadowCoord : TEXCOORD7;
  29. #endif
  30. float4 positionCS : SV_POSITION;
  31. UNITY_VERTEX_INPUT_INSTANCE_ID
  32. UNITY_VERTEX_OUTPUT_STEREO
  33. };
  34. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  35. {
  36. inputData.positionWS = input.posWS;
  37. #ifdef _NORMALMAP
  38. half3 viewDirWS = half3(input.normal.w, input.tangent.w, input.bitangent.w);
  39. inputData.normalWS = TransformTangentToWorld(normalTS,
  40. half3x3(input.tangent.xyz, input.bitangent.xyz, input.normal.xyz));
  41. #else
  42. half3 viewDirWS = input.viewDir;
  43. inputData.normalWS = input.normal;
  44. #endif
  45. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  46. viewDirWS = SafeNormalize(viewDirWS);
  47. inputData.viewDirectionWS = viewDirWS;
  48. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  49. inputData.shadowCoord = input.shadowCoord;
  50. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  51. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  52. #else
  53. inputData.shadowCoord = float4(0, 0, 0, 0);
  54. #endif
  55. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  56. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  57. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  58. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  59. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // Vertex and Fragment functions //
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // Used in Standard (Simple Lighting) shader
  65. Varyings LitPassVertexSimple(Attributes input)
  66. {
  67. Varyings output = (Varyings)0;
  68. UNITY_SETUP_INSTANCE_ID(input);
  69. UNITY_TRANSFER_INSTANCE_ID(input, output);
  70. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  71. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  72. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  73. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  74. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  75. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  76. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  77. output.posWS.xyz = vertexInput.positionWS;
  78. output.positionCS = vertexInput.positionCS;
  79. #ifdef _NORMALMAP
  80. output.normal = half4(normalInput.normalWS, viewDirWS.x);
  81. output.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  82. output.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  83. #else
  84. output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
  85. output.viewDir = viewDirWS;
  86. #endif
  87. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  88. OUTPUT_SH(output.normal.xyz, output.vertexSH);
  89. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  90. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  91. output.shadowCoord = GetShadowCoord(vertexInput);
  92. #endif
  93. return output;
  94. }
  95. // Used for StandardSimpleLighting shader
  96. half4 LitPassFragmentSimple(Varyings input) : SV_Target
  97. {
  98. UNITY_SETUP_INSTANCE_ID(input);
  99. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  100. float2 uv = input.uv;
  101. half4 diffuseAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  102. half3 diffuse = diffuseAlpha.rgb * _BaseColor.rgb;
  103. half alpha = diffuseAlpha.a * _BaseColor.a;
  104. AlphaDiscard(alpha, _Cutoff);
  105. #ifdef _ALPHAPREMULTIPLY_ON
  106. diffuse *= alpha;
  107. #endif
  108. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  109. half3 emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));
  110. half4 specular = SampleSpecularSmoothness(uv, alpha, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap));
  111. half smoothness = specular.a;
  112. InputData inputData;
  113. InitializeInputData(input, normalTS, inputData);
  114. half4 color = UniversalFragmentBlinnPhong(inputData, diffuse, specular, smoothness, emission, alpha);
  115. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  116. color.a = OutputAlpha(color.a, _Surface);
  117. return color;
  118. }
  119. #endif