InTerra_InputsAndFunctions.cginc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #ifdef _LAYERS_ONE
  2. #define _LAYER_COUNT 1
  3. #else
  4. #ifdef _LAYERS_TWO
  5. #define _LAYER_COUNT 2
  6. #elif defined(_LAYERS_EIGHT)
  7. #define _LAYER_COUNT 8
  8. #else
  9. #define _LAYER_COUNT 4
  10. #endif
  11. #endif
  12. #ifndef TERRAIN_BASEGEN
  13. struct Input
  14. {
  15. float3 worldPos;
  16. #if defined(INTERRA_TERRAIN)
  17. float4 tc;
  18. #else
  19. float4 mainTC_tWeightY_hOffset;
  20. #if defined(_OBJECT_PARALLAX) || (defined(INTERRA_MESH_TERRAIN) && defined(_TERRAIN_PARALLAX))
  21. float3 tangentViewDirObject;
  22. #endif
  23. #endif
  24. float3 worldNormal;
  25. float3 terrainNormals;
  26. #ifdef _TERRAIN_PARALLAX
  27. float3 tangentViewDir;
  28. #endif
  29. UNITY_FOG_COORDS(0) // needed because finalcolor oppresses fog code generation.
  30. INTERNAL_DATA
  31. };
  32. #endif
  33. #ifdef INTERRA_TERRAIN
  34. #if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X)
  35. sampler2D _TerrainHeightmapTexture;
  36. sampler2D _TerrainNormalmapTexture;
  37. float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
  38. #endif
  39. UNITY_INSTANCING_BUFFER_START(Terrain)
  40. UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
  41. UNITY_INSTANCING_BUFFER_END(Terrain)
  42. #ifdef _ALPHATEST_ON
  43. sampler2D _TerrainHolesTexture;
  44. void ClipHoles(float2 uv)
  45. {
  46. float hole = tex2D(_TerrainHolesTexture, uv).r;
  47. clip(hole == 0.0f ? -1 : 1);
  48. }
  49. #endif
  50. #if defined(TERRAIN_BASE_PASS) && (defined(UNITY_PASS_META) || defined(TRIPLANAR))
  51. // When we render albedo for GI baking, we actually need to take the ST, or for triplanar mapping
  52. float4 _MainTex_ST;
  53. #endif
  54. float3 _TerrainSizeXZPosY;
  55. #endif
  56. UNITY_DECLARE_TEX2D (_Control);
  57. #if defined(_LAYERS_EIGHT)
  58. #endif
  59. UNITY_DECLARE_TEX2D(_Control2);
  60. UNITY_DECLARE_TEX2D(_Control1);
  61. float4 _Control_ST, _Control_TexelSize;
  62. float4 _TerrainHeightmapTexture_TexelSize;
  63. float4 _TerrainPosition, _TerrainSize;
  64. float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
  65. float _NumLayersCount;
  66. float4 _HT_distance, _MipMapFade;
  67. int _MipMapLevel;
  68. float _HT_distance_scale, _HT_cover;
  69. fixed _HeightTransition, _Distance_HeightTransition, _TriplanarOneToAllSteep, _TriplanarSharpness;
  70. fixed _ParallaxAffineStepsTerrain;
  71. #if defined(TERRAIN_BASEGEN) || defined(TERRAIN_BASE_PASS)
  72. sampler2D _TerrainColorTintTexture;
  73. #else
  74. UNITY_DECLARE_TEX2D_NOSAMPLER(_TerrainColorTintTexture);
  75. UNITY_DECLARE_TEX2D_NOSAMPLER(_TerrainNormalTintTexture);
  76. #endif
  77. float _TerrainColorTintStrenght;
  78. float4 _TerrainColorTintTexture_ST;
  79. float _TerrainNormalTintStrenght;
  80. float4 _TerrainNormalTintTexture_ST;
  81. float4 _TerrainNormalTintDistance;
  82. float _HeightmapBlending;
  83. float _GlobalWetness;
  84. float _Gamma;
  85. float _Tracks;
  86. float _Terrain_Parallax;
  87. float _WorldMapping;
  88. fixed _ControlNumber;
  89. float _MipMapMinLod;
  90. //-----Track Property -----
  91. float _TrackAO;
  92. float _TrackEdgeNormals, _TrackEdgeSharpness;
  93. float _TrackNormalStrenght;
  94. float _TrackDetailNormalStrenght;
  95. float _TrackHeightOffset;
  96. float4 _TrackDetailTexture_ST;
  97. float _ParallaxTrackAffineSteps;
  98. float _ParallaxTrackSteps;
  99. float _TrackHeightTransition;
  100. float _TrackMultiplyStrenght;
  101. UNITY_DECLARE_TEX2D_NOSAMPLER(_TrackDetailTexture);
  102. UNITY_DECLARE_TEX2D_NOSAMPLER(_TrackDetailNormalTexture);
  103. //----- Global Property -----
  104. float _InTerra_TrackArea;
  105. float3 _InTerra_TrackPosition;
  106. UNITY_DECLARE_TEX2D_NOSAMPLER(_InTerra_TrackTexture);
  107. float4 _InTerra_TrackTexture_TexelSize;
  108. float2 _InTerra_GlobalPuddles;
  109. float3 _InTerra_GlobalRaindropRipples;
  110. float _InTerra_GlobalWetness;
  111. float4 _InTerra_GlobalRaindropsDistance;
  112. #ifndef INTERRA_TERRAIN
  113. fixed _GlobalWetnessDisabled;
  114. fixed _Detail;
  115. UNITY_DECLARE_TEX2D(_MainTex);
  116. #if !defined(_LAYERS_ONE)
  117. UNITY_DECLARE_TEX2D_NOSAMPLER(_BumpMap);
  118. UNITY_DECLARE_TEX2D_NOSAMPLER(_EmissionMap);
  119. UNITY_DECLARE_TEX2D_NOSAMPLER(_DetailAlbedoMap);
  120. UNITY_DECLARE_TEX2D_NOSAMPLER(_DetailNormalMap);
  121. #else
  122. sampler2D _BumpMap, _EmissionMap;
  123. sampler2D _DetailAlbedoMap, _DetailNormalMap;
  124. #endif
  125. #if !defined(DIFFUSE)
  126. UNITY_DECLARE_TEX2D_NOSAMPLER(_MainMask);
  127. #endif
  128. half4 _Color;
  129. half _BumpScale, _Ao, _Glossiness, _Metallic, _MipMapCount;
  130. float4 _MainTex_ST;
  131. float _EmissionEnabled, _EmissionIntensity;
  132. float4 _EmissionMap_ST;
  133. half4 _EmissionColor;
  134. fixed _HasMask, _PassNumber;
  135. half4 _MaskMapRemapScale, _MaskMapRemapOffset;
  136. float4 _DetailAlbedoMap_ST;
  137. half _DetailStrenght, _DetailNormalMapScale, _DetailNormalStrenght;
  138. #ifdef _OBJECT_PARALLAX
  139. float _Object_Parallax, _ParallaxHeight, _ParallaxSteps, _ParallaxAffineSteps;
  140. #endif
  141. half4 _Intersection, _Intersection2, _NormIntersect;
  142. half _Sharpness;
  143. half _Steepness, _SteepDistortion, _SteepIntersection;
  144. half4 _TerrainSmoothness, _TerrainMetallic;
  145. fixed _DisableOffsetY, _DisableDistanceBlending;
  146. sampler2D _TerrainHeightmapTexture;
  147. sampler2D _TerrainNormalmapTexture;
  148. #if defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)
  149. float4 _TerrainNormalScale;
  150. #endif
  151. #endif
  152. #ifdef INTERRA_MESH_TERRAIN
  153. fixed _NormalsFromHeightmap;
  154. fixed _CheckHeight;
  155. #endif
  156. fixed _TwoLayersOnly;
  157. #if defined(INTERRA_OBJECT) //|| defined(TERRAIN_CUSTOM_MESH)
  158. #define SplatST(n) float4 _SplatUV##n
  159. #else
  160. #define SplatST(n) float4 _Splat##n##_ST, _SplatUV##n
  161. #endif
  162. float4 _TerrainNormalScale1;
  163. half4 _TerrainSmoothness1, _TerrainMetallic1;
  164. #define DECLARE_TERRAIN_LAYER_PROPS(n) \
  165. float _Metallic##n; \
  166. float _Smoothness##n; \
  167. float _NormalScale##n; \
  168. float4 _DiffuseRemapOffset##n; \
  169. float4 _DiffuseRemapScale##n; \
  170. float4 _MaskMapRemapOffset##n; \
  171. float4 _MaskMapRemapScale##n; \
  172. float _LayerHasMask##n; \
  173. float4 _Normal##n##_TexelSize; \
  174. float4 _Splat##n##_TexelSize; \
  175. float4 _Mask##n##_TexelSize; \
  176. float4 _Specular##n; \
  177. SplatST(n); \
  178. UNITY_DECLARE_TEX2D_NOSAMPLER(_Splat##n); \
  179. UNITY_DECLARE_TEX2D_NOSAMPLER(_Normal##n); \
  180. UNITY_DECLARE_TEX2D_NOSAMPLER(_Mask##n); \
  181. DECLARE_TERRAIN_LAYER_PROPS(0)
  182. #ifndef _LAYERS_ONE
  183. DECLARE_TERRAIN_LAYER_PROPS(1)
  184. #ifndef _LAYERS_TWO
  185. DECLARE_TERRAIN_LAYER_PROPS(2)
  186. DECLARE_TERRAIN_LAYER_PROPS(3)
  187. #ifdef _LAYERS_EIGHT
  188. DECLARE_TERRAIN_LAYER_PROPS(4)
  189. DECLARE_TERRAIN_LAYER_PROPS(5)
  190. DECLARE_TERRAIN_LAYER_PROPS(6)
  191. DECLARE_TERRAIN_LAYER_PROPS(7)
  192. #endif
  193. #endif
  194. #endif
  195. SamplerState sampler_Splat0;
  196. SamplerState sampler_Mask0;
  197. //=======================================================================================
  198. //=================================== FUNCTIONS =====================================
  199. //=======================================================================================
  200. half3 BlendNormal(half3 n1, half3 n2)
  201. {
  202. return normalize(half3(n1.xy + n2.xy, n1.z * n2.z));
  203. }
  204. float2 ObjectFrontUV(float posOffset, half4 splatUV, float offsetZ)
  205. {
  206. return float2((posOffset + splatUV.z) / splatUV.x, (offsetZ + splatUV.w) / splatUV.y);
  207. }
  208. float2 ObjectSideUV(float posOffset, half4 splatUV, float offsetX)
  209. {
  210. return float2((offsetX + splatUV.z) / splatUV.x, (posOffset + splatUV.w) / splatUV.y);
  211. }
  212. half3 WorldTangent(float3 wTangent, float3 wBTangent, half3 mixedNormal)
  213. {
  214. mixedNormal.xy = mul(float2x2(wTangent.xz, wBTangent.xz), mixedNormal.xy);
  215. return half3(mixedNormal);
  216. }
  217. half3 WorldTangentFrontSide(float3 wTangent, float3 wBTangent, half3 mixedNormal, half3 normal_front, half3 normal_side, fixed3 flipUV, half3 weights)
  218. {
  219. normal_front.y *= -flipUV.z;
  220. normal_front.xy = mul(float2x2(wTangent.xy, wBTangent.xy), normal_front.xy);
  221. normal_side.x *= -flipUV.x;
  222. normal_side.xy = mul(float2x2(wTangent.yz, wBTangent.yz), normal_side.xy);
  223. return half3(mixedNormal * weights.y + normal_front * weights.z + normal_side * weights.x);
  224. }
  225. half2 HeightBlendTwoTextures(float2 splat, float2 heights, fixed sharpness)
  226. {
  227. splat *= (1 / (1 * pow(2, heights * (-(sharpness)))) + 1) * 0.5;
  228. splat /= (splat.r + splat.g);
  229. return splat;
  230. }
  231. #if defined(TRIPLANAR) && defined(INTERRA_TERRAIN)
  232. float2 TerrainFrontUV(float3 wPos, half4 splatUV, float2 tc, float3 flip)
  233. {
  234. return float2(tc.x * -flip.z, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.y / _TerrainSizeXZPosY.y) + splatUV.w);
  235. }
  236. float2 TerrainSideUV(float3 wPos, half4 splatUV, float2 tc, float3 flip)
  237. {
  238. return float2(tc.y * flip.x, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.x / _TerrainSizeXZPosY.x) + splatUV.z);
  239. }
  240. #endif
  241. void TriplanarOneToAllSteep(in out half4 blendMask[2], float weightY, in out half splatWeight)
  242. {
  243. if (_TriplanarOneToAllSteep == 1)
  244. {
  245. #if !defined(TERRAIN_SPLAT_ADDPASS)
  246. blendMask[0] = float4(saturate(blendMask[0].r + weightY), saturate((blendMask[0].gba) - weightY));
  247. blendMask[1] = float4(saturate((blendMask[1].rgba) - weightY));
  248. splatWeight = saturate(splatWeight + weightY);
  249. blendMask[0] = float4(saturate(blendMask[0].r + weightY), saturate(blendMask[0].gba - weightY));
  250. splatWeight = saturate(splatWeight + weightY);
  251. #else
  252. blendMask[0] = float4(saturate(blendMask[0].rgba - weightY));
  253. splatWeight = saturate(splatWeight - weightY);
  254. #endif
  255. }
  256. }
  257. half3 TriplanarNormal(half3 normal, half3 tangent, half3 bTangent, half3 normal_front, half3 normal_side, float3 weights, fixed3 flipUV)
  258. {
  259. #ifndef INTERRA_TERRAIN
  260. normal_front.y *= -flipUV.z;
  261. normal_front.xy = mul(float2x2(tangent.xy, bTangent.xy), normal_front.xy);
  262. normal_side.x *= -flipUV.x;
  263. normal_side.xy = mul(float2x2(tangent.yz, bTangent.yz), normal_side.xy);
  264. #else
  265. normal_side.xy = normal_side.yx; //this is needed because the uv was rotated
  266. normal_front.xy *= -flipUV.z;
  267. normal_side.x *= -flipUV.x;
  268. normal_side.y *= flipUV.x;
  269. #endif
  270. return half3 (normal + normal_front + normal_side);
  271. }
  272. void TriplanarBase(in out half4 baseMap, half4 front, half4 side, float3 weights, float2 splat, fixed firstToAllSteep)
  273. {
  274. 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));
  275. }
  276. #define MipMapLod(i, lod) float(_MipMapLevel + (lod * log2(max(_Mask##i##_TexelSize.z, _Mask##i##_TexelSize.w)) + 1))
  277. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  278. #define DiffuseRemap(i) float4(_DiffuseRemapScale##i.xyzw)
  279. #else
  280. #define DiffuseRemap(i) float4(_DiffuseRemapScale##i.xyzw + _DiffuseRemapOffset##i.xyzw)
  281. #endif
  282. #if defined (PARALLAX)
  283. float3 TangentViewDir(float3 normal, float4 tangent, float3 viewDir)
  284. {
  285. float3x3 objectToTangent = float3x3((tangent.xyz), (cross(normal, tangent.xyz)) * tangent.w, (normal));
  286. return mul(objectToTangent, viewDir);
  287. }
  288. float GetParallaxHeight(Texture2D mask, float2 uv, float2 offset, float lod, int invert)
  289. {
  290. return abs(mask.SampleLevel(sampler_Splat0, uv + offset, lod).b - invert);
  291. }
  292. //this function is based on Parallax Occlusion Mapping from Unity Shader Graph URP/HDRP
  293. float2 ParallaxOffset(Texture2D mask, int numSteps, float amplitude, float2 uv, float3 tangentViewDir, int affineSteps, int lod, int invert)
  294. {
  295. float2 offset = 0;
  296. if (numSteps > 0)
  297. {
  298. float3 viewDir = float3(tangentViewDir.xy * amplitude * -0.01f, tangentViewDir.z);
  299. float stepSize = (1.0 / numSteps);
  300. float2 texOffsetPerStep = (stepSize * viewDir);
  301. // Do a first step before the loop to init all value correctly
  302. float2 texOffsetCurrent = float2(0.0f, 0.0f);
  303. float prevHeight = GetParallaxHeight(mask, uv, texOffsetCurrent, lod, invert);
  304. texOffsetCurrent += texOffsetPerStep;
  305. float currHeight = GetParallaxHeight(mask, uv, texOffsetCurrent, lod, invert);
  306. float rayHeight = 1.0f - stepSize; // Start at top less one sample
  307. for (int stepIndex = 0; stepIndex < numSteps; ++stepIndex)
  308. {
  309. // Have we found a height below our ray height ? then we have an intersection
  310. if (currHeight > rayHeight)
  311. break; // end the loop
  312. prevHeight = currHeight;
  313. rayHeight -= stepSize;
  314. texOffsetCurrent += texOffsetPerStep;
  315. currHeight = GetParallaxHeight(mask, uv, texOffsetCurrent, lod, invert);
  316. }
  317. if (affineSteps <= 1)
  318. {
  319. float delta0 = currHeight - rayHeight;
  320. float delta1 = (rayHeight + stepSize) - prevHeight;
  321. float ratio = delta0 / (delta0 + delta1);
  322. offset = texOffsetCurrent - ratio * texOffsetPerStep;
  323. }
  324. else
  325. {
  326. float pt0 = rayHeight + stepSize;
  327. float pt1 = rayHeight;
  328. float delta0 = pt0 - prevHeight;
  329. float delta1 = pt1 - currHeight;
  330. float delta;
  331. // Secant method to affine the search
  332. // Ref: Faster Relief Mapping Using the Secant Method - Eric Risser
  333. for (int i = 0; i < affineSteps; ++i)
  334. {
  335. // intersectionHeight is the height [0..1] for the intersection between view ray and heightfield line
  336. float intersectionHeight = (pt0 * delta1 - pt1 * delta0) / (delta1 - delta0);
  337. // Retrieve offset require to find this intersectionHeight
  338. offset = (1 - intersectionHeight) * texOffsetPerStep * numSteps;
  339. currHeight = GetParallaxHeight(mask, uv, offset, lod, invert);
  340. delta = intersectionHeight - currHeight;
  341. if (abs(delta) <= 0.01f)
  342. break;
  343. // intersectionHeight < currHeight => new lower bounds
  344. if (delta < 0.0f)
  345. {
  346. delta1 = delta;
  347. pt1 = intersectionHeight;
  348. }
  349. else
  350. {
  351. delta0 = delta;
  352. pt0 = intersectionHeight;
  353. }
  354. }
  355. }
  356. }
  357. return offset;
  358. }
  359. void ParallaxUV(inout float2 uv[_LAYER_COUNT], float3 tangentViewDir, half4 blendMask[2], int lod, float weight)
  360. {
  361. #define uvParallax(i, blendMask) \
  362. UNITY_BRANCH if (blendMask * weight > 0.0001f) \
  363. { \
  364. uv[i] += ParallaxOffset(_Mask##i, _DiffuseRemapOffset##i.w, DiffuseRemap(i).w, uv[i], tangentViewDir, _ParallaxAffineStepsTerrain, _MipMapLevel + (lod * (log2(max(_Mask##i##_TexelSize.z, _Mask##i##_TexelSize.w)) + 1)), 0);\
  365. }\
  366. UNITY_BRANCH if (_Terrain_Parallax == 1)
  367. {
  368. uvParallax(0, blendMask[0].r);
  369. #ifndef _LAYERS_ONE
  370. uvParallax(1, blendMask[0].g);
  371. #ifndef _LAYERS_TWO
  372. if (_TwoLayersOnly < 1)
  373. {
  374. uvParallax(2, blendMask[0].b);
  375. uvParallax(3, blendMask[0].a);
  376. #ifdef _LAYERS_EIGHT
  377. uvParallax(4, blendMask[1].r);
  378. uvParallax(5, blendMask[1].g);
  379. uvParallax(6, blendMask[1].b);
  380. uvParallax(7, blendMask[1].a);
  381. #endif
  382. }
  383. #endif
  384. #endif
  385. }
  386. }
  387. #endif
  388. float4 TrackSplatValues(half4 blendMask[2], float4 trackSplats[_LAYER_COUNT])
  389. {
  390. #ifdef _LAYERS_ONE
  391. return trackSplats[0];
  392. #else
  393. float4 color = (blendMask[0].r * trackSplats[0])
  394. + (blendMask[0].g * trackSplats[1]);
  395. #ifndef _LAYERS_TWO
  396. if (_TwoLayersOnly < 1)
  397. {
  398. color += (blendMask[0].b * trackSplats[2])
  399. + (blendMask[0].a * trackSplats[3]);
  400. #ifdef _LAYERS_EIGHT
  401. color += (blendMask[1].r * trackSplats[4])
  402. + (blendMask[1].g * trackSplats[5])
  403. + (blendMask[1].b * trackSplats[6])
  404. + (blendMask[1].a * trackSplats[7]);
  405. #endif
  406. }
  407. #endif
  408. return color;
  409. #endif
  410. }
  411. #ifndef INTERRA_TERRAIN
  412. #define SpecularValueR(i) _Specular##i.r;
  413. #define SpecularValueG(i) _Specular##i.g;
  414. #define SpecularValueB(i) _Specular##i.b;
  415. #else
  416. #define SpecularValueR(i) _Gamma ? _Specular##i.r : pow(_Specular##i.r,1/2.2f);
  417. #define SpecularValueG(i) _Gamma ? _Specular##i.g : pow(_Specular##i.g,1/2.2f);
  418. #define SpecularValueB(i) _Gamma ? _Specular##i.b : pow(_Specular##i.b,1/2.2f);
  419. #endif
  420. void UnpackTrackSplatValues(out float4 trackSplats[_LAYER_COUNT])
  421. {
  422. float value;
  423. int precision = 1024;
  424. #define trackSplat(i) value = SpecularValueR(i); \
  425. trackSplats[i].z = value % precision; \
  426. value = floor(value / precision); \
  427. trackSplats[i].x = value; \
  428. trackSplats[i] /= (precision - 1); \
  429. \
  430. trackSplats[i].y = (_DiffuseRemapOffset##i.w * 10.0f) % 1 ; \
  431. trackSplats[i].w = floor((_DiffuseRemapOffset##i.w % 1 ) * 10.0f); \
  432. trackSplat(0);
  433. #ifndef _LAYERS_ONE
  434. trackSplat(1);
  435. #ifndef _LAYERS_TWO
  436. if (_TwoLayersOnly < 1)
  437. {
  438. trackSplat(2);
  439. trackSplat(3);
  440. #ifdef _LAYERS_EIGHT
  441. trackSplat(4);
  442. trackSplat(5);
  443. trackSplat(6);
  444. trackSplat(7);
  445. #endif
  446. }
  447. #endif
  448. #endif
  449. }
  450. void UnpackTrackSplatColor(out float4 trackSplatsColor[_LAYER_COUNT])
  451. {
  452. float color;
  453. float value;
  454. int precision = 1024;
  455. #define trackSplatColor(i) color = SpecularValueG(i) \
  456. \
  457. trackSplatsColor[i].y = color % precision; \
  458. color = floor(color / precision); \
  459. trackSplatsColor[i].x = color; \
  460. value = SpecularValueB(i); \
  461. trackSplatsColor[i].w = value % precision; \
  462. value = floor(value / precision); \
  463. trackSplatsColor[i].z = value % precision; \
  464. trackSplatsColor[i] /= (precision - 1); \
  465. trackSplatColor(0);
  466. #ifndef _LAYERS_ONE
  467. trackSplatColor(1);
  468. #ifndef _LAYERS_TWO
  469. if (_TwoLayersOnly < 1)
  470. {
  471. trackSplatColor(2);
  472. trackSplatColor(3);
  473. #ifdef _LAYERS_EIGHT
  474. trackSplatColor(4);
  475. trackSplatColor(5);
  476. trackSplatColor(6);
  477. trackSplatColor(7);
  478. #endif
  479. }
  480. #endif
  481. #endif
  482. }
  483. half3 UnpackNormalGAWithScale(half4 packednormal, float scale, float hasMask)
  484. {
  485. fixed3 normal;
  486. UNITY_BRANCH if (hasMask > 0)
  487. {
  488. normal.xy = (packednormal.wy * 2 - 1) * scale;
  489. normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
  490. }
  491. else
  492. {
  493. normal = float3(0, 0, 1);
  494. }
  495. return normal;
  496. }
  497. float3 BlendNormals(float3 n1, float3 n2)
  498. {
  499. #ifdef INTERRA_OBJECT
  500. float3 t = n1.xyz + float3(0.0, 0.0, 1.0);
  501. float3 u = n2.xyz * float3(-1.0, -1.0, 1.0);
  502. float3 r = (t / t.z) * dot(t, u) - u;
  503. return r;
  504. #else
  505. return (float3(n1.xy + n2.xy, n1.z));
  506. #endif
  507. }
  508. float MipLevel(float2 texture_coord)
  509. {
  510. float2 dx = ddx(texture_coord);
  511. float2 dy = ddy(texture_coord);
  512. float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
  513. return max(0, 0.5 * log2(delta_max_sqr)) - 0.75;
  514. }
  515. #if defined(_NORMALMAPS) && !defined(_TERRAIN_NORMAL_IN_MASK)
  516. #define SampleNormals(i) (UnpackNormalWithScale(UNITY_SAMPLE_TEX2D_SAMPLER(_Normal##i, _Splat0, uv[i]), _NormalScale##i).xyz)
  517. #elif defined(_TERRAIN_NORMAL_IN_MASK)
  518. #define SampleNormals(i) float3(UnpackNormalGAWithScale(mask[i], _NormalScale##i, _LayerHasMask##i).xyz)
  519. #else
  520. #define SampleNormals(i) float3(0, 0, 1)
  521. #endif
  522. float3 SmoothMaskOrAlbedo(half mask, half albedo, float hasMask, float smoothness)
  523. {
  524. UNITY_BRANCH if (hasMask > 0)
  525. {
  526. albedo = mask;
  527. }
  528. else
  529. {
  530. albedo *= smoothness;
  531. }
  532. return albedo;
  533. }
  534. #ifdef _TERRAIN_MASK_MAPS
  535. #define Smoothness(i) SmoothMaskOrAlbedo(mask[i].a, albedo[i].a, _LayerHasMask##i, _Smoothness##i)
  536. #else
  537. #define Smoothness(i) albedo[i].a *= _Smoothness##i
  538. #endif
  539. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  540. #ifdef INTERRA_OBJECT
  541. #define UV(i) (posOffset.xz + _SplatUV##i.zw + _SteepDistortion) / _SplatUV##i.xy;
  542. #else
  543. #define UV(i) (posOffset.xz + _SplatUV##i.zw) / _SplatUV##i.xy;
  544. #endif
  545. #ifdef PARALLAX
  546. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ + (_DiffuseRemapScale##i.w * 0.004 * _SplatUV##i.x) * -flip.z);
  547. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX + (_DiffuseRemapScale##i.w * 0.004 * _SplatUV##i.y) * -flip.x);
  548. #else
  549. #if defined(TESSELLATION_ON)
  550. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ + (-_DiffuseRemapOffset##i.y * 0.005 - _TerrainTessOffset) * -flip.z);
  551. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX + (-_DiffuseRemapOffset##i.y * 0.005 - _TerrainTessOffset) * -flip.x);
  552. #else
  553. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ);
  554. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX);
  555. #endif
  556. #endif
  557. #else
  558. #define UV(i) splatBaseUV * _Splat##i##_ST.xy + _Splat##i##_ST.zw;
  559. #define fUV(i) TerrainFrontUV(worldPos, _Splat##i##_ST, uvSplat[i], flip);
  560. #define sUV(i) TerrainSideUV(worldPos, _Splat##i##_ST, uvSplat[i], flip);
  561. #endif
  562. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  563. #ifndef TRIPLANAR
  564. void UvSplat(out float2 uvSplat[_LAYER_COUNT], float3 posOffset)
  565. #else
  566. void UvSplat(out float2 uvSplat[_LAYER_COUNT], out float2 uvFront[_LAYER_COUNT], out float2 uvSide[_LAYER_COUNT], float3 posOffset, float offsetZ, float offsetX, float3 flip)
  567. #endif
  568. #else
  569. #if !defined(TRIPLANAR) || defined(TERRAIN_BASEGEN)
  570. void UvSplat(out float2 uvSplat[_LAYER_COUNT], float2 splatBaseUV)
  571. #else
  572. void UvSplat(out float2 uvSplat[_LAYER_COUNT], out float2 uvFront[_LAYER_COUNT], out float2 uvSide[_LAYER_COUNT], float3 worldPos, float2 splatBaseUV, float3 flip)
  573. #endif
  574. #endif
  575. {
  576. #if !defined(TRIPLANAR) || defined(TERRAIN_BASEGEN)
  577. #define SplatUV(i) \
  578. uvSplat[i] = UV(i);
  579. #else
  580. #define SplatUV(i) \
  581. uvSplat[i] = UV(i); \
  582. uvFront[i] = fUV(i); \
  583. uvSide[i] = sUV(i); \
  584. #endif
  585. SplatUV(0);
  586. #ifndef _LAYERS_ONE
  587. SplatUV(1);
  588. #ifndef _LAYERS_TWO
  589. if (_TwoLayersOnly < 1)
  590. {
  591. SplatUV(2);
  592. SplatUV(3);
  593. #ifdef _LAYERS_EIGHT
  594. SplatUV(4);
  595. SplatUV(5);
  596. SplatUV(6);
  597. SplatUV(7);
  598. #endif
  599. }
  600. #endif
  601. #endif
  602. }
  603. void DistantUV(out float2 distantUV[_LAYER_COUNT], float2 uvSplat[_LAYER_COUNT])
  604. {
  605. #define uvDistant(i) \
  606. distantUV[i] = uvSplat[i] * (_DiffuseRemapOffset##i.r + 1) * _HT_distance_scale; \
  607. uvDistant(0);
  608. #ifndef _LAYERS_ONE
  609. uvDistant(1);
  610. #ifndef _LAYERS_TWO
  611. if (_TwoLayersOnly < 1)
  612. {
  613. uvDistant(2);
  614. uvDistant(3);
  615. #ifdef _LAYERS_EIGHT
  616. uvDistant(4);
  617. uvDistant(5);
  618. uvDistant(6);
  619. uvDistant(7);
  620. #endif
  621. }
  622. #endif
  623. #endif
  624. }
  625. float4 RemapMasks(half4 mask, float4 remapScale, float4 remapOffset)
  626. {
  627. #ifdef _TERRAIN_NORMAL_IN_MASK
  628. mask.rb * remapScale.gb + remapOffset.gb;
  629. return mask;
  630. #else
  631. return mask * remapScale + remapOffset;
  632. #endif
  633. }
  634. #define PI 3.14159265359
  635. #define TAU 6.283185307
  636. float random(in float2 st)
  637. {
  638. return frac(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453123);
  639. }
  640. float2 rotate_2d(float2 p_input, float p_theta)
  641. {
  642. float2x2 l_rot_matrix = float2x2(cos(p_theta), -sin(p_theta),
  643. sin(p_theta), cos(p_theta));
  644. return mul(l_rot_matrix, p_input);
  645. }
  646. float3 RainRipples(float2 uv, float scale, float rotation)
  647. {
  648. float3 normal = float3(0, 0, 1);
  649. uv.xy = rotate_2d(uv.xy, rotation);
  650. float2 sUV = (uv.xy * scale + scale * 0.1f);
  651. sUV.x += step(1.0f, (sUV.y % 2.0)) * 0.5f;
  652. float2 center = float2(0.5f, 0.5f);
  653. float size = 0.25f;
  654. float2 tile = floor(sUV);
  655. float2 fract = frac(sUV);
  656. float2 offset = (center - fract.xy) * size;
  657. float2 cUV = (fract - 0.5) / size + offset;
  658. float2 polarUV = float2(length(cUV), atan2(cUV.y, cUV.x));
  659. float time = _Time.x * scale * 3.0f;
  660. float radius = frac(time * 0.9f + (random(floor(sUV) + 5000.0).x)) * 1.25f;
  661. if (radius < 0.8f)
  662. {
  663. float thickness = min((size * 0.5f + 0.25f) * 0.4f, radius);
  664. float start = radius + thickness;
  665. float end = max(0., radius - thickness);
  666. if (radius > 0.15)
  667. {
  668. thickness *= (0.9 - (radius * 0.9f));
  669. }
  670. float radius2 = radius - thickness * 1.25f;
  671. float cAngle = smoothstep(start, end, polarUV.x) * PI;
  672. float start2 = radius2 + thickness;
  673. float end2 = max(0., radius2 - thickness);
  674. float cAngle2 = smoothstep(start2, end2, polarUV.x) * PI;
  675. float rippleMask = smoothstep(thickness, -0.1f, abs(polarUV.x - radius));
  676. float rippleMask2 = smoothstep(thickness, -0.1f, abs(polarUV.x - radius2));
  677. float decayMask = min(1., max(0., 1. - polarUV.x));
  678. cAngle = lerp(cAngle, PI * 0.5f, (1.0f - rippleMask * decayMask));
  679. cAngle2 = lerp(cAngle2, PI * 0.5f, (1.0f - rippleMask2 * decayMask));
  680. float c = lerp(cos(cAngle), cos(cAngle2), 0.5f);
  681. normal = float3(c * sin(polarUV.y), c * cos(polarUV.y), sin(cAngle));
  682. float opacity = _InTerra_GlobalRaindropRipples.y;
  683. normal.xy = rotate_2d(normal.xy, rotation) * (opacity - (radius * opacity));
  684. }
  685. return normal;
  686. }
  687. #if defined(TERRAIN_MASK) || defined(DIFFUSE)
  688. #ifdef DIFFUSE
  689. #define Mask(i, blendMask) UNITY_SAMPLE_TEX2D_SAMPLER(_Splat##i, _Splat0, uv[i])
  690. #else
  691. #ifdef TERRAIN_MASK
  692. #define Mask(i, blendMask) UNITY_SAMPLE_TEX2D_SAMPLER(_Mask##i, _Splat0, uv[i])
  693. #else
  694. #define Mask(i, blendMask) float4(_Metallic##i, 1, 0.5, 0);
  695. #endif
  696. #endif
  697. #if defined(DIFFUSE)
  698. #define RemapMask(i) mask[i];
  699. #else
  700. #ifdef _TERRAIN_NORMAL_IN_MASK
  701. #define RemapMask(i) mask[i] * float4(_MaskMapRemapScale##i.g, 1, _MaskMapRemapScale##i.b, 1) \
  702. + float4(_MaskMapRemapOffset##i.g, 0, _MaskMapRemapOffset##i.b, 0);
  703. #else
  704. #define RemapMask(i) mask[i] * _MaskMapRemapScale##i + _MaskMapRemapOffset##i;
  705. #endif
  706. #endif
  707. #else
  708. #define Mask(i, blendMask) float4(_Metallic##i, 1.0f, 0.5f, 0.0f);
  709. #define RemapMask(i) mask[i];
  710. #endif
  711. void SampleMask(out half4 mask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], half4 blendMask[2], float weight)
  712. {
  713. #define SampleMasks(i, blendMask) \
  714. UNITY_BRANCH if (blendMask * weight > 0.0f ) \
  715. { \
  716. mask[i] = Mask(i, blendMask); \
  717. mask[i] = RemapMask(i); \
  718. } \
  719. else \
  720. { \
  721. mask[i] = float4(_Metallic##i, 1.0f, 0.5f, 0.0f); \
  722. } \
  723. SampleMasks(0, blendMask[0].r);
  724. #ifndef _LAYERS_ONE
  725. SampleMasks(1, blendMask[0].g);
  726. #ifndef _LAYERS_TWO
  727. if (_TwoLayersOnly < 1)
  728. {
  729. SampleMasks(2, blendMask[0].b);
  730. SampleMasks(3, blendMask[0].a);
  731. #ifdef _LAYERS_EIGHT
  732. SampleMasks(4, blendMask[1].r);
  733. SampleMasks(5, blendMask[1].g);
  734. SampleMasks(6, blendMask[1].b);
  735. SampleMasks(7, blendMask[1].a);
  736. #endif
  737. }
  738. #endif
  739. #endif
  740. #undef SampleMasks
  741. }
  742. void MaskWeight(inout half4 mask[_LAYER_COUNT], half4 mask_front[_LAYER_COUNT], half4 mask_side[_LAYER_COUNT], half4 blendMask[2], inout float3 triplanarWeights, float heightBlendingSharpness)
  743. {
  744. for (int i = 0; i < _LAYER_COUNT; ++i)
  745. {
  746. mask[i] = (mask[i] * triplanarWeights.y) + (mask_front[i] * triplanarWeights.z) + (mask_side[i] * triplanarWeights.x);
  747. }
  748. }
  749. half4 MaskSplatWeight(half4 mask[_LAYER_COUNT], half4 blendMask[2], out half4 mixedMask)
  750. {
  751. float splatWeight[_LAYER_COUNT];
  752. mixedMask = 0;
  753. #ifdef _TERRAIN_NORMAL_IN_MASK
  754. #define MixdMask(i, blendMask) float4(_Metallic##i, mask[i].r, mask[i].b, 0.0f) * blendMask
  755. #elif defined(_TERRAIN_MASK_HEIGHTMAP_ONLY)
  756. #define MixdMask(i, blendMask) float4(_Metallic##i, 1.0f, mask[i].b, 0.0f) * blendMask
  757. #else
  758. #define MixdMask(i, blendMask) mask[i] * blendMask
  759. #endif
  760. #define MixdMasks(i, blendMask) \
  761. UNITY_BRANCH if (blendMask > 0) \
  762. { \
  763. mixedMask += MixdMask(i, blendMask); \
  764. }
  765. MixdMasks(0, blendMask[0].r);
  766. #ifndef _LAYERS_ONE
  767. MixdMasks(1, blendMask[0].g);
  768. #ifndef _LAYERS_TWO
  769. if (_TwoLayersOnly < 1)
  770. {
  771. MixdMasks(2, blendMask[0].b);
  772. MixdMasks(3, blendMask[0].a);
  773. #ifdef _LAYERS_EIGHT
  774. MixdMasks(4, blendMask[1].r);
  775. MixdMasks(5, blendMask[1].g);
  776. MixdMasks(6, blendMask[1].b);
  777. MixdMasks(7, blendMask[1].a);
  778. #endif
  779. }
  780. #endif
  781. #endif
  782. return mixedMask;
  783. }
  784. #ifndef DIFFUSE
  785. #define SampleSplt(i) UNITY_SAMPLE_TEX2D_SAMPLER(_Splat##i, _Splat0, uv[i])
  786. #else
  787. #define SampleSplt(i) mask[i]
  788. #endif
  789. void SampleSplat(float2 uv[_LAYER_COUNT], half4 blendMask[2], float weight, inout half4 mask[_LAYER_COUNT], out float4 mixAlbedo, out float3 mixNormal, out half4 albedo[_LAYER_COUNT])
  790. {
  791. float3 normal[_LAYER_COUNT];
  792. mixAlbedo = float4(0, 0, 0, 0);
  793. mixNormal = float3(0, 0, 0);
  794. #define Samples(i, blendMask) blendMask *= weight; \
  795. UNITY_BRANCH if (blendMask > 1e-5f) \
  796. { \
  797. albedo[i] = SampleSplt(i); \
  798. albedo[i].rgb *= DiffuseRemap(i).xyz; \
  799. albedo[i].a = Smoothness(i).x; \
  800. normal[i] = SampleNormals(i).xyz; \
  801. mixAlbedo += albedo[i].xyzw * blendMask; \
  802. mixNormal += normal[i].xyz * blendMask; \
  803. } \
  804. else \
  805. { \
  806. albedo[i] = float4(0, 0, 0, 0); \
  807. normal[i] = float3(0, 0, 1); \
  808. } \
  809. mixNormal.z += +1e-5f;
  810. Samples(0, blendMask[0].r);
  811. #ifndef _LAYERS_ONE
  812. Samples(1, blendMask[0].g);
  813. #ifndef _LAYERS_TWO
  814. if (_TwoLayersOnly < 1)
  815. {
  816. Samples(2, blendMask[0].b);
  817. Samples(3, blendMask[0].a);
  818. #ifdef _LAYERS_EIGHT
  819. Samples(4, blendMask[1].r);
  820. Samples(5, blendMask[1].g);
  821. Samples(6, blendMask[1].b);
  822. Samples(7, blendMask[1].a);
  823. #endif
  824. }
  825. #endif
  826. #endif
  827. #undef Samples
  828. }
  829. #ifdef TERRAIN_MASK
  830. float AmbientOcclusion(half4 mask[_LAYER_COUNT], half4 blendMask[2])
  831. {
  832. float occlusion[_LAYER_COUNT];
  833. #ifdef _TERRAIN_NORMAL_IN_MASK
  834. UNITY_UNROLL for (int i = 0; i < _LAYER_COUNT; ++i)
  835. {
  836. occlusion[i] = mask[i].r;
  837. }
  838. #else
  839. UNITY_UNROLL for (int i = 0; i < _LAYER_COUNT; ++i)
  840. {
  841. occlusion[i] = mask[i].g;
  842. }
  843. #endif
  844. float ao = 0;
  845. ao = occlusion[0] * blendMask[0].r;
  846. #ifndef _LAYERS_ONE
  847. ao += occlusion[1] * blendMask[0].g;
  848. #ifndef _LAYERS_TWO
  849. if (_TwoLayersOnly < 1)
  850. {
  851. ao += occlusion[2] * blendMask[0].b
  852. + occlusion[3] * blendMask[0].a;
  853. #ifdef _LAYERS_EIGHT
  854. ao += occlusion[4] * blendMask[1].r
  855. + occlusion[5] * blendMask[1].g
  856. + occlusion[6] * blendMask[1].b
  857. + occlusion[7] * blendMask[1].a;
  858. #endif
  859. }
  860. #endif
  861. #endif
  862. return ao;
  863. }
  864. #endif
  865. #ifndef _TERRAIN_MASK_MAPS
  866. #define Metallic(i, blendMask) _Metallic##i * blendMask;
  867. #else
  868. #define Metallic(i, blendMask) mask[i].r * blendMask;
  869. #endif
  870. float MetallicMask(half4 mask[_LAYER_COUNT], half4 blendMask[2])
  871. {
  872. float metallic = 0;
  873. #define Metallics(i, blendMask) \
  874. UNITY_BRANCH if (blendMask > 0.0001f) \
  875. { \
  876. metallic += Metallic(i, blendMask); \
  877. }
  878. Metallics(0, blendMask[0].r);
  879. #ifndef _LAYERS_ONE
  880. Metallics(1, blendMask[0].g);
  881. #ifndef _LAYERS_TWO
  882. if (_TwoLayersOnly < 1)
  883. {
  884. Metallics(2, blendMask[0].b);
  885. Metallics(3, blendMask[0].a);
  886. #ifdef _LAYERS_EIGHT
  887. Metallics(4, blendMask[1].r);
  888. Metallics(5, blendMask[1].g);
  889. Metallics(6, blendMask[1].b);
  890. Metallics(7, blendMask[1].a);
  891. #endif
  892. }
  893. #endif
  894. #endif
  895. return metallic;
  896. }
  897. float HeightSum(half4 mask[_LAYER_COUNT], half4 blendMask[2])
  898. {
  899. float height[_LAYER_COUNT];
  900. for (int i = 0; i < _LAYER_COUNT; ++i)
  901. {
  902. #ifdef DIFFUSE
  903. height[i] = mask[i].a;
  904. #else
  905. height[i] = mask[i].b;
  906. #endif
  907. }
  908. #ifdef _LAYERS_ONE
  909. return float(height[0]);
  910. #else
  911. float heightSum = dot(blendMask[0].rg, float2(height[0], height[1]));
  912. #ifndef _LAYERS_TWO
  913. if (_TwoLayersOnly < 1)
  914. {
  915. heightSum += dot(blendMask[0].ba, float2(height[2], height[3]));
  916. #ifdef _LAYERS_EIGHT
  917. heightSum += dot(blendMask[1], float4(height[4], height[5], height[6], height[7]));
  918. #endif
  919. }
  920. #endif
  921. return heightSum;
  922. #endif
  923. }
  924. #ifdef _TERRAIN_BLEND_HEIGHT
  925. void HeightBlend(half4 mask[_LAYER_COUNT], inout half4 blendMask[2], float sharpness)
  926. {
  927. float heights[_LAYER_COUNT];
  928. for (int i = 0; i < _LAYER_COUNT; ++i)
  929. {
  930. #ifdef DIFFUSE
  931. heights[i] = mask[i].a;
  932. #else
  933. heights[i] = mask[i].b;
  934. #endif
  935. }
  936. #ifdef _LAYERS_TWO
  937. float2 height = float2(heights[0], heights[1]);
  938. blendMask[0].rg *= (1 / (pow(2, (height + blendMask[0].rg) * (-(sharpness)))) + 1) * 0.5;
  939. blendMask[0].rg /= (blendMask[0].r + blendMask[0].g);
  940. #else
  941. if (_TwoLayersOnly < 1)
  942. {
  943. float4 height = float4 (heights[0], heights[1], heights[2], heights[3]);
  944. blendMask[0].rgba *= (1 / (pow(2, (height + blendMask[0].rgba) * (-(sharpness)))) + 1) * 0.5;
  945. float heightSum = blendMask[0].r + blendMask[0].g + blendMask[0].b + blendMask[0].a;
  946. #ifdef _LAYERS_EIGHT
  947. float4 height1 = float4 (heights[4], heights[5], heights[6], heights[7]);
  948. blendMask[1].rgba *= (1 / (pow(2, (height1 + blendMask[1].rgba) * (-(sharpness)))) + 1) * 0.5;
  949. heightSum += blendMask[1].r + blendMask[1].g + blendMask[1].b + blendMask[1].a;
  950. blendMask[1].rgba /= heightSum;
  951. #endif
  952. blendMask[0].rgba /= heightSum;
  953. }
  954. else
  955. {
  956. float2 height = float2(heights[0], heights[1]);
  957. blendMask[0].rg *= (1 / (pow(2, (height + blendMask[0].rg) * (-(sharpness)))) + 1) * 0.5;
  958. blendMask[0].rg /= (blendMask[0].r + blendMask[0].g);
  959. }
  960. #endif
  961. }
  962. #endif
  963. #ifndef _TERRAIN_BASEMAP_GEN
  964. void SampleSplatTOL(in out half4 mixedAlbedo, in out half3 mixedNormal, float2 uv[_LAYER_COUNT], half4 blendMask[2], float weight, half4 mask[_LAYER_COUNT])
  965. {
  966. float4 albedo[1];
  967. float3 normal[1];
  968. albedo[0] = float4(0, 0, 0, 0);
  969. normal[0] = float3(0, 0, 1);
  970. blendMask[0].r *= weight;
  971. #ifndef TERRAIN_SPLAT_ADDPASS
  972. UNITY_BRANCH if (blendMask[0].r > 1e-5f)
  973. {
  974. albedo[0] = SampleSplt(0);
  975. albedo[0].rgb *= DiffuseRemap(0).rgb;
  976. albedo[0].a = Smoothness(0).x;
  977. normal[0] = SampleNormals(0);
  978. }
  979. mixedAlbedo = (albedo[0] * blendMask[0].r);
  980. mixedNormal = (normal[0] * blendMask[0].r);
  981. #else
  982. mixedAlbedo = float4(0, 0, 0, 0);
  983. mixedNormal = float3(0, 0, 0);
  984. #endif
  985. }
  986. void SampleMaskTOL(out half4 mask[_LAYER_COUNT], half4 noTriplanarMask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], float weight)
  987. {
  988. #ifndef TERRAIN_SPLAT_ADDPASS
  989. UNITY_BRANCH if (weight > 1e-5f)
  990. {
  991. mask[0] = Mask(0, weight);
  992. mask[0] = RemapMask(0);
  993. }
  994. else
  995. {
  996. mask[0] = float4(0, 0, 0.5, 0);
  997. }
  998. #else
  999. mask[0] = noTriplanarMask[0];
  1000. #endif
  1001. mask[1] = noTriplanarMask[1];
  1002. #ifndef _LAYERS_TWO
  1003. if (_TwoLayersOnly < 1)
  1004. {
  1005. mask[2] = noTriplanarMask[2];
  1006. mask[3] = noTriplanarMask[3];
  1007. #ifdef _LAYERS_EIGHT
  1008. mask[4] = noTriplanarMask[4];
  1009. mask[5] = noTriplanarMask[5];
  1010. mask[6] = noTriplanarMask[6];
  1011. mask[7] = noTriplanarMask[7];
  1012. #endif
  1013. }
  1014. #endif
  1015. }
  1016. #endif
  1017. void UvSplatDistort(out float2 uvSplat[_LAYER_COUNT], float2 posOffset, fixed distortion)
  1018. {
  1019. uvSplat[0] = ((posOffset + (_SplatUV0.zw + distortion)) / _SplatUV0.xy);
  1020. }