InTerra_TerrainLit_Tessellation.shader 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 Tessellation/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] [ToggleUI] _SupportDecals("Support Decals", Float) = 1.0
  38. [HideInInspector] [ToggleUI] _ReceivesSSR("Receives SSR", Float) = 1.0
  39. [HideInInspector] [ToggleUI] _AddPrecomputedVelocity("AddPrecomputedVelocity", Float) = 0.0
  40. //inTerra
  41. _HT_distance("Distance", vector) = (3,10,0,25)
  42. _HT_distance_scale("Scale", Range(0,0.5)) = 0.25
  43. _HT_cover("Cover strenght", Range(0,1)) = 0.6
  44. _Distance_HeightTransition("Distance Height blending Sharpness ", Range(0,60)) = 10
  45. _TriplanarSharpness("Triplanar Sharpness", Range(4,10)) = 9
  46. _ParallaxAffineStepsTerrain("", Float) = 3
  47. _TriplanarOneToAllSteep("", Float) = 0
  48. _TerrainColorTintTexture("Color Tint Texture", 2D) = "white" {}
  49. _TerrainColorTintStrenght("Color Tint Strenght", Range(1, 0)) = 0
  50. _TerrainNormalTintTexture("Additional Normal Texture", 2D) = "bump" {}
  51. _TerrainNormalTintStrenght("Additional Normal Strenght", Range(0, 1)) = 0.0
  52. _TerrainNormalTintDistance("Additional Normal Distance", vector) = (3,10,0,25)
  53. [HideInInspector] _TerrainSizeXZPosY("", Vector) = (0,0,0)
  54. //Tessellation
  55. _TessellationFactor("Tessellation Factor", Range(0.0, 64.0)) = 8.0
  56. _TessellationFactorMinDistance("Tessellation start fading distance", Float) = 5.0
  57. _TessellationFactorMaxDistance("Tessellation end fading distance", Float) = 25.0
  58. _TessellationFactorTriangleSize("Tessellation triangle size", Float) = 100.0
  59. _TessellationShapeFactor("Tessellation shape factor", Range(0.0, 1.0)) = 0.75 // Only use with Phong
  60. [HideInInspector]_TessellationBackFaceCullEpsilon("Tessellation back face epsilon", Range(-1, 0)) = -0.25
  61. [HideInInspector]_TessellationMaxDisplacement("Float", Float) = 1
  62. _MipMapFade("MipMap fade", vector) = (5,20,-5,35)
  63. _MipMapLevel("MipMap level", Float) = 0
  64. _Tessellation_HeightTransition("Tessellation Height blending Sharpness ", Range(0,60)) = 15
  65. _TessellationShadowQuality("Quality of Tessellation Shadows", Range(0,1)) = 0.5
  66. _TrackTessallationHeightTransition("Track Tessellation Height blending Sharpness ", Range(0,60)) = 15
  67. _TrackTessallationHeightOffset("",Range(-1.0 ,1.0)) = 0
  68. _TrackAO("", Range(0,1)) = 0.8
  69. _TrackTessallation("", Range(0,1)) = 0
  70. _TrackEdgeNormals("Track Edge Normals", Float) = 2
  71. _TrackDetailTexture("Track Color Detail Texture", 2D) = "white" {}
  72. [Normal] _TrackDetailNormalTexture("Track Normal Detail Texture", 2D) = "bump" {}
  73. _TrackDetailNormalStrenght("Track Detail Normal Strenght", Float) = 1
  74. _TrackNormalStrenght("Track Normal Strenght", Float) = 1
  75. _TrackEdgeSharpness("Track Edge Normals", Range(0.001,4)) = 1
  76. _TrackHeightOffset("Track Heightmap Offset", Range(-1,1)) = 0
  77. _TrackMultiplyStrenght("Track Multiply strenght", Float) = 3
  78. _TrackHeightTransition("Track Normal Strenght", Range(0, 60)) = 20
  79. _ParallaxTrackAffineSteps("", Float) = 3
  80. _ParallaxTrackSteps("", Float) = 5
  81. _Gamma("", Float) = 0
  82. _WorldMapping("", Float) = 0
  83. _HeightmapBlending("", Float) = 1
  84. _Terrain_Parallax("", Float) = 0
  85. _Tracks("", Float) = 0
  86. }
  87. HLSLINCLUDE
  88. #pragma target 5.0
  89. #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
  90. // Terrain builtin keywords
  91. #pragma shader_feature_local _TERRAIN_8_LAYERS
  92. #define _NORMALMAP
  93. #define _MASKMAP
  94. #pragma shader_feature_local _SPECULAR_OCCLUSION_NONE
  95. // Sample normal in pixel shader when doing instancing
  96. #pragma shader_feature_local _TERRAIN_INSTANCED_PERPIXEL_NORMAL
  97. #pragma shader_feature_local_fragment _DISABLE_DECALS
  98. #pragma shader_feature_local _ADD_PRECOMPUTED_VELOCITY
  99. //enable GPU instancing support
  100. #pragma multi_compile_instancing
  101. #pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap
  102. #pragma multi_compile _ _ALPHATEST_ON
  103. //-----------TESSELLATION-------------------------------
  104. #define TESSELLATION_ON
  105. #define HAVE_TESSELLATION_MODIFICATION
  106. #define _TESSELLATION_DISPLACEMENT
  107. #define _HEIGHTMAP
  108. #define _CONSERVATIVE_DEPTH_OFFSET //heightmap
  109. //--------------------------------------------------------------
  110. // Define _DEFERRED_CAPABLE_MATERIAL for shader capable to run in deferred pass
  111. #define _DEFERRED_CAPABLE_MATERIAL
  112. #if defined(_TRANSPARENT_WRITES_MOTION_VEC) && defined(_SURFACE_TYPE_TRANSPARENT)
  113. #define _WRITE_TRANSPARENT_MOTION_VECTOR
  114. #endif
  115. // InTerra Keywords
  116. #pragma shader_feature_local __ _TERRAIN_TRIPLANAR_ONE _TERRAIN_TRIPLANAR
  117. #pragma shader_feature_local_fragment _TERRAIN_DISTANCEBLEND
  118. #pragma shader_feature_local_domain _TESSELLATION_PHONG
  119. #pragma shader_feature_local _LAYERS_TWO _LAYERS_16
  120. #define INTERRA_TERRAIN
  121. #include "InTerra_TerrainLit_Splatmap_Includes.hlsl"
  122. ENDHLSL
  123. SubShader
  124. {
  125. PackageRequirements { "com.unity.render-pipelines.high-definition":"[12.0,13.99.9]" }
  126. // This tags allow to use the shader replacement features
  127. Tags
  128. {
  129. "RenderPipeline" = "HDRenderPipeline"
  130. "RenderType" = "Opaque"
  131. "SplatCount" = "8"
  132. "MaskMapR" = "Metallic"
  133. "MaskMapG" = "AO"
  134. "MaskMapB" = "Height"
  135. "MaskMapA" = "Smoothness"
  136. "DiffuseA" = "Smoothness (becomes Density when Mask map is assigned)" // when MaskMap is disabled
  137. "DiffuseA_MaskMapUsed" = "Density" // when MaskMap is enabled
  138. "TerrainCompatible" = "True"
  139. }
  140. // 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.
  141. Pass
  142. {
  143. Name "GBuffer"
  144. Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index
  145. Cull [_CullMode]
  146. ZTest [_ZTestGBuffer]
  147. Stencil
  148. {
  149. WriteMask [_StencilWriteMaskGBuffer]
  150. Ref [_StencilRefGBuffer]
  151. Comp Always
  152. Pass Replace
  153. }
  154. HLSLPROGRAM
  155. #pragma require tessellation tessHW
  156. #pragma multi_compile _ DEBUG_DISPLAY
  157. #pragma multi_compile _ LIGHTMAP_ON
  158. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  159. #pragma multi_compile _ DYNAMICLIGHTMAP_ON
  160. #pragma multi_compile_fragment _ SHADOWS_SHADOWMASK
  161. #pragma multi_compile_fragment PROBE_VOLUMES_OFF PROBE_VOLUMES_L1 PROBE_VOLUMES_L2
  162. // Setup DECALS_OFF so the shader stripper can remove variants
  163. #pragma multi_compile_fragment DECALS_OFF DECALS_3RT DECALS_4RT
  164. #pragma multi_compile_fragment _ DECAL_SURFACE_GRADIENT
  165. #pragma multi_compile_fragment _ LIGHT_LAYERS
  166. #define SHADERPASS SHADERPASS_GBUFFER
  167. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  168. #include "InTerra_TerrainLitTemplate.hlsl"
  169. #include "InTerra_TerrainLit_Splatmap.hlsl"
  170. #pragma vertex Vert
  171. #pragma fragment Frag
  172. #pragma hull Hull
  173. #pragma domain Domain
  174. ENDHLSL
  175. }
  176. // Extracts information for lightmapping, GI (emission, albedo, ...)
  177. // This pass it not used during regular rendering.
  178. Pass
  179. {
  180. Name "META"
  181. Tags{ "LightMode" = "META" }
  182. Cull Off
  183. HLSLPROGRAM
  184. // Lightmap memo
  185. // 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,
  186. // both direct and indirect lighting) will hand up in the "regular" lightmap->LIGHTMAP_ON.
  187. // No tessellation for Meta pass
  188. #undef TESSELLATION_ON
  189. #define SHADERPASS SHADERPASS_LIGHT_TRANSPORT
  190. #pragma shader_feature EDITOR_VISUALIZATION
  191. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  192. #include "InTerra_TerrainLitTemplate.hlsl"
  193. #include "InTerra_TerrainLit_Splatmap.hlsl"
  194. #pragma vertex Vert
  195. #pragma fragment Frag
  196. ENDHLSL
  197. }
  198. Pass
  199. {
  200. Name "ShadowCaster"
  201. Tags{ "LightMode" = "ShadowCaster" }
  202. Cull[_CullMode]
  203. ZClip [_ZClip]
  204. ZWrite On
  205. ZTest LEqual
  206. ColorMask 0
  207. HLSLPROGRAM
  208. #pragma require tessellation tessHW
  209. #define SHADERPASS SHADERPASS_SHADOWS
  210. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  211. #include "InTerra_TerrainLitTemplate.hlsl"
  212. #include "InTerra_TerrainLit_Splatmap.hlsl"
  213. #pragma vertex Vert
  214. #pragma fragment Frag
  215. #pragma hull Hull
  216. #pragma domain Domain
  217. ENDHLSL
  218. }
  219. Pass
  220. {
  221. Name "DepthOnly"
  222. Tags{ "LightMode" = "DepthOnly" }
  223. Cull[_CullMode]
  224. // To be able to tag stencil with disableSSR information for forward
  225. Stencil
  226. {
  227. WriteMask [_StencilWriteMaskDepth]
  228. Ref [_StencilRefDepth]
  229. Comp Always
  230. Pass Replace
  231. }
  232. ZWrite On
  233. HLSLPROGRAM
  234. #pragma require tessellation tessHW
  235. // In deferred, depth only pass don't output anything.
  236. // In forward it output the normal buffer
  237. #pragma multi_compile _ WRITE_NORMAL_BUFFER
  238. #pragma multi_compile_fragment _ WRITE_DECAL_BUFFER
  239. #pragma multi_compile _ WRITE_MSAA_DEPTH
  240. #define SHADERPASS SHADERPASS_DEPTH_ONLY
  241. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  242. #include "InTerra_TerrainLitTemplate.hlsl"
  243. #ifdef WRITE_NORMAL_BUFFER
  244. #if defined(_NORMALMAP)
  245. #define OVERRIDE_SPLAT_SAMPLER_NAME sampler_Normal0
  246. #elif defined(_MASKMAP)
  247. #define OVERRIDE_SPLAT_SAMPLER_NAME sampler_Mask0
  248. #endif
  249. #endif
  250. #include "InTerra_TerrainLit_Splatmap.hlsl"
  251. #pragma vertex Vert
  252. #pragma fragment Frag
  253. #pragma hull Hull
  254. #pragma domain Domain
  255. ENDHLSL
  256. }
  257. Pass
  258. {
  259. Name "Forward"
  260. Tags { "LightMode" = "Forward" } // This will be only for transparent object based on the RenderQueue index
  261. Stencil
  262. {
  263. WriteMask [_StencilWriteMask]
  264. Ref [_StencilRef]
  265. Comp Always
  266. Pass Replace
  267. }
  268. // In case of forward we want to have depth equal for opaque mesh
  269. ZTest [_ZTestDepthEqualForOpaque]
  270. ZWrite [_ZWrite]
  271. Cull [_CullMode]
  272. HLSLPROGRAM
  273. #pragma multi_compile _ DEBUG_DISPLAY
  274. #pragma multi_compile _ LIGHTMAP_ON
  275. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  276. #pragma multi_compile _ DYNAMICLIGHTMAP_ON
  277. #pragma multi_compile_fragment _ SHADOWS_SHADOWMASK
  278. #pragma multi_compile_fragment PROBE_VOLUMES_OFF PROBE_VOLUMES_L1 PROBE_VOLUMES_L2
  279. #pragma multi_compile_fragment SCREEN_SPACE_SHADOWS_OFF SCREEN_SPACE_SHADOWS_ON
  280. // Setup DECALS_OFF so the shader stripper can remove variants
  281. #pragma multi_compile_fragment DECALS_OFF DECALS_3RT DECALS_4RT
  282. #pragma multi_compile_fragment _ DECAL_SURFACE_GRADIENT
  283. // Supported shadow modes per light type
  284. #pragma multi_compile_fragment SHADOW_LOW SHADOW_MEDIUM SHADOW_HIGH SHADOW_VERY_HIGH
  285. #pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
  286. #pragma require tessellation tessHW
  287. #define SHADERPASS SHADERPASS_FORWARD
  288. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  289. #include "InTerra_TerrainLitTemplate.hlsl"
  290. #include "InTerra_TerrainLit_Splatmap.hlsl"
  291. #pragma vertex Vert
  292. #pragma fragment Frag
  293. #pragma hull Hull
  294. #pragma domain Domain
  295. ENDHLSL
  296. }
  297. Pass
  298. {
  299. Name "SceneSelectionPass"
  300. Tags { "LightMode" = "SceneSelectionPass" }
  301. Cull Off
  302. HLSLPROGRAM
  303. #pragma editor_sync_compilation
  304. #define SHADERPASS SHADERPASS_DEPTH_ONLY
  305. #define SCENESELECTIONPASS
  306. #undef TESSELLATION_ON
  307. #include "InTerra_HDRP_DefinedGlobalKeywords.hlsl"
  308. #include "InTerra_TerrainLitTemplate.hlsl"
  309. #include "InTerra_TerrainLit_Splatmap.hlsl"
  310. #pragma vertex Vert
  311. #pragma fragment Frag
  312. ENDHLSL
  313. }
  314. UsePass "Hidden/Nature/Terrain/Utilities/PICKING"
  315. }
  316. Dependency "BaseMapShader" = "Hidden/InTerra/HDRP/TerrainLit_Basemap"
  317. Dependency "BaseMapGenShader" = "Hidden/InTerra/HDRP/TerrainLit_BasemapGen"
  318. CustomEditor "InTerra.InTerra_TerrainShaderGUI"
  319. }