SimpleLitForwardPass.hlsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // Vertex and Fragment functions //
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // Used in Standard (Simple Lighting) shader
  63. Varyings LitPassVertexSimple(Attributes input)
  64. {
  65. Varyings output = (Varyings)0;
  66. UNITY_SETUP_INSTANCE_ID(input);
  67. UNITY_TRANSFER_INSTANCE_ID(input, output);
  68. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  69. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  70. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  71. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  72. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  73. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  74. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  75. output.posWS.xyz = vertexInput.positionWS;
  76. output.positionCS = vertexInput.positionCS;
  77. #ifdef _NORMALMAP
  78. output.normal = half4(normalInput.normalWS, viewDirWS.x);
  79. output.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  80. output.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  81. #else
  82. output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
  83. output.viewDir = viewDirWS;
  84. #endif
  85. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  86. OUTPUT_SH(output.normal.xyz, output.vertexSH);
  87. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  88. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  89. output.shadowCoord = GetShadowCoord(vertexInput);
  90. #endif
  91. return output;
  92. }
  93. // Used for StandardSimpleLighting shader
  94. half4 LitPassFragmentSimple(Varyings input) : SV_Target
  95. {
  96. UNITY_SETUP_INSTANCE_ID(input);
  97. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  98. float2 uv = input.uv;
  99. half4 diffuseAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  100. half3 diffuse = diffuseAlpha.rgb * _BaseColor.rgb;
  101. half alpha = diffuseAlpha.a * _BaseColor.a;
  102. AlphaDiscard(alpha, _Cutoff);
  103. #ifdef _ALPHAPREMULTIPLY_ON
  104. diffuse *= alpha;
  105. #endif
  106. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  107. half3 emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));
  108. half4 specular = SampleSpecularSmoothness(uv, alpha, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap));
  109. half smoothness = specular.a;
  110. InputData inputData;
  111. InitializeInputData(input, normalTS, inputData);
  112. half4 color = UniversalFragmentBlinnPhong(inputData, diffuse, specular, smoothness, emission, alpha);
  113. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  114. return color;
  115. };
  116. #endif