InTerra_Mixing.cginc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. //====================================================================
  2. //===========================| VERTEX |===============================
  3. //====================================================================
  4. #ifndef TERRAIN_BASEGEN
  5. void SplatmapVert(inout appdata_full v, out Input data)
  6. {
  7. UNITY_INITIALIZE_OUTPUT(Input, data);
  8. #if !defined(INTERRA_TERRAIN)
  9. data.worldNormal = UnityObjectToWorldNormal(v.normal);
  10. data.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  11. #endif
  12. //=================== TERRAIN INSTANCING ====================
  13. #if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) && !defined(INTERRA_OBJECT) && !defined(INTERRA_MESH_TERRAIN)
  14. float2 patchVertex = v.vertex.xy;
  15. float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData);
  16. float4 uvscale = instanceData.z * _TerrainHeightmapRecipSize;
  17. float4 uvoffset = instanceData.xyxy * uvscale;
  18. uvoffset.xy += 0.5f * _TerrainHeightmapRecipSize.xy;
  19. float2 sampleCoords = (patchVertex.xy * uvscale.xy + uvoffset.xy);
  20. float hm = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(sampleCoords, 0, 0)));
  21. v.vertex.xz = (patchVertex.xy + instanceData.xy) * _TerrainHeightmapScale.xz * instanceData.z;
  22. v.vertex.y = hm * _TerrainHeightmapScale.y;
  23. v.vertex.w = 1.0f;
  24. v.texcoord.xy = (patchVertex.xy * uvscale.zw + uvoffset.zw);
  25. v.texcoord3 = v.texcoord2 = v.texcoord1 = v.texcoord;
  26. #ifdef TERRAIN_INSTANCED_PERPIXEL_NORMAL
  27. v.normal = float3(0, 1, 0);
  28. data.tc.zw = sampleCoords;
  29. #else
  30. float3 nor = tex2Dlod(_TerrainNormalmapTexture, float4(sampleCoords, 0, 0)).xyz;
  31. v.normal = 2.0f * nor - 1.0f;
  32. #endif
  33. float3 nor = tex2Dlod(_TerrainNormalmapTexture, float4(sampleCoords, 0, 0)).xyz;
  34. data.terrainNormals = UnityObjectToWorldNormal(2.0f * nor - 1.0f);
  35. #endif
  36. //==============================================================
  37. float3 wDir = WorldSpaceViewDir(v.vertex);
  38. #if defined(INTERRA_TERRAIN)
  39. data.tc.xy = v.texcoord.xy;
  40. #ifdef TERRAIN_BASE_PASS
  41. #ifdef UNITY_PASS_META
  42. data.tc.xy = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
  43. #endif
  44. #else
  45. float4 pos = UnityObjectToClipPos(v.vertex);
  46. UNITY_TRANSFER_FOG(data, pos);
  47. #endif
  48. v.tangent.xyz = cross(v.normal, float3(0, 0, 1));
  49. v.tangent.w = -1;
  50. #ifdef PARALLAX
  51. data.tangentViewDir = TangentViewDir(v.normal, v.tangent, wDir);
  52. #endif
  53. #else
  54. #if defined(_OBJECT_PARALLAX) || (defined(INTERRA_MESH_TERRAIN) && defined(_TERRAIN_PARALLAX)) && !defined(DIFFUSE)
  55. data.tangentViewDirObject = TangentViewDir(v.normal, v.tangent, ObjSpaceViewDir(v.vertex));
  56. #endif
  57. float2 hmUV = float2 ((data.worldPos.x - _TerrainPosition.x) * (1 / _TerrainSize.x), (data.worldPos.z - _TerrainPosition.z) * (1 / _TerrainSize.z));
  58. float4 ts = float4(_TerrainHeightmapTexture_TexelSize.x, _TerrainHeightmapTexture_TexelSize.y, 0, 0);
  59. float4 hsX = _TerrainHeightmapScale.w / _TerrainHeightmapScale.x;
  60. float4 hsZ = _TerrainHeightmapScale.w / _TerrainHeightmapScale.z;
  61. float4 heightToNormal;
  62. float3 terrainNormal;
  63. heightToNormal[0] = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(hmUV + float2(ts * float2(0, -1)), 0, 0))).r * hsZ;
  64. heightToNormal[1] = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(hmUV + float2(ts * float2(-1, 0)), 0, 0))).r * hsX;
  65. heightToNormal[2] = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(hmUV + float2(ts * float2(1, 0)), 0, 0))).r * hsX;
  66. heightToNormal[3] = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(hmUV + float2(ts * float2(0, 1)), 0, 0))).r * hsZ;
  67. terrainNormal.x = heightToNormal[1] - heightToNormal[2];
  68. terrainNormal.z = heightToNormal[0] - heightToNormal[3];
  69. terrainNormal.y = 1;
  70. float3 height = UnpackHeightmap(tex2Dlod(_TerrainHeightmapTexture, float4(hmUV, 0, 0)));
  71. float heightOffset = data.worldPos.y - _TerrainPosition.y + (height * -_TerrainHeightmapScale.y);
  72. #ifdef INTERRA_MESH_TERRAIN
  73. if (_NormalsFromHeightmap)
  74. {
  75. v.normal = UnityObjectToWorldNormal(terrainNormal);
  76. }
  77. if (_CheckHeight)
  78. {
  79. heightOffset = _TerrainPosition.y - (height * -_TerrainHeightmapScale.y);
  80. float3 worldVertPos = float3(data.worldPos.x, heightOffset, data.worldPos.z);
  81. v.vertex.y = mul(unity_WorldToObject, worldVertPos).y;
  82. }
  83. #endif
  84. float3 tWeights = pow(abs(terrainNormal), _TriplanarSharpness);
  85. tWeights = tWeights / (tWeights.x + tWeights.y + tWeights.z);
  86. data.mainTC_tWeightY_hOffset = float4(v.texcoord.xy, tWeights.y, heightOffset);
  87. #if defined(PARALLAX) && defined(_TERRAIN_PARALLAX)
  88. if (_Terrain_Parallax)
  89. {
  90. float intersection = smoothstep(_NormIntersect.y, _NormIntersect.x, heightOffset);
  91. float3 mixedNormal = lerp(data.worldNormal, normalize(terrainNormal), intersection);
  92. half3 axisSign = sign(mixedNormal);
  93. half3 tangentY = normalize(cross(mixedNormal, half3(0, 0, axisSign.y)));
  94. half3 bitangentY = normalize(cross(tangentY, mixedNormal)) * axisSign.y;
  95. half3x3 tbnY = half3x3(tangentY, bitangentY, mixedNormal);
  96. data.tangentViewDir = mul(tbnY, wDir);
  97. }
  98. #endif
  99. float3 wTangent = UnityObjectToWorldDir(v.tangent);
  100. float3 wBTangent = normalize(cross(data.worldNormal, wTangent)) * v.tangent.w * unity_WorldTransformParams.w;
  101. float3x3 tangentTransform_World = float3x3(wTangent, wBTangent, data.worldNormal);
  102. data.terrainNormals = mul(tangentTransform_World, terrainNormal);
  103. #endif
  104. }
  105. #endif
  106. //==============================================================================
  107. //==========================| FRAGMENT MIXING |============================
  108. //==============================================================================
  109. #ifdef DIFFUSE
  110. void SplatmapMix(Input IN, out half weight, out fixed4 mixedDiffuse, out fixed3 mixedNormal)
  111. #else
  112. #ifdef INTERRA_OBJECT
  113. void SplatmapMix(Input IN, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal, out float ao, out half metallic, out half3 emission)
  114. #else
  115. void SplatmapMix(Input IN, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal, out float ao, out half metallic)
  116. #endif
  117. #endif
  118. {
  119. //---------------- VARIABLES -----------------
  120. half4 blendMask[2];
  121. blendMask[0] = half4(1, 0, 0, 0);
  122. blendMask[1] = half4(0, 0, 0, 0);
  123. mixedDiffuse = 1;
  124. mixedNormal = float3( 0 ,0, 1);
  125. half4 mixedMask = 0;
  126. half heightSum = 0.5f;
  127. half dHeightSum = 0.5f;
  128. #ifndef DIFFUSE
  129. ao = 1;
  130. metallic = 0.0f;
  131. #endif
  132. float2 uvSplat[_LAYER_COUNT];
  133. half4 splat[_LAYER_COUNT], mask[_LAYER_COUNT];
  134. float2 uvSplat_front[_LAYER_COUNT], uvSplat_side[_LAYER_COUNT];
  135. half4 splat_front[_LAYER_COUNT], splat_side[_LAYER_COUNT], mask_front[_LAYER_COUNT], mask_side[_LAYER_COUNT];
  136. #ifdef _TERRAIN_DISTANCEBLEND
  137. float2 distantUV[_LAYER_COUNT];
  138. half4 dSplat[_LAYER_COUNT], dMask[_LAYER_COUNT];
  139. float2 distantUV_front[_LAYER_COUNT], distantUV_side[_LAYER_COUNT];
  140. #ifdef TRIPLANAR
  141. half4 dSplat_front[_LAYER_COUNT], dSplat_side[_LAYER_COUNT], dMask_front[_LAYER_COUNT], dMask_side[_LAYER_COUNT];
  142. #endif
  143. #endif
  144. #ifdef _TRACKS
  145. float4 trackSplats[_LAYER_COUNT];
  146. float4 trackSplatsColor[_LAYER_COUNT];
  147. float4 trackDepth = 0;
  148. #endif
  149. float3 worldTangent = WorldNormalVector(IN, float3(1, 0, 0));
  150. float3 worldBitangent = WorldNormalVector(IN, float3(0, 1, 0));
  151. //---------------- CLIP HOLES ----------------
  152. #if defined(_ALPHATEST_ON) && defined(INTERRA_TERRAIN)
  153. ClipHoles(IN.tc.xy);
  154. #endif
  155. //================================================================
  156. //----------------------- SPLAT MAP CONTROL ----------------------
  157. //================================================================
  158. #ifndef INTERRA_TERRAIN
  159. float2 terrainUV = (IN.worldPos.xz - _TerrainPosition.xz) * (1 / _TerrainSize.xz);
  160. #ifndef _LAYERS_ONE
  161. float2 splatMapUV = (terrainUV * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  162. #endif
  163. half2 tintUV = terrainUV * _TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw;
  164. half2 normalTintUV = terrainUV * _TerrainNormalTintTexture_ST.xy + _TerrainNormalTintTexture_ST.zw;
  165. #else
  166. float2 splatMapUV = (IN.tc.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  167. half2 tintUV = IN.tc.xy * _TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw;
  168. half2 normalTintUV = IN.tc.xy * _TerrainNormalTintTexture_ST.xy + _TerrainNormalTintTexture_ST.zw;
  169. #endif
  170. #if !defined(_LAYERS_ONE)
  171. blendMask[0] = UNITY_SAMPLE_TEX2D(_Control, splatMapUV);
  172. #ifdef _LAYERS_EIGHT
  173. blendMask[1] = UNITY_SAMPLE_TEX2D_SAMPLER(_Control1, _Control, splatMapUV);
  174. #endif
  175. #endif
  176. #if defined(INTERRA_TERRAIN)
  177. #if defined(_LAYERS_EIGHT)
  178. weight = 1;
  179. #else
  180. weight = dot(blendMask[0], half4(1, 1, 1, 1));
  181. #if !defined(SHADER_API_MOBILE) && defined(TERRAIN_SPLAT_ADDPASS)
  182. clip(weight == 0.0f ? -1 : 1);
  183. #endif
  184. blendMask[0] /= (weight + 1e-3f);
  185. #endif
  186. #endif
  187. #if !defined(INTERRA_TERRAIN) || defined(TRIPLANAR) || defined(_PUDDLES) || defined(PARALLAX)
  188. #ifdef UNITY_INSTANCING_ENABLED
  189. float3 wNormal = IN.terrainNormals;
  190. #else
  191. float3 wNormal = WorldNormalVector(IN, float3(0, 0, 1));
  192. #endif
  193. float3 flipUV = wNormal < 0 ? -1 : 1;
  194. float3 triplanarWeights = abs(wNormal);
  195. triplanarWeights = pow(triplanarWeights, _TriplanarSharpness);
  196. triplanarWeights = triplanarWeights / (triplanarWeights.x + triplanarWeights.y + triplanarWeights.z);
  197. #if defined(INTERRA_OBJECT)
  198. TriplanarOneToAllSteep(blendMask, (1 - IN.mainTC_tWeightY_hOffset.z), weight);
  199. #else
  200. TriplanarOneToAllSteep(blendMask, (1 - triplanarWeights.y), weight);
  201. #endif
  202. #endif
  203. #if defined(INTERRA_OBJECT) && defined(_LAYERS_TWO)
  204. blendMask[0].r = _ControlNumber == 0 ? blendMask[0].r : _ControlNumber == 1 ? blendMask[0].g : _ControlNumber == 2 ? blendMask[0].b : blendMask[0].a;
  205. blendMask[0].g = 1 - blendMask[0].r;
  206. #else
  207. if (_TwoLayersOnly > 0)
  208. {
  209. blendMask[0].r = 1 - blendMask[0].g;
  210. blendMask[0].ba = 0;
  211. blendMask[1] = 0;
  212. }
  213. #endif
  214. #if defined(_TERRAIN_BLEND_HEIGHT) && !defined(TERRAIN_SPLAT_ADDPASS) && !defined(_LAYERS_EIGHT)
  215. blendMask[0] = (blendMask[0].r + blendMask[0].g + blendMask[0].b + blendMask[0].a == 0.0f ? 1.0f : blendMask[0]); //this is preventing the black area when more than one pass
  216. #endif
  217. half4 origBlendMask[2] = blendMask;
  218. #if defined(_TERRAIN_DISTANCEBLEND)
  219. float dist = smoothstep(_HT_distance.x, _HT_distance.y, (distance(IN.worldPos, _WorldSpaceCameraPos)));
  220. half4 dBlendMask[2];
  221. half4 dOrigBlendMask[2] = blendMask;
  222. dBlendMask[0] = blendMask[0];
  223. dBlendMask[1] = blendMask[1];
  224. half4 dMixedMask = 0;
  225. float sampleDistMask = dist < 0.01f ? 1 : 0;
  226. if(sampleDistMask)
  227. {
  228. dBlendMask[0] *= 0.0f;
  229. #ifdef _LAYERS_EIGHT
  230. dBlendMask[1] *= 0.0f;
  231. #endif
  232. }
  233. #endif
  234. #if defined(PARALLAX) && !defined(DIFFUSE)
  235. int lod = smoothstep(_MipMapFade.x, _MipMapFade.y, (distance(IN.worldPos, _WorldSpaceCameraPos)));
  236. #endif
  237. #ifndef INTERRA_TERRAIN
  238. float2 mainUV = TRANSFORM_TEX(IN.mainTC_tWeightY_hOffset.xy, _MainTex);
  239. #if defined(PARALLAX) && defined(_OBJECT_PARALLAX)
  240. float2 mainParallaxOffset = 0;
  241. if (_Object_Parallax)
  242. {
  243. mainParallaxOffset = ParallaxOffset(_MainMask, _ParallaxSteps, _ParallaxHeight, mainUV, normalize(IN.tangentViewDirObject), _ParallaxAffineSteps, _MipMapLevel + (_MipMapLevel + (lod * _MipMapCount)), 0);
  244. mainUV += mainParallaxOffset;
  245. }
  246. #endif
  247. float4 objectAlbedo = UNITY_SAMPLE_TEX2D(_MainTex, mainUV) * _Color;
  248. weight = 0;
  249. #endif
  250. //===========================================================
  251. //-------------------------- UVs ----------------------------
  252. //===========================================================
  253. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  254. #if defined(INTERRA_OBJECT)
  255. #ifndef _OBJECT_TRIPLANAR
  256. _SteepDistortion = wNormal.y > 0.5 ? 0 : (1 - wNormal.y) * _SteepDistortion;
  257. _SteepDistortion *= objectAlbedo.r;
  258. #else
  259. _SteepDistortion = 0;
  260. #endif
  261. #endif
  262. float3 positionOffset = _WorldMapping ? IN.worldPos : (IN.worldPos - _TerrainPosition);
  263. #ifndef TRIPLANAR
  264. UvSplat(uvSplat, positionOffset);
  265. #else
  266. float offsetZ = -flipUV.z * IN.worldPos.y;
  267. float offsetX = -flipUV.x * IN.worldPos.y;
  268. #if defined(INTERRA_OBJECT)
  269. offsetZ = _DisableOffsetY == 1 ? -flipUV.z * IN.worldPos.y : IN.mainTC_tWeightY_hOffset.w * -flipUV.z + (IN.worldPos.z);
  270. offsetX = _DisableOffsetY == 1 ? -flipUV.x * IN.worldPos.y : IN.mainTC_tWeightY_hOffset.w * -flipUV.x + (IN.worldPos.x);
  271. #endif
  272. offsetZ -= _TerrainPosition.z;
  273. offsetX -= _TerrainPosition.x;
  274. UvSplat(uvSplat, uvSplat_front, uvSplat_side, positionOffset, offsetZ, offsetX, flipUV);
  275. #endif
  276. #else
  277. float2 uv = _WorldMapping ? (IN.worldPos.xz / _TerrainSizeXZPosY) : IN.tc.xy;
  278. #ifndef TRIPLANAR
  279. UvSplat(uvSplat, uv);
  280. #else
  281. UvSplat(uvSplat, uvSplat_front, uvSplat_side, IN.worldPos, uv, flipUV);
  282. #endif
  283. #endif
  284. //---------------- DISTANCE UV -----------------
  285. #ifdef _TERRAIN_DISTANCEBLEND
  286. DistantUV(distantUV, uvSplat);
  287. #ifdef TRIPLANAR
  288. #ifdef _TERRAIN_TRIPLANAR_ONE
  289. distantUV_front[0] = uvSplat_front[0] * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale;
  290. distantUV_side[0] = uvSplat_side[0] * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale;
  291. #else
  292. DistantUV(distantUV_front, uvSplat_front);
  293. DistantUV(distantUV_side, uvSplat_side);
  294. #endif
  295. #endif
  296. #endif
  297. //--------------- PARALLAX OFFSET -----------------
  298. #if defined(PARALLAX) && defined(_TERRAIN_PARALLAX)
  299. if(_Terrain_Parallax)
  300. {
  301. ParallaxUV(uvSplat, normalize(IN.tangentViewDir), blendMask, lod, triplanarWeights.y);
  302. }
  303. #endif
  304. //======================================================================
  305. //------------------------- OBJECT INTERSECTION ----------------------
  306. //======================================================================
  307. #ifdef INTERRA_OBJECT
  308. float steepWeights = _SteepIntersection == 1 ? saturate(wNormal.y + _Steepness) : 1;
  309. float intersect1 = smoothstep(_Intersection.y, _Intersection.x, IN.mainTC_tWeightY_hOffset.w) * steepWeights;
  310. float intersect2 = smoothstep(_Intersection2.y, _Intersection2.x, IN.mainTC_tWeightY_hOffset.w) * (1 - steepWeights);
  311. float intersection = intersect1 + intersect2;
  312. if (intersection < 1e-5f)
  313. {
  314. blendMask[0] = 0.0f;
  315. blendMask[1] = 0.0f;
  316. #if defined(_TERRAIN_DISTANCEBLEND)
  317. dBlendMask[0] = 0.0f;
  318. dBlendMask[1] = 0.0f;
  319. #endif
  320. }
  321. #endif
  322. //====================================================================
  323. //----------------------------- MASK MAPS --------------------------
  324. //====================================================================
  325. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  326. #ifdef _TERRAIN_TRIPLANAR_ONE
  327. #ifndef TERRAIN_SPLAT_ADDPASS
  328. float tolWeightY = saturate(triplanarWeights.y + (1 - blendMask[0].r));
  329. #else
  330. float tolWeightY = 1.0f;
  331. #endif
  332. SampleMask(mask, uvSplat, blendMask, tolWeightY);
  333. SampleMaskTOL(mask_front, mask, uvSplat_front, triplanarWeights.z);
  334. SampleMaskTOL(mask_side, mask, uvSplat_side, triplanarWeights.x);
  335. #else
  336. SampleMask(mask, uvSplat, blendMask, triplanarWeights.y);
  337. SampleMask(mask_front, uvSplat_front, blendMask, triplanarWeights.z);
  338. SampleMask(mask_side, uvSplat_side, blendMask, triplanarWeights.x);
  339. #endif
  340. MaskWeight(mask, mask_front, mask_side, blendMask, triplanarWeights, _HeightTransition);
  341. #else
  342. SampleMask(mask, uvSplat, blendMask, 1.0f);
  343. #endif
  344. #ifdef _TERRAIN_DISTANCEBLEND
  345. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  346. #ifdef _TERRAIN_TRIPLANAR_ONE
  347. #ifndef TERRAIN_SPLAT_ADDPASS
  348. float dTolWeightY = saturate(triplanarWeights.y + (1 - dBlendMask[0].r));
  349. #else
  350. float dTolWeightY = 1.0f;
  351. #endif
  352. SampleMask(dMask, distantUV, dBlendMask, dTolWeightY);
  353. SampleMaskTOL(dMask_front, dMask, distantUV_front, blendMask[0].r * triplanarWeights.z );
  354. SampleMaskTOL(dMask_side, dMask, distantUV_side, blendMask[0].r * triplanarWeights.x );
  355. #else
  356. SampleMask(dMask, distantUV, dBlendMask, triplanarWeights.y);
  357. SampleMask(dMask_front, distantUV_front, dBlendMask, triplanarWeights.z);
  358. SampleMask(dMask_side, distantUV_side, dBlendMask, triplanarWeights.x);
  359. #endif
  360. MaskWeight(dMask, dMask_front, dMask_side, dBlendMask, triplanarWeights, _Distance_HeightTransition);
  361. #else
  362. SampleMask(dMask, distantUV, dBlendMask, 1.0f);
  363. #endif
  364. dBlendMask = dOrigBlendMask;
  365. #endif
  366. blendMask = origBlendMask;
  367. //====================================================================
  368. //------------------------- HEIGHTMAP BLENDINGS ----------------------
  369. //====================================================================
  370. #if defined(_TERRAIN_BLEND_HEIGHT) && !defined(_LAYERS_ONE) && !defined(TERRAIN_SPLAT_ADDPASS)
  371. #if defined( _TERRAIN_BASEMAP_GEN) && !defined(_LAYERS_EIGHT)
  372. if (_NumLayersCount <= 4)
  373. #endif
  374. {
  375. if (_HeightmapBlending == 1)
  376. {
  377. HeightBlend(mask, blendMask, _HeightTransition);
  378. #ifdef _TERRAIN_DISTANCEBLEND
  379. HeightBlend(dMask, dBlendMask, _Distance_HeightTransition);
  380. #endif
  381. }
  382. }
  383. #endif
  384. MaskSplatWeight(mask, blendMask, mixedMask);
  385. #ifdef _TERRAIN_DISTANCEBLEND
  386. MaskSplatWeight(dMask, dBlendMask, dMixedMask);
  387. dMixedMask = lerp(mixedMask, dMixedMask, _HT_cover);
  388. mixedMask = lerp(mixedMask, dMixedMask, dist);
  389. #endif
  390. #if !defined(DIFFUSE)
  391. ao = mixedMask.g;
  392. metallic = mixedMask.r;
  393. #endif
  394. #if defined(_TERRAIN_BLEND_HEIGHT) && !defined(_LAYERS_ONE) && !defined(TERRAIN_SPLAT_ADDPASS)
  395. if (_HeightmapBlending == 1)
  396. {
  397. heightSum = mixedMask.b;
  398. #ifdef _TERRAIN_DISTANCEBLEND
  399. if(sampleDistMask)
  400. {
  401. dBlendMask[0] *= sampleDistMask;
  402. #ifdef _LAYERS_EIGHT
  403. dBlendMask[1] *= sampleDistMask;
  404. #endif
  405. }
  406. #endif
  407. }
  408. #endif
  409. //-------------------- HEIGHTMAP OBJECT INTERSECTION ----------------
  410. #ifdef INTERRA_OBJECT
  411. #ifdef DIFFUSE
  412. half height = objectAlbedo.a * _MaskMapRemapScale.b + _MaskMapRemapOffset.b;
  413. #else
  414. half4 objectMask = UNITY_SAMPLE_TEX2D_SAMPLER(_MainMask, _MainTex, mainUV);
  415. objectMask.rgba = objectMask.rgba * _MaskMapRemapScale.rgba + _MaskMapRemapOffset.rgba;
  416. half height = objectMask.b;
  417. #endif
  418. half terrainHeith = 0.5f;
  419. #if defined(_TERRAIN_BLEND_HEIGHT) && !defined(TERRAIN_SPLAT_ADDPASS)
  420. terrainHeith = lerp(heightSum, 1, intersection);
  421. #endif
  422. float2 heightIntersect = (1.0f / (pow(2.0f, float2(((1.0f - intersection) * height), (intersection * terrainHeith)) * (-(_Sharpness)))) + 1.0f) * 0.5f;
  423. heightIntersect /= (heightIntersect.r + heightIntersect.g);
  424. heightSum = (heightSum * heightIntersect.g) + (height * heightIntersect.r);
  425. if (heightIntersect.g < 1e-5f)
  426. {
  427. blendMask[0] = 0.0f;
  428. blendMask[1] = 0.0f;
  429. #if defined(_TERRAIN_DISTANCEBLEND)
  430. dBlendMask[0] = 0.0f;
  431. dBlendMask[1] = 0.0f;
  432. #endif
  433. }
  434. #endif
  435. //=======================================================================
  436. //-------------------- PUDDLES & RAINDROPS NORMALS --------------------
  437. //=======================================================================
  438. #if !defined(DIFFUSE) && defined(_PUDDLES)
  439. float2 puddlesHeight = float2(0.0f , 1.0f);
  440. float horizontalWeight = 0.0f;
  441. float3 ripNormal = float3(0, 0, 1);
  442. float3 puddleWeight = pow(abs(wNormal), 10.0f);
  443. puddleWeight = puddleWeight / (puddleWeight.x + puddleWeight.y + puddleWeight.z);
  444. puddlesHeight = (1 / (1 * pow(2, float2(max(0.0f, _InTerra_GlobalPuddles.x), heightSum) * (-(100.0f)))) + 1) * 0.5;
  445. puddlesHeight /= (puddlesHeight.r + puddlesHeight.g);
  446. horizontalWeight = smoothstep(0.9999f, 1.0f, saturate(puddleWeight.y - ( (puddleWeight.x + puddleWeight.z) * 100000000.0f)));
  447. puddlesHeight.r *= horizontalWeight;
  448. _InTerra_GlobalRaindropRipples.z = 1.0f / _InTerra_GlobalRaindropRipples.z * 0.1f;
  449. if (_InTerra_GlobalRaindropRipples.x > 0.0f)
  450. {
  451. if (puddlesHeight.x > 0.8f && _InTerra_GlobalRaindropsDistance.y > 0.0f)
  452. {
  453. float raindropsDistance = smoothstep(_InTerra_GlobalRaindropsDistance.x, _InTerra_GlobalRaindropsDistance.y, distance(IN.worldPos, _WorldSpaceCameraPos));
  454. float rainIndex = 1 / (min(5.0f, _InTerra_GlobalRaindropRipples.x) * 4.0f);
  455. for (float i = 5.0; i > 4.0; i -= rainIndex)
  456. {
  457. ripNormal = BlendNormal(ripNormal, RainRipples(IN.worldPos.zx * _InTerra_GlobalRaindropRipples.z * i + i * 0.25f , i, i * 0.25f));
  458. }
  459. ripNormal = lerp(ripNormal, float3(0, 0, 1), raindropsDistance);
  460. }
  461. float2 uvRefractOffset = ripNormal.xy * 0.01;
  462. for (int i = 0; i < _LAYER_COUNT; ++i)
  463. {
  464. uvSplat[i] += uvRefractOffset;
  465. }
  466. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  467. ripNormal = WorldTangent(worldTangent, worldBitangent, ripNormal);
  468. #endif
  469. }
  470. #endif
  471. //=======================================================================
  472. //-------------------- ALBEDO, SMOOTHNESS & NORMAL ---------------------
  473. //=======================================================================
  474. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  475. float4 frontDiffuse;
  476. float3 frontNormal;
  477. float4 sideDiffuse;
  478. float3 sideNormal;
  479. #ifdef _TERRAIN_TRIPLANAR_ONE
  480. SampleSplat(uvSplat, blendMask, tolWeightY, mask, mixedDiffuse, mixedNormal, splat);
  481. SampleSplatTOL(frontDiffuse, frontNormal, uvSplat_front, blendMask, triplanarWeights.z, mask);
  482. SampleSplatTOL(sideDiffuse, sideNormal, uvSplat_side, blendMask, triplanarWeights.x, mask);
  483. #else
  484. SampleSplat(uvSplat, blendMask, triplanarWeights.y, mask, mixedDiffuse, mixedNormal, splat);
  485. SampleSplat(uvSplat_front, blendMask, triplanarWeights.z, mask, frontDiffuse, frontNormal, splat);
  486. SampleSplat(uvSplat_side, blendMask, triplanarWeights.x, mask, sideDiffuse, sideNormal, splat);
  487. #endif
  488. #else
  489. SampleSplat(uvSplat, blendMask, 1.0f, mask, mixedDiffuse, mixedNormal, splat);
  490. #endif
  491. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  492. mixedNormal = WorldTangent(worldTangent, worldBitangent, mixedNormal);
  493. #endif
  494. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  495. mixedDiffuse = mixedDiffuse + frontDiffuse + sideDiffuse;
  496. mixedNormal = TriplanarNormal(mixedNormal, worldTangent, worldBitangent, frontNormal, sideNormal, triplanarWeights, flipUV);
  497. #endif
  498. //---------------------- HIDE TILING --------------------
  499. #ifdef _TERRAIN_DISTANCEBLEND
  500. float4 distantDiffuse;
  501. float3 distantNormal;
  502. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  503. float4 dFrontDiffuse;
  504. float3 dFontNormal;
  505. float4 dSideDiffuse;
  506. float3 dSideNormal;
  507. #ifdef _TERRAIN_TRIPLANAR_ONE
  508. SampleSplat(distantUV, dBlendMask, dTolWeightY, dMask, distantDiffuse, distantNormal, dSplat);
  509. SampleSplatTOL(dFrontDiffuse, dFontNormal, distantUV_front, dBlendMask, triplanarWeights.z, dMask);
  510. SampleSplatTOL(dSideDiffuse, dSideNormal, distantUV_side, dBlendMask, triplanarWeights.x, dMask);
  511. #else
  512. SampleSplat(distantUV, dBlendMask, triplanarWeights.y, dMask, distantDiffuse, distantNormal, dSplat);
  513. SampleSplat(distantUV_front, dBlendMask, triplanarWeights.z, dMask, dFrontDiffuse, dFontNormal, dSplat);
  514. SampleSplat(distantUV_side, dBlendMask, triplanarWeights.x, dMask, dSideDiffuse, dSideNormal, dSplat);
  515. #endif
  516. #else
  517. SampleSplat(distantUV, dBlendMask, 1.0f, dMask, distantDiffuse, distantNormal, dSplat);
  518. #endif
  519. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  520. distantNormal = WorldTangent(worldTangent, worldBitangent, distantNormal);
  521. #endif
  522. #if defined(TRIPLANAR) && !defined(_TERRAIN_BASEMAP_GEN)
  523. distantDiffuse = distantDiffuse + dFrontDiffuse + dSideDiffuse;
  524. distantNormal = TriplanarNormal(distantNormal, worldTangent, worldBitangent, dFontNormal, dSideNormal, triplanarWeights, flipUV);
  525. #endif
  526. distantDiffuse = lerp(mixedDiffuse, distantDiffuse, _HT_cover);
  527. distantNormal = lerp(mixedNormal, distantNormal, _HT_cover);
  528. #ifdef _TERRAIN_BASEMAP_GEN
  529. mixedDiffuse = distantDiffuse;
  530. #else
  531. mixedDiffuse = lerp(mixedDiffuse, distantDiffuse, dist);
  532. mixedNormal = lerp(mixedNormal, distantNormal, dist);
  533. #endif
  534. #endif
  535. //===================================================================
  536. //------------------ TINT & ADDITIONAL NORMALS ----------------------
  537. //===================================================================
  538. #if defined(TERRAIN_BASE_PASS)
  539. half4 tint = tex2D(_TerrainColorTintTexture, tintUV);
  540. #else
  541. half4 tint = UNITY_SAMPLE_TEX2D_SAMPLER(_TerrainColorTintTexture, _Splat0, tintUV);
  542. #ifdef _NORMALMAPS
  543. float3 normalTint = UnpackNormalWithScale(UNITY_SAMPLE_TEX2D_SAMPLER(_TerrainNormalTintTexture, _Splat0, normalTintUV), 1.0f);
  544. #ifndef INTERRA_TERRAIN
  545. normalTint = WorldTangent(worldTangent, worldBitangent, normalTint);
  546. #endif
  547. float normalDist = smoothstep(_TerrainNormalTintDistance.x, _TerrainNormalTintDistance.y, (distance(IN.worldPos, _WorldSpaceCameraPos)));
  548. #if !defined(TERRAIN_BASE_PASS)
  549. mixedNormal = lerp(mixedNormal, BlendNormals(mixedNormal, normalTint), _TerrainNormalTintStrenght * normalDist).rgb;
  550. #endif
  551. #endif
  552. #endif
  553. //===================================================================
  554. //---------------------------- TRACKS ---------------------------
  555. //===================================================================
  556. #if defined(_TRACKS) && !defined(_TERRAIN_BASEMAP_GEN)
  557. if(_Tracks)
  558. {
  559. float halfTrackArea = _InTerra_TrackArea * 0.5f;
  560. float _InTerra_TrackFading = 30;
  561. float2 trackUV = float2((IN.worldPos.x - _InTerra_TrackPosition.x) + (halfTrackArea), -(IN.worldPos.z - _InTerra_TrackPosition.z - (halfTrackArea))) * 1.0f / _InTerra_TrackArea;
  562. float trackDist = smoothstep(_InTerra_TrackArea - 1.0f, _InTerra_TrackArea - _InTerra_TrackFading, (distance(IN.worldPos, float3(_WorldSpaceCameraPos.x, _WorldSpaceCameraPos.y, _WorldSpaceCameraPos.z))));
  563. float2 minDist = step(float2(0.0f, 0.0f), trackUV);
  564. float2 maxDist = step(float2(0.0f, 0.0f), 1.0 - trackUV);
  565. trackDist *= (minDist.x * minDist.y * maxDist.x * maxDist.y);
  566. if(trackDist > 0.0001f)
  567. {
  568. UnpackTrackSplatValues(trackSplats);
  569. UnpackTrackSplatColor(trackSplatsColor);
  570. float4 trackColor = TrackSplatValues(blendMask, trackSplatsColor) ;
  571. float4 trackValues = TrackSplatValues(blendMask, trackSplats);
  572. #ifndef INTERRA_TERRAIN
  573. float2 trackDetailUV = float2(terrainUV.x, -terrainUV.y) * _TrackDetailTexture_ST.xy * _TerrainSize.xz + _TrackDetailTexture_ST.zw;
  574. #else
  575. float2 trackDetailUV = float2(IN.tc.x, -IN.tc.y) * _TrackDetailTexture_ST.xy * _TerrainSizeXZPosY.xy + _TrackDetailTexture_ST.zw;
  576. #endif
  577. #if defined(PARALLAX) && defined(_TERRAIN_PARALLAX)
  578. if (_Terrain_Parallax)
  579. {
  580. float3 tangentViewDirTerrain = normalize(IN.tangentViewDir);
  581. float2 trackParallaxOffset = ParallaxOffset(_InTerra_TrackTexture, _ParallaxTrackSteps, -trackValues.y, trackUV, float3(-tangentViewDirTerrain.x, tangentViewDirTerrain.y, -tangentViewDirTerrain.z), _ParallaxTrackAffineSteps, 0, 1);
  582. trackUV += trackParallaxOffset;
  583. trackDetailUV += (trackParallaxOffset) * (_TrackDetailTexture_ST.xy * _InTerra_TrackArea);
  584. }
  585. #endif
  586. trackDepth = UNITY_SAMPLE_TEX2D_SAMPLER(_InTerra_TrackTexture, _Control, trackUV);
  587. float4 trackDetail = UNITY_SAMPLE_TEX2D_SAMPLER(_TrackDetailTexture, _Splat0, trackDetailUV);
  588. float trackHeightMap = saturate(trackDepth.b + _TrackHeightOffset);
  589. float2 trackIntersect = float2(trackDepth.b, 1- trackDepth.b);
  590. trackIntersect *= (1 / (pow(2, float2(trackHeightMap, heightSum) * (-(_TrackHeightTransition)))) + 1) * 0.5;
  591. trackIntersect /= (trackIntersect.r + trackIntersect.g);
  592. float trackEdge = saturate(pow(trackDepth.a, _TrackEdgeSharpness));
  593. float track = trackIntersect.r * trackDist;
  594. float colorOpacity = saturate(track * trackColor.a);
  595. trackColor = lerp(trackColor, (trackColor * trackDetail), trackValues.a);
  596. mixedDiffuse.rgb = lerp(mixedDiffuse, trackColor, colorOpacity);
  597. mixedDiffuse.a = lerp(mixedDiffuse.a, trackValues.x, track);
  598. #if defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)
  599. if (trackDepth.a > 0.0001f)
  600. {
  601. float3 trackDetailNormal = UnpackNormalWithScale(UNITY_SAMPLE_TEX2D_SAMPLER(_TrackDetailNormalTexture, _Splat0, trackDetailUV), _TrackDetailNormalStrenght);
  602. trackDetailNormal.y *= -1;
  603. float normalsOffset = _InTerra_TrackTexture_TexelSize * 2.0f;
  604. float texelArea = _InTerra_TrackTexture_TexelSize * 100 * _InTerra_TrackArea;
  605. float normalStrenghts = _TrackNormalStrenght / texelArea;
  606. float normalEdgeStrenghts = _TrackEdgeNormals / texelArea;
  607. float4 heights[4];
  608. heights[0] = UNITY_SAMPLE_TEX2D_SAMPLER(_InTerra_TrackTexture, _Control, trackUV + float2(0.0, normalsOffset));
  609. heights[1] = UNITY_SAMPLE_TEX2D_SAMPLER(_InTerra_TrackTexture, _Control, trackUV + float2( normalsOffset, 0.0));
  610. heights[2] = UNITY_SAMPLE_TEX2D_SAMPLER(_InTerra_TrackTexture, _Control, trackUV + float2(-normalsOffset, 0.0));
  611. heights[3] = UNITY_SAMPLE_TEX2D_SAMPLER(_InTerra_TrackTexture, _Control, trackUV + float2(0.0, -normalsOffset));
  612. for (int i = 0; i < 4; ++i)
  613. {
  614. heights[i] *= float4(1.0f ,1.0f, normalStrenghts, normalEdgeStrenghts);
  615. }
  616. float3 edgeNormals = float3(float2(heights[1].a - heights[2].a, heights[3].a - heights[0].a), 1.0f);
  617. float3 trackNormal = float3(float2(heights[1].b - heights[2].b, heights[3].b - heights[0].b), 1.0f) ;
  618. trackNormal = (lerp((trackNormal), BlendNormals(trackDetailNormal, trackNormal), trackValues.a)) * trackIntersect.r;
  619. trackNormal = normalize(lerp(edgeNormals, trackNormal, trackDepth.b));
  620. trackNormal.z += 1e-5f;
  621. trackNormal = (lerp(edgeNormals, trackNormal, trackDepth.b));
  622. float normalOpacity = saturate(trackValues.z * (trackEdge + track)) * trackDist;
  623. #ifndef INTERRA_TERRAIN
  624. trackNormal = WorldTangent(worldTangent, worldBitangent, trackNormal);
  625. #endif
  626. mixedNormal = lerp(mixedNormal, trackNormal, normalOpacity);
  627. }
  628. #endif
  629. #ifndef DIFFUSE
  630. ao = lerp(ao, _TrackAO, track);
  631. #endif
  632. }
  633. }
  634. #endif
  635. #ifndef DIFFUSE
  636. mixedDiffuse.a = lerp(mixedDiffuse.a, 1.0f, _InTerra_GlobalWetness);
  637. mixedNormal.xy = _InTerra_GlobalWetness > 0.4 ? mixedNormal.xy * (1 - min(0.8f, (_InTerra_GlobalWetness * 2.25f - 1))) : mixedNormal.xy;
  638. #endif
  639. #ifdef INTERRA_MESH_TERRAIN
  640. if (_CheckHeight)
  641. {
  642. mixedDiffuse.rgb *= float3(1.0f, 0.80f, 0.60f);
  643. }
  644. #endif
  645. //=======================================================================
  646. //=====================| OBJECT INTEGRATION |========================
  647. //=======================================================================
  648. #ifdef INTERRA_OBJECT
  649. #ifndef DIFFUSE
  650. objectAlbedo.a = _HasMask == 1 ? objectMask.a : _Glossiness;
  651. objectAlbedo.a = _GlobalWetnessDisabled ? objectAlbedo.a : lerp(objectAlbedo.a, 1.0f, _InTerra_GlobalWetness);
  652. half objectMetallic = _HasMask == 1 ? objectMask.r : _Metallic;
  653. half objectAo = _HasMask == 1 ? objectMask.g : _Ao;
  654. #endif
  655. float3 mainNormD = float3(0, 0, 1);
  656. if (_Detail > 0.0f)
  657. {
  658. float2 detailUV = TRANSFORM_TEX(IN.mainTC_tWeightY_hOffset.xy, _DetailAlbedoMap);
  659. #if defined(PARALLAX) && defined(_OBJECT_PARALLAX)
  660. if (_Object_Parallax)
  661. {
  662. detailUV += mainParallaxOffset * (_DetailAlbedoMap_ST.xy / _MainTex_ST.xy);
  663. }
  664. #endif
  665. #if !defined(_LAYERS_ONE)
  666. half3 detailTexture = UNITY_SAMPLE_TEX2D_SAMPLER(_DetailAlbedoMap, _MainTex, detailUV);
  667. mainNormD = UnpackNormalWithScale(UNITY_SAMPLE_TEX2D_SAMPLER(_DetailNormalMap, _MainTex, detailUV), _DetailNormalMapScale);
  668. #else
  669. half4 detailTexture = tex2D(_DetailAlbedoMap, detailUV);
  670. mainNormD = UnpackNormalWithScale(tex2D(_DetailNormalMap, detailUV), _DetailNormalMapScale);
  671. #endif
  672. objectAlbedo.rgb = lerp(objectAlbedo.rgb, objectAlbedo.rgb * (detailTexture * unity_ColorSpaceDouble.r), _DetailStrenght);
  673. }
  674. fixed3 mainNorm = fixed3(0,0,1);
  675. #if !defined(_LAYERS_ONE)
  676. mainNorm = UnpackNormalWithScale(UNITY_SAMPLE_TEX2D_SAMPLER(_BumpMap, _MainTex, mainUV), _BumpScale);
  677. #else
  678. mainNorm = UnpackNormalWithScale(tex2D(_BumpMap, mainUV), _BumpScale);
  679. #endif
  680. if (_Detail > 0.0f)
  681. {
  682. mainNorm = (lerp(mainNorm, BlendNormals(mainNorm, mainNormD), _DetailStrenght));
  683. }
  684. if (!_GlobalWetnessDisabled)
  685. {
  686. mainNorm.xy = _InTerra_GlobalWetness > 0.4 ? mainNorm.xy * (1 - min(0.8f, (_InTerra_GlobalWetness * 2.25f - 1))) : mainNorm.xy;
  687. }
  688. mixedDiffuse = lerp(mixedDiffuse, objectAlbedo, heightIntersect.r);
  689. float3 terrainNormal = float3(mixedNormal.z * IN.terrainNormals) + 1e-5f;
  690. terrainNormal.xy += mixedNormal.xy;
  691. float intersectNormal = smoothstep(_NormIntersect.y, _NormIntersect.x, IN.mainTC_tWeightY_hOffset.w);
  692. mixedNormal = lerp(mixedNormal, terrainNormal, intersectNormal);
  693. mixedNormal = lerp(mixedNormal, mainNorm, heightIntersect.r);
  694. #ifndef DIFFUSE
  695. metallic = lerp(metallic, objectMetallic, heightIntersect.r);
  696. ao = lerp(ao, objectAo, heightIntersect.r);
  697. emission = 0;
  698. UNITY_BRANCH if (_EmissionEnabled > 0)
  699. {
  700. float2 emissionUV = TRANSFORM_TEX(IN.mainTC_tWeightY_hOffset.xy, _EmissionMap);
  701. #if defined(PARALLAX) && defined(_OBJECT_PARALLAX)
  702. if (_Object_Parallax)
  703. {
  704. emissionUV += mainParallaxOffset * (_EmissionMap_ST.xy / _MainTex_ST.xy);
  705. }
  706. #endif
  707. #if !defined(_LAYERS_ONE)
  708. half4 emissionTexture = UNITY_SAMPLE_TEX2D_SAMPLER(_EmissionMap, _MainTex, emissionUV);
  709. #else
  710. half4 emissionTexture = tex2D(_EmissionMap, emissionUV);
  711. #endif
  712. emission = lerp(half4(0, 0, 0, 0), emissionTexture * _EmissionColor, heightIntersect.r);
  713. }
  714. #endif
  715. #endif
  716. //===============| PUDDLES & RAINDROPS FINAL MIX |====================
  717. #if !defined(DIFFUSE) && defined(_PUDDLES)
  718. if (_InTerra_GlobalPuddles.y > 0.0f)
  719. {
  720. float wettnessAround = smoothstep(_InTerra_GlobalPuddles.y, _InTerra_GlobalPuddles.x, heightSum) * horizontalWeight;
  721. mixedNormal.xy = wettnessAround > 0.4 ? mixedNormal.xy * (1 - min(0.7f, (wettnessAround * 2.25f - 1))) : mixedNormal.xy;
  722. mixedDiffuse.a = lerp(mixedDiffuse.a, 1.0f, wettnessAround);
  723. mixedDiffuse.a = lerp(mixedDiffuse.a, 1.0f, puddlesHeight.r * horizontalWeight);
  724. mixedNormal = lerp(mixedNormal, ripNormal, puddlesHeight.r * horizontalWeight);
  725. ao = lerp(ao, lerp(ao, 0.9f, 0.75f), puddlesHeight.r * horizontalWeight);
  726. }
  727. #endif
  728. //=======================================================================
  729. #if defined(DIFFUSE) && defined(_NORMALMAPS)
  730. mixedNormal = mixedNormal + (splat[0].r * 0.000001f); //this is just a workaround for some Unity bug to prevent missing _Splat0 sampler
  731. #endif
  732. #if defined(INSTANCING_ON) && defined(SHADER_TARGET_SURFACE_ANALYSIS) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL)
  733. mixedNormal = float3(0, 0, 1); // make sure that surface shader compiler realizes we write to normal, as UNITY_INSTANCING_ENABLED is not defined for SHADER_TARGET_SURFACE_ANALYSIS.
  734. #endif
  735. #if defined(UNITY_INSTANCING_ENABLED) && !defined(SHADER_API_D3D11_9X) && defined(TERRAIN_INSTANCED_PERPIXEL_NORMAL)
  736. float3 geomNormal = normalize(tex2D(_TerrainNormalmapTexture, IN.tc.zw).xyz * 2 - 1);
  737. #if defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)
  738. float3 geomTangent = normalize(cross(geomNormal, float3(0, 0, 1)));
  739. float3 geomBitangent = normalize(cross(geomTangent, geomNormal));
  740. mixedNormal = mixedNormal.x * geomTangent
  741. + mixedNormal.y * geomBitangent
  742. + mixedNormal.z * geomNormal;
  743. #else
  744. mixedNormal = geomNormal;
  745. #endif
  746. mixedNormal = mixedNormal.xzy;
  747. #endif
  748. mixedNormal.z += +1e-5f;
  749. }
  750. #ifndef TERRAIN_SURFACE_OUTPUT
  751. #define TERRAIN_SURFACE_OUTPUT SurfaceOutput
  752. #endif
  753. void SplatmapFinalColor(Input IN, TERRAIN_SURFACE_OUTPUT o, inout fixed4 color)
  754. {
  755. color *= o.Alpha;
  756. #ifdef TERRAIN_SPLAT_ADDPASS
  757. UNITY_APPLY_FOG_COLOR(IN.fogCoord, color, fixed4(0,0,0,0));
  758. #else
  759. UNITY_APPLY_FOG(IN.fogCoord, color);
  760. #endif
  761. }
  762. void SplatmapFinalPrepass(Input IN, TERRAIN_SURFACE_OUTPUT o, inout fixed4 normalSpec)
  763. {
  764. normalSpec *= o.Alpha;
  765. }
  766. void SplatmapFinalGBuffer(Input IN, TERRAIN_SURFACE_OUTPUT o, inout half4 outGBuffer0, inout half4 outGBuffer1, inout half4 outGBuffer2, inout half4 emission)
  767. {
  768. UnityStandardDataApplyWeightToGbuffer(outGBuffer0, outGBuffer1, outGBuffer2, o.Alpha);
  769. emission *= o.Alpha;
  770. }