LitInput.hlsl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef UNIVERSAL_LIT_INPUT_INCLUDED
  2. #define UNIVERSAL_LIT_INPUT_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  6. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ParallaxMapping.hlsl"
  7. #if defined(_DETAIL_MULX2) || defined(_DETAIL_SCALED)
  8. #define _DETAIL
  9. #endif
  10. // NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.
  11. CBUFFER_START(UnityPerMaterial)
  12. float4 _BaseMap_ST;
  13. float4 _DetailAlbedoMap_ST;
  14. half4 _BaseColor;
  15. half4 _SpecColor;
  16. half4 _EmissionColor;
  17. half _Cutoff;
  18. half _Smoothness;
  19. half _Metallic;
  20. half _BumpScale;
  21. half _Parallax;
  22. half _OcclusionStrength;
  23. half _ClearCoatMask;
  24. half _ClearCoatSmoothness;
  25. half _DetailAlbedoMapScale;
  26. half _DetailNormalMapScale;
  27. half _Surface;
  28. CBUFFER_END
  29. // NOTE: Do not ifdef the properties for dots instancing, but ifdef the actual usage.
  30. // Otherwise you might break CPU-side as property constant-buffer offsets change per variant.
  31. // NOTE: Dots instancing is orthogonal to the constant buffer above.
  32. #ifdef UNITY_DOTS_INSTANCING_ENABLED
  33. UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
  34. UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor)
  35. UNITY_DOTS_INSTANCED_PROP(float4, _SpecColor)
  36. UNITY_DOTS_INSTANCED_PROP(float4, _EmissionColor)
  37. UNITY_DOTS_INSTANCED_PROP(float , _Cutoff)
  38. UNITY_DOTS_INSTANCED_PROP(float , _Smoothness)
  39. UNITY_DOTS_INSTANCED_PROP(float , _Metallic)
  40. UNITY_DOTS_INSTANCED_PROP(float , _BumpScale)
  41. UNITY_DOTS_INSTANCED_PROP(float , _Parallax)
  42. UNITY_DOTS_INSTANCED_PROP(float , _OcclusionStrength)
  43. UNITY_DOTS_INSTANCED_PROP(float , _ClearCoatMask)
  44. UNITY_DOTS_INSTANCED_PROP(float , _ClearCoatSmoothness)
  45. UNITY_DOTS_INSTANCED_PROP(float , _DetailAlbedoMapScale)
  46. UNITY_DOTS_INSTANCED_PROP(float , _DetailNormalMapScale)
  47. UNITY_DOTS_INSTANCED_PROP(float , _Surface)
  48. UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
  49. #define _BaseColor UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata__BaseColor)
  50. #define _SpecColor UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata__SpecColor)
  51. #define _EmissionColor UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata__EmissionColor)
  52. #define _Cutoff UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__Cutoff)
  53. #define _Smoothness UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__Smoothness)
  54. #define _Metallic UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__Metallic)
  55. #define _BumpScale UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__BumpScale)
  56. #define _Parallax UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__Parallax)
  57. #define _OcclusionStrength UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__OcclusionStrength)
  58. #define _ClearCoatMask UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__ClearCoatMask)
  59. #define _ClearCoatSmoothness UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__ClearCoatSmoothness)
  60. #define _DetailAlbedoMapScale UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__DetailAlbedoMapScale)
  61. #define _DetailNormalMapScale UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__DetailNormalMapScale)
  62. #define _Surface UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float , Metadata__Surface)
  63. #endif
  64. TEXTURE2D(_ParallaxMap); SAMPLER(sampler_ParallaxMap);
  65. TEXTURE2D(_OcclusionMap); SAMPLER(sampler_OcclusionMap);
  66. TEXTURE2D(_DetailMask); SAMPLER(sampler_DetailMask);
  67. TEXTURE2D(_DetailAlbedoMap); SAMPLER(sampler_DetailAlbedoMap);
  68. TEXTURE2D(_DetailNormalMap); SAMPLER(sampler_DetailNormalMap);
  69. TEXTURE2D(_MetallicGlossMap); SAMPLER(sampler_MetallicGlossMap);
  70. TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);
  71. TEXTURE2D(_ClearCoatMap); SAMPLER(sampler_ClearCoatMap);
  72. #ifdef _SPECULAR_SETUP
  73. #define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_SpecGlossMap, sampler_SpecGlossMap, uv)
  74. #else
  75. #define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicGlossMap, sampler_MetallicGlossMap, uv)
  76. #endif
  77. half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
  78. {
  79. half4 specGloss;
  80. #ifdef _METALLICSPECGLOSSMAP
  81. specGloss = SAMPLE_METALLICSPECULAR(uv);
  82. #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  83. specGloss.a = albedoAlpha * _Smoothness;
  84. #else
  85. specGloss.a *= _Smoothness;
  86. #endif
  87. #else // _METALLICSPECGLOSSMAP
  88. #if _SPECULAR_SETUP
  89. specGloss.rgb = _SpecColor.rgb;
  90. #else
  91. specGloss.rgb = _Metallic.rrr;
  92. #endif
  93. #ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  94. specGloss.a = albedoAlpha * _Smoothness;
  95. #else
  96. specGloss.a = _Smoothness;
  97. #endif
  98. #endif
  99. return specGloss;
  100. }
  101. half SampleOcclusion(float2 uv)
  102. {
  103. #ifdef _OCCLUSIONMAP
  104. // TODO: Controls things like these by exposing SHADER_QUALITY levels (low, medium, high)
  105. #if defined(SHADER_API_GLES)
  106. return SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
  107. #else
  108. half occ = SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;
  109. return LerpWhiteTo(occ, _OcclusionStrength);
  110. #endif
  111. #else
  112. return 1.0;
  113. #endif
  114. }
  115. // Returns clear coat parameters
  116. // .x/.r == mask
  117. // .y/.g == smoothness
  118. half2 SampleClearCoat(float2 uv)
  119. {
  120. #if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)
  121. half2 clearCoatMaskSmoothness = half2(_ClearCoatMask, _ClearCoatSmoothness);
  122. #if defined(_CLEARCOATMAP)
  123. clearCoatMaskSmoothness *= SAMPLE_TEXTURE2D(_ClearCoatMap, sampler_ClearCoatMap, uv).rg;
  124. #endif
  125. return clearCoatMaskSmoothness;
  126. #else
  127. return half2(0.0, 1.0);
  128. #endif // _CLEARCOAT
  129. }
  130. void ApplyPerPixelDisplacement(half3 viewDirTS, inout float2 uv)
  131. {
  132. #if defined(_PARALLAXMAP)
  133. uv += ParallaxMapping(TEXTURE2D_ARGS(_ParallaxMap, sampler_ParallaxMap), viewDirTS, _Parallax, uv);
  134. #endif
  135. }
  136. // Used for scaling detail albedo. Main features:
  137. // - Depending if detailAlbedo brightens or darkens, scale magnifies effect.
  138. // - No effect is applied if detailAlbedo is 0.5.
  139. half3 ScaleDetailAlbedo(half3 detailAlbedo, half scale)
  140. {
  141. // detailAlbedo = detailAlbedo * 2.0h - 1.0h;
  142. // detailAlbedo *= _DetailAlbedoMapScale;
  143. // detailAlbedo = detailAlbedo * 0.5h + 0.5h;
  144. // return detailAlbedo * 2.0f;
  145. // A bit more optimized
  146. return 2.0h * detailAlbedo * scale - scale + 1.0h;
  147. }
  148. half3 ApplyDetailAlbedo(float2 detailUv, half3 albedo, half detailMask)
  149. {
  150. #if defined(_DETAIL)
  151. half3 detailAlbedo = SAMPLE_TEXTURE2D(_DetailAlbedoMap, sampler_DetailAlbedoMap, detailUv).rgb;
  152. // In order to have same performance as builtin, we do scaling only if scale is not 1.0 (Scaled version has 6 additional instructions)
  153. #if defined(_DETAIL_SCALED)
  154. detailAlbedo = ScaleDetailAlbedo(detailAlbedo, _DetailAlbedoMapScale);
  155. #else
  156. detailAlbedo = 2.0h * detailAlbedo;
  157. #endif
  158. return albedo * LerpWhiteTo(detailAlbedo, detailMask);
  159. #else
  160. return albedo;
  161. #endif
  162. }
  163. half3 ApplyDetailNormal(float2 detailUv, half3 normalTS, half detailMask)
  164. {
  165. #if defined(_DETAIL)
  166. #if BUMP_SCALE_NOT_SUPPORTED
  167. half3 detailNormalTS = UnpackNormal(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv));
  168. #else
  169. half3 detailNormalTS = UnpackNormalScale(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv), _DetailNormalMapScale);
  170. #endif
  171. // With UNITY_NO_DXT5nm unpacked vector is not normalized for BlendNormalRNM
  172. // For visual consistancy we going to do in all cases
  173. detailNormalTS = normalize(detailNormalTS);
  174. return lerp(normalTS, BlendNormalRNM(normalTS, detailNormalTS), detailMask); // todo: detailMask should lerp the angle of the quaternion rotation, not the normals
  175. #else
  176. return normalTS;
  177. #endif
  178. }
  179. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  180. {
  181. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  182. outSurfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);
  183. half4 specGloss = SampleMetallicSpecGloss(uv, albedoAlpha.a);
  184. outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
  185. #if _SPECULAR_SETUP
  186. outSurfaceData.metallic = 1.0h;
  187. outSurfaceData.specular = specGloss.rgb;
  188. #else
  189. outSurfaceData.metallic = specGloss.r;
  190. outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
  191. #endif
  192. outSurfaceData.smoothness = specGloss.a;
  193. outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
  194. outSurfaceData.occlusion = SampleOcclusion(uv);
  195. outSurfaceData.emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));
  196. #if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)
  197. half2 clearCoat = SampleClearCoat(uv);
  198. outSurfaceData.clearCoatMask = clearCoat.r;
  199. outSurfaceData.clearCoatSmoothness = clearCoat.g;
  200. #else
  201. outSurfaceData.clearCoatMask = 0.0h;
  202. outSurfaceData.clearCoatSmoothness = 0.0h;
  203. #endif
  204. #if defined(_DETAIL)
  205. half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).a;
  206. float2 detailUv = uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;
  207. outSurfaceData.albedo = ApplyDetailAlbedo(detailUv, outSurfaceData.albedo, detailMask);
  208. outSurfaceData.normalTS = ApplyDetailNormal(detailUv, outSurfaceData.normalTS, detailMask);
  209. #endif
  210. }
  211. #endif // UNIVERSAL_INPUT_SURFACE_PBR_INCLUDED