Core.cginc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #ifndef TERRAIN_TO_MESH_CORE_CGINC
  2. #define TERRAIN_TO_MESH_CORE_CGINC
  3. #include "Variables.cginc"
  4. float4 TerrainToMeshRemap(float4 value, float4 outMin, float4 outMax)
  5. {
  6. return outMin + value * (outMax - outMin);
  7. }
  8. //Unity_NormalStrength_float
  9. float3 TerrainToMeshNormalStrength(float3 In, float Strength)
  10. {
  11. return float3(In.rg * Strength, lerp(1, In.b, saturate(Strength)));
  12. }
  13. #if defined(_T2M_TEXTURE_SAMPLE_TYPE_ARRAY)
  14. #define T2M_UNPACK_SPLATMAP(uv,index) UNITY_SAMPLE_TEX2DARRAY(_T2M_SplatMaps2DArray, float3(uv, index));
  15. #define T2M_UNPACK_PAINTMAP(uv,index,sum,splat) float4 paintColor##index = (_T2M_Layer_##index##_MapsUsage.x > 0.5 ? UNITY_SAMPLE_TEX2DARRAY(_T2M_DiffuseMaps2DArray, float3(uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw, paintMapUsageIndex)) : float4(1, 1, 1, 1)); paintMapUsageIndex += _T2M_Layer_##index##_MapsUsage.x > 0.5 ? 1 : 0; sum += paintColor##index * _T2M_Layer_##index##_ColorTint * splat;
  16. #define T2M_UNPACK_NORMAL_MAP(index,uv,sum,splat) sum += TerrainToMeshNormalStrength(UnpackNormal(UNITY_SAMPLE_TEX2DARRAY(_T2M_NormalMaps2DArray, float3(uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw, normalMapUsageIndex))), _T2M_Layer_##index##_NormalScale) * splat; normalMapUsageIndex += 1;
  17. #define T2M_UNPACK_MASK(index,uv,sum,splat) sum += TerrainToMeshRemap(UNITY_SAMPLE_TEX2DARRAY(_T2M_MaskMaps2DArray, float3(uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw, maskMapUsageIndex)), _T2M_Layer_##index##_MaskMapRemapMin, _T2M_Layer_##index##_MaskMapRemapMax) * splat; maskMapUsageIndex += 1;
  18. #else
  19. #define T2M_UNPACK_SPLATMAP(uv,index) UNITY_SAMPLE_TEX2D_SAMPLER(_T2M_SplatMap_##index, _T2M_SplatMap_0, uv);
  20. #define T2M_UNPACK_PAINTMAP(uv,index,sum,splat) float4 paintColor##index = UNITY_SAMPLE_TEX2D_SAMPLER(_T2M_Layer_##index##_Diffuse, _T2M_Layer_0_Diffuse, uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw); sum += paintColor##index * _T2M_Layer_##index##_ColorTint * splat;
  21. #define T2M_UNPACK_NORMAL_MAP(index,uv,sum,splat) sum += TerrainToMeshNormalStrength(UnpackNormal(UNITY_SAMPLE_TEX2D_SAMPLER(_T2M_Layer_##index##_NormalMap, _T2M_Layer_0_Diffuse, uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw)), _T2M_Layer_##index##_NormalScale) * splat;
  22. #define T2M_UNPACK_MASK(index,uv,sum,splat) sum += TerrainToMeshRemap(UNITY_SAMPLE_TEX2D_SAMPLER(_T2M_Layer_##index##_Mask, _T2M_Layer_0_Diffuse, uv * _T2M_Layer_##index##_uvScaleOffset.xy + _T2M_Layer_##index##_uvScaleOffset.zw), _T2M_Layer_##index##_MaskMapRemapMin, _T2M_Layer_##index##_MaskMapRemapMax) * splat;
  23. #endif
  24. #define T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(index,sum,splat) sum += float4(_T2M_Layer_##index##_MetallicOcclusionSmoothness.rgb, lerp(_T2M_Layer_##index##_MetallicOcclusionSmoothness.a, paintColor##index.a, _T2M_Layer_##index##_SmoothnessFromDiffuseAlpha)) * splat;
  25. float TerrainToMeshCalculateClipValue(float2 uv)
  26. {
  27. #if defined(_ALPHATEST_ON)
  28. float4 holesmap = UNITY_SAMPLE_TEX2D(_T2M_HolesMap, uv);
  29. return holesmap.r;
  30. #else
  31. return 1;
  32. #endif
  33. }
  34. void TerrainToMeshCalculateLayersBlend(float2 uv, out float3 albedoValue, out float alphaValue, out float3 normalValue, out float metallicValue, out float smoothnessValue, out float occlusionValue)
  35. {
  36. //Splatmaps//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. float4 splatmap0 = T2M_UNPACK_SPLATMAP(uv, 0);
  38. #if defined(NEED_SPLAT_MAP_1)
  39. float4 splatmap1 = T2M_UNPACK_SPLATMAP(uv, 1);
  40. #endif
  41. #if defined(NEED_SPLAT_MAP_2)
  42. float4 splatmap2 = T2M_UNPACK_SPLATMAP(uv, 2);
  43. #endif
  44. #if defined(NEED_SPLAT_MAP_3)
  45. float4 splatmap3 = T2M_UNPACK_SPLATMAP(uv, 3);
  46. #endif
  47. //Paint Textures////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. float4 paintColorSum = 0;
  49. #if defined(_T2M_TEXTURE_SAMPLE_TYPE_ARRAY)
  50. int paintMapUsageIndex = 0;
  51. #endif
  52. T2M_UNPACK_PAINTMAP(uv, 0, paintColorSum, splatmap0.r);
  53. T2M_UNPACK_PAINTMAP(uv, 1, paintColorSum, splatmap0.g);
  54. #if defined(NEED_PAINT_MAP_2)
  55. T2M_UNPACK_PAINTMAP(uv, 2, paintColorSum, splatmap0.b);
  56. #endif
  57. #if defined(NEED_PAINT_MAP_3)
  58. T2M_UNPACK_PAINTMAP(uv, 3, paintColorSum, splatmap0.a);
  59. #endif
  60. #if defined(NEED_SPLAT_MAP_1)
  61. #if defined(NEED_PAINT_MAP_4)
  62. T2M_UNPACK_PAINTMAP(uv, 4, paintColorSum, splatmap1.r);
  63. #endif
  64. #if defined(NEED_PAINT_MAP_5)
  65. T2M_UNPACK_PAINTMAP(uv, 5, paintColorSum, splatmap1.g);
  66. #endif
  67. #if defined(NEED_PAINT_MAP_6)
  68. T2M_UNPACK_PAINTMAP(uv, 6, paintColorSum, splatmap1.b);
  69. #endif
  70. #if defined(NEED_PAINT_MAP_7)
  71. T2M_UNPACK_PAINTMAP(uv, 7, paintColorSum, splatmap1.a);
  72. #endif
  73. #endif
  74. #if defined(NEED_SPLAT_MAP_2)
  75. #if defined(NEED_PAINT_MAP_8)
  76. T2M_UNPACK_PAINTMAP(uv, 8, paintColorSum, splatmap2.r);
  77. #endif
  78. #if defined(NEED_PAINT_MAP_9)
  79. T2M_UNPACK_PAINTMAP(uv, 9, paintColorSum, splatmap2.g);
  80. #endif
  81. #if defined(NEED_PAINT_MAP_10)
  82. T2M_UNPACK_PAINTMAP(uv, 10, paintColorSum, splatmap2.b);
  83. #endif
  84. #if defined(NEED_PAINT_MAP_11)
  85. T2M_UNPACK_PAINTMAP(uv, 11, paintColorSum, splatmap2.a);
  86. #endif
  87. #endif
  88. #if defined(NEED_SPLAT_MAP_3)
  89. #if defined(NEED_PAINT_MAP_12)
  90. T2M_UNPACK_PAINTMAP(uv, 12, paintColorSum, splatmap3.r);
  91. #endif
  92. #if defined(NEED_PAINT_MAP_13)
  93. T2M_UNPACK_PAINTMAP(uv, 13, paintColorSum, splatmap3.g);
  94. #endif
  95. #if defined(NEED_PAINT_MAP_14)
  96. T2M_UNPACK_PAINTMAP(uv, 14, paintColorSum, splatmap3.b);
  97. #endif
  98. #if defined(NEED_PAINT_MAP_15)
  99. T2M_UNPACK_PAINTMAP(uv, 15, paintColorSum, splatmap3.a);
  100. #endif
  101. #endif
  102. albedoValue = paintColorSum.rgb;
  103. alphaValue = paintColorSum.a;
  104. //Normal//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. #ifdef TERRAIN_TO_MESH_NEED_NORMAL
  106. float3 emptyNormal = float3(0, 0, 1);
  107. normalValue = 0;
  108. #if defined(_T2M_TEXTURE_SAMPLE_TYPE_ARRAY)
  109. int normalMapUsageIndex = 0;
  110. #endif
  111. #if defined(_T2M_LAYER_0_NORMAL)
  112. T2M_UNPACK_NORMAL_MAP(0, uv, normalValue, splatmap0.r);
  113. #else
  114. normalValue += splatmap0.r * emptyNormal;
  115. #endif
  116. #if defined(_T2M_LAYER_1_NORMAL)
  117. T2M_UNPACK_NORMAL_MAP(1, uv, normalValue, splatmap0.g);
  118. #else
  119. normalValue += splatmap0.g * emptyNormal;
  120. #endif
  121. #ifdef NEED_PAINT_MAP_2
  122. #if defined(_T2M_LAYER_2_NORMAL)
  123. T2M_UNPACK_NORMAL_MAP(2, uv, normalValue, splatmap0.b);
  124. #else
  125. normalValue += splatmap0.b * emptyNormal;
  126. #endif
  127. #endif
  128. #ifdef NEED_PAINT_MAP_3
  129. #if defined(_T2M_LAYER_3_NORMAL)
  130. T2M_UNPACK_NORMAL_MAP(3, uv, normalValue, splatmap0.a);
  131. #else
  132. normalValue += splatmap0.a * emptyNormal;
  133. #endif
  134. #endif
  135. #if defined(NEED_SPLAT_MAP_1)
  136. #ifdef NEED_PAINT_MAP_4
  137. #if defined(_T2M_LAYER_4_NORMAL)
  138. T2M_UNPACK_NORMAL_MAP(4, uv, normalValue, splatmap1.r);
  139. #else
  140. normalValue += splatmap1.r * emptyNormal;
  141. #endif
  142. #endif
  143. #ifdef NEED_PAINT_MAP_5
  144. #if defined(_T2M_LAYER_5_NORMAL)
  145. T2M_UNPACK_NORMAL_MAP(5, uv, normalValue, splatmap1.g);
  146. #else
  147. normalValue += splatmap1.g * emptyNormal;
  148. #endif
  149. #endif
  150. #ifdef NEED_PAINT_MAP_6
  151. #if defined(_T2M_LAYER_6_NORMAL)
  152. T2M_UNPACK_NORMAL_MAP(6, uv, normalValue, splatmap1.b);
  153. #else
  154. normalValue += splatmap1.b * emptyNormal;
  155. #endif
  156. #endif
  157. #ifdef NEED_PAINT_MAP_7
  158. #if defined(_T2M_LAYER_7_NORMAL)
  159. T2M_UNPACK_NORMAL_MAP(7, uv, normalValue, splatmap1.a);
  160. #else
  161. normalValue += splatmap1.a * emptyNormal;
  162. #endif
  163. #endif
  164. #endif
  165. #if defined(NEED_SPLAT_MAP_2)
  166. #ifdef NEED_PAINT_MAP_8
  167. #if defined(_T2M_LAYER_8_NORMAL)
  168. T2M_UNPACK_NORMAL_MAP(8, uv, normalValue, splatmap2.r);
  169. #else
  170. normalValue += splatmap2.r * emptyNormal;
  171. #endif
  172. #endif
  173. #ifdef NEED_PAINT_MAP_9
  174. #if defined(_T2M_LAYER_9_NORMAL)
  175. T2M_UNPACK_NORMAL_MAP(9, uv, normalValue, splatmap2.g);
  176. #else
  177. normalValue += splatmap2.g * emptyNormal;
  178. #endif
  179. #endif
  180. #ifdef NEED_PAINT_MAP_10
  181. #if defined(_T2M_LAYER_10_NORMAL)
  182. T2M_UNPACK_NORMAL_MAP(10, uv, normalValue, splatmap2.b);
  183. #else
  184. normalValue += splatmap2.b * emptyNormal;
  185. #endif
  186. #endif
  187. #ifdef NEED_PAINT_MAP_11
  188. #if defined(_T2M_LAYER_11_NORMAL)
  189. T2M_UNPACK_NORMAL_MAP(11, uv, normalValue, splatmap2.a);
  190. #else
  191. normalValue += splatmap2.a * emptyNormal;
  192. #endif
  193. #endif
  194. #endif
  195. #if defined(NEED_SPLAT_MAP_3)
  196. #ifdef NEED_PAINT_MAP_12
  197. #if defined(_T2M_LAYER_12_NORMAL)
  198. T2M_UNPACK_NORMAL_MAP(12, uv, normalValue, splatmap3.r);
  199. #else
  200. normalValue += splatmap3.r * emptyNormal;
  201. #endif
  202. #endif
  203. #ifdef NEED_PAINT_MAP_13
  204. #if defined(_T2M_LAYER_13_NORMAL)
  205. T2M_UNPACK_NORMAL_MAP(13, uv, normalValue, splatmap3.g);
  206. #else
  207. normalValue += splatmap3.g * emptyNormal;
  208. #endif
  209. #endif
  210. #ifdef NEED_PAINT_MAP_14
  211. #if defined(_T2M_LAYER_14_NORMAL)
  212. T2M_UNPACK_NORMAL_MAP(14, uv, normalValue, splatmap3.b);
  213. #else
  214. normalValue += splatmap3.b * emptyNormal;
  215. #endif
  216. #endif
  217. #ifdef NEED_PAINT_MAP_15
  218. #if defined(_T2M_LAYER_15_NORMAL)
  219. T2M_UNPACK_NORMAL_MAP(15, uv, normalValue, splatmap3.a);
  220. #else
  221. normalValue += splatmap3.a * emptyNormal;
  222. #endif
  223. #endif
  224. #endif
  225. #else
  226. normalValue = float3(0, 0, 1);
  227. #endif
  228. //Metallic, Occlusion, Smoothness////////////////////////////////////////////////////////////////////////////////////////////////////////
  229. #ifdef TERRAIN_TO_MESH_NEED_METALLIC_SMOOTHNESS_OCCLUSION
  230. float4 metallicSmoothnessOcclusion = 0;
  231. #if defined(_T2M_TEXTURE_SAMPLE_TYPE_ARRAY)
  232. int maskMapUsageIndex = 0;
  233. #endif
  234. #if defined(_T2M_LAYER_0_MASK)
  235. T2M_UNPACK_MASK(0, uv, metallicSmoothnessOcclusion, splatmap0.r);
  236. #else
  237. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(0, metallicSmoothnessOcclusion, splatmap0.r);
  238. #endif
  239. #if defined(_T2M_LAYER_1_MASK)
  240. T2M_UNPACK_MASK(1, uv, metallicSmoothnessOcclusion, splatmap0.g);
  241. #else
  242. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(1, metallicSmoothnessOcclusion, splatmap0.g);
  243. #endif
  244. #if defined(NEED_PAINT_MAP_2)
  245. #if defined(_T2M_LAYER_2_MASK)
  246. T2M_UNPACK_MASK(2, uv, metallicSmoothnessOcclusion, splatmap0.b);
  247. #else
  248. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(2, metallicSmoothnessOcclusion, splatmap0.b);
  249. #endif
  250. #endif
  251. #if defined(NEED_PAINT_MAP_3)
  252. #if defined(_T2M_LAYER_3_MASK)
  253. T2M_UNPACK_MASK(3, uv, metallicSmoothnessOcclusion, splatmap0.a);
  254. #else
  255. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(3, metallicSmoothnessOcclusion, splatmap0.a);
  256. #endif
  257. #endif
  258. #if defined(NEED_SPLAT_MAP_1)
  259. #if defined(NEED_PAINT_MAP_4)
  260. #if defined(_T2M_LAYER_4_MASK)
  261. T2M_UNPACK_MASK(4, uv, metallicSmoothnessOcclusion, splatmap1.r);
  262. #else
  263. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(4, metallicSmoothnessOcclusion, splatmap1.r);
  264. #endif
  265. #endif
  266. #if defined(NEED_PAINT_MAP_5)
  267. #if defined(_T2M_LAYER_5_MASK)
  268. T2M_UNPACK_MASK(5, uv, metallicSmoothnessOcclusion, splatmap1.g);
  269. #else
  270. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(5, metallicSmoothnessOcclusion, splatmap1.g);
  271. #endif
  272. #endif
  273. #if defined(NEED_PAINT_MAP_6)
  274. #if defined(_T2M_LAYER_6_MASK)
  275. T2M_UNPACK_MASK(6, uv, metallicSmoothnessOcclusion, splatmap1.b);
  276. #else
  277. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(6, metallicSmoothnessOcclusion, splatmap1.b);
  278. #endif
  279. #endif
  280. #if defined(NEED_PAINT_MAP_7)
  281. #if defined(_T2M_LAYER_7_MASK)
  282. T2M_UNPACK_MASK(7, uv, metallicSmoothnessOcclusion, splatmap1.a);
  283. #else
  284. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(7, metallicSmoothnessOcclusion, splatmap1.a);
  285. #endif
  286. #endif
  287. #endif
  288. #if defined(NEED_SPLAT_MAP_2)
  289. #if defined(NEED_PAINT_MAP_8)
  290. #if defined(_T2M_LAYER_8_MASK)
  291. T2M_UNPACK_MASK(8, uv, metallicSmoothnessOcclusion, splatmap2.r);
  292. #else
  293. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(8, metallicSmoothnessOcclusion, splatmap2.r);
  294. #endif
  295. #endif
  296. #if defined(NEED_PAINT_MAP_9)
  297. #if defined(_T2M_LAYER_9_MASK)
  298. T2M_UNPACK_MASK(9, uv, metallicSmoothnessOcclusion, splatmap2.g);
  299. #else
  300. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(9, metallicSmoothnessOcclusion, splatmap2.g);
  301. #endif
  302. #endif
  303. #if defined(NEED_PAINT_MAP_10)
  304. #if defined(_T2M_LAYER_10_MASK)
  305. T2M_UNPACK_MASK(10, uv, metallicSmoothnessOcclusion, splatmap2.b);
  306. #else
  307. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(10, metallicSmoothnessOcclusion, splatmap2.b);
  308. #endif
  309. #endif
  310. #if defined(NEED_PAINT_MAP_11)
  311. #if defined(_T2M_LAYER_11_MASK)
  312. T2M_UNPACK_MASK(11, uv, metallicSmoothnessOcclusion, splatmap2.a);
  313. #else
  314. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(11, metallicSmoothnessOcclusion, splatmap2.a);
  315. #endif
  316. #endif
  317. #endif
  318. #if defined(NEED_SPLAT_MAP_3)
  319. #if defined(NEED_PAINT_MAP_12)
  320. #if defined(_T2M_LAYER_12_MASK)
  321. T2M_UNPACK_MASK(12, uv, metallicSmoothnessOcclusion, splatmap3.r);
  322. #else
  323. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(12, metallicSmoothnessOcclusion, splatmap3.r);
  324. #endif
  325. #endif
  326. #if defined(NEED_PAINT_MAP_13)
  327. #if defined(_T2M_LAYER_13_MASK)
  328. T2M_UNPACK_MASK(13, uv, metallicSmoothnessOcclusion, splatmap3.g);
  329. #else
  330. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(13, metallicSmoothnessOcclusion, splatmap3.g);
  331. #endif
  332. #endif
  333. #if defined(NEED_PAINT_MAP_14)
  334. #if defined(_T2M_LAYER_14_MASK)
  335. T2M_UNPACK_MASK(14, uv, metallicSmoothnessOcclusion, splatmap3.b);
  336. #else
  337. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(14, metallicSmoothnessOcclusion, splatmap3.b);
  338. #endif
  339. #endif
  340. #if defined(NEED_PAINT_MAP_15)
  341. #if defined(_T2M_LAYER_15_MASK)
  342. T2M_UNPACK_MASK(15, uv, metallicSmoothnessOcclusion, splatmap3.a);
  343. #else
  344. T2M_UNPACK_METALLIC_OCCLUSION_SMOOTHNESS(15, metallicSmoothnessOcclusion, splatmap3.a);
  345. #endif
  346. #endif
  347. #endif
  348. metallicSmoothnessOcclusion = saturate(metallicSmoothnessOcclusion);
  349. metallicValue = metallicSmoothnessOcclusion.r;
  350. smoothnessValue = metallicSmoothnessOcclusion.a;
  351. occlusionValue = metallicSmoothnessOcclusion.g;
  352. #else
  353. metallicValue = 0;
  354. smoothnessValue = 0;
  355. occlusionValue = 1;
  356. #endif
  357. }
  358. #endif