InTerra_Functions.hlsl 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  2. #ifdef _LAYERS_ONE
  3. #define _LAYER_COUNT 1
  4. #else
  5. #ifdef _LAYERS_TWO
  6. #define _LAYER_COUNT 2
  7. #else
  8. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  9. #ifdef _LAYERS_EIGHT
  10. #define _LAYER_COUNT 8
  11. #endif
  12. #ifdef _LAYERS_SIXTEEN
  13. #define _LAYER_COUNT 16
  14. #endif
  15. #else
  16. #define _LAYER_COUNT 4
  17. #endif
  18. #endif
  19. #endif
  20. #ifdef INTERRA_MESH_TERRAIN
  21. SAMPLER(SamplerState_Linear_Repeat);
  22. #endif
  23. #else
  24. #include "InTerra_LayersDeclaration.hlsl"
  25. half4 _HT_distance;
  26. float _HT_distance_scale, _HT_cover;
  27. half _Distance_Height_blending, _Distance_HeightTransition, _TriplanarSharpness;
  28. half _ControlNumber;
  29. half _ParallaxAffineStepsTerrain;
  30. float _TerrainColorTintStrenght;
  31. float4 _TerrainColorTintTexture_ST;
  32. float4 _TerrainNormalTintDistance;
  33. float _TerrainNormalTintStrenght;
  34. float4 _TerrainNormalTintTexture_ST;
  35. half _TriplanarOneToAllSteep;
  36. float3 _TerrainSizeXZPosY;
  37. float4 _MipMapFade;
  38. float _MipMapLevel;
  39. TEXTURE2D(_TerrainColorTintTexture);
  40. TEXTURE2D(_TerrainNormalTintTexture); SAMPLER(SamplerState_Linear_Repeat);
  41. //-----Track Properties -----
  42. float _TrackTessallation;
  43. float _TrackDetailStrenght;
  44. float _TrackNormalStrenght;
  45. float _TrackEdgeNormals, _TrackEdgeSharpness;
  46. float _TrackDetailNormalStrenght;
  47. float _TrackHeightOffset;
  48. float _TrackTessallationHeightOffset;
  49. float _TrackTessallationHeightTransition;
  50. float _TrackAO;
  51. float4 _TrackDetailTexture_ST;
  52. float _ParallaxTrackAffineSteps;
  53. float _ParallaxTrackSteps;
  54. float _TrackHeightTransition;
  55. float _Gamma;
  56. float _WorldMapping;
  57. TEXTURE2D(_TrackDetailTexture);
  58. TEXTURE2D(_TrackDetailNormalTexture);
  59. float _HeightmapBlending;
  60. float _Tracks;
  61. float _Terrain_Parallax;
  62. TEXTURE2D_ARRAY(_SplatArray16);
  63. TEXTURE2D_ARRAY(_NormalArray16);
  64. float2 TerrainFrontUV(float3 wPos, float4 splatUV, float2 tc, float3 flip)
  65. {
  66. return float2(tc.x * -flip.z, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.y / _TerrainSizeXZPosY.y) + splatUV.w);
  67. }
  68. float2 TerrainSideUV(float3 wPos, float4 splatUV, float2 tc, float3 flip)
  69. {
  70. return float2(tc.y * flip.x, (wPos.y - _TerrainSizeXZPosY.z) * (splatUV.x / _TerrainSizeXZPosY.x) + splatUV.z);
  71. }
  72. #endif
  73. //----- Global Properties -----
  74. float _InTerra_TrackArea;
  75. float3 _InTerra_TrackPosition;
  76. TEXTURE2D(_InTerra_TrackTexture);
  77. float4 _InTerra_TrackTexture_TexelSize;
  78. float _InTerra_GlobalWetness;
  79. float _InTerra_TracksLayer;
  80. float _InTerra_TracksFading;
  81. float _InTerra_TracksFadingTime;
  82. float _InTerra_TrackTextureSize;
  83. float _InTerra_TrackLayer;
  84. float3 _InTerra_GlobalPuddles;
  85. float3 _InTerra_GlobalRaindropRipples;
  86. float4 _InTerra_GlobalRaindropsDistance;
  87. //------------------------------
  88. #ifndef _TERRAIN_BASEMAP
  89. //==========================================================================================
  90. //====================================== FUNCTIONS =====================================
  91. //==========================================================================================
  92. float2 ObjectFrontUV(float posOffset, float4 splatUV, float offsetZ)
  93. {
  94. return float2((posOffset + splatUV.z) / splatUV.x, (offsetZ + splatUV.w) / splatUV.y);
  95. }
  96. float2 ObjectSideUV(float posOffset, float4 splatUV, float offsetX)
  97. {
  98. return float2((offsetX + splatUV.z) / splatUV.x, (posOffset + splatUV.w) / splatUV.y);
  99. }
  100. float3 WorldTangent(float3 wTangent, float3 wBTangent, float3 mixedNormal)
  101. {
  102. mixedNormal.xy = mul(float2x2(wTangent.xz, wBTangent.xz), mixedNormal.xy);
  103. return half3(mixedNormal);
  104. }
  105. float2 HeightBlendTwoTextures(float2 splat, float2 heights, float sharpness)
  106. {
  107. splat *= (1 / (1 * pow(2, heights * (-(sharpness)))) + 1) * 0.5;
  108. splat /= (splat.r + splat.g);
  109. return splat;
  110. }
  111. void TriplanarOneToAllSteep(in out float4 blendMask[4], float weightY)
  112. {
  113. if (_TriplanarOneToAllSteep == 1)
  114. {
  115. blendMask[0] = float4(saturate(blendMask[0].r + weightY), saturate((blendMask[0].gba) - weightY));
  116. blendMask[1] = float4(saturate((blendMask[1].rgba) - weightY));
  117. #ifdef _LAYERS_SIXTEEN
  118. blendMask[2] = float4(saturate((blendMask[2].rgba) - weightY));
  119. blendMask[3] = float4(saturate((blendMask[3].rgba) - weightY));
  120. #endif
  121. }
  122. }
  123. float3 TriplanarNormal(float3 normal, float3 tangent, float3 bTangent, float3 normal_front, float3 normal_side, float3 triplanarWeights, half3 flipUV)
  124. {
  125. #ifdef INTERRA_OBJECT
  126. normal_front.y *= -flipUV.z;
  127. normal_front.xy = mul(float2x2(tangent.xy, bTangent.xy), normal_front.xy);
  128. normal_side.x *= -flipUV.x;
  129. normal_side.xy = mul(float2x2(tangent.yz, bTangent.yz), normal_side.xy);
  130. #else
  131. normal_side.xy = normal_side.yx; //this is needed because the uv was rotated
  132. normal_front.xy *= -flipUV.z;
  133. normal_side.x *= -flipUV.x;
  134. normal_side.y *= flipUV.x;
  135. #endif
  136. return half3 (normal + normal_front + normal_side);
  137. }
  138. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  139. #define DiffuseRemap(i) float4(_DiffuseRemapScale##i.xyzw)
  140. #else
  141. #define DiffuseRemap(i) float4(_DiffuseRemapScale##i.xyzw + _DiffuseRemapOffset##i.xyzw)
  142. #endif
  143. #ifndef _TERRAIN_BASEMAP_GEN
  144. #if defined(PARALLAX)
  145. float GetParallaxHeight(texture2D maskT, sampler maskS, float2 uv, float lod, float2 offset, int invert)
  146. {
  147. return abs(SAMPLE_TEXTURE2D_LOD(maskT, maskS, float2(uv + offset), lod).b -invert);
  148. }
  149. //this function is based on Parallax Occlusion Mapping from Shader Graph URP
  150. float2 ParallaxOffset(texture2D maskT, sampler maskS, int numSteps, float amplitude, float2 uv, float3 tangentViewDir, float affineSteps, float lod, int invert)
  151. {
  152. float2 offset = 0;
  153. if (numSteps > 0)
  154. {
  155. float3 viewDir = float3(tangentViewDir.xy * amplitude * -0.01, tangentViewDir.z);
  156. float stepSize = (1.0 / numSteps);
  157. float2 texOffsetPerStep = stepSize * viewDir.xy;
  158. // Do a first step before the loop to init all value correctly
  159. float2 texOffsetCurrent = float2(0.0, 0.0);
  160. float prevHeight = GetParallaxHeight(maskT, maskS, uv, lod, texOffsetCurrent, invert);
  161. texOffsetCurrent += texOffsetPerStep;
  162. float currHeight = GetParallaxHeight(maskT, maskS, uv, lod, texOffsetCurrent, invert);
  163. float rayHeight = 1.0 - stepSize; // Start at top less one sample
  164. for (int stepIndex = 0; stepIndex < numSteps; ++stepIndex)
  165. {
  166. // Have we found a height below our ray height ? then we have an intersection
  167. if (currHeight > rayHeight)
  168. break; // end the loop
  169. prevHeight = currHeight;
  170. rayHeight -= stepSize;
  171. texOffsetCurrent += texOffsetPerStep;
  172. currHeight = GetParallaxHeight(maskT, maskS, uv, lod, texOffsetCurrent, invert);
  173. }
  174. if (affineSteps <= 1)
  175. {
  176. float delta0 = currHeight - rayHeight;
  177. float delta1 = (rayHeight + stepSize) - prevHeight;
  178. float ratio = delta0 / (delta0 + delta1);
  179. offset = texOffsetCurrent - ratio * texOffsetPerStep;
  180. currHeight = GetParallaxHeight(maskT, maskS, uv, lod, texOffsetCurrent, invert);
  181. }
  182. else
  183. {
  184. float pt0 = rayHeight + stepSize;
  185. float pt1 = rayHeight;
  186. float delta0 = pt0 - prevHeight;
  187. float delta1 = pt1 - currHeight;
  188. float delta;
  189. // Secant method to affine the search
  190. // Ref: Faster Relief Mapping Using the Secant Method - Eric Risser
  191. for (int i = 0; i < affineSteps; ++i)
  192. {
  193. // intersectionHeight is the height [0..1] for the intersection between view ray and heightfield line
  194. float intersectionHeight = (pt0 * delta1 - pt1 * delta0) / (delta1 - delta0);
  195. // Retrieve offset require to find this intersectionHeight
  196. offset = (1 - intersectionHeight) * texOffsetPerStep * numSteps;
  197. currHeight = GetParallaxHeight(maskT, maskS, uv, lod, offset, invert);
  198. delta = intersectionHeight - currHeight;
  199. if (abs(delta) <= 0.01)
  200. break;
  201. // intersectionHeight < currHeight => new lower bounds
  202. if (delta < 0.0)
  203. {
  204. delta1 = delta;
  205. pt1 = intersectionHeight;
  206. }
  207. else
  208. {
  209. delta0 = delta;
  210. pt0 = intersectionHeight;
  211. }
  212. }
  213. }
  214. }
  215. return offset;
  216. }
  217. #if defined(_TERRAIN_PARALLAX) && !defined(_TERRAIN_BASEMAP_GEN) && !defined(TESSELLATION_ON)
  218. void ParallaxUV(inout float2 uv[_LAYER_COUNT], float3 tangentViewDir, float lod)
  219. {
  220. #define uvParallax(i) \
  221. uv[i] += ParallaxOffset(_Mask##i, sampler_Splat0, _DiffuseRemapOffset##i.w, DiffuseRemap(i).w, uv[i], tangentViewDir, _ParallaxAffineStepsTerrain, _MipMapLevel + (lod * (log2(max(_Mask##i##_TexelSize.z, _Mask##i##_TexelSize.w)) + 1)), 0); \
  222. uvParallax(0);
  223. #ifndef _LAYERS_ONE
  224. uvParallax(1);
  225. #ifndef _LAYERS_TWO
  226. uvParallax(2);
  227. uvParallax(3);
  228. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  229. uvParallax(4);
  230. uvParallax(5);
  231. uvParallax(6);
  232. uvParallax(7);
  233. #endif
  234. #ifdef _LAYERS_SIXTEEN
  235. uvParallax(8);
  236. uvParallax(9);
  237. uvParallax(10);
  238. uvParallax(11);
  239. uvParallax(12);
  240. uvParallax(13);
  241. uvParallax(14);
  242. uvParallax(15);
  243. #endif
  244. #endif
  245. #endif
  246. }
  247. #endif
  248. #endif
  249. #endif
  250. float3 UnpackNormals(float4 packednormal, float normalScale)
  251. {
  252. #ifdef SURFACE_GRADIENT
  253. #ifdef UNITY_NO_DXT5nm
  254. return float3(UnpackDerivativeNormalRGB(packednormal, normalScale), 0);
  255. #else
  256. return float3(UnpackDerivativeNormalRGorAG(packednormal, normalScale), 0);
  257. #endif
  258. #else
  259. #ifdef UNITY_NO_DXT5nm
  260. return UnpackNormalRGB(packednormal, normalScale);
  261. #else
  262. return UnpackNormalAG(packednormal, normalScale);
  263. #endif
  264. #endif
  265. }
  266. float3 UnpackNormalGAWithScale(float4 packednormal, float scale, half hasMask)
  267. {
  268. UNITY_BRANCH if (hasMask > 0)
  269. {
  270. #ifdef SURFACE_GRADIENT
  271. return float3(UnpackDerivativeNormalAG(packednormal, scale), 0);
  272. #else
  273. return UnpackNormalAG(packednormal, scale);
  274. #endif
  275. }
  276. else
  277. {
  278. return float3(0, 0, 1);
  279. }
  280. }
  281. float3 BlendNormals(float3 n1, float3 n2)
  282. {
  283. #ifdef INTERRA_OBJECT
  284. float3 t = n1.xyz + float3(0.0, 0.0, 1.0);
  285. float3 u = n2.xyz * float3(-1.0, -1.0, 1.0);
  286. float3 r = (t / t.z) * dot(t, u) - u;
  287. return r;
  288. #else
  289. return (float3(n1.xy + n2.xy, n1.z));
  290. #endif
  291. }
  292. #if defined(_NORMALMAPS) && !defined(_TERRAIN_NORMAL_IN_MASK)
  293. #if defined(_LAYERS_SIXTEEN) && (defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN))
  294. #define SampleNormals(i) (UnpackNormals(SAMPLE_TEXTURE2D_ARRAY(_NormalArray16, sampler_Splat0, uv[i], i), _NormalScale##i).xyz)
  295. #else
  296. #define SampleNormals(i) (UnpackNormals(SAMPLE_TEXTURE2D(_Normal##i, sampler_Splat0, uv[i]), _NormalScale##i).xyz)
  297. #endif
  298. #elif defined(_TERRAIN_NORMAL_IN_MASK)
  299. #define SampleNormals(i) float3(UnpackNormalGAWithScale(mask[i], _NormalScale##i, _LayerHasMask##i).xyz)
  300. #else
  301. #define SampleNormals(i) float3(0, 0, 1)
  302. #endif
  303. float3 SmoothMaskOrAlbedo(half mask, half albedo, float hasMask, float smoothness)
  304. {
  305. UNITY_BRANCH if (hasMask > 0)
  306. {
  307. albedo = mask;
  308. }
  309. else
  310. {
  311. albedo *= smoothness;
  312. }
  313. return albedo;
  314. }
  315. #ifdef _TERRAIN_MASK_MAPS
  316. #define Smoothness(i) SmoothMaskOrAlbedo(mask[i].a, albedo[i].a, _LayerHasMask##i, _Smoothness##i)
  317. #else
  318. #define Smoothness(i) albedo[i].a *= _Smoothness##i
  319. #endif
  320. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  321. #if defined(INTERRA_OBJECT) && !defined(TESSELLATION_SAMPLING) && !defined(_OBJECT_TRIPLANAR)
  322. #define UV(i) (posOffset.xz + _SplatUV##i.zw + distortion) / _SplatUV##i.xy;
  323. #else
  324. #define UV(i) (posOffset.xz + _SplatUV##i.zw) / _SplatUV##i.xy;
  325. #endif
  326. #ifdef PARALLAX
  327. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ + (_DiffuseRemapScale##i.w * 0.004 * _SplatUV##i.x) * -flip.z);
  328. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX + (_DiffuseRemapScale##i.w * 0.004 * _SplatUV##i.y) * -flip.x);
  329. #else
  330. #if defined(TESSELLATION_ON)
  331. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ + (-_DiffuseRemapOffset##i.y * 0.005 - _TerrainTessOffset) * -flip.z);
  332. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX + (-_DiffuseRemapOffset##i.y * 0.005 - _TerrainTessOffset) * -flip.x);
  333. #else
  334. #define fUV(i) ObjectFrontUV(posOffset.x, _SplatUV##i, offsetZ);
  335. #define sUV(i) ObjectSideUV(posOffset.z, _SplatUV##i, offsetX);
  336. #endif
  337. #endif
  338. #else
  339. #define UV(i) splatBaseUV * _Splat##i##_ST.xy + _Splat##i##_ST.zw;
  340. #define fUV(i) TerrainFrontUV(worldPos, _Splat##i##_ST, uvSplat[i], flip);
  341. #define sUV(i) TerrainSideUV(worldPos, _Splat##i##_ST, uvSplat[i], flip);
  342. #endif
  343. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  344. #ifndef TRIPLANAR
  345. void UvSplat(out float2 uvSplat[_LAYER_COUNT], float3 posOffset, float distortion)
  346. #else
  347. 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)
  348. #endif
  349. #else
  350. #ifndef TRIPLANAR
  351. void UvSplat(out float2 uvSplat[_LAYER_COUNT], float2 splatBaseUV)
  352. #else
  353. void UvSplat(out float2 uvSplat[_LAYER_COUNT], out float2 uvFront[_LAYER_COUNT], out float2 uvSide[_LAYER_COUNT], float3 worldPos, float2 splatBaseUV, float3 flip)
  354. #endif
  355. #endif
  356. {
  357. #ifndef TRIPLANAR
  358. #define SplatUV(i) \
  359. uvSplat[i] = UV(i);
  360. #else
  361. #define SplatUV(i) \
  362. uvSplat[i] = UV(i); \
  363. uvFront[i] = fUV(i); \
  364. uvSide[i] = sUV(i); \
  365. #endif
  366. SplatUV(0);
  367. #ifndef _LAYERS_ONE
  368. SplatUV(1);
  369. #ifndef _LAYERS_TWO
  370. SplatUV(2);
  371. SplatUV(3);
  372. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  373. SplatUV(4);
  374. SplatUV(5);
  375. SplatUV(6);
  376. SplatUV(7);
  377. #endif
  378. #ifdef _LAYERS_SIXTEEN
  379. SplatUV(8);
  380. SplatUV(9);
  381. SplatUV(10);
  382. SplatUV(11);
  383. SplatUV(12);
  384. SplatUV(13);
  385. SplatUV(14);
  386. SplatUV(15);
  387. #endif
  388. #endif
  389. #endif
  390. }
  391. void DistantUV(out float2 distantUV[_LAYER_COUNT], float2 uvSplat[_LAYER_COUNT])
  392. {
  393. #define uvDistant(i) \
  394. distantUV[i] = uvSplat[i] * (_DiffuseRemapOffset##i.r + 1) * _HT_distance_scale; \
  395. uvDistant(0);
  396. #ifndef _LAYERS_ONE
  397. uvDistant(1);
  398. #ifndef _LAYERS_TWO
  399. uvDistant(2);
  400. uvDistant(3);
  401. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  402. uvDistant(4);
  403. uvDistant(5);
  404. uvDistant(6);
  405. uvDistant(7);
  406. #endif
  407. #ifdef _LAYERS_SIXTEEN
  408. uvDistant(8);
  409. uvDistant(9);
  410. uvDistant(10);
  411. uvDistant(11);
  412. uvDistant(12);
  413. uvDistant(13);
  414. uvDistant(14);
  415. uvDistant(15);
  416. #endif
  417. #endif
  418. #endif
  419. }
  420. float4 RemapMasks(float4 mask, float4 remapScale, float4 remapOffset)
  421. {
  422. #ifdef _TERRAIN_NORMAL_IN_MASK
  423. mask.rb * remapScale.gb + remapOffset.gb;
  424. return mask;
  425. #else
  426. return mask * remapScale + remapOffset;
  427. #endif
  428. }
  429. #ifdef TERRAIN_MASK
  430. #define Mask(i) SAMPLE_TEXTURE2D(_Mask##i, sampler_Splat0, uv[i]);
  431. #ifdef _TERRAIN_NORMAL_IN_MASK
  432. #define RemapMask(i) mask[i] * float4(_MaskMapRemapScale##i.g, 1, _MaskMapRemapScale##i.b, 1) \
  433. + float4(_MaskMapRemapOffset##i.g, 0, _MaskMapRemapOffset##i.b, 0);
  434. #else
  435. #define RemapMask(i) mask[i] * _MaskMapRemapScale##i + _MaskMapRemapOffset##i;
  436. #endif
  437. #else
  438. #define Mask(i) float4(_Metallic##i, 1, 0.5, 0);
  439. #define RemapMask(i) mask[i];
  440. #endif
  441. void SampleMask(out float4 mask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], float4 blendMask[4], float weight)
  442. {
  443. #define SampleMasks(i, blendMask) \
  444. UNITY_BRANCH if (blendMask * weight > 1e-5f ) \
  445. { \
  446. mask[i] = Mask(i); \
  447. mask[i] = RemapMask(i); \
  448. } \
  449. else \
  450. { \
  451. mask[i] = float4(_Metallic##i, 1, 0.5, 0); \
  452. } \
  453. SampleMasks(0, blendMask[0].r);
  454. #ifndef _LAYERS_ONE
  455. SampleMasks(1, blendMask[0].g);
  456. #ifndef _LAYERS_TWO
  457. SampleMasks(2, blendMask[0].b);
  458. SampleMasks(3, blendMask[0].a);
  459. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  460. SampleMasks(4, blendMask[1].r);
  461. SampleMasks(5, blendMask[1].g);
  462. SampleMasks(6, blendMask[1].b);
  463. SampleMasks(7, blendMask[1].a);
  464. #endif
  465. #ifdef _LAYERS_SIXTEEN
  466. SampleMasks(8, blendMask[2].r);
  467. SampleMasks(9, blendMask[2].g);
  468. SampleMasks(10, blendMask[2].b);
  469. SampleMasks(11, blendMask[2].a);
  470. SampleMasks(12, blendMask[3].r);
  471. SampleMasks(13, blendMask[3].g);
  472. SampleMasks(14, blendMask[3].b);
  473. SampleMasks(15, blendMask[3].a);
  474. #endif
  475. #endif
  476. #endif
  477. #undef SampleMasks
  478. }
  479. #if defined(_LAYERS_SIXTEEN) && (defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN))
  480. #define SampleSplats(i) SAMPLE_TEXTURE2D_ARRAY(_SplatArray16, sampler_Splat0, uv[i], i)
  481. #else
  482. #define SampleSplats(i) SAMPLE_TEXTURE2D(_Splat##i, sampler_Splat0, uv[i])
  483. #endif
  484. void SampleSplat(float2 uv[_LAYER_COUNT], float4 blendMask[4], float weight, inout float4 mask[_LAYER_COUNT], out float4 mixAlbedo, out float3 mixNormal)
  485. {
  486. float4 albedo[_LAYER_COUNT];
  487. float3 normal[_LAYER_COUNT];
  488. mixAlbedo = 0;
  489. mixNormal = 0;
  490. #define Samples(i, blendMask) blendMask *= weight; \
  491. UNITY_BRANCH if (blendMask > 1e-5f) \
  492. { \
  493. albedo[i] = SampleSplats(i); \
  494. albedo[i].rgb *= DiffuseRemap(i).xyz; \
  495. albedo[i].a = Smoothness(i).x; \
  496. normal[i] = SampleNormals(i).xyz; \
  497. mixAlbedo += albedo[i].xyzw * blendMask.x; \
  498. mixNormal += normal[i].xyz * blendMask.x; \
  499. } \
  500. else \
  501. { \
  502. albedo[i] = float4(0, 0, 0, 0); \
  503. normal[i] = float3(0, 1, 0); \
  504. } \
  505. Samples(0, blendMask[0].r);
  506. #ifndef _LAYERS_ONE
  507. Samples(1, blendMask[0].g);
  508. #ifndef _LAYERS_TWO
  509. Samples(2, blendMask[0].b);
  510. Samples(3, blendMask[0].a);
  511. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  512. Samples(4, blendMask[1].r);
  513. Samples(5, blendMask[1].g);
  514. Samples(6, blendMask[1].b);
  515. Samples(7, blendMask[1].a);
  516. #endif
  517. #ifdef _LAYERS_SIXTEEN
  518. Samples( 8, blendMask[2].r);
  519. Samples( 9, blendMask[2].g);
  520. Samples(10, blendMask[2].b);
  521. Samples(11, blendMask[2].a);
  522. Samples(12, blendMask[3].r);
  523. Samples(13, blendMask[3].g);
  524. Samples(14, blendMask[3].b);
  525. Samples(15, blendMask[3].a);
  526. #endif
  527. #endif
  528. #endif
  529. #undef Samples
  530. }
  531. half4 MaskSplatWeight(float4 mask[_LAYER_COUNT], float4 blendMask[4], out float4 mixedMask)
  532. {
  533. float splatWeight[_LAYER_COUNT];
  534. mixedMask = 0;
  535. #ifdef _TERRAIN_NORMAL_IN_MASK
  536. #define MixdMask(i, blendMask) float4(_Metallic##i, mask[i].r, mask[i].b, 0.0f) * blendMask
  537. #elif defined(_TERRAIN_MASK_HEIGHTMAP_ONLY)
  538. #define MixdMask(i, blendMask) float4(_Metallic##i, 1.0f, mask[i].b, 0.0f) * blendMask
  539. #else
  540. #define MixdMask(i, blendMask) mask[i] * blendMask
  541. #endif
  542. #define MixdMasks(i, blendMask) \
  543. UNITY_BRANCH if (blendMask > 0) \
  544. { \
  545. mixedMask += MixdMask(i, blendMask); \
  546. }
  547. MixdMasks(0, blendMask[0].r);
  548. #ifndef _LAYERS_ONE
  549. MixdMasks(1, blendMask[0].g);
  550. #ifndef _LAYERS_TWO
  551. MixdMasks(2, blendMask[0].b);
  552. MixdMasks(3, blendMask[0].a);
  553. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  554. MixdMasks(4, blendMask[1].r);
  555. MixdMasks(5, blendMask[1].g);
  556. MixdMasks(6, blendMask[1].b);
  557. MixdMasks(7, blendMask[1].a);
  558. #endif
  559. #ifdef _LAYERS_SIXTEEN
  560. MixdMasks( 8, blendMask[2].r);
  561. MixdMasks( 9, blendMask[2].g);
  562. MixdMasks(10, blendMask[2].b);
  563. MixdMasks(11, blendMask[2].a);
  564. MixdMasks(12, blendMask[3].r);
  565. MixdMasks(13, blendMask[3].g);
  566. MixdMasks(14, blendMask[3].b);
  567. MixdMasks(15, blendMask[3].a);
  568. #endif
  569. #endif
  570. #endif
  571. return mixedMask;
  572. }
  573. void MaskWeight(inout float4 mask[_LAYER_COUNT], float4 mask_front[_LAYER_COUNT], float4 mask_side[_LAYER_COUNT], float4 blendMask[4], inout float3 triplanarWeights, float heightBlendingSharpness)
  574. {
  575. float splatWeight[_LAYER_COUNT];
  576. float3 heights = 0;
  577. splatWeight[0] = blendMask[0].r;
  578. #ifndef _LAYERS_ONE
  579. splatWeight[1] = blendMask[0].g;
  580. #ifndef _LAYERS_TWO
  581. splatWeight[2] = blendMask[0].b;
  582. splatWeight[3] = blendMask[0].a;
  583. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  584. splatWeight[4] = blendMask[1].r;
  585. splatWeight[5] = blendMask[1].g;
  586. splatWeight[6] = blendMask[1].b;
  587. splatWeight[7] = blendMask[1].a;
  588. #endif
  589. #ifdef _LAYERS_SIXTEEN
  590. splatWeight[8] = blendMask[2].r;
  591. splatWeight[9] = blendMask[2].g;
  592. splatWeight[10] = blendMask[2].b;
  593. splatWeight[11] = blendMask[2].a;
  594. splatWeight[12] = blendMask[3].r;
  595. splatWeight[13] = blendMask[3].g;
  596. splatWeight[14] = blendMask[3].b;
  597. splatWeight[15] = blendMask[3].a;
  598. #endif
  599. #endif
  600. #endif
  601. for (int i = 0; i < _LAYER_COUNT; ++i)
  602. {
  603. mask[i] = (mask[i] * triplanarWeights.y) + (mask_front[i] * triplanarWeights.z) + (mask_side[i] * triplanarWeights.x);
  604. #if defined(_TERRAIN_BLEND_HEIGHT)
  605. if (_HeightmapBlending == 1)
  606. {
  607. heights += float3(mask_side[i].b, mask[i].b, mask_front[i].b) * splatWeight[i];
  608. }
  609. #endif
  610. }
  611. #if defined(_TERRAIN_BLEND_HEIGHT)
  612. if (_HeightmapBlending == 1)
  613. {
  614. triplanarWeights.rgb *= (1 / (1 * pow(2, (heights + triplanarWeights) * (-(heightBlendingSharpness)))) + 1) * 0.5;
  615. triplanarWeights.rgb /= (triplanarWeights.r + triplanarWeights.g + triplanarWeights.b);
  616. }
  617. #endif
  618. }
  619. float HeightSum(float4 mask[_LAYER_COUNT], float4 blendMask[4])
  620. {
  621. #ifdef _LAYERS_ONE
  622. return float(mask[0].b);
  623. #else
  624. float heightSum = dot(blendMask[0].rg, float2(mask[0].b, mask[1].b));
  625. #ifndef _LAYERS_TWO
  626. heightSum += dot(blendMask[0].ba, float2(mask[2].b, mask[3].b));
  627. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  628. heightSum += dot(blendMask[1], float4(mask[4].b, mask[5].b, mask[6].b, mask[7].b));
  629. #endif
  630. #endif
  631. return heightSum;
  632. #endif
  633. }
  634. float4 TrackSplatValues(float4 blendMask[4], float4 trackSplats[_LAYER_COUNT])
  635. {
  636. #ifdef _LAYERS_ONE
  637. return trackSplats[0];
  638. #else
  639. float4 color = (blendMask[0].r * trackSplats[0])
  640. + (blendMask[0].g * trackSplats[1]);
  641. #ifndef _LAYERS_TWO
  642. color += (blendMask[0].b * trackSplats[2])
  643. + (blendMask[0].a * trackSplats[3]);
  644. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  645. color += (blendMask[1].r * trackSplats[4])
  646. + (blendMask[1].g * trackSplats[5])
  647. + (blendMask[1].b * trackSplats[6])
  648. + (blendMask[1].a * trackSplats[7]);
  649. #endif
  650. #ifdef _LAYERS_SIXTEEN
  651. color += (blendMask[2].r * trackSplats[8])
  652. + (blendMask[2].g * trackSplats[9])
  653. + (blendMask[2].b * trackSplats[10])
  654. + (blendMask[2].a * trackSplats[11])
  655. + (blendMask[3].r * trackSplats[12])
  656. + (blendMask[3].g * trackSplats[13])
  657. + (blendMask[3].b * trackSplats[14])
  658. + (blendMask[3].a * trackSplats[15]);
  659. #endif
  660. #endif
  661. return color;
  662. #endif
  663. }
  664. #define SpecularValueR(i) _Gamma ? _Specular##i.r : pow(abs(_Specular##i.r),1/2.2f);
  665. #define SpecularValueG(i) _Gamma ? _Specular##i.g : pow(abs(_Specular##i.g),1/2.2f);
  666. #define SpecularValueB(i) _Gamma ? _Specular##i.b : pow(abs(_Specular##i.b),1/2.2f);
  667. void UnpackTrackSplatValues(out float4 trackSplats[_LAYER_COUNT])
  668. {
  669. float value;
  670. int precision = 1024;
  671. #define trackSplat(i) value = SpecularValueR(i); \
  672. trackSplats[i].z = value % precision; \
  673. value = floor(value / precision); \
  674. trackSplats[i].x = value; \
  675. trackSplats[i] /= (precision - 1); \
  676. \
  677. trackSplats[i].y = (_DiffuseRemapOffset##i.w * 10.0f) % 1 ; \
  678. trackSplats[i].w = floor((_DiffuseRemapOffset##i.w % 1 ) * 10.0f); \
  679. trackSplat(0);
  680. #ifndef _LAYERS_ONE
  681. trackSplat(1);
  682. #ifndef _LAYERS_TWO
  683. trackSplat(2);
  684. trackSplat(3);
  685. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  686. trackSplat(4);
  687. trackSplat(5);
  688. trackSplat(6);
  689. trackSplat(7);
  690. #endif
  691. #ifdef _LAYERS_SIXTEEN
  692. trackSplat(8);
  693. trackSplat(9);
  694. trackSplat(10);
  695. trackSplat(11);
  696. trackSplat(12);
  697. trackSplat(13);
  698. trackSplat(14);
  699. trackSplat(15);
  700. #endif
  701. #endif
  702. #endif
  703. }
  704. void UnpackTrackSplatColor(out float4 trackSplatsColor[_LAYER_COUNT])
  705. {
  706. float color;
  707. float value;
  708. int precision = 1024;
  709. #define trackSplatColor(i) color = SpecularValueG(i) \
  710. \
  711. trackSplatsColor[i].y = color % precision; \
  712. color = floor(color / precision); \
  713. trackSplatsColor[i].x = color; \
  714. value = SpecularValueB(i); \
  715. trackSplatsColor[i].w = value % precision; \
  716. value = floor(value / precision); \
  717. trackSplatsColor[i].z = value % precision; \
  718. trackSplatsColor[i] /= (precision - 1); \
  719. trackSplatColor(0);
  720. #ifndef _LAYERS_ONE
  721. trackSplatColor(1);
  722. #ifndef _LAYERS_TWO
  723. trackSplatColor(2);
  724. trackSplatColor(3);
  725. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  726. trackSplatColor(4);
  727. trackSplatColor(5);
  728. trackSplatColor(6);
  729. trackSplatColor(7);
  730. #endif
  731. #ifdef _LAYERS_SIXTEEN
  732. trackSplatColor(8);
  733. trackSplatColor(9);
  734. trackSplatColor(10);
  735. trackSplatColor(11);
  736. trackSplatColor(12);
  737. trackSplatColor(13);
  738. trackSplatColor(14);
  739. trackSplatColor(15);
  740. #endif
  741. #endif
  742. #endif
  743. }
  744. #ifdef _TERRAIN_BLEND_HEIGHT
  745. void HeightBlend(float4 mask[_LAYER_COUNT], inout float4 blendMask[4], float sharpness)
  746. {
  747. #ifdef _LAYERS_TWO
  748. float2 height = float2(mask[0].b, mask[1].b);
  749. blendMask[0].rg *= (1 / (pow(2, (height + blendMask[0].rg) * (-(sharpness)))) + 1) * 0.5;
  750. blendMask[0].rg /= (blendMask[0].r + blendMask[0].g);
  751. #else
  752. float4 height = float4 (mask[0].b, mask[1].b, mask[2].b, mask[3].b);
  753. blendMask[0].rgba *= (1 / (pow(2, (height + blendMask[0].rgba) * (-(sharpness)))) + 1) * 0.5;
  754. float heightSum = blendMask[0].r + blendMask[0].g + blendMask[0].b + blendMask[0].a;
  755. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  756. float4 height1 = float4 (mask[4].b, mask[5].b, mask[6].b, mask[7].b);
  757. blendMask[1].rgba *= (1 / (pow(2, (height1 + blendMask[1].rgba) * (-(sharpness)))) + 1) * 0.5;
  758. heightSum += blendMask[1].r + blendMask[1].g + blendMask[1].b + blendMask[1].a;
  759. #ifdef _LAYERS_SIXTEEN
  760. float4 height2 = float4 (mask[8].b, mask[9].b, mask[10].b, mask[11].b);
  761. blendMask[2].rgba *= (1 / (pow(2, (height2 + blendMask[2].rgba) * (-(sharpness)))) + 1) * 0.5;
  762. heightSum += blendMask[2].r + blendMask[2].g + blendMask[2].b + blendMask[2].a;
  763. float4 height3 = float4 (mask[12].b, mask[13].b, mask[14].b, mask[15].b);
  764. blendMask[3].rgba *= (1 / (pow(2, (height3 + blendMask[3].rgba) * (-(sharpness)))) + 1) * 0.5;
  765. heightSum += blendMask[3].r + blendMask[3].g + blendMask[3].b + blendMask[3].a;
  766. blendMask[2].rgba /= heightSum;
  767. blendMask[3].rgba /= heightSum;
  768. #endif
  769. blendMask[1].rgba /= heightSum;
  770. #endif
  771. blendMask[0].rgba /= heightSum;
  772. #endif
  773. }
  774. #endif
  775. #ifndef _TERRAIN_BASEMAP_GEN
  776. void SampleSplatTOL(in out float4 mixedAlbedo, in out float3 mixedNormal, float2 uv[_LAYER_COUNT], float4 blendMask[4], float weight, float4 mask[_LAYER_COUNT])
  777. {
  778. float4 albedo[1];
  779. float3 normal[1];
  780. albedo[0] = float4(0, 0, 0, 0);
  781. normal[0] = float3(0, 0, 1);
  782. blendMask[0].r *= weight;
  783. UNITY_BRANCH if (blendMask[0].r > 1e-5f)
  784. {
  785. albedo[0] = SAMPLE_TEXTURE2D(_Splat0, SamplerState_Linear_Repeat, uv[0]);
  786. albedo[0].rgb *= DiffuseRemap(0).rgb;
  787. albedo[0].a = Smoothness(0).x;
  788. normal[0] = SampleNormals(0);
  789. }
  790. mixedAlbedo = (albedo[0] * blendMask[0].r);
  791. mixedNormal = (normal[0] * blendMask[0].r);
  792. }
  793. void SampleMaskTOL(out float4 mask[_LAYER_COUNT], float4 noTriplanarMask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], float weight)
  794. {
  795. UNITY_BRANCH if (weight > 1e-5f)
  796. {
  797. mask[0] = Mask(0);
  798. mask[0] = RemapMask(0);
  799. }
  800. else
  801. {
  802. mask[0] = float4(0, 0, 0.5, 0);
  803. }
  804. mask[1] = noTriplanarMask[1];
  805. #ifndef _LAYERS_TWO
  806. mask[2] = noTriplanarMask[2];
  807. mask[3] = noTriplanarMask[3];
  808. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  809. mask[4] = noTriplanarMask[4];
  810. mask[5] = noTriplanarMask[5];
  811. mask[6] = noTriplanarMask[6];
  812. mask[7] = noTriplanarMask[7];
  813. #endif
  814. #ifdef _LAYERS_SIXTEEN
  815. mask[8] = noTriplanarMask[8];
  816. mask[9] = noTriplanarMask[9];
  817. mask[10] = noTriplanarMask[10];
  818. mask[11] = noTriplanarMask[11];
  819. mask[12] = noTriplanarMask[12];
  820. mask[13] = noTriplanarMask[13];
  821. mask[14] = noTriplanarMask[14];
  822. mask[15] = noTriplanarMask[15];
  823. #endif
  824. #endif
  825. }
  826. #define TAU 6.283185307
  827. float random(in float2 st)
  828. {
  829. return frac(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453123);
  830. }
  831. float2 rotate_2d(float2 p_input, float p_theta)
  832. {
  833. float2x2 l_rot_matrix = float2x2(cos(p_theta), -sin(p_theta),
  834. sin(p_theta), cos(p_theta));
  835. return mul(l_rot_matrix, p_input);
  836. }
  837. float3 RainRipples(float2 uv, float scale, float rotation)
  838. {
  839. float3 normal = float3(0, 0, 1);
  840. uv.xy = rotate_2d(uv.xy, rotation);
  841. float2 sUV = (uv.xy * scale + scale * 0.1f);
  842. sUV.x += step(1.0f, (sUV.y % 2.0)) * 0.5f;
  843. float2 center = float2(0.5f, 0.5f);
  844. float size = 0.25f;
  845. float2 tile = floor(sUV);
  846. float2 fract = frac(sUV);
  847. float2 offset = (center - fract.xy) * size;
  848. float2 cUV = (fract - 0.5) / size + offset;
  849. float2 polarUV = float2(length(cUV), atan2(cUV.y, cUV.x));
  850. float time = _Time.x * scale * 3.0f;
  851. float radius = frac(time * 0.9f + (random(floor(sUV) + 5000.0).x)) * 1.25f;
  852. if (radius < 0.8f)
  853. {
  854. float thickness = min((size * 0.5f + 0.25f) * 0.4f, radius);
  855. float start = radius + thickness;
  856. float end = max(0., radius - thickness);
  857. if (radius > 0.15)
  858. {
  859. thickness *= (0.9 - (radius * 0.9f));
  860. }
  861. float radius2 = radius - thickness * 1.25f;
  862. float cAngle = smoothstep(start, end, polarUV.x) * PI;
  863. float start2 = radius2 + thickness;
  864. float end2 = max(0., radius2 - thickness);
  865. float cAngle2 = smoothstep(start2, end2, polarUV.x) * PI;
  866. float rippleMask = smoothstep(thickness, -0.1f, abs(polarUV.x - radius));
  867. float rippleMask2 = smoothstep(thickness, -0.1f, abs(polarUV.x - radius2));
  868. float decayMask = min(1., max(0., 1. - polarUV.x));
  869. cAngle = lerp(cAngle, PI * 0.5f, (1.0f - rippleMask * decayMask));
  870. cAngle2 = lerp(cAngle2, PI * 0.5f, (1.0f - rippleMask2 * decayMask));
  871. float c = lerp(cos(cAngle), cos(cAngle2), 0.5f);
  872. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  873. normal = float3(c * sin(polarUV.y), c * cos(polarUV.y), sin(cAngle));
  874. #else
  875. normal = float3(c * sin(polarUV.y) * -1, c * cos(polarUV.y) * -1, sin(cAngle));
  876. #endif
  877. float opacity = _InTerra_GlobalRaindropRipples.y;
  878. normal.xy = rotate_2d(normal.xy, rotation) * (opacity - (radius * opacity));
  879. }
  880. return normal;
  881. }
  882. #endif
  883. #endif
  884. #ifdef INTERRA_OBJECT
  885. float ObjectTerrainIntersection(float3 worldNormal, float TerrainHeightOffset, float4 mask[_LAYER_COUNT], float4 blendMask[4], float objectHeightMap, float sharpness)
  886. {
  887. float steepWeights = _SteepIntersection == 1 ? saturate(worldNormal.y + _Steepness) : 1;
  888. float intersect1 = smoothstep(_Intersection.y, _Intersection.x, TerrainHeightOffset) * steepWeights;
  889. float intersect2 = smoothstep(_Intersection2.y, _Intersection2.x, TerrainHeightOffset) * (1 - steepWeights);
  890. float intersection = intersect1 + intersect2;
  891. float heightSum;
  892. #ifdef _TERRAIN_BLEND_HEIGHT
  893. heightSum = lerp(HeightSum(mask, blendMask), 1, intersection);
  894. #else
  895. heightSum = 0.5;
  896. #endif
  897. float2 heightIntersect = (1 / (1 * pow(2, float2(((1 - intersection) * objectHeightMap), (intersection * heightSum)) * (-(sharpness)))) + 1) * 0.5;
  898. heightIntersect /= (heightIntersect.r + heightIntersect.g);
  899. return heightIntersect.x;
  900. }
  901. #endif