InTerra_TerrainLit.shader 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //NOTE! This file is based on Unity file "TerrainLit.shader" which was used as a template for adding all the InTerra features.
  2. Shader "InTerra/HDRP/Terrain (Lit with Features)"
  3. {
  4. Properties
  5. {
  6. [HideInInspector] [ToggleUI] _EnableHeightBlend("EnableHeightBlend", Float) = 0.0
  7. _HeightTransition("Height Transition", Range(0, 60)) = 0.0
  8. [HideInInspector] [Enum(Off, 0, From Ambient Occlusion, 1)] _SpecularOcclusionMode("Specular Occlusion Mode", Int) = 1
  9. [HideInInspector][PerRendererData] _NumLayersCount("Total Layer Count", Float) = 1.0
  10. // Following are builtin properties
  11. // Stencil state
  12. // Forward
  13. [HideInInspector] _StencilRef("_StencilRef", Int) = 0 // StencilUsage.Clear
  14. [HideInInspector] _StencilWriteMask("_StencilWriteMask", Int) = 3 // StencilUsage.RequiresDeferredLighting | StencilUsage.SubsurfaceScattering
  15. // GBuffer
  16. [HideInInspector] _StencilRefGBuffer("_StencilRefGBuffer", Int) = 2 // StencilUsage.RequiresDeferredLighting
  17. [HideInInspector] _StencilWriteMaskGBuffer("_StencilWriteMaskGBuffer", Int) = 3 // StencilUsage.RequiresDeferredLighting | StencilUsage.SubsurfaceScattering
  18. // Depth prepass
  19. [HideInInspector] _StencilRefDepth("_StencilRefDepth", Int) = 0 // Nothing
  20. [HideInInspector] _StencilWriteMaskDepth("_StencilWriteMaskDepth", Int) = 8 // StencilUsage.TraceReflectionRay
  21. // Blending state
  22. [HideInInspector] _ZWrite ("__zw", Float) = 1.0
  23. [HideInInspector][ToggleUI] _TransparentZWrite("_TransparentZWrite", Float) = 0.0
  24. [HideInInspector] _CullMode("__cullmode", Float) = 2.0
  25. [HideInInspector] _ZTestDepthEqualForOpaque("_ZTestDepthEqualForOpaque", Int) = 4 // Less equal
  26. [HideInInspector] _ZTestGBuffer("_ZTestGBuffer", Int) = 4
  27. [ToggleUI] _EnableInstancedPerPixelNormal("Instanced per pixel normal", Float) = 1.0
  28. [HideInInspector] _TerrainHolesTexture("Holes Map (RGB)", 2D) = "white" {}
  29. // Caution: C# code in BaseLitUI.cs call LightmapEmissionFlagsProperty() which assume that there is an existing "_EmissionColor"
  30. // value that exist to identify if the GI emission need to be enabled.
  31. // In our case we don't use such a mechanism but need to keep the code quiet. We declare the value and always enable it.
  32. // TODO: Fix the code in legacy unity so we can customize the behavior for GI
  33. [HideInInspector] _EmissionColor("Color", Color) = (1, 1, 1)
  34. // HACK: GI Baking system relies on some properties existing in the shader ("_MainTex", "_Cutoff" and "_Color") for opacity handling, so we need to store our version of those parameters in the hard-coded name the GI baking system recognizes.
  35. [HideInInspector] _MainTex("Albedo", 2D) = "white" {}
  36. [HideInInspector] _Color("Color", Color) = (1,1,1,1)
  37. [HideInInspector] _Control1("Albedo", 2D) = "black" {}
  38. [HideInInspector] _Control2("Albedo", 2D) = "black" {}
  39. [HideInInspector] _Control3("Albedo", 2D) = "black" {}
  40. [HideInInspector] [ToggleUI] _SupportDecals("Support Decals", Float) = 1.0
  41. [HideInInspector] [ToggleUI] _ReceivesSSR("Receives SSR", Float) = 1.0
  42. [HideInInspector] [ToggleUI] _AddPrecomputedVelocity("AddPrecomputedVelocity", Float) = 0.0
  43. //inTerra
  44. _HT_distance("Distance", vector) = (3, 10, 0, 25)
  45. _HT_distance_scale("Scale", Range(0,0.5)) = 0.25
  46. _HT_cover("Cover strenght", Range(0,1)) = 0.6
  47. _Distance_HeightTransition("Distance Height blending Sharpness ", Range(0,60)) = 10
  48. _TriplanarSharpness("Triplanar Sharpness", Range(4,10)) = 9
  49. _ParallaxAffineStepsTerrain("", Float) = 3
  50. _MipMapFade("Parallax MipMap fade", vector) = (4,15,0,30)
  51. _MipMapLevel("Parallax MipMap level", Float) = 0
  52. _TriplanarOneToAllSteep("", Float) = 0
  53. _TerrainColorTintTexture("Color Tint Texture", 2D) = "white" {}
  54. _TerrainColorTintStrenght("Color Tint Strenght", Range(1, 0)) = 0
  55. _TerrainNormalTintTexture("Additional Normal Texture", 2D) = "bump" {}
  56. _TerrainNormalTintStrenght("Additional Normal Strenght", Range(0, 1)) = 0.0
  57. _TerrainNormalTintDistance("Additional Normal Distance", vector) = (3,10,0,25)
  58. [HideInInspector] _TerrainSizeXZPosY("", Vector) = (0,0,0)
  59. _TrackAO("", Range(0,1)) = 0.8
  60. _TrackTessallation("", Range(0,1)) = 0
  61. _TrackEdgeNormals("Track Edge Normals", Float) = 2
  62. _TrackDetailTexture("Track Color Detail Texture", 2D) = "white" {}
  63. [Normal] _TrackDetailNormalTexture("Track Normal Detail Texture", 2D) = "bump" {}
  64. _TrackDetailNormalStrenght("Track Detail Normal Strenght", Float) = 1
  65. _TrackNormalStrenght("Track Normal Strenght", Float) = 1
  66. _TrackEdgeSharpness("Track Edge Normals", Range(0.001,4)) = 1
  67. _TrackHeightOffset("Track Heightmap Offset", Range(-1,1)) = 0
  68. _TrackMultiplyStrenght("Track Multiply strenght", Float) = 3
  69. _TrackHeightTransition("Track Normal Strenght", Range(0, 60)) = 20
  70. _ParallaxTrackAffineSteps("", Float) = 3
  71. _ParallaxTrackSteps("", Float) = 5
  72. _Gamma("", Float) = 0
  73. _WorldMapping("", Float) = 0
  74. _HeightmapBlending("", Float) = 1
  75. _Terrain_Parallax("", Float) = 0
  76. _Tracks("", Float) = 0
  77. [KeywordEnum(Two, Four, Eight, Sixteen)] _Layers("Layers",int) = 2
  78. }
  79. HLSLINCLUDE
  80. #pragma target 4.5
  81. #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
  82. #pragma shader_feature _COLOR_RED _COLOR_GREEN _COLOR_BLUE
  83. // Terrain builtin keywords
  84. #pragma shader_feature_local _TERRAIN_8_LAYERS
  85. #define _NORMALMAP
  86. #define _MASKMAP
  87. #pragma shader_feature_local _SPECULAR_OCCLUSION_NONE
  88. // Sample normal in pixel shader when doing instancing
  89. #pragma shader_feature_local _TERRAIN_INSTANCED_PERPIXEL_NORMAL
  90. #pragma shader_feature_local _DISABLE_DECALS
  91. #pragma shader_feature_local _ADD_PRECOMPUTED_VELOCITY
  92. //enable GPU instancing support
  93. #pragma multi_compile_instancing
  94. #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap
  95. #pragma multi_compile _ _ALPHATEST_ON
  96. // All our shaders use same name for entry point
  97. #pragma vertex Vert
  98. #pragma fragment Frag
  99. // Define _DEFERRED_CAPABLE_MATERIAL for shader capable to run in deferred pass
  100. #define _DEFERRED_CAPABLE_MATERIAL
  101. // InTerra Keywords
  102. #pragma shader_feature_local __ _TERRAIN_TRIPLANAR_ONE _TERRAIN_TRIPLANAR
  103. #pragma shader_feature_local_fragment _TERRAIN_DISTANCEBLEND
  104. #pragma shader_feature_local __ _LAYERS_TWO _LAYERS_EIGHT _LAYERS_SIXTEEN
  105. #define INTERRA_TERRAIN
  106. #include "InTerra_TerrainLit_Splatmap_Includes.hlsl"
  107. ENDHLSL
  108. SubShader
  109. {
  110. PackageRequirements { "com.unity.render-pipelines.high-definition":"[12.0,13.99.99]" }
  111. // This tags allow to use the shader replacement features
  112. Tags
  113. {
  114. "RenderPipeline" = "HDRenderPipeline"
  115. "RenderType" = "Opaque"
  116. "SplatCount" = "16"
  117. "MaskMapR" = "Metallic"
  118. "MaskMapG" = "AO"
  119. "MaskMapB" = "Height"
  120. "MaskMapA" = "Smoothness"
  121. "DiffuseA" = "Smoothness (becomes Density when Mask map is assigned)" // when MaskMap is disabled
  122. "DiffuseA_MaskMapUsed" = "Density" // when MaskMap is enabled
  123. "TerrainCompatible" = "True"
  124. }
  125. // Caution: The outline selection in the editor use the vertex shader/hull/domain shader of the first pass declare. So it should not bethe meta pass.
  126. Pass
  127. {
  128. Name "GBuffer"
  129. Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index
  130. Cull [_CullMode]
  131. ZTest [_ZTestGBuffer]
  132. Stencil
  133. {
  134. WriteMask [_StencilWriteMaskGBuffer]
  135. Ref [_StencilRefGBuffer]
  136. Comp Always
  137. Pass Replace
  138. }
  139. HLSLPROGRAM
  140. #pragma multi_compile _ DEBUG_DISPLAY
  141. #pragma multi_compile _ LIGHTMAP_ON
  142. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  143. #pragma multi_compile _ DYNAMICLIGHTMAP_ON
  144. #pragma multi_compile_fragment _ SHADOWS_SHADOWMASK
  145. #pragma multi_compile_fragment PROBE_VOLUMES_OFF PROBE_VOLUMES_L1 PROBE_VOLUMES_L2
  146. // Setup DECALS_OFF so the shader stripper can remove variants
  147. #pragma multi_compile_fragment DECALS_OFF DECALS_3RT DECALS_4RT
  148. #pragma multi_compile_fragment _ DECAL_SURFACE_GRADIENT
  149. #pragma multi_compile_fragment _ LIGHT_LAYERS
  150. #define SHADERPASS SHADERPASS_GBUFFER
  151. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  152. #include "InTerra_TerrainLitTemplate.hlsl"
  153. #include "InTerra_TerrainLit_Splatmap.hlsl"
  154. ENDHLSL
  155. }
  156. // Extracts information for lightmapping, GI (emission, albedo, ...)
  157. // This pass it not used during regular rendering.
  158. Pass
  159. {
  160. Name "META"
  161. Tags{ "LightMode" = "META" }
  162. Cull Off
  163. HLSLPROGRAM
  164. // Lightmap memo
  165. // DYNAMICLIGHTMAP_ON is used when we have an "enlighten lightmap" ie a lightmap updated at runtime by enlighten.This lightmap contain indirect lighting from realtime lights and realtime emissive material.Offline baked lighting(from baked material / light,
  166. // both direct and indirect lighting) will hand up in the "regular" lightmap->LIGHTMAP_ON.
  167. #define SHADERPASS SHADERPASS_LIGHT_TRANSPORT
  168. #pragma shader_feature EDITOR_VISUALIZATION
  169. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  170. #include "InTerra_TerrainLitTemplate.hlsl"
  171. #include "InTerra_TerrainLit_Splatmap.hlsl"
  172. ENDHLSL
  173. }
  174. Pass
  175. {
  176. Name "ShadowCaster"
  177. Tags{ "LightMode" = "ShadowCaster" }
  178. Cull[_CullMode]
  179. ZClip [_ZClip]
  180. ZWrite On
  181. ZTest LEqual
  182. ColorMask 0
  183. HLSLPROGRAM
  184. #define SHADERPASS SHADERPASS_SHADOWS
  185. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  186. #include "InTerra_TerrainLitTemplate.hlsl"
  187. #include "InTerra_TerrainLit_Splatmap.hlsl"
  188. ENDHLSL
  189. }
  190. Pass
  191. {
  192. Name "DepthOnly"
  193. Tags{ "LightMode" = "DepthOnly" }
  194. Cull[_CullMode]
  195. // To be able to tag stencil with disableSSR information for forward
  196. Stencil
  197. {
  198. WriteMask [_StencilWriteMaskDepth]
  199. Ref [_StencilRefDepth]
  200. Comp Always
  201. Pass Replace
  202. }
  203. ZWrite On
  204. HLSLPROGRAM
  205. // In deferred, depth only pass don't output anything.
  206. // In forward it output the normal buffer
  207. #pragma multi_compile _ WRITE_NORMAL_BUFFER
  208. #pragma multi_compile_fragment _ WRITE_DECAL_BUFFER
  209. #pragma multi_compile _ WRITE_MSAA_DEPTH
  210. #define SHADERPASS SHADERPASS_DEPTH_ONLY
  211. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  212. #include "InTerra_TerrainLitTemplate.hlsl"
  213. #ifdef WRITE_NORMAL_BUFFER
  214. #if defined(_NORMALMAP)
  215. #define OVERRIDE_SPLAT_SAMPLER_NAME sampler_Normal0
  216. #elif defined(_MASKMAP)
  217. #define OVERRIDE_SPLAT_SAMPLER_NAME sampler_Mask0
  218. #endif
  219. #endif
  220. #include "InTerra_TerrainLit_Splatmap.hlsl"
  221. ENDHLSL
  222. }
  223. Pass
  224. {
  225. Name "Forward"
  226. Tags { "LightMode" = "Forward" } // This will be only for transparent object based on the RenderQueue index
  227. Stencil
  228. {
  229. WriteMask [_StencilWriteMask]
  230. Ref [_StencilRef]
  231. Comp Always
  232. Pass Replace
  233. }
  234. // In case of forward we want to have depth equal for opaque mesh
  235. ZTest [_ZTestDepthEqualForOpaque]
  236. ZWrite [_ZWrite]
  237. Cull [_CullMode]
  238. HLSLPROGRAM
  239. #pragma multi_compile _ DEBUG_DISPLAY
  240. #pragma multi_compile _ LIGHTMAP_ON
  241. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  242. #pragma multi_compile _ DYNAMICLIGHTMAP_ON
  243. #pragma multi_compile_fragment _ SHADOWS_SHADOWMASK
  244. #pragma multi_compile_fragment PROBE_VOLUMES_OFF PROBE_VOLUMES_L1 PROBE_VOLUMES_L2
  245. #pragma multi_compile_fragment SCREEN_SPACE_SHADOWS_OFF SCREEN_SPACE_SHADOWS_ON
  246. // Setup DECALS_OFF so the shader stripper can remove variants
  247. #pragma multi_compile_fragment DECALS_OFF DECALS_3RT DECALS_4RT
  248. #pragma multi_compile_fragment _ DECAL_SURFACE_GRADIENT
  249. // Supported shadow modes per light type
  250. #pragma multi_compile_fragment SHADOW_LOW SHADOW_MEDIUM SHADOW_HIGH SHADOW_VERY_HIGH
  251. #pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
  252. #define SHADERPASS SHADERPASS_FORWARD
  253. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  254. #include "InTerra_TerrainLitTemplate.hlsl"
  255. #include "InTerra_TerrainLit_Splatmap.hlsl"
  256. ENDHLSL
  257. }
  258. Pass
  259. {
  260. Name "SceneSelectionPass"
  261. Tags { "LightMode" = "SceneSelectionPass" }
  262. Cull Off
  263. HLSLPROGRAM
  264. #pragma editor_sync_compilation
  265. #define SHADERPASS SHADERPASS_DEPTH_ONLY
  266. #define SCENESELECTIONPASS
  267. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  268. #include "InTerra_TerrainLitTemplate.hlsl"
  269. #include "InTerra_TerrainLit_Splatmap.hlsl"
  270. ENDHLSL
  271. }
  272. UsePass "Hidden/Nature/Terrain/Utilities/PICKING"
  273. }
  274. Dependency "BaseMapShader" = "Hidden/InTerra/HDRP/TerrainLit_Basemap"
  275. Dependency "BaseMapGenShader" = "Hidden/InTerra/HDRP/TerrainLit_BasemapGen"
  276. CustomEditor "InTerra.InTerra_TerrainShaderGUI"
  277. }