InTerra_TerrainLitPasses.hlsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #ifndef UNIVERSAL_TERRAIN_LIT_PASSES_INCLUDED
  2. #define UNIVERSAL_TERRAIN_LIT_PASSES_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #ifdef UNITY_6000_1_OR_NEWER
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/GBufferOutput.hlsl"
  6. #else
  7. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  8. #endif
  9. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl"
  10. #if defined(PARALLAX) || defined(_TERRAIN_TRIPLANAR) || defined(_TERRAIN_TRIPLANAR_ONE) || defined(_PUDDLES)
  11. #define TANGENT
  12. #endif
  13. struct Attributes
  14. {
  15. float4 positionOS : POSITION;
  16. float3 normalOS : NORMAL;
  17. float2 texcoord : TEXCOORD0;
  18. UNITY_VERTEX_INPUT_INSTANCE_ID
  19. };
  20. struct Varyings
  21. {
  22. float4 uvMainAndLM : TEXCOORD0; // xy: control, zw: lightmap
  23. #ifndef TERRAIN_SPLAT_BASEPASS
  24. float4 uvSplat01 : TEXCOORD1; // xy: splat0, zw: splat1
  25. float4 uvSplat23 : TEXCOORD2; // xy: splat2, zw: splat3
  26. #endif
  27. #if (defined(_NORMALMAPS) || defined(TANGENT)) && !defined(TERRAIN_SPLAT_BASEPASS)
  28. half4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
  29. half4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
  30. half4 bitangent : TEXCOORD5; // xyz: bitangent, w: viewDir.z
  31. #if defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  32. half3 vertexSH : TEXCOORD12; // SH
  33. #endif
  34. #else
  35. half3 normal : TEXCOORD3;
  36. half3 vertexSH : TEXCOORD4; // SH
  37. #endif
  38. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  39. half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
  40. #else
  41. half fogFactor : TEXCOORD6;
  42. #endif
  43. float3 positionWS : TEXCOORD7;
  44. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  45. float4 shadowCoord : TEXCOORD8;
  46. #endif
  47. #if defined(DYNAMICLIGHTMAP_ON)
  48. float2 dynamicLightmapUV : TEXCOORD9;
  49. #endif
  50. float4 uvSplat45 : TEXCOORD10; // xy: splat4, zw: splat5
  51. float4 uvSplat67 : TEXCOORD11; // xy: splat6, zw: splat7
  52. float4 clipPos : SV_POSITION;
  53. UNITY_VERTEX_OUTPUT_STEREO
  54. };
  55. void InitializeInputData(Varyings IN, half3 normalTS, out InputData inputData)
  56. {
  57. inputData = (InputData)0;
  58. inputData.positionWS = IN.positionWS;
  59. inputData.positionCS = IN.clipPos;
  60. #if (defined(_NORMALMAPS) || defined(TANGENT)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL) && !defined(TERRAIN_SPLAT_BASEPASS)
  61. half3 viewDirWS = half3(IN.normal.w, IN.tangent.w, IN.bitangent.w);
  62. inputData.tangentToWorld = half3x3(-IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz);
  63. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  64. half3 SH = 0;
  65. #elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  66. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(IN.positionWS);
  67. float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
  68. half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
  69. half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
  70. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(-tangentWS, cross(normalWS, tangentWS), normalWS));
  71. half3 SH = IN.vertexSH;
  72. #else
  73. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(IN.positionWS);
  74. inputData.normalWS = IN.normal;
  75. half3 SH = IN.vertexSH;
  76. #endif
  77. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  78. inputData.viewDirectionWS = viewDirWS;
  79. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  80. inputData.shadowCoord = IN.shadowCoord;
  81. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  82. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  83. #else
  84. inputData.shadowCoord = float4(0, 0, 0, 0);
  85. #endif
  86. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  87. inputData.fogCoord = InitializeInputDataFog(float4(IN.positionWS, 1.0), IN.fogFactorAndVertexLight.x);
  88. inputData.vertexLighting = IN.fogFactorAndVertexLight.yzw;
  89. #else
  90. inputData.fogCoord = InitializeInputDataFog(float4(IN.positionWS, 1.0), IN.fogFactor);
  91. #endif
  92. #if defined(DYNAMICLIGHTMAP_ON)
  93. inputData.bakedGI = SAMPLE_GI(IN.uvMainAndLM.zw, IN.dynamicLightmapUV, SH, inputData.normalWS);
  94. #else
  95. inputData.bakedGI = SAMPLE_GI(IN.uvMainAndLM.zw, SH, inputData.normalWS);
  96. #endif
  97. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(IN.clipPos);
  98. inputData.shadowMask = SAMPLE_SHADOWMASK(IN.uvMainAndLM.zw)
  99. #if defined(DEBUG_DISPLAY)
  100. #if defined(DYNAMICLIGHTMAP_ON)
  101. inputData.dynamicLightmapUV = IN.dynamicLightmapUV;
  102. #endif
  103. #if defined(LIGHTMAP_ON)
  104. inputData.staticLightmapUV = IN.uvMainAndLM.zw;
  105. #else
  106. inputData.vertexSH = SH;
  107. #endif
  108. #endif
  109. }
  110. void TriplanarBase(in out half4 baseMap, half4 front, half4 side, float3 weights, float2 splat, half firstToAllSteep)
  111. {
  112. baseMap = firstToAllSteep == 1 ? (baseMap * weights.y + front * weights.z + side * weights.x) : (baseMap * saturate(weights.y + (1 - splat.g))) + (((front * weights.z) + (side * weights.x)) * (splat.r));
  113. }
  114. float2 TerrainFrontUV(float3 wPos, half4 splatUV, float2 tc, float3 flip)
  115. {
  116. return float2(tc.x * -flip.z, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.y / _TerrainSizeXZPosY.y) + splatUV.w);
  117. }
  118. float2 TerrainSideUV(float3 wPos, half4 splatUV, float2 tc, float3 flip)
  119. {
  120. return float2(tc.y * flip.x, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.x / _TerrainSizeXZPosY.x) + splatUV.z);
  121. }
  122. #ifndef TERRAIN_SPLAT_BASEPASS
  123. #include "InTerra_SplatmapMix.hlsl"
  124. #endif
  125. void SplatmapFinalColor(inout half4 color, half fogCoord)
  126. {
  127. color.rgb *= color.a;
  128. #ifndef TERRAIN_GBUFFER // Technically we don't need fogCoord, but it is still passed from the vertex shader.
  129. #ifdef TERRAIN_SPLAT_ADDPASS
  130. color.rgb = MixFogColor(color.rgb, half3(0,0,0), fogCoord);
  131. #else
  132. color.rgb = MixFog(color.rgb, fogCoord);
  133. #endif
  134. #endif
  135. }
  136. ///////////////////////////////////////////////////////////////////////////////
  137. // Vertex and Fragment functions //
  138. ///////////////////////////////////////////////////////////////////////////////
  139. // Used in Standard Terrain shader
  140. Varyings SplatmapVert(Attributes v)
  141. {
  142. Varyings o = (Varyings)0;
  143. UNITY_SETUP_INSTANCE_ID(v);
  144. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  145. TerrainInstancing(v.positionOS, v.normalOS, v.texcoord);
  146. VertexPositionInputs Attributes = GetVertexPositionInputs(v.positionOS.xyz);
  147. o.uvMainAndLM.xy = v.texcoord;
  148. o.uvMainAndLM.zw = v.texcoord * unity_LightmapST.xy + unity_LightmapST.zw;
  149. float2 tc = _WorldMapping ? (Attributes.positionWS.xz / _TerrainSizeXZPosY.xy) : v.texcoord;
  150. #ifndef TERRAIN_SPLAT_BASEPASS
  151. o.uvSplat01.xy = TRANSFORM_TEX(tc, _Splat0);
  152. o.uvSplat01.zw = TRANSFORM_TEX(tc, _Splat1);
  153. #ifndef _LAYERS_TWO
  154. o.uvSplat23.xy = TRANSFORM_TEX(tc, _Splat2);
  155. o.uvSplat23.zw = TRANSFORM_TEX(tc, _Splat3);
  156. #ifdef _LAYERS_EIGHT
  157. o.uvSplat45.xy = TRANSFORM_TEX(tc, _Splat4);
  158. o.uvSplat45.zw = TRANSFORM_TEX(tc, _Splat5);
  159. o.uvSplat67.xy = TRANSFORM_TEX(tc, _Splat6);
  160. o.uvSplat67.zw = TRANSFORM_TEX(tc, _Splat7);
  161. #endif
  162. #endif
  163. #endif
  164. #if defined(DYNAMICLIGHTMAP_ON)
  165. o.dynamicLightmapUV = v.texcoord * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  166. #endif
  167. #if (defined(_NORMALMAPS) || defined(TANGENT)) && !defined(TERRAIN_SPLAT_BASEPASS)
  168. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(Attributes.positionWS);
  169. float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normalOS), 1.0);
  170. VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, vertexTangent);
  171. o.normal = half4(normalInput.normalWS, viewDirWS.x);
  172. o.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  173. o.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  174. #else
  175. o.normal = half4(TransformObjectToWorldNormal(v.normalOS), 0);
  176. o.vertexSH = SampleSH(o.normal);
  177. #endif
  178. half fogFactor = 0;
  179. #if !defined(_FOG_FRAGMENT)
  180. fogFactor = ComputeFogFactor(Attributes.positionCS.z);
  181. #endif
  182. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  183. o.fogFactorAndVertexLight.x = fogFactor;
  184. o.fogFactorAndVertexLight.yzw = VertexLighting(Attributes.positionWS, o.normal.xyz);
  185. #else
  186. o.fogFactor = fogFactor;
  187. #endif
  188. o.positionWS = Attributes.positionWS;
  189. o.clipPos = Attributes.positionCS;
  190. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  191. o.shadowCoord = GetShadowCoord(Attributes);
  192. #endif
  193. return o;
  194. }
  195. // Used in Standard Terrain shader
  196. #ifdef TERRAIN_GBUFFER
  197. #ifdef UNITY_6000_1_OR_NEWER
  198. GBufferFragOutput SplatmapFragment(Varyings IN)
  199. #else
  200. FragmentOutput SplatmapFragment(Varyings IN)
  201. #endif
  202. #else
  203. #ifdef UNITY_6000_1_OR_NEWER
  204. void SplatmapFragment(
  205. Varyings IN
  206. , out half4 outColor : SV_Target0
  207. #ifdef _WRITE_RENDERING_LAYERS
  208. , out float4 outRenderingLayers : SV_Target1
  209. #endif
  210. )
  211. #else
  212. half4 SplatmapFragment(Varyings IN) : SV_TARGET
  213. #endif
  214. #endif
  215. {
  216. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
  217. #ifdef _ALPHATEST_ON
  218. ClipHoles(IN.uvMainAndLM.xy);
  219. #endif
  220. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  221. #ifdef TERRAIN_SPLAT_BASEPASS
  222. half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uvMainAndLM.xy);
  223. half4 metallic_AO_splat01 = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MetallicTex, IN.uvMainAndLM.xy);
  224. #ifdef _TERRAIN_TRIPLANAR_ONE
  225. float3 weights = abs(IN.normal);
  226. weights = pow(weights, _TriplanarSharpness);
  227. weights = weights / (weights.x + weights.y + weights.z);
  228. float3 flipUV = float3(1, 1, -1); //IN.normal.rgb < 0 ? -1 : 1;
  229. float2 frontUV = TerrainFrontUV(IN.positionWS, _MainTex_ST, IN.uvMainAndLM.xy, flipUV);
  230. float2 sideUV = TerrainSideUV(IN.positionWS, _MainTex_ST, IN.uvMainAndLM.xy, flipUV);
  231. half4 cFront = SAMPLE_TEXTURE2D(_TriplanarTex, sampler_TriplanarTex, frontUV);
  232. half4 cSide = SAMPLE_TEXTURE2D(_TriplanarTex, sampler_TriplanarTex, sideUV);
  233. TriplanarBase(mainTex, cFront, cSide, weights, metallic_AO_splat01.b, _TriplanarOneToAllSteep);
  234. float3 tint = SAMPLE_TEXTURE2D(_TerrainColorTintTexture, SamplerState_Linear_Repeat, IN.uvMainAndLM.xy *_TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw).rgb;
  235. mainTex.rgb = lerp(mainTex.rgb, ((mainTex.rgb) * (tint)), _TerrainColorTintStrenght).rgb;
  236. half4 mAoFront = SAMPLE_TEXTURE2D(_Triplanar_MetallicAO, sampler_Triplanar_MetallicAO, frontUV);
  237. half4 mAoSide = SAMPLE_TEXTURE2D(_Triplanar_MetallicAO, sampler_Triplanar_MetallicAO, sideUV);
  238. TriplanarBase(metallic_AO_splat01, mAoFront, mAoSide, weights, metallic_AO_splat01.b, _TriplanarOneToAllSteep);
  239. #endif
  240. half3 albedo = mainTex.rgb;
  241. half smoothness = lerp(mainTex.a, 1.0f, _InTerra_GlobalWetness);
  242. half metallic = metallic_AO_splat01.r;
  243. half occlusion = metallic_AO_splat01.g;
  244. half alpha = 1;
  245. #else
  246. half alpha;
  247. half4 mixedDiffuse = half4(0.0f, 0.0f, 0.0f, 0.0f);;
  248. half4 defaultSmoothness;
  249. half metallic = 0;
  250. half occlusion = 1;
  251. half smoothness = 0;
  252. float3 tangentViewDir = float3(0,0,0);
  253. float2 uv[_LAYER_COUNT];
  254. uv[0] = IN.uvSplat01.xy;
  255. uv[1] = IN.uvSplat01.zw;
  256. #ifndef _LAYERS_TWO
  257. uv[2] = IN.uvSplat23.xy;
  258. uv[3] = IN.uvSplat23.zw;
  259. #ifdef _LAYERS_EIGHT
  260. float2 tc = _WorldMapping ? (IN.positionWS.xz / _TerrainSizeXZPosY.xy) : IN.uvMainAndLM.xy;
  261. uv[4] = TRANSFORM_TEX(tc, _Splat4);
  262. uv[5] = TRANSFORM_TEX(tc, _Splat5);
  263. uv[6] = TRANSFORM_TEX(tc, _Splat6);
  264. uv[7] = TRANSFORM_TEX(tc, _Splat7);
  265. #endif
  266. #endif
  267. #if defined(_LAYERS_EIGHT) && defined(TERRAIN_SPLAT_ADDPASS)
  268. alpha = 0;
  269. #else
  270. #ifdef PARALLAX
  271. float3x3 objectToTangent = float3x3((IN.tangent.xyz), (cross(IN.normal.xyz, IN.tangent.xyz)) * -1, IN.normal.xyz);
  272. tangentViewDir = -normalize(mul(objectToTangent, GetWorldSpaceViewDir(IN.positionWS)));
  273. #endif
  274. #if defined(TRIPLANAR) || defined(PARALLAX) || defined(_PUDDLES)
  275. SplatmapMix(IN.uvMainAndLM, uv, IN.normal.xyz, tangentViewDir, IN.positionWS, alpha, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  276. #else
  277. SplatmapMix(IN.uvMainAndLM, uv, float3(0,0,0), float3(0, 0, 0), IN.positionWS, alpha, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  278. #endif
  279. #endif
  280. half3 albedo = mixedDiffuse.rgb;
  281. #endif
  282. InputData inputData;
  283. InitializeInputData(IN, normalTS, inputData);
  284. // SETUP_DEBUG_TEXTURE_DATA(inputData, IN.uvMainAndLM.xy, _BaseMap);
  285. #if defined(_DBUFFER)
  286. half3 specular = half3(0.0h, 0.0h, 0.0h);
  287. ApplyDecal(IN.clipPos,
  288. albedo,
  289. specular,
  290. inputData.normalWS,
  291. metallic,
  292. occlusion,
  293. smoothness);
  294. #endif
  295. #ifdef TERRAIN_GBUFFER
  296. BRDFData brdfData;
  297. InitializeBRDFData(albedo, metallic, /* specular */ half3(0.0h, 0.0h, 0.0h), smoothness, alpha, brdfData);
  298. // Baked lighting.
  299. half4 color;
  300. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  301. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  302. color.rgb = GlobalIllumination(brdfData, inputData.bakedGI, occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  303. color.a = alpha;
  304. SplatmapFinalColor(color, inputData.fogCoord);
  305. // Dynamic lighting: emulate SplatmapFinalColor() by scaling gbuffer material properties. This will not give the same results
  306. // as forward renderer because we apply blending pre-lighting instead of post-lighting.
  307. // Blending of smoothness and normals is also not correct but close enough?
  308. brdfData.albedo.rgb *= alpha;
  309. brdfData.diffuse.rgb *= alpha;
  310. brdfData.specular.rgb *= alpha;
  311. brdfData.reflectivity *= alpha;
  312. inputData.normalWS = inputData.normalWS * alpha;
  313. smoothness *= alpha;
  314. #ifdef UNITY_6000_1_OR_NEWER
  315. return PackGBuffersBRDFData(brdfData, inputData, smoothness, color.rgb, occlusion);
  316. #else
  317. return BRDFDataToGbuffer(brdfData, inputData, smoothness, color.rgb, occlusion);
  318. #endif
  319. #else
  320. half4 color = UniversalFragmentPBR(inputData, albedo, metallic, /* specular */ half3(0.0h, 0.0h, 0.0h), smoothness, occlusion, /* emission */ half3(0, 0, 0), alpha);
  321. SplatmapFinalColor(color, inputData.fogCoord);
  322. #ifdef UNITY_6000_1_OR_NEWER
  323. outColor = half4(color.rgb, 1.0h);
  324. #ifdef _WRITE_RENDERING_LAYERS
  325. uint renderingLayers = GetMeshRenderingLayer();
  326. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  327. #endif
  328. #else
  329. return half4(color.rgb, 1.0h);
  330. #endif
  331. #endif
  332. }
  333. // Shadow pass
  334. // Shadow Casting Light geometric parameters. These variables are used when applying the shadow Normal Bias and are set by UnityEngine.Rendering.Universal.ShadowUtils.SetupShadowCasterConstantBuffer in com.unity.render-pipelines.universal/Runtime/ShadowUtils.cs
  335. // For Directional lights, _LightDirection is used when applying shadow Normal Bias.
  336. // For Spot lights and Point lights, _LightPosition is used to compute the actual light direction because it is different at each shadow caster geometry vertex.
  337. float3 _LightDirection;
  338. float3 _LightPosition;
  339. struct AttributesLean
  340. {
  341. float4 position : POSITION;
  342. float3 normalOS : NORMAL;
  343. float2 texcoord : TEXCOORD0;
  344. UNITY_VERTEX_INPUT_INSTANCE_ID
  345. };
  346. struct VaryingsLean
  347. {
  348. float4 clipPos : SV_POSITION;
  349. float2 texcoord : TEXCOORD0;
  350. UNITY_VERTEX_OUTPUT_STEREO
  351. };
  352. VaryingsLean ShadowPassVertex(AttributesLean v)
  353. {
  354. VaryingsLean o = (VaryingsLean)0;
  355. UNITY_SETUP_INSTANCE_ID(v);
  356. TerrainInstancing(v.position, v.normalOS, v.texcoord);
  357. float3 positionWS = TransformObjectToWorld(v.position.xyz);
  358. float3 normalWS = TransformObjectToWorldNormal(v.normalOS);
  359. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  360. float3 lightDirectionWS = normalize(_LightPosition - positionWS);
  361. #else
  362. float3 lightDirectionWS = _LightDirection;
  363. #endif
  364. float4 clipPos = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
  365. #if UNITY_REVERSED_Z
  366. clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
  367. #else
  368. clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
  369. #endif
  370. o.clipPos = clipPos;
  371. o.texcoord = v.texcoord;
  372. return o;
  373. }
  374. half4 ShadowPassFragment(VaryingsLean IN) : SV_TARGET
  375. {
  376. #ifdef _ALPHATEST_ON
  377. ClipHoles(IN.texcoord);
  378. #endif
  379. return 0;
  380. }
  381. // Depth pass
  382. VaryingsLean DepthOnlyVertex(AttributesLean v)
  383. {
  384. VaryingsLean o = (VaryingsLean)0;
  385. UNITY_SETUP_INSTANCE_ID(v);
  386. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  387. TerrainInstancing(v.position, v.normalOS);
  388. o.clipPos = TransformObjectToHClip(v.position.xyz);
  389. o.texcoord = v.texcoord;
  390. return o;
  391. }
  392. half4 DepthOnlyFragment(VaryingsLean IN) : SV_TARGET
  393. {
  394. #ifdef _ALPHATEST_ON
  395. ClipHoles(IN.texcoord);
  396. #endif
  397. #ifdef SCENESELECTIONPASS
  398. // We use depth prepass for scene selection in the editor, this code allow to output the outline correctly
  399. return half4(_ObjectId, _PassValue, 1.0, 1.0);
  400. #endif
  401. return 0;
  402. }
  403. #endif