mapblock_mesh.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "mapblock_mesh.h"
  17. #include "client.h"
  18. #include "mapblock.h"
  19. #include "map.h"
  20. #include "profiler.h"
  21. #include "shader.h"
  22. #include "mesh.h"
  23. #include "minimap.h"
  24. #include "content_mapblock.h"
  25. #include "util/directiontables.h"
  26. #include "client/meshgen/collector.h"
  27. #include "client/renderingengine.h"
  28. #include <array>
  29. /*
  30. MeshMakeData
  31. */
  32. MeshMakeData::MeshMakeData(Client *client, bool use_shaders):
  33. m_client(client),
  34. m_use_shaders(use_shaders)
  35. {}
  36. void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos)
  37. {
  38. m_blockpos = blockpos;
  39. v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE;
  40. m_vmanip.clear();
  41. VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE,
  42. blockpos_nodes + v3s16(1,1,1) * MAP_BLOCKSIZE*2-v3s16(1,1,1));
  43. m_vmanip.addArea(voxel_area);
  44. }
  45. void MeshMakeData::fillBlockData(const v3s16 &block_offset, MapNode *data)
  46. {
  47. v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  48. VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
  49. v3s16 bp = m_blockpos + block_offset;
  50. v3s16 blockpos_nodes = bp * MAP_BLOCKSIZE;
  51. m_vmanip.copyFrom(data, data_area, v3s16(0,0,0), blockpos_nodes, data_size);
  52. }
  53. void MeshMakeData::fill(MapBlock *block)
  54. {
  55. fillBlockDataBegin(block->getPos());
  56. fillBlockData(v3s16(0,0,0), block->getData());
  57. // Get map for reading neighbor blocks
  58. Map *map = block->getParent();
  59. for (const v3s16 &dir : g_26dirs) {
  60. v3s16 bp = m_blockpos + dir;
  61. MapBlock *b = map->getBlockNoCreateNoEx(bp);
  62. if(b)
  63. fillBlockData(dir, b->getData());
  64. }
  65. }
  66. void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
  67. {
  68. if (crack_level >= 0)
  69. m_crack_pos_relative = crack_pos - m_blockpos*MAP_BLOCKSIZE;
  70. }
  71. void MeshMakeData::setSmoothLighting(bool smooth_lighting)
  72. {
  73. m_smooth_lighting = smooth_lighting;
  74. }
  75. /*
  76. Light and vertex color functions
  77. */
  78. /*
  79. Calculate non-smooth lighting at interior of node.
  80. Single light bank.
  81. */
  82. static u8 getInteriorLight(enum LightBank bank, MapNode n, s32 increment,
  83. const NodeDefManager *ndef)
  84. {
  85. u8 light = n.getLight(bank, ndef);
  86. if (light > 0)
  87. light = rangelim(light + increment, 0, LIGHT_SUN);
  88. return decode_light(light);
  89. }
  90. /*
  91. Calculate non-smooth lighting at interior of node.
  92. Both light banks.
  93. */
  94. u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef)
  95. {
  96. u16 day = getInteriorLight(LIGHTBANK_DAY, n, increment, ndef);
  97. u16 night = getInteriorLight(LIGHTBANK_NIGHT, n, increment, ndef);
  98. return day | (night << 8);
  99. }
  100. /*
  101. Calculate non-smooth lighting at face of node.
  102. Single light bank.
  103. */
  104. static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
  105. v3s16 face_dir, const NodeDefManager *ndef)
  106. {
  107. u8 light;
  108. u8 l1 = n.getLight(bank, ndef);
  109. u8 l2 = n2.getLight(bank, ndef);
  110. if(l1 > l2)
  111. light = l1;
  112. else
  113. light = l2;
  114. // Boost light level for light sources
  115. u8 light_source = MYMAX(ndef->get(n).light_source,
  116. ndef->get(n2).light_source);
  117. if(light_source > light)
  118. light = light_source;
  119. return decode_light(light);
  120. }
  121. /*
  122. Calculate non-smooth lighting at face of node.
  123. Both light banks.
  124. */
  125. u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
  126. const NodeDefManager *ndef)
  127. {
  128. u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, face_dir, ndef);
  129. u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, face_dir, ndef);
  130. return day | (night << 8);
  131. }
  132. /*
  133. Calculate smooth lighting at the XYZ- corner of p.
  134. Both light banks
  135. */
  136. static u16 getSmoothLightCombined(const v3s16 &p,
  137. const std::array<v3s16,8> &dirs, MeshMakeData *data)
  138. {
  139. const NodeDefManager *ndef = data->m_client->ndef();
  140. u16 ambient_occlusion = 0;
  141. u16 light_count = 0;
  142. u8 light_source_max = 0;
  143. u16 light_day = 0;
  144. u16 light_night = 0;
  145. bool direct_sunlight = false;
  146. auto add_node = [&] (u8 i, bool obstructed = false) -> bool {
  147. if (obstructed) {
  148. ambient_occlusion++;
  149. return false;
  150. }
  151. MapNode n = data->m_vmanip.getNodeNoExNoEmerge(p + dirs[i]);
  152. if (n.getContent() == CONTENT_IGNORE)
  153. return true;
  154. const ContentFeatures &f = ndef->get(n);
  155. if (f.light_source > light_source_max)
  156. light_source_max = f.light_source;
  157. // Check f.solidness because fast-style leaves look better this way
  158. if (f.param_type == CPT_LIGHT && f.solidness != 2) {
  159. u8 light_level_day = n.getLightNoChecks(LIGHTBANK_DAY, &f);
  160. u8 light_level_night = n.getLightNoChecks(LIGHTBANK_NIGHT, &f);
  161. if (light_level_day == LIGHT_SUN)
  162. direct_sunlight = true;
  163. light_day += decode_light(light_level_day);
  164. light_night += decode_light(light_level_night);
  165. light_count++;
  166. } else {
  167. ambient_occlusion++;
  168. }
  169. return f.light_propagates;
  170. };
  171. bool obstructed[4] = { true, true, true, true };
  172. add_node(0);
  173. bool opaque1 = !add_node(1);
  174. bool opaque2 = !add_node(2);
  175. bool opaque3 = !add_node(3);
  176. obstructed[0] = opaque1 && opaque2;
  177. obstructed[1] = opaque1 && opaque3;
  178. obstructed[2] = opaque2 && opaque3;
  179. for (u8 k = 0; k < 3; ++k)
  180. if (add_node(k + 4, obstructed[k]))
  181. obstructed[3] = false;
  182. if (add_node(7, obstructed[3])) { // wrap light around nodes
  183. ambient_occlusion -= 3;
  184. for (u8 k = 0; k < 3; ++k)
  185. add_node(k + 4, !obstructed[k]);
  186. }
  187. if (light_count == 0) {
  188. light_day = light_night = 0;
  189. } else {
  190. light_day /= light_count;
  191. light_night /= light_count;
  192. }
  193. // boost direct sunlight, if any
  194. if (direct_sunlight)
  195. light_day = 0xFF;
  196. // Boost brightness around light sources
  197. bool skip_ambient_occlusion_day = false;
  198. if (decode_light(light_source_max) >= light_day) {
  199. light_day = decode_light(light_source_max);
  200. skip_ambient_occlusion_day = true;
  201. }
  202. bool skip_ambient_occlusion_night = false;
  203. if(decode_light(light_source_max) >= light_night) {
  204. light_night = decode_light(light_source_max);
  205. skip_ambient_occlusion_night = true;
  206. }
  207. if (ambient_occlusion > 4) {
  208. static thread_local const float ao_gamma = rangelim(
  209. g_settings->getFloat("ambient_occlusion_gamma"), 0.25, 4.0);
  210. // Table of gamma space multiply factors.
  211. static thread_local const float light_amount[3] = {
  212. powf(0.75, 1.0 / ao_gamma),
  213. powf(0.5, 1.0 / ao_gamma),
  214. powf(0.25, 1.0 / ao_gamma)
  215. };
  216. //calculate table index for gamma space multiplier
  217. ambient_occlusion -= 5;
  218. if (!skip_ambient_occlusion_day)
  219. light_day = rangelim(core::round32(
  220. light_day * light_amount[ambient_occlusion]), 0, 255);
  221. if (!skip_ambient_occlusion_night)
  222. light_night = rangelim(core::round32(
  223. light_night * light_amount[ambient_occlusion]), 0, 255);
  224. }
  225. return light_day | (light_night << 8);
  226. }
  227. /*
  228. Calculate smooth lighting at the given corner of p.
  229. Both light banks.
  230. Node at p is solid, and thus the lighting is face-dependent.
  231. */
  232. u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data)
  233. {
  234. return getSmoothLightTransparent(p + face_dir, corner - 2 * face_dir, data);
  235. }
  236. /*
  237. Calculate smooth lighting at the given corner of p.
  238. Both light banks.
  239. Node at p is not solid, and the lighting is not face-dependent.
  240. */
  241. u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data)
  242. {
  243. const std::array<v3s16,8> dirs = {{
  244. // Always shine light
  245. v3s16(0,0,0),
  246. v3s16(corner.X,0,0),
  247. v3s16(0,corner.Y,0),
  248. v3s16(0,0,corner.Z),
  249. // Can be obstructed
  250. v3s16(corner.X,corner.Y,0),
  251. v3s16(corner.X,0,corner.Z),
  252. v3s16(0,corner.Y,corner.Z),
  253. v3s16(corner.X,corner.Y,corner.Z)
  254. }};
  255. return getSmoothLightCombined(p, dirs, data);
  256. }
  257. void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio){
  258. f32 rg = daynight_ratio / 1000.0f - 0.04f;
  259. f32 b = (0.98f * daynight_ratio) / 1000.0f + 0.078f;
  260. sunlight->r = rg;
  261. sunlight->g = rg;
  262. sunlight->b = b;
  263. }
  264. void final_color_blend(video::SColor *result,
  265. u16 light, u32 daynight_ratio)
  266. {
  267. video::SColorf dayLight;
  268. get_sunlight_color(&dayLight, daynight_ratio);
  269. final_color_blend(result,
  270. encode_light(light, 0), dayLight);
  271. }
  272. void final_color_blend(video::SColor *result,
  273. const video::SColor &data, const video::SColorf &dayLight)
  274. {
  275. static const video::SColorf artificialColor(1.04f, 1.04f, 1.04f);
  276. video::SColorf c(data);
  277. f32 n = 1 - c.a;
  278. f32 r = c.r * (c.a * dayLight.r + n * artificialColor.r) * 2.0f;
  279. f32 g = c.g * (c.a * dayLight.g + n * artificialColor.g) * 2.0f;
  280. f32 b = c.b * (c.a * dayLight.b + n * artificialColor.b) * 2.0f;
  281. // Emphase blue a bit in darker places
  282. // Each entry of this array represents a range of 8 blue levels
  283. static const u8 emphase_blue_when_dark[32] = {
  284. 1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
  285. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  286. };
  287. b += emphase_blue_when_dark[irr::core::clamp((s32) ((r + g + b) / 3 * 255),
  288. 0, 255) / 8] / 255.0f;
  289. result->setRed(core::clamp((s32) (r * 255.0f), 0, 255));
  290. result->setGreen(core::clamp((s32) (g * 255.0f), 0, 255));
  291. result->setBlue(core::clamp((s32) (b * 255.0f), 0, 255));
  292. }
  293. /*
  294. Mesh generation helpers
  295. */
  296. // This table is moved outside getNodeVertexDirs to avoid the compiler using
  297. // a mutex to initialize this table at runtime right in the hot path.
  298. // For details search the internet for "cxa_guard_acquire".
  299. static const v3s16 vertex_dirs_table[] = {
  300. // ( 1, 0, 0)
  301. v3s16( 1,-1, 1), v3s16( 1,-1,-1),
  302. v3s16( 1, 1,-1), v3s16( 1, 1, 1),
  303. // ( 0, 1, 0)
  304. v3s16( 1, 1,-1), v3s16(-1, 1,-1),
  305. v3s16(-1, 1, 1), v3s16( 1, 1, 1),
  306. // ( 0, 0, 1)
  307. v3s16(-1,-1, 1), v3s16( 1,-1, 1),
  308. v3s16( 1, 1, 1), v3s16(-1, 1, 1),
  309. // invalid
  310. v3s16(), v3s16(), v3s16(), v3s16(),
  311. // ( 0, 0,-1)
  312. v3s16( 1,-1,-1), v3s16(-1,-1,-1),
  313. v3s16(-1, 1,-1), v3s16( 1, 1,-1),
  314. // ( 0,-1, 0)
  315. v3s16( 1,-1, 1), v3s16(-1,-1, 1),
  316. v3s16(-1,-1,-1), v3s16( 1,-1,-1),
  317. // (-1, 0, 0)
  318. v3s16(-1,-1,-1), v3s16(-1,-1, 1),
  319. v3s16(-1, 1, 1), v3s16(-1, 1,-1)
  320. };
  321. /*
  322. vertex_dirs: v3s16[4]
  323. */
  324. static void getNodeVertexDirs(const v3s16 &dir, v3s16 *vertex_dirs)
  325. {
  326. /*
  327. If looked from outside the node towards the face, the corners are:
  328. 0: bottom-right
  329. 1: bottom-left
  330. 2: top-left
  331. 3: top-right
  332. */
  333. // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
  334. // (0,0,1), (0,0,-1)
  335. assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z == 1);
  336. // Convert direction to single integer for table lookup
  337. u8 idx = (dir.X + 2 * dir.Y + 3 * dir.Z) & 7;
  338. idx = (idx - 1) * 4;
  339. #if defined(__GNUC__) && !defined(__clang__)
  340. #pragma GCC diagnostic push
  341. #if __GNUC__ > 7
  342. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  343. #endif
  344. #endif
  345. memcpy(vertex_dirs, &vertex_dirs_table[idx], 4 * sizeof(v3s16));
  346. #if defined(__GNUC__) && !defined(__clang__)
  347. #pragma GCC diagnostic pop
  348. #endif
  349. }
  350. static void getNodeTextureCoords(v3f base, const v3f &scale, const v3s16 &dir, float *u, float *v)
  351. {
  352. if (dir.X > 0 || dir.Y > 0 || dir.Z < 0)
  353. base -= scale;
  354. if (dir == v3s16(0,0,1)) {
  355. *u = -base.X - 1;
  356. *v = -base.Y - 1;
  357. } else if (dir == v3s16(0,0,-1)) {
  358. *u = base.X + 1;
  359. *v = -base.Y - 2;
  360. } else if (dir == v3s16(1,0,0)) {
  361. *u = base.Z + 1;
  362. *v = -base.Y - 2;
  363. } else if (dir == v3s16(-1,0,0)) {
  364. *u = -base.Z - 1;
  365. *v = -base.Y - 1;
  366. } else if (dir == v3s16(0,1,0)) {
  367. *u = base.X + 1;
  368. *v = -base.Z - 2;
  369. } else if (dir == v3s16(0,-1,0)) {
  370. *u = base.X;
  371. *v = base.Z;
  372. }
  373. }
  374. struct FastFace
  375. {
  376. TileSpec tile;
  377. video::S3DVertex vertices[4]; // Precalculated vertices
  378. /*!
  379. * The face is divided into two triangles. If this is true,
  380. * vertices 0 and 2 are connected, othervise vertices 1 and 3
  381. * are connected.
  382. */
  383. bool vertex_0_2_connected;
  384. };
  385. static void makeFastFace(const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li3,
  386. const v3f &tp, const v3f &p, const v3s16 &dir, const v3f &scale, std::vector<FastFace> &dest)
  387. {
  388. // Position is at the center of the cube.
  389. v3f pos = p * BS;
  390. float x0 = 0.0f;
  391. float y0 = 0.0f;
  392. float w = 1.0f;
  393. float h = 1.0f;
  394. v3f vertex_pos[4];
  395. v3s16 vertex_dirs[4];
  396. getNodeVertexDirs(dir, vertex_dirs);
  397. if (tile.world_aligned)
  398. getNodeTextureCoords(tp, scale, dir, &x0, &y0);
  399. v3s16 t;
  400. u16 t1;
  401. switch (tile.rotation) {
  402. case 0:
  403. break;
  404. case 1: //R90
  405. t = vertex_dirs[0];
  406. vertex_dirs[0] = vertex_dirs[3];
  407. vertex_dirs[3] = vertex_dirs[2];
  408. vertex_dirs[2] = vertex_dirs[1];
  409. vertex_dirs[1] = t;
  410. t1 = li0;
  411. li0 = li3;
  412. li3 = li2;
  413. li2 = li1;
  414. li1 = t1;
  415. break;
  416. case 2: //R180
  417. t = vertex_dirs[0];
  418. vertex_dirs[0] = vertex_dirs[2];
  419. vertex_dirs[2] = t;
  420. t = vertex_dirs[1];
  421. vertex_dirs[1] = vertex_dirs[3];
  422. vertex_dirs[3] = t;
  423. t1 = li0;
  424. li0 = li2;
  425. li2 = t1;
  426. t1 = li1;
  427. li1 = li3;
  428. li3 = t1;
  429. break;
  430. case 3: //R270
  431. t = vertex_dirs[0];
  432. vertex_dirs[0] = vertex_dirs[1];
  433. vertex_dirs[1] = vertex_dirs[2];
  434. vertex_dirs[2] = vertex_dirs[3];
  435. vertex_dirs[3] = t;
  436. t1 = li0;
  437. li0 = li1;
  438. li1 = li2;
  439. li2 = li3;
  440. li3 = t1;
  441. break;
  442. case 4: //FXR90
  443. t = vertex_dirs[0];
  444. vertex_dirs[0] = vertex_dirs[3];
  445. vertex_dirs[3] = vertex_dirs[2];
  446. vertex_dirs[2] = vertex_dirs[1];
  447. vertex_dirs[1] = t;
  448. t1 = li0;
  449. li0 = li3;
  450. li3 = li2;
  451. li2 = li1;
  452. li1 = t1;
  453. y0 += h;
  454. h *= -1;
  455. break;
  456. case 5: //FXR270
  457. t = vertex_dirs[0];
  458. vertex_dirs[0] = vertex_dirs[1];
  459. vertex_dirs[1] = vertex_dirs[2];
  460. vertex_dirs[2] = vertex_dirs[3];
  461. vertex_dirs[3] = t;
  462. t1 = li0;
  463. li0 = li1;
  464. li1 = li2;
  465. li2 = li3;
  466. li3 = t1;
  467. y0 += h;
  468. h *= -1;
  469. break;
  470. case 6: //FYR90
  471. t = vertex_dirs[0];
  472. vertex_dirs[0] = vertex_dirs[3];
  473. vertex_dirs[3] = vertex_dirs[2];
  474. vertex_dirs[2] = vertex_dirs[1];
  475. vertex_dirs[1] = t;
  476. t1 = li0;
  477. li0 = li3;
  478. li3 = li2;
  479. li2 = li1;
  480. li1 = t1;
  481. x0 += w;
  482. w *= -1;
  483. break;
  484. case 7: //FYR270
  485. t = vertex_dirs[0];
  486. vertex_dirs[0] = vertex_dirs[1];
  487. vertex_dirs[1] = vertex_dirs[2];
  488. vertex_dirs[2] = vertex_dirs[3];
  489. vertex_dirs[3] = t;
  490. t1 = li0;
  491. li0 = li1;
  492. li1 = li2;
  493. li2 = li3;
  494. li3 = t1;
  495. x0 += w;
  496. w *= -1;
  497. break;
  498. case 8: //FX
  499. y0 += h;
  500. h *= -1;
  501. break;
  502. case 9: //FY
  503. x0 += w;
  504. w *= -1;
  505. break;
  506. default:
  507. break;
  508. }
  509. for (u16 i = 0; i < 4; i++) {
  510. vertex_pos[i] = v3f(
  511. BS / 2 * vertex_dirs[i].X,
  512. BS / 2 * vertex_dirs[i].Y,
  513. BS / 2 * vertex_dirs[i].Z
  514. );
  515. }
  516. for (v3f &vpos : vertex_pos) {
  517. vpos.X *= scale.X;
  518. vpos.Y *= scale.Y;
  519. vpos.Z *= scale.Z;
  520. vpos += pos;
  521. }
  522. f32 abs_scale = 1.0f;
  523. if (scale.X < 0.999f || scale.X > 1.001f) abs_scale = scale.X;
  524. else if (scale.Y < 0.999f || scale.Y > 1.001f) abs_scale = scale.Y;
  525. else if (scale.Z < 0.999f || scale.Z > 1.001f) abs_scale = scale.Z;
  526. v3f normal(dir.X, dir.Y, dir.Z);
  527. u16 li[4] = { li0, li1, li2, li3 };
  528. u16 day[4];
  529. u16 night[4];
  530. for (u8 i = 0; i < 4; i++) {
  531. day[i] = li[i] >> 8;
  532. night[i] = li[i] & 0xFF;
  533. }
  534. bool vertex_0_2_connected = abs(day[0] - day[2]) + abs(night[0] - night[2])
  535. < abs(day[1] - day[3]) + abs(night[1] - night[3]);
  536. v2f32 f[4] = {
  537. core::vector2d<f32>(x0 + w * abs_scale, y0 + h),
  538. core::vector2d<f32>(x0, y0 + h),
  539. core::vector2d<f32>(x0, y0),
  540. core::vector2d<f32>(x0 + w * abs_scale, y0) };
  541. // equivalent to dest.push_back(FastFace()) but faster
  542. dest.emplace_back();
  543. FastFace& face = *dest.rbegin();
  544. for (u8 i = 0; i < 4; i++) {
  545. video::SColor c = encode_light(li[i], tile.emissive_light);
  546. if (!tile.emissive_light)
  547. applyFacesShading(c, normal);
  548. face.vertices[i] = video::S3DVertex(vertex_pos[i], normal, c, f[i]);
  549. }
  550. /*
  551. Revert triangles for nicer looking gradient if the
  552. brightness of vertices 1 and 3 differ less than
  553. the brightness of vertices 0 and 2.
  554. */
  555. face.vertex_0_2_connected = vertex_0_2_connected;
  556. face.tile = tile;
  557. }
  558. /*
  559. Nodes make a face if contents differ and solidness differs.
  560. Return value:
  561. 0: No face
  562. 1: Face uses m1's content
  563. 2: Face uses m2's content
  564. equivalent: Whether the blocks share the same face (eg. water and glass)
  565. TODO: Add 3: Both faces drawn with backface culling, remove equivalent
  566. */
  567. static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
  568. const NodeDefManager *ndef)
  569. {
  570. *equivalent = false;
  571. if (m1 == m2 || m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
  572. return 0;
  573. const ContentFeatures &f1 = ndef->get(m1);
  574. const ContentFeatures &f2 = ndef->get(m2);
  575. // Contents don't differ for different forms of same liquid
  576. if (f1.sameLiquid(f2))
  577. return 0;
  578. u8 c1 = f1.solidness;
  579. u8 c2 = f2.solidness;
  580. if (c1 == c2)
  581. return 0;
  582. if (c1 == 0)
  583. c1 = f1.visual_solidness;
  584. else if (c2 == 0)
  585. c2 = f2.visual_solidness;
  586. if (c1 == c2) {
  587. *equivalent = true;
  588. // If same solidness, liquid takes precense
  589. if (f1.isLiquid())
  590. return 1;
  591. if (f2.isLiquid())
  592. return 2;
  593. }
  594. if (c1 > c2)
  595. return 1;
  596. return 2;
  597. }
  598. /*
  599. Gets nth node tile (0 <= n <= 5).
  600. */
  601. void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile)
  602. {
  603. const NodeDefManager *ndef = data->m_client->ndef();
  604. const ContentFeatures &f = ndef->get(mn);
  605. tile = f.tiles[tileindex];
  606. bool has_crack = p == data->m_crack_pos_relative;
  607. for (TileLayer &layer : tile.layers) {
  608. if (layer.texture_id == 0)
  609. continue;
  610. if (!layer.has_color)
  611. mn.getColor(f, &(layer.color));
  612. // Apply temporary crack
  613. if (has_crack)
  614. layer.material_flags |= MATERIAL_FLAG_CRACK;
  615. }
  616. }
  617. /*
  618. Gets node tile given a face direction.
  619. */
  620. void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile)
  621. {
  622. const NodeDefManager *ndef = data->m_client->ndef();
  623. // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
  624. // (0,0,1), (0,0,-1) or (0,0,0)
  625. assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z <= 1);
  626. // Convert direction to single integer for table lookup
  627. // 0 = (0,0,0)
  628. // 1 = (1,0,0)
  629. // 2 = (0,1,0)
  630. // 3 = (0,0,1)
  631. // 4 = invalid, treat as (0,0,0)
  632. // 5 = (0,0,-1)
  633. // 6 = (0,-1,0)
  634. // 7 = (-1,0,0)
  635. u8 dir_i = ((dir.X + 2 * dir.Y + 3 * dir.Z) & 7) * 2;
  636. // Get rotation for things like chests
  637. u8 facedir = mn.getFaceDir(ndef, true);
  638. static const u16 dir_to_tile[24 * 16] =
  639. {
  640. // 0 +X +Y +Z -Z -Y -X -> value=tile,rotation
  641. 0,0, 2,0 , 0,0 , 4,0 , 0,0, 5,0 , 1,0 , 3,0 , // rotate around y+ 0 - 3
  642. 0,0, 4,0 , 0,3 , 3,0 , 0,0, 2,0 , 1,1 , 5,0 ,
  643. 0,0, 3,0 , 0,2 , 5,0 , 0,0, 4,0 , 1,2 , 2,0 ,
  644. 0,0, 5,0 , 0,1 , 2,0 , 0,0, 3,0 , 1,3 , 4,0 ,
  645. 0,0, 2,3 , 5,0 , 0,2 , 0,0, 1,0 , 4,2 , 3,1 , // rotate around z+ 4 - 7
  646. 0,0, 4,3 , 2,0 , 0,1 , 0,0, 1,1 , 3,2 , 5,1 ,
  647. 0,0, 3,3 , 4,0 , 0,0 , 0,0, 1,2 , 5,2 , 2,1 ,
  648. 0,0, 5,3 , 3,0 , 0,3 , 0,0, 1,3 , 2,2 , 4,1 ,
  649. 0,0, 2,1 , 4,2 , 1,2 , 0,0, 0,0 , 5,0 , 3,3 , // rotate around z- 8 - 11
  650. 0,0, 4,1 , 3,2 , 1,3 , 0,0, 0,3 , 2,0 , 5,3 ,
  651. 0,0, 3,1 , 5,2 , 1,0 , 0,0, 0,2 , 4,0 , 2,3 ,
  652. 0,0, 5,1 , 2,2 , 1,1 , 0,0, 0,1 , 3,0 , 4,3 ,
  653. 0,0, 0,3 , 3,3 , 4,1 , 0,0, 5,3 , 2,3 , 1,3 , // rotate around x+ 12 - 15
  654. 0,0, 0,2 , 5,3 , 3,1 , 0,0, 2,3 , 4,3 , 1,0 ,
  655. 0,0, 0,1 , 2,3 , 5,1 , 0,0, 4,3 , 3,3 , 1,1 ,
  656. 0,0, 0,0 , 4,3 , 2,1 , 0,0, 3,3 , 5,3 , 1,2 ,
  657. 0,0, 1,1 , 2,1 , 4,3 , 0,0, 5,1 , 3,1 , 0,1 , // rotate around x- 16 - 19
  658. 0,0, 1,2 , 4,1 , 3,3 , 0,0, 2,1 , 5,1 , 0,0 ,
  659. 0,0, 1,3 , 3,1 , 5,3 , 0,0, 4,1 , 2,1 , 0,3 ,
  660. 0,0, 1,0 , 5,1 , 2,3 , 0,0, 3,1 , 4,1 , 0,2 ,
  661. 0,0, 3,2 , 1,2 , 4,2 , 0,0, 5,2 , 0,2 , 2,2 , // rotate around y- 20 - 23
  662. 0,0, 5,2 , 1,3 , 3,2 , 0,0, 2,2 , 0,1 , 4,2 ,
  663. 0,0, 2,2 , 1,0 , 5,2 , 0,0, 4,2 , 0,0 , 3,2 ,
  664. 0,0, 4,2 , 1,1 , 2,2 , 0,0, 3,2 , 0,3 , 5,2
  665. };
  666. u16 tile_index = facedir * 16 + dir_i;
  667. getNodeTileN(mn, p, dir_to_tile[tile_index], data, tile);
  668. tile.rotation = tile.world_aligned ? 0 : dir_to_tile[tile_index + 1];
  669. }
  670. static void getTileInfo(
  671. // Input:
  672. MeshMakeData *data,
  673. const v3s16 &p,
  674. const v3s16 &face_dir,
  675. // Output:
  676. bool &makes_face,
  677. v3s16 &p_corrected,
  678. v3s16 &face_dir_corrected,
  679. u16 *lights,
  680. u8 &waving,
  681. TileSpec &tile
  682. )
  683. {
  684. VoxelManipulator &vmanip = data->m_vmanip;
  685. const NodeDefManager *ndef = data->m_client->ndef();
  686. v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
  687. const MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
  688. // Don't even try to get n1 if n0 is already CONTENT_IGNORE
  689. if (n0.getContent() == CONTENT_IGNORE) {
  690. makes_face = false;
  691. return;
  692. }
  693. const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(blockpos_nodes + p + face_dir);
  694. if (n1.getContent() == CONTENT_IGNORE) {
  695. makes_face = false;
  696. return;
  697. }
  698. // This is hackish
  699. bool equivalent = false;
  700. u8 mf = face_contents(n0.getContent(), n1.getContent(),
  701. &equivalent, ndef);
  702. if (mf == 0) {
  703. makes_face = false;
  704. return;
  705. }
  706. makes_face = true;
  707. MapNode n = n0;
  708. if (mf == 1) {
  709. p_corrected = p;
  710. face_dir_corrected = face_dir;
  711. } else {
  712. n = n1;
  713. p_corrected = p + face_dir;
  714. face_dir_corrected = -face_dir;
  715. }
  716. getNodeTile(n, p_corrected, face_dir_corrected, data, tile);
  717. const ContentFeatures &f = ndef->get(n);
  718. waving = f.waving;
  719. tile.emissive_light = f.light_source;
  720. // eg. water and glass
  721. if (equivalent) {
  722. for (TileLayer &layer : tile.layers)
  723. layer.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
  724. }
  725. if (!data->m_smooth_lighting) {
  726. lights[0] = lights[1] = lights[2] = lights[3] =
  727. getFaceLight(n0, n1, face_dir, ndef);
  728. } else {
  729. v3s16 vertex_dirs[4];
  730. getNodeVertexDirs(face_dir_corrected, vertex_dirs);
  731. v3s16 light_p = blockpos_nodes + p_corrected;
  732. for (u16 i = 0; i < 4; i++)
  733. lights[i] = getSmoothLightSolid(light_p, face_dir_corrected, vertex_dirs[i], data);
  734. }
  735. }
  736. /*
  737. startpos:
  738. translate_dir: unit vector with only one of x, y or z
  739. face_dir: unit vector with only one of x, y or z
  740. */
  741. static void updateFastFaceRow(
  742. MeshMakeData *data,
  743. const v3s16 &&startpos,
  744. v3s16 translate_dir,
  745. const v3f &&translate_dir_f,
  746. const v3s16 &&face_dir,
  747. std::vector<FastFace> &dest)
  748. {
  749. static thread_local const bool waving_liquids =
  750. g_settings->getBool("enable_shaders") &&
  751. g_settings->getBool("enable_waving_water");
  752. v3s16 p = startpos;
  753. u16 continuous_tiles_count = 1;
  754. bool makes_face = false;
  755. v3s16 p_corrected;
  756. v3s16 face_dir_corrected;
  757. u16 lights[4] = {0, 0, 0, 0};
  758. u8 waving = 0;
  759. TileSpec tile;
  760. // Get info of first tile
  761. getTileInfo(data, p, face_dir,
  762. makes_face, p_corrected, face_dir_corrected,
  763. lights, waving, tile);
  764. // Unroll this variable which has a significant build cost
  765. TileSpec next_tile;
  766. for (u16 j = 0; j < MAP_BLOCKSIZE; j++) {
  767. // If tiling can be done, this is set to false in the next step
  768. bool next_is_different = true;
  769. bool next_makes_face = false;
  770. v3s16 next_p_corrected;
  771. v3s16 next_face_dir_corrected;
  772. u16 next_lights[4] = {0, 0, 0, 0};
  773. // If at last position, there is nothing to compare to and
  774. // the face must be drawn anyway
  775. if (j != MAP_BLOCKSIZE - 1) {
  776. p += translate_dir;
  777. getTileInfo(data, p, face_dir,
  778. next_makes_face, next_p_corrected,
  779. next_face_dir_corrected, next_lights,
  780. waving,
  781. next_tile);
  782. if (next_makes_face == makes_face
  783. && next_p_corrected == p_corrected + translate_dir
  784. && next_face_dir_corrected == face_dir_corrected
  785. && memcmp(next_lights, lights, sizeof(lights)) == 0
  786. // Don't apply fast faces to waving water.
  787. && (waving != 3 || !waving_liquids)
  788. && next_tile.isTileable(tile)) {
  789. next_is_different = false;
  790. continuous_tiles_count++;
  791. }
  792. }
  793. if (next_is_different) {
  794. /*
  795. Create a face if there should be one
  796. */
  797. if (makes_face) {
  798. // Floating point conversion of the position vector
  799. v3f pf(p_corrected.X, p_corrected.Y, p_corrected.Z);
  800. // Center point of face (kind of)
  801. v3f sp = pf - ((f32)continuous_tiles_count * 0.5f - 0.5f)
  802. * translate_dir_f;
  803. v3f scale(1, 1, 1);
  804. if (translate_dir.X != 0)
  805. scale.X = continuous_tiles_count;
  806. if (translate_dir.Y != 0)
  807. scale.Y = continuous_tiles_count;
  808. if (translate_dir.Z != 0)
  809. scale.Z = continuous_tiles_count;
  810. makeFastFace(tile, lights[0], lights[1], lights[2], lights[3],
  811. pf, sp, face_dir_corrected, scale, dest);
  812. g_profiler->avg("Meshgen: Tiles per face [#]", continuous_tiles_count);
  813. }
  814. continuous_tiles_count = 1;
  815. }
  816. makes_face = next_makes_face;
  817. p_corrected = next_p_corrected;
  818. face_dir_corrected = next_face_dir_corrected;
  819. memcpy(lights, next_lights, sizeof(lights));
  820. if (next_is_different)
  821. tile = std::move(next_tile); // faster than copy
  822. }
  823. }
  824. static void updateAllFastFaceRows(MeshMakeData *data,
  825. std::vector<FastFace> &dest)
  826. {
  827. /*
  828. Go through every y,z and get top(y+) faces in rows of x+
  829. */
  830. for (s16 y = 0; y < MAP_BLOCKSIZE; y++)
  831. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  832. updateFastFaceRow(data,
  833. v3s16(0, y, z),
  834. v3s16(1, 0, 0), //dir
  835. v3f (1, 0, 0),
  836. v3s16(0, 1, 0), //face dir
  837. dest);
  838. /*
  839. Go through every x,y and get right(x+) faces in rows of z+
  840. */
  841. for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
  842. for (s16 y = 0; y < MAP_BLOCKSIZE; y++)
  843. updateFastFaceRow(data,
  844. v3s16(x, y, 0),
  845. v3s16(0, 0, 1), //dir
  846. v3f (0, 0, 1),
  847. v3s16(1, 0, 0), //face dir
  848. dest);
  849. /*
  850. Go through every y,z and get back(z+) faces in rows of x+
  851. */
  852. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  853. for (s16 y = 0; y < MAP_BLOCKSIZE; y++)
  854. updateFastFaceRow(data,
  855. v3s16(0, y, z),
  856. v3s16(1, 0, 0), //dir
  857. v3f (1, 0, 0),
  858. v3s16(0, 0, 1), //face dir
  859. dest);
  860. }
  861. static void applyTileColor(PreMeshBuffer &pmb)
  862. {
  863. video::SColor tc = pmb.layer.color;
  864. if (tc == video::SColor(0xFFFFFFFF))
  865. return;
  866. for (video::S3DVertex &vertex : pmb.vertices) {
  867. video::SColor *c = &vertex.Color;
  868. c->set(c->getAlpha(),
  869. c->getRed() * tc.getRed() / 255,
  870. c->getGreen() * tc.getGreen() / 255,
  871. c->getBlue() * tc.getBlue() / 255);
  872. }
  873. }
  874. /*
  875. MapBlockMesh
  876. */
  877. MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
  878. m_minimap_mapblock(NULL),
  879. m_tsrc(data->m_client->getTextureSource()),
  880. m_shdrsrc(data->m_client->getShaderSource()),
  881. m_animation_force_timer(0), // force initial animation
  882. m_last_crack(-1),
  883. m_last_daynight_ratio((u32) -1)
  884. {
  885. for (auto &m : m_mesh)
  886. m = new scene::SMesh();
  887. m_enable_shaders = data->m_use_shaders;
  888. m_enable_vbo = g_settings->getBool("enable_vbo");
  889. if (data->m_client->getMinimap()) {
  890. m_minimap_mapblock = new MinimapMapblock;
  891. m_minimap_mapblock->getMinimapNodes(
  892. &data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);
  893. }
  894. // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
  895. // 24-155ms for MAP_BLOCKSIZE=32 (NOTE: probably outdated)
  896. //TimeTaker timer1("MapBlockMesh()");
  897. std::vector<FastFace> fastfaces_new;
  898. fastfaces_new.reserve(512);
  899. /*
  900. We are including the faces of the trailing edges of the block.
  901. This means that when something changes, the caller must
  902. also update the meshes of the blocks at the leading edges.
  903. NOTE: This is the slowest part of this method.
  904. */
  905. {
  906. // 4-23ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
  907. //TimeTaker timer2("updateAllFastFaceRows()");
  908. updateAllFastFaceRows(data, fastfaces_new);
  909. }
  910. // End of slow part
  911. /*
  912. Convert FastFaces to MeshCollector
  913. */
  914. MeshCollector collector;
  915. {
  916. // avg 0ms (100ms spikes when loading textures the first time)
  917. // (NOTE: probably outdated)
  918. //TimeTaker timer2("MeshCollector building");
  919. for (const FastFace &f : fastfaces_new) {
  920. static const u16 indices[] = {0, 1, 2, 2, 3, 0};
  921. static const u16 indices_alternate[] = {0, 1, 3, 2, 3, 1};
  922. const u16 *indices_p =
  923. f.vertex_0_2_connected ? indices : indices_alternate;
  924. collector.append(f.tile, f.vertices, 4, indices_p, 6);
  925. }
  926. }
  927. /*
  928. Add special graphics:
  929. - torches
  930. - flowing water
  931. - fences
  932. - whatever
  933. */
  934. {
  935. MapblockMeshGenerator generator(data, &collector);
  936. generator.generate();
  937. }
  938. /*
  939. Convert MeshCollector to SMesh
  940. */
  941. for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
  942. for(u32 i = 0; i < collector.prebuffers[layer].size(); i++)
  943. {
  944. PreMeshBuffer &p = collector.prebuffers[layer][i];
  945. applyTileColor(p);
  946. // Generate animation data
  947. // - Cracks
  948. if (p.layer.material_flags & MATERIAL_FLAG_CRACK) {
  949. // Find the texture name plus ^[crack:N:
  950. std::ostringstream os(std::ios::binary);
  951. os << m_tsrc->getTextureName(p.layer.texture_id) << "^[crack";
  952. if (p.layer.material_flags & MATERIAL_FLAG_CRACK_OVERLAY)
  953. os << "o"; // use ^[cracko
  954. u8 tiles = p.layer.scale;
  955. if (tiles > 1)
  956. os << ":" << (u32)tiles;
  957. os << ":" << (u32)p.layer.animation_frame_count << ":";
  958. m_crack_materials.insert(std::make_pair(
  959. std::pair<u8, u32>(layer, i), os.str()));
  960. // Replace tile texture with the cracked one
  961. p.layer.texture = m_tsrc->getTextureForMesh(
  962. os.str() + "0",
  963. &p.layer.texture_id);
  964. }
  965. // - Texture animation
  966. if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
  967. // Add to MapBlockMesh in order to animate these tiles
  968. m_animation_tiles[std::pair<u8, u32>(layer, i)] = p.layer;
  969. m_animation_frames[std::pair<u8, u32>(layer, i)] = 0;
  970. if (g_settings->getBool(
  971. "desynchronize_mapblock_texture_animation")) {
  972. // Get starting position from noise
  973. m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] =
  974. 100000 * (2.0 + noise3d(
  975. data->m_blockpos.X, data->m_blockpos.Y,
  976. data->m_blockpos.Z, 0));
  977. } else {
  978. // Play all synchronized
  979. m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] = 0;
  980. }
  981. // Replace tile texture with the first animation frame
  982. p.layer.texture = (*p.layer.frames)[0].texture;
  983. }
  984. if (!m_enable_shaders) {
  985. // Extract colors for day-night animation
  986. // Dummy sunlight to handle non-sunlit areas
  987. video::SColorf sunlight;
  988. get_sunlight_color(&sunlight, 0);
  989. u32 vertex_count = p.vertices.size();
  990. for (u32 j = 0; j < vertex_count; j++) {
  991. video::SColor *vc = &p.vertices[j].Color;
  992. video::SColor copy = *vc;
  993. if (vc->getAlpha() == 0) // No sunlight - no need to animate
  994. final_color_blend(vc, copy, sunlight); // Finalize color
  995. else // Record color to animate
  996. m_daynight_diffs[std::pair<u8, u32>(layer, i)][j] = copy;
  997. // The sunlight ratio has been stored,
  998. // delete alpha (for the final rendering).
  999. vc->setAlpha(255);
  1000. }
  1001. }
  1002. // Create material
  1003. video::SMaterial material;
  1004. material.setFlag(video::EMF_LIGHTING, false);
  1005. material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  1006. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  1007. material.setFlag(video::EMF_FOG_ENABLE, true);
  1008. material.setTexture(0, p.layer.texture);
  1009. if (m_enable_shaders) {
  1010. material.MaterialType = m_shdrsrc->getShaderInfo(
  1011. p.layer.shader_id).material;
  1012. p.layer.applyMaterialOptionsWithShaders(material);
  1013. if (p.layer.normal_texture)
  1014. material.setTexture(1, p.layer.normal_texture);
  1015. material.setTexture(2, p.layer.flags_texture);
  1016. } else {
  1017. p.layer.applyMaterialOptions(material);
  1018. }
  1019. scene::SMesh *mesh = (scene::SMesh *)m_mesh[layer];
  1020. scene::SMeshBuffer *buf = new scene::SMeshBuffer();
  1021. buf->Material = material;
  1022. buf->append(&p.vertices[0], p.vertices.size(),
  1023. &p.indices[0], p.indices.size());
  1024. mesh->addMeshBuffer(buf);
  1025. buf->drop();
  1026. }
  1027. if (m_mesh[layer]) {
  1028. #if 0
  1029. // Usually 1-700 faces and 1-7 materials
  1030. std::cout << "Updated MapBlock has " << fastfaces_new.size()
  1031. << " faces and uses " << m_mesh[layer]->getMeshBufferCount()
  1032. << " materials (meshbuffers)" << std::endl;
  1033. #endif
  1034. // Use VBO for mesh (this just would set this for ever buffer)
  1035. if (m_enable_vbo)
  1036. m_mesh[layer]->setHardwareMappingHint(scene::EHM_STATIC);
  1037. }
  1038. }
  1039. //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
  1040. // Check if animation is required for this mesh
  1041. m_has_animation =
  1042. !m_crack_materials.empty() ||
  1043. !m_daynight_diffs.empty() ||
  1044. !m_animation_tiles.empty();
  1045. }
  1046. MapBlockMesh::~MapBlockMesh()
  1047. {
  1048. for (scene::IMesh *m : m_mesh) {
  1049. if (m_enable_vbo) {
  1050. for (u32 i = 0; i < m->getMeshBufferCount(); i++) {
  1051. scene::IMeshBuffer *buf = m->getMeshBuffer(i);
  1052. RenderingEngine::get_video_driver()->removeHardwareBuffer(buf);
  1053. }
  1054. }
  1055. m->drop();
  1056. }
  1057. delete m_minimap_mapblock;
  1058. }
  1059. bool MapBlockMesh::animate(bool faraway, float time, int crack,
  1060. u32 daynight_ratio)
  1061. {
  1062. if (!m_has_animation) {
  1063. m_animation_force_timer = 100000;
  1064. return false;
  1065. }
  1066. m_animation_force_timer = myrand_range(5, 100);
  1067. // Cracks
  1068. if (crack != m_last_crack) {
  1069. for (auto &crack_material : m_crack_materials) {
  1070. scene::IMeshBuffer *buf = m_mesh[crack_material.first.first]->
  1071. getMeshBuffer(crack_material.first.second);
  1072. std::string basename = crack_material.second;
  1073. // Create new texture name from original
  1074. std::ostringstream os;
  1075. os << basename << crack;
  1076. u32 new_texture_id = 0;
  1077. video::ITexture *new_texture =
  1078. m_tsrc->getTextureForMesh(os.str(), &new_texture_id);
  1079. buf->getMaterial().setTexture(0, new_texture);
  1080. // If the current material is also animated,
  1081. // update animation info
  1082. auto anim_iter = m_animation_tiles.find(crack_material.first);
  1083. if (anim_iter != m_animation_tiles.end()) {
  1084. TileLayer &tile = anim_iter->second;
  1085. tile.texture = new_texture;
  1086. tile.texture_id = new_texture_id;
  1087. // force animation update
  1088. m_animation_frames[crack_material.first] = -1;
  1089. }
  1090. }
  1091. m_last_crack = crack;
  1092. }
  1093. // Texture animation
  1094. for (auto &animation_tile : m_animation_tiles) {
  1095. const TileLayer &tile = animation_tile.second;
  1096. // Figure out current frame
  1097. int frameoffset = m_animation_frame_offsets[animation_tile.first];
  1098. int frame = (int)(time * 1000 / tile.animation_frame_length_ms
  1099. + frameoffset) % tile.animation_frame_count;
  1100. // If frame doesn't change, skip
  1101. if (frame == m_animation_frames[animation_tile.first])
  1102. continue;
  1103. m_animation_frames[animation_tile.first] = frame;
  1104. scene::IMeshBuffer *buf = m_mesh[animation_tile.first.first]->
  1105. getMeshBuffer(animation_tile.first.second);
  1106. const FrameSpec &animation_frame = (*tile.frames)[frame];
  1107. buf->getMaterial().setTexture(0, animation_frame.texture);
  1108. if (m_enable_shaders) {
  1109. if (animation_frame.normal_texture)
  1110. buf->getMaterial().setTexture(1,
  1111. animation_frame.normal_texture);
  1112. buf->getMaterial().setTexture(2, animation_frame.flags_texture);
  1113. }
  1114. }
  1115. // Day-night transition
  1116. if (!m_enable_shaders && (daynight_ratio != m_last_daynight_ratio)) {
  1117. // Force reload mesh to VBO
  1118. if (m_enable_vbo)
  1119. for (scene::IMesh *m : m_mesh)
  1120. m->setDirty();
  1121. video::SColorf day_color;
  1122. get_sunlight_color(&day_color, daynight_ratio);
  1123. for (auto &daynight_diff : m_daynight_diffs) {
  1124. scene::IMeshBuffer *buf = m_mesh[daynight_diff.first.first]->
  1125. getMeshBuffer(daynight_diff.first.second);
  1126. video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();
  1127. for (const auto &j : daynight_diff.second)
  1128. final_color_blend(&(vertices[j.first].Color), j.second,
  1129. day_color);
  1130. }
  1131. m_last_daynight_ratio = daynight_ratio;
  1132. }
  1133. return true;
  1134. }
  1135. video::SColor encode_light(u16 light, u8 emissive_light)
  1136. {
  1137. // Get components
  1138. u32 day = (light & 0xff);
  1139. u32 night = (light >> 8);
  1140. // Add emissive light
  1141. night += emissive_light * 2.5f;
  1142. if (night > 255)
  1143. night = 255;
  1144. // Since we don't know if the day light is sunlight or
  1145. // artificial light, assume it is artificial when the night
  1146. // light bank is also lit.
  1147. if (day < night)
  1148. day = 0;
  1149. else
  1150. day = day - night;
  1151. u32 sum = day + night;
  1152. // Ratio of sunlight:
  1153. u32 r;
  1154. if (sum > 0)
  1155. r = day * 255 / sum;
  1156. else
  1157. r = 0;
  1158. // Average light:
  1159. float b = (day + night) / 2;
  1160. return video::SColor(r, b, b, b);
  1161. }