content_mapblock.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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 "content_mapblock.h"
  17. #include "util/numeric.h"
  18. #include "util/directiontables.h"
  19. #include "mapblock_mesh.h"
  20. #include "settings.h"
  21. #include "nodedef.h"
  22. #include "client/tile.h"
  23. #include "mesh.h"
  24. #include <IMeshManipulator.h>
  25. #include "client/meshgen/collector.h"
  26. #include "client/renderingengine.h"
  27. #include "client.h"
  28. #include "noise.h"
  29. // Distance of light extrapolation (for oversized nodes)
  30. // After this distance, it gives up and considers light level constant
  31. #define SMOOTH_LIGHTING_OVERSIZE 1.0
  32. // Node edge count (for glasslike-framed)
  33. #define FRAMED_EDGE_COUNT 12
  34. // Node neighbor count, including edge-connected, but not vertex-connected
  35. // (for glasslike-framed)
  36. // Corresponding offsets are listed in g_27dirs
  37. #define FRAMED_NEIGHBOR_COUNT 18
  38. static const v3s16 light_dirs[8] = {
  39. v3s16(-1, -1, -1),
  40. v3s16(-1, -1, 1),
  41. v3s16(-1, 1, -1),
  42. v3s16(-1, 1, 1),
  43. v3s16( 1, -1, -1),
  44. v3s16( 1, -1, 1),
  45. v3s16( 1, 1, -1),
  46. v3s16( 1, 1, 1),
  47. };
  48. // Standard index set to make a quad on 4 vertices
  49. static constexpr u16 quad_indices[] = {0, 1, 2, 2, 3, 0};
  50. const std::string MapblockMeshGenerator::raillike_groupname = "connect_to_raillike";
  51. MapblockMeshGenerator::MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output)
  52. {
  53. data = input;
  54. collector = output;
  55. nodedef = data->m_client->ndef();
  56. meshmanip = RenderingEngine::get_scene_manager()->getMeshManipulator();
  57. enable_mesh_cache = g_settings->getBool("enable_mesh_cache") &&
  58. !data->m_smooth_lighting; // Mesh cache is not supported with smooth lighting
  59. blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
  60. }
  61. void MapblockMeshGenerator::useTile(int index, u8 set_flags, u8 reset_flags, bool special)
  62. {
  63. if (special)
  64. getSpecialTile(index, &tile, p == data->m_crack_pos_relative);
  65. else
  66. getTile(index, &tile);
  67. if (!data->m_smooth_lighting)
  68. color = encode_light(light, f->light_source);
  69. for (auto &layer : tile.layers) {
  70. layer.material_flags |= set_flags;
  71. layer.material_flags &= ~reset_flags;
  72. }
  73. }
  74. // Returns a tile, ready for use, non-rotated.
  75. void MapblockMeshGenerator::getTile(int index, TileSpec *tile)
  76. {
  77. getNodeTileN(n, p, index, data, *tile);
  78. }
  79. // Returns a tile, ready for use, rotated according to the node facedir.
  80. void MapblockMeshGenerator::getTile(v3s16 direction, TileSpec *tile)
  81. {
  82. getNodeTile(n, p, direction, data, *tile);
  83. }
  84. // Returns a special tile, ready for use, non-rotated.
  85. void MapblockMeshGenerator::getSpecialTile(int index, TileSpec *tile, bool apply_crack)
  86. {
  87. *tile = f->special_tiles[index];
  88. TileLayer *top_layer = nullptr;
  89. for (auto &layernum : tile->layers) {
  90. TileLayer *layer = &layernum;
  91. if (layer->texture_id == 0)
  92. continue;
  93. top_layer = layer;
  94. if (!layer->has_color)
  95. n.getColor(*f, &layer->color);
  96. }
  97. if (apply_crack)
  98. top_layer->material_flags |= MATERIAL_FLAG_CRACK;
  99. }
  100. void MapblockMeshGenerator::drawQuad(v3f *coords, const v3s16 &normal,
  101. float vertical_tiling)
  102. {
  103. const v2f tcoords[4] = {v2f(0.0, 0.0), v2f(1.0, 0.0),
  104. v2f(1.0, vertical_tiling), v2f(0.0, vertical_tiling)};
  105. video::S3DVertex vertices[4];
  106. bool shade_face = !f->light_source && (normal != v3s16(0, 0, 0));
  107. v3f normal2(normal.X, normal.Y, normal.Z);
  108. for (int j = 0; j < 4; j++) {
  109. vertices[j].Pos = coords[j] + origin;
  110. vertices[j].Normal = normal2;
  111. if (data->m_smooth_lighting)
  112. vertices[j].Color = blendLightColor(coords[j]);
  113. else
  114. vertices[j].Color = color;
  115. if (shade_face)
  116. applyFacesShading(vertices[j].Color, normal2);
  117. vertices[j].TCoords = tcoords[j];
  118. }
  119. collector->append(tile, vertices, 4, quad_indices, 6);
  120. }
  121. // Create a cuboid.
  122. // tiles - the tiles (materials) to use (for all 6 faces)
  123. // tilecount - number of entries in tiles, 1<=tilecount<=6
  124. // lights - vertex light levels. The order is the same as in light_dirs.
  125. // NULL may be passed if smooth lighting is disabled.
  126. // txc - texture coordinates - this is a list of texture coordinates
  127. // for the opposite corners of each face - therefore, there
  128. // should be (2+2)*6=24 values in the list. The order of
  129. // the faces in the list is up-down-right-left-back-front
  130. // (compatible with ContentFeatures).
  131. void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
  132. TileSpec *tiles, int tilecount, const LightInfo *lights, const f32 *txc)
  133. {
  134. assert(tilecount >= 1 && tilecount <= 6); // pre-condition
  135. v3f min = box.MinEdge;
  136. v3f max = box.MaxEdge;
  137. video::SColor colors[6];
  138. if (!data->m_smooth_lighting) {
  139. for (int face = 0; face != 6; ++face) {
  140. colors[face] = encode_light(light, f->light_source);
  141. }
  142. if (!f->light_source) {
  143. applyFacesShading(colors[0], v3f(0, 1, 0));
  144. applyFacesShading(colors[1], v3f(0, -1, 0));
  145. applyFacesShading(colors[2], v3f(1, 0, 0));
  146. applyFacesShading(colors[3], v3f(-1, 0, 0));
  147. applyFacesShading(colors[4], v3f(0, 0, 1));
  148. applyFacesShading(colors[5], v3f(0, 0, -1));
  149. }
  150. }
  151. video::S3DVertex vertices[24] = {
  152. // top
  153. video::S3DVertex(min.X, max.Y, max.Z, 0, 1, 0, colors[0], txc[0], txc[1]),
  154. video::S3DVertex(max.X, max.Y, max.Z, 0, 1, 0, colors[0], txc[2], txc[1]),
  155. video::S3DVertex(max.X, max.Y, min.Z, 0, 1, 0, colors[0], txc[2], txc[3]),
  156. video::S3DVertex(min.X, max.Y, min.Z, 0, 1, 0, colors[0], txc[0], txc[3]),
  157. // bottom
  158. video::S3DVertex(min.X, min.Y, min.Z, 0, -1, 0, colors[1], txc[4], txc[5]),
  159. video::S3DVertex(max.X, min.Y, min.Z, 0, -1, 0, colors[1], txc[6], txc[5]),
  160. video::S3DVertex(max.X, min.Y, max.Z, 0, -1, 0, colors[1], txc[6], txc[7]),
  161. video::S3DVertex(min.X, min.Y, max.Z, 0, -1, 0, colors[1], txc[4], txc[7]),
  162. // right
  163. video::S3DVertex(max.X, max.Y, min.Z, 1, 0, 0, colors[2], txc[ 8], txc[9]),
  164. video::S3DVertex(max.X, max.Y, max.Z, 1, 0, 0, colors[2], txc[10], txc[9]),
  165. video::S3DVertex(max.X, min.Y, max.Z, 1, 0, 0, colors[2], txc[10], txc[11]),
  166. video::S3DVertex(max.X, min.Y, min.Z, 1, 0, 0, colors[2], txc[ 8], txc[11]),
  167. // left
  168. video::S3DVertex(min.X, max.Y, max.Z, -1, 0, 0, colors[3], txc[12], txc[13]),
  169. video::S3DVertex(min.X, max.Y, min.Z, -1, 0, 0, colors[3], txc[14], txc[13]),
  170. video::S3DVertex(min.X, min.Y, min.Z, -1, 0, 0, colors[3], txc[14], txc[15]),
  171. video::S3DVertex(min.X, min.Y, max.Z, -1, 0, 0, colors[3], txc[12], txc[15]),
  172. // back
  173. video::S3DVertex(max.X, max.Y, max.Z, 0, 0, 1, colors[4], txc[16], txc[17]),
  174. video::S3DVertex(min.X, max.Y, max.Z, 0, 0, 1, colors[4], txc[18], txc[17]),
  175. video::S3DVertex(min.X, min.Y, max.Z, 0, 0, 1, colors[4], txc[18], txc[19]),
  176. video::S3DVertex(max.X, min.Y, max.Z, 0, 0, 1, colors[4], txc[16], txc[19]),
  177. // front
  178. video::S3DVertex(min.X, max.Y, min.Z, 0, 0, -1, colors[5], txc[20], txc[21]),
  179. video::S3DVertex(max.X, max.Y, min.Z, 0, 0, -1, colors[5], txc[22], txc[21]),
  180. video::S3DVertex(max.X, min.Y, min.Z, 0, 0, -1, colors[5], txc[22], txc[23]),
  181. video::S3DVertex(min.X, min.Y, min.Z, 0, 0, -1, colors[5], txc[20], txc[23]),
  182. };
  183. static const u8 light_indices[24] = {
  184. 3, 7, 6, 2,
  185. 0, 4, 5, 1,
  186. 6, 7, 5, 4,
  187. 3, 2, 0, 1,
  188. 7, 3, 1, 5,
  189. 2, 6, 4, 0
  190. };
  191. for (int face = 0; face < 6; face++) {
  192. int tileindex = MYMIN(face, tilecount - 1);
  193. const TileSpec &tile = tiles[tileindex];
  194. for (int j = 0; j < 4; j++) {
  195. video::S3DVertex &vertex = vertices[face * 4 + j];
  196. v2f &tcoords = vertex.TCoords;
  197. switch (tile.rotation) {
  198. case 0:
  199. break;
  200. case 1: // R90
  201. tcoords.rotateBy(90, irr::core::vector2df(0, 0));
  202. break;
  203. case 2: // R180
  204. tcoords.rotateBy(180, irr::core::vector2df(0, 0));
  205. break;
  206. case 3: // R270
  207. tcoords.rotateBy(270, irr::core::vector2df(0, 0));
  208. break;
  209. case 4: // FXR90
  210. tcoords.X = 1.0 - tcoords.X;
  211. tcoords.rotateBy(90, irr::core::vector2df(0, 0));
  212. break;
  213. case 5: // FXR270
  214. tcoords.X = 1.0 - tcoords.X;
  215. tcoords.rotateBy(270, irr::core::vector2df(0, 0));
  216. break;
  217. case 6: // FYR90
  218. tcoords.Y = 1.0 - tcoords.Y;
  219. tcoords.rotateBy(90, irr::core::vector2df(0, 0));
  220. break;
  221. case 7: // FYR270
  222. tcoords.Y = 1.0 - tcoords.Y;
  223. tcoords.rotateBy(270, irr::core::vector2df(0, 0));
  224. break;
  225. case 8: // FX
  226. tcoords.X = 1.0 - tcoords.X;
  227. break;
  228. case 9: // FY
  229. tcoords.Y = 1.0 - tcoords.Y;
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. }
  236. if (data->m_smooth_lighting) {
  237. for (int j = 0; j < 24; ++j) {
  238. video::S3DVertex &vertex = vertices[j];
  239. vertex.Color = encode_light(
  240. lights[light_indices[j]].getPair(MYMAX(0.0f, vertex.Normal.Y)),
  241. f->light_source);
  242. if (!f->light_source)
  243. applyFacesShading(vertex.Color, vertex.Normal);
  244. }
  245. }
  246. // Add to mesh collector
  247. for (int k = 0; k < 6; ++k) {
  248. int tileindex = MYMIN(k, tilecount - 1);
  249. collector->append(tiles[tileindex], vertices + 4 * k, 4, quad_indices, 6);
  250. }
  251. }
  252. // Gets the base lighting values for a node
  253. void MapblockMeshGenerator::getSmoothLightFrame()
  254. {
  255. for (int k = 0; k < 8; ++k)
  256. frame.sunlight[k] = false;
  257. for (int k = 0; k < 8; ++k) {
  258. LightPair light(getSmoothLightTransparent(blockpos_nodes + p, light_dirs[k], data));
  259. frame.lightsDay[k] = light.lightDay;
  260. frame.lightsNight[k] = light.lightNight;
  261. // If there is direct sunlight and no ambient occlusion at some corner,
  262. // mark the vertical edge (top and bottom corners) containing it.
  263. if (light.lightDay == 255) {
  264. frame.sunlight[k] = true;
  265. frame.sunlight[k ^ 2] = true;
  266. }
  267. }
  268. }
  269. // Calculates vertex light level
  270. // vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
  271. LightInfo MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
  272. {
  273. // Light levels at (logical) node corners are known. Here,
  274. // trilinear interpolation is used to calculate light level
  275. // at a given point in the node.
  276. f32 x = core::clamp(vertex_pos.X / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  277. f32 y = core::clamp(vertex_pos.Y / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  278. f32 z = core::clamp(vertex_pos.Z / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  279. f32 lightDay = 0.0; // daylight
  280. f32 lightNight = 0.0;
  281. f32 lightBoosted = 0.0; // daylight + direct sunlight, if any
  282. for (int k = 0; k < 8; ++k) {
  283. f32 dx = (k & 4) ? x : 1 - x;
  284. f32 dy = (k & 2) ? y : 1 - y;
  285. f32 dz = (k & 1) ? z : 1 - z;
  286. // Use direct sunlight (255), if any; use daylight otherwise.
  287. f32 light_boosted = frame.sunlight[k] ? 255 : frame.lightsDay[k];
  288. lightDay += dx * dy * dz * frame.lightsDay[k];
  289. lightNight += dx * dy * dz * frame.lightsNight[k];
  290. lightBoosted += dx * dy * dz * light_boosted;
  291. }
  292. return LightInfo{lightDay, lightNight, lightBoosted};
  293. }
  294. // Calculates vertex color to be used in mapblock mesh
  295. // vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
  296. // tile_color - node's tile color
  297. video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos)
  298. {
  299. LightInfo light = blendLight(vertex_pos);
  300. return encode_light(light.getPair(), f->light_source);
  301. }
  302. video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos,
  303. const v3f &vertex_normal)
  304. {
  305. LightInfo light = blendLight(vertex_pos);
  306. video::SColor color = encode_light(light.getPair(MYMAX(0.0f, vertex_normal.Y)), f->light_source);
  307. if (!f->light_source)
  308. applyFacesShading(color, vertex_normal);
  309. return color;
  310. }
  311. void MapblockMeshGenerator::generateCuboidTextureCoords(const aabb3f &box, f32 *coords)
  312. {
  313. f32 tx1 = (box.MinEdge.X / BS) + 0.5;
  314. f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
  315. f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
  316. f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
  317. f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
  318. f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
  319. f32 txc[24] = {
  320. tx1, 1 - tz2, tx2, 1 - tz1, // up
  321. tx1, tz1, tx2, tz2, // down
  322. tz1, 1 - ty2, tz2, 1 - ty1, // right
  323. 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1, // left
  324. 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1, // back
  325. tx1, 1 - ty2, tx2, 1 - ty1, // front
  326. };
  327. for (int i = 0; i != 24; ++i)
  328. coords[i] = txc[i];
  329. }
  330. void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
  331. TileSpec *tiles, int tile_count)
  332. {
  333. f32 texture_coord_buf[24];
  334. f32 dx1 = box.MinEdge.X;
  335. f32 dy1 = box.MinEdge.Y;
  336. f32 dz1 = box.MinEdge.Z;
  337. f32 dx2 = box.MaxEdge.X;
  338. f32 dy2 = box.MaxEdge.Y;
  339. f32 dz2 = box.MaxEdge.Z;
  340. box.MinEdge += origin;
  341. box.MaxEdge += origin;
  342. if (!txc) {
  343. generateCuboidTextureCoords(box, texture_coord_buf);
  344. txc = texture_coord_buf;
  345. }
  346. if (!tiles) {
  347. tiles = &tile;
  348. tile_count = 1;
  349. }
  350. if (data->m_smooth_lighting) {
  351. LightInfo lights[8];
  352. for (int j = 0; j < 8; ++j) {
  353. v3f d;
  354. d.X = (j & 4) ? dx2 : dx1;
  355. d.Y = (j & 2) ? dy2 : dy1;
  356. d.Z = (j & 1) ? dz2 : dz1;
  357. lights[j] = blendLight(d);
  358. }
  359. drawCuboid(box, tiles, tile_count, lights, txc);
  360. } else {
  361. drawCuboid(box, tiles, tile_count, nullptr, txc);
  362. }
  363. }
  364. void MapblockMeshGenerator::prepareLiquidNodeDrawing()
  365. {
  366. getSpecialTile(0, &tile_liquid_top);
  367. getSpecialTile(1, &tile_liquid);
  368. MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(p.X, p.Y + 1, p.Z));
  369. MapNode nbottom = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(p.X, p.Y - 1, p.Z));
  370. c_flowing = nodedef->getId(f->liquid_alternative_flowing);
  371. c_source = nodedef->getId(f->liquid_alternative_source);
  372. top_is_same_liquid = (ntop.getContent() == c_flowing) || (ntop.getContent() == c_source);
  373. draw_liquid_bottom = (nbottom.getContent() != c_flowing) && (nbottom.getContent() != c_source);
  374. if (draw_liquid_bottom) {
  375. const ContentFeatures &f2 = nodedef->get(nbottom.getContent());
  376. if (f2.solidness > 1)
  377. draw_liquid_bottom = false;
  378. }
  379. if (data->m_smooth_lighting)
  380. return; // don't need to pre-compute anything in this case
  381. if (f->light_source != 0) {
  382. // If this liquid emits light and doesn't contain light, draw
  383. // it at what it emits, for an increased effect
  384. u8 e = decode_light(f->light_source);
  385. light = LightPair(std::max(e, light.lightDay), std::max(e, light.lightNight));
  386. } else if (nodedef->get(ntop).param_type == CPT_LIGHT) {
  387. // Otherwise, use the light of the node on top if possible
  388. light = LightPair(getInteriorLight(ntop, 0, nodedef));
  389. }
  390. color_liquid_top = encode_light(light, f->light_source);
  391. color = encode_light(light, f->light_source);
  392. }
  393. void MapblockMeshGenerator::getLiquidNeighborhood()
  394. {
  395. u8 range = rangelim(nodedef->get(c_flowing).liquid_range, 1, 8);
  396. for (int w = -1; w <= 1; w++)
  397. for (int u = -1; u <= 1; u++) {
  398. NeighborData &neighbor = liquid_neighbors[w + 1][u + 1];
  399. v3s16 p2 = p + v3s16(u, 0, w);
  400. MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  401. neighbor.content = n2.getContent();
  402. neighbor.level = -0.5 * BS;
  403. neighbor.is_same_liquid = false;
  404. neighbor.top_is_same_liquid = false;
  405. if (neighbor.content == CONTENT_IGNORE)
  406. continue;
  407. if (neighbor.content == c_source) {
  408. neighbor.is_same_liquid = true;
  409. neighbor.level = 0.5 * BS;
  410. } else if (neighbor.content == c_flowing) {
  411. neighbor.is_same_liquid = true;
  412. u8 liquid_level = (n2.param2 & LIQUID_LEVEL_MASK);
  413. if (liquid_level <= LIQUID_LEVEL_MAX + 1 - range)
  414. liquid_level = 0;
  415. else
  416. liquid_level -= (LIQUID_LEVEL_MAX + 1 - range);
  417. neighbor.level = (-0.5 + (liquid_level + 0.5) / range) * BS;
  418. }
  419. // Check node above neighbor.
  420. // NOTE: This doesn't get executed if neighbor
  421. // doesn't exist
  422. p2.Y++;
  423. n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  424. if (n2.getContent() == c_source || n2.getContent() == c_flowing)
  425. neighbor.top_is_same_liquid = true;
  426. }
  427. }
  428. void MapblockMeshGenerator::calculateCornerLevels()
  429. {
  430. for (int k = 0; k < 2; k++)
  431. for (int i = 0; i < 2; i++)
  432. corner_levels[k][i] = getCornerLevel(i, k);
  433. }
  434. f32 MapblockMeshGenerator::getCornerLevel(int i, int k)
  435. {
  436. float sum = 0;
  437. int count = 0;
  438. int air_count = 0;
  439. for (int dk = 0; dk < 2; dk++)
  440. for (int di = 0; di < 2; di++) {
  441. NeighborData &neighbor_data = liquid_neighbors[k + dk][i + di];
  442. content_t content = neighbor_data.content;
  443. // If top is liquid, draw starting from top of node
  444. if (neighbor_data.top_is_same_liquid)
  445. return 0.5 * BS;
  446. // Source always has the full height
  447. if (content == c_source)
  448. return 0.5 * BS;
  449. // Flowing liquid has level information
  450. if (content == c_flowing) {
  451. sum += neighbor_data.level;
  452. count++;
  453. } else if (content == CONTENT_AIR) {
  454. air_count++;
  455. if (air_count >= 2)
  456. return -0.5 * BS + 0.2;
  457. }
  458. }
  459. if (count > 0)
  460. return sum / count;
  461. return 0;
  462. }
  463. void MapblockMeshGenerator::drawLiquidSides()
  464. {
  465. struct LiquidFaceDesc {
  466. v3s16 dir; // XZ
  467. v3s16 p[2]; // XZ only; 1 means +, 0 means -
  468. };
  469. struct UV {
  470. int u, v;
  471. };
  472. static const LiquidFaceDesc base_faces[4] = {
  473. {v3s16( 1, 0, 0), {v3s16(1, 0, 1), v3s16(1, 0, 0)}},
  474. {v3s16(-1, 0, 0), {v3s16(0, 0, 0), v3s16(0, 0, 1)}},
  475. {v3s16( 0, 0, 1), {v3s16(0, 0, 1), v3s16(1, 0, 1)}},
  476. {v3s16( 0, 0, -1), {v3s16(1, 0, 0), v3s16(0, 0, 0)}},
  477. };
  478. static const UV base_vertices[4] = {
  479. {0, 1},
  480. {1, 1},
  481. {1, 0},
  482. {0, 0}
  483. };
  484. for (const auto &face : base_faces) {
  485. const NeighborData &neighbor = liquid_neighbors[face.dir.Z + 1][face.dir.X + 1];
  486. // No face between nodes of the same liquid, unless there is node
  487. // at the top to which it should be connected. Again, unless the face
  488. // there would be inside the liquid
  489. if (neighbor.is_same_liquid) {
  490. if (!top_is_same_liquid)
  491. continue;
  492. if (neighbor.top_is_same_liquid)
  493. continue;
  494. }
  495. const ContentFeatures &neighbor_features = nodedef->get(neighbor.content);
  496. // Don't draw face if neighbor is blocking the view
  497. if (neighbor_features.solidness == 2)
  498. continue;
  499. video::S3DVertex vertices[4];
  500. for (int j = 0; j < 4; j++) {
  501. const UV &vertex = base_vertices[j];
  502. const v3s16 &base = face.p[vertex.u];
  503. v3f pos;
  504. pos.X = (base.X - 0.5) * BS;
  505. pos.Z = (base.Z - 0.5) * BS;
  506. if (vertex.v)
  507. pos.Y = neighbor.is_same_liquid ? corner_levels[base.Z][base.X] : -0.5 * BS;
  508. else
  509. pos.Y = !top_is_same_liquid ? corner_levels[base.Z][base.X] : 0.5 * BS;
  510. if (data->m_smooth_lighting)
  511. color = blendLightColor(pos);
  512. pos += origin;
  513. vertices[j] = video::S3DVertex(pos.X, pos.Y, pos.Z, 0, 0, 0, color, vertex.u, vertex.v);
  514. };
  515. collector->append(tile_liquid, vertices, 4, quad_indices, 6);
  516. }
  517. }
  518. void MapblockMeshGenerator::drawLiquidTop()
  519. {
  520. // To get backface culling right, the vertices need to go
  521. // clockwise around the front of the face. And we happened to
  522. // calculate corner levels in exact reverse order.
  523. static const int corner_resolve[4][2] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  524. video::S3DVertex vertices[4] = {
  525. video::S3DVertex(-BS / 2, 0, BS / 2, 0, 0, 0, color_liquid_top, 0, 1),
  526. video::S3DVertex( BS / 2, 0, BS / 2, 0, 0, 0, color_liquid_top, 1, 1),
  527. video::S3DVertex( BS / 2, 0, -BS / 2, 0, 0, 0, color_liquid_top, 1, 0),
  528. video::S3DVertex(-BS / 2, 0, -BS / 2, 0, 0, 0, color_liquid_top, 0, 0),
  529. };
  530. for (int i = 0; i < 4; i++) {
  531. int u = corner_resolve[i][0];
  532. int w = corner_resolve[i][1];
  533. vertices[i].Pos.Y += corner_levels[w][u];
  534. if (data->m_smooth_lighting)
  535. vertices[i].Color = blendLightColor(vertices[i].Pos);
  536. vertices[i].Pos += origin;
  537. }
  538. // Default downwards-flowing texture animation goes from
  539. // -Z towards +Z, thus the direction is +Z.
  540. // Rotate texture to make animation go in flow direction
  541. // Positive if liquid moves towards +Z
  542. f32 dz = (corner_levels[0][0] + corner_levels[0][1]) -
  543. (corner_levels[1][0] + corner_levels[1][1]);
  544. // Positive if liquid moves towards +X
  545. f32 dx = (corner_levels[0][0] + corner_levels[1][0]) -
  546. (corner_levels[0][1] + corner_levels[1][1]);
  547. f32 tcoord_angle = atan2(dz, dx) * core::RADTODEG;
  548. v2f tcoord_center(0.5, 0.5);
  549. v2f tcoord_translate(blockpos_nodes.Z + p.Z, blockpos_nodes.X + p.X);
  550. tcoord_translate.rotateBy(tcoord_angle);
  551. tcoord_translate.X -= floor(tcoord_translate.X);
  552. tcoord_translate.Y -= floor(tcoord_translate.Y);
  553. for (video::S3DVertex &vertex : vertices) {
  554. vertex.TCoords.rotateBy(tcoord_angle, tcoord_center);
  555. vertex.TCoords += tcoord_translate;
  556. }
  557. std::swap(vertices[0].TCoords, vertices[2].TCoords);
  558. collector->append(tile_liquid_top, vertices, 4, quad_indices, 6);
  559. }
  560. void MapblockMeshGenerator::drawLiquidBottom()
  561. {
  562. video::S3DVertex vertices[4] = {
  563. video::S3DVertex(-BS / 2, -BS / 2, -BS / 2, 0, 0, 0, color_liquid_top, 0, 0),
  564. video::S3DVertex( BS / 2, -BS / 2, -BS / 2, 0, 0, 0, color_liquid_top, 1, 0),
  565. video::S3DVertex( BS / 2, -BS / 2, BS / 2, 0, 0, 0, color_liquid_top, 1, 1),
  566. video::S3DVertex(-BS / 2, -BS / 2, BS / 2, 0, 0, 0, color_liquid_top, 0, 1),
  567. };
  568. for (int i = 0; i < 4; i++) {
  569. if (data->m_smooth_lighting)
  570. vertices[i].Color = blendLightColor(vertices[i].Pos);
  571. vertices[i].Pos += origin;
  572. }
  573. collector->append(tile_liquid_top, vertices, 4, quad_indices, 6);
  574. }
  575. void MapblockMeshGenerator::drawLiquidNode()
  576. {
  577. prepareLiquidNodeDrawing();
  578. getLiquidNeighborhood();
  579. calculateCornerLevels();
  580. drawLiquidSides();
  581. if (!top_is_same_liquid)
  582. drawLiquidTop();
  583. if (draw_liquid_bottom)
  584. drawLiquidBottom();
  585. }
  586. void MapblockMeshGenerator::drawGlasslikeNode()
  587. {
  588. useTile(0, 0, 0);
  589. for (int face = 0; face < 6; face++) {
  590. // Check this neighbor
  591. v3s16 dir = g_6dirs[face];
  592. v3s16 neighbor_pos = blockpos_nodes + p + dir;
  593. MapNode neighbor = data->m_vmanip.getNodeNoExNoEmerge(neighbor_pos);
  594. // Don't make face if neighbor is of same type
  595. if (neighbor.getContent() == n.getContent())
  596. continue;
  597. // Face at Z-
  598. v3f vertices[4] = {
  599. v3f(-BS / 2, BS / 2, -BS / 2),
  600. v3f( BS / 2, BS / 2, -BS / 2),
  601. v3f( BS / 2, -BS / 2, -BS / 2),
  602. v3f(-BS / 2, -BS / 2, -BS / 2),
  603. };
  604. for (v3f &vertex : vertices) {
  605. switch (face) {
  606. case D6D_ZP:
  607. vertex.rotateXZBy(180); break;
  608. case D6D_YP:
  609. vertex.rotateYZBy( 90); break;
  610. case D6D_XP:
  611. vertex.rotateXZBy( 90); break;
  612. case D6D_ZN:
  613. vertex.rotateXZBy( 0); break;
  614. case D6D_YN:
  615. vertex.rotateYZBy(-90); break;
  616. case D6D_XN:
  617. vertex.rotateXZBy(-90); break;
  618. }
  619. }
  620. drawQuad(vertices, dir);
  621. }
  622. }
  623. void MapblockMeshGenerator::drawGlasslikeFramedNode()
  624. {
  625. TileSpec tiles[6];
  626. for (int face = 0; face < 6; face++)
  627. getTile(g_6dirs[face], &tiles[face]);
  628. TileSpec glass_tiles[6];
  629. if (tiles[1].layers[0].texture &&
  630. tiles[2].layers[0].texture &&
  631. tiles[3].layers[0].texture) {
  632. glass_tiles[0] = tiles[4];
  633. glass_tiles[1] = tiles[0];
  634. glass_tiles[2] = tiles[4];
  635. glass_tiles[3] = tiles[4];
  636. glass_tiles[4] = tiles[3];
  637. glass_tiles[5] = tiles[4];
  638. } else {
  639. for (auto &glass_tile : glass_tiles)
  640. glass_tile = tiles[4];
  641. }
  642. u8 param2 = n.getParam2();
  643. bool H_merge = !(param2 & 128);
  644. bool V_merge = !(param2 & 64);
  645. param2 &= 63;
  646. static const float a = BS / 2;
  647. static const float g = a - 0.003;
  648. static const float b = .876 * ( BS / 2 );
  649. static const aabb3f frame_edges[FRAMED_EDGE_COUNT] = {
  650. aabb3f( b, b, -a, a, a, a), // y+
  651. aabb3f(-a, b, -a, -b, a, a), // y+
  652. aabb3f( b, -a, -a, a, -b, a), // y-
  653. aabb3f(-a, -a, -a, -b, -b, a), // y-
  654. aabb3f( b, -a, b, a, a, a), // x+
  655. aabb3f( b, -a, -a, a, a, -b), // x+
  656. aabb3f(-a, -a, b, -b, a, a), // x-
  657. aabb3f(-a, -a, -a, -b, a, -b), // x-
  658. aabb3f(-a, b, b, a, a, a), // z+
  659. aabb3f(-a, -a, b, a, -b, a), // z+
  660. aabb3f(-a, -a, -a, a, -b, -b), // z-
  661. aabb3f(-a, b, -a, a, a, -b), // z-
  662. };
  663. static const aabb3f glass_faces[6] = {
  664. aabb3f(-g, -g, g, g, g, g), // z+
  665. aabb3f(-g, g, -g, g, g, g), // y+
  666. aabb3f( g, -g, -g, g, g, g), // x+
  667. aabb3f(-g, -g, -g, g, g, -g), // z-
  668. aabb3f(-g, -g, -g, g, -g, g), // y-
  669. aabb3f(-g, -g, -g, -g, g, g), // x-
  670. };
  671. // tables of neighbour (connect if same type and merge allowed),
  672. // checked with g_26dirs
  673. // 1 = connect, 0 = face visible
  674. bool nb[FRAMED_NEIGHBOR_COUNT] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  675. // 1 = check
  676. static const bool check_nb_vertical [FRAMED_NEIGHBOR_COUNT] = {0,1,0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
  677. static const bool check_nb_horizontal [FRAMED_NEIGHBOR_COUNT] = {1,0,1,1,0,1, 0,0,0,0, 1,1,1,1, 0,0,0,0};
  678. static const bool check_nb_all [FRAMED_NEIGHBOR_COUNT] = {1,1,1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1};
  679. const bool *check_nb = check_nb_all;
  680. // neighbours checks for frames visibility
  681. if (H_merge || V_merge) {
  682. if (!H_merge)
  683. check_nb = check_nb_vertical; // vertical-only merge
  684. if (!V_merge)
  685. check_nb = check_nb_horizontal; // horizontal-only merge
  686. content_t current = n.getContent();
  687. for (int i = 0; i < FRAMED_NEIGHBOR_COUNT; i++) {
  688. if (!check_nb[i])
  689. continue;
  690. v3s16 n2p = blockpos_nodes + p + g_26dirs[i];
  691. MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
  692. content_t n2c = n2.getContent();
  693. if (n2c == current)
  694. nb[i] = 1;
  695. }
  696. }
  697. // edge visibility
  698. static const u8 nb_triplet[FRAMED_EDGE_COUNT][3] = {
  699. {1, 2, 7}, {1, 5, 6}, {4, 2, 15}, {4, 5, 14},
  700. {2, 0, 11}, {2, 3, 13}, {5, 0, 10}, {5, 3, 12},
  701. {0, 1, 8}, {0, 4, 16}, {3, 4, 17}, {3, 1, 9},
  702. };
  703. tile = tiles[1];
  704. for (int edge = 0; edge < FRAMED_EDGE_COUNT; edge++) {
  705. bool edge_invisible;
  706. if (nb[nb_triplet[edge][2]])
  707. edge_invisible = nb[nb_triplet[edge][0]] & nb[nb_triplet[edge][1]];
  708. else
  709. edge_invisible = nb[nb_triplet[edge][0]] ^ nb[nb_triplet[edge][1]];
  710. if (edge_invisible)
  711. continue;
  712. drawAutoLightedCuboid(frame_edges[edge]);
  713. }
  714. for (int face = 0; face < 6; face++) {
  715. if (nb[face])
  716. continue;
  717. tile = glass_tiles[face];
  718. drawAutoLightedCuboid(glass_faces[face]);
  719. }
  720. // Optionally render internal liquid level defined by param2
  721. // Liquid is textured with 1 tile defined in nodedef 'special_tiles'
  722. if (param2 > 0 && f->param_type_2 == CPT2_GLASSLIKE_LIQUID_LEVEL &&
  723. f->special_tiles[0].layers[0].texture) {
  724. // Internal liquid level has param2 range 0 .. 63,
  725. // convert it to -0.5 .. 0.5
  726. float vlev = (param2 / 63.0) * 2.0 - 1.0;
  727. getSpecialTile(0, &tile);
  728. drawAutoLightedCuboid(aabb3f(-(nb[5] ? g : b),
  729. -(nb[4] ? g : b),
  730. -(nb[3] ? g : b),
  731. (nb[2] ? g : b),
  732. (nb[1] ? g : b) * vlev,
  733. (nb[0] ? g : b)));
  734. }
  735. }
  736. void MapblockMeshGenerator::drawAllfacesNode()
  737. {
  738. static const aabb3f box(-BS / 2, -BS / 2, -BS / 2, BS / 2, BS / 2, BS / 2);
  739. useTile(0, 0, 0);
  740. drawAutoLightedCuboid(box);
  741. }
  742. void MapblockMeshGenerator::drawTorchlikeNode()
  743. {
  744. u8 wall = n.getWallMounted(nodedef);
  745. u8 tileindex = 0;
  746. switch (wall) {
  747. case DWM_YP: tileindex = 1; break; // ceiling
  748. case DWM_YN: tileindex = 0; break; // floor
  749. default: tileindex = 2; // side (or invalid—should we care?)
  750. }
  751. useTile(tileindex, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  752. float size = BS / 2 * f->visual_scale;
  753. v3f vertices[4] = {
  754. v3f(-size, size, 0),
  755. v3f( size, size, 0),
  756. v3f( size, -size, 0),
  757. v3f(-size, -size, 0),
  758. };
  759. for (v3f &vertex : vertices) {
  760. switch (wall) {
  761. case DWM_YP:
  762. vertex.rotateXZBy(-45); break;
  763. case DWM_YN:
  764. vertex.rotateXZBy( 45); break;
  765. case DWM_XP:
  766. vertex.rotateXZBy( 0); break;
  767. case DWM_XN:
  768. vertex.rotateXZBy(180); break;
  769. case DWM_ZP:
  770. vertex.rotateXZBy( 90); break;
  771. case DWM_ZN:
  772. vertex.rotateXZBy(-90); break;
  773. }
  774. }
  775. drawQuad(vertices);
  776. }
  777. void MapblockMeshGenerator::drawSignlikeNode()
  778. {
  779. u8 wall = n.getWallMounted(nodedef);
  780. useTile(0, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  781. static const float offset = BS / 16;
  782. float size = BS / 2 * f->visual_scale;
  783. // Wall at X+ of node
  784. v3f vertices[4] = {
  785. v3f(BS / 2 - offset, size, size),
  786. v3f(BS / 2 - offset, size, -size),
  787. v3f(BS / 2 - offset, -size, -size),
  788. v3f(BS / 2 - offset, -size, size),
  789. };
  790. for (v3f &vertex : vertices) {
  791. switch (wall) {
  792. case DWM_YP:
  793. vertex.rotateXYBy( 90); break;
  794. case DWM_YN:
  795. vertex.rotateXYBy(-90); break;
  796. case DWM_XP:
  797. vertex.rotateXZBy( 0); break;
  798. case DWM_XN:
  799. vertex.rotateXZBy(180); break;
  800. case DWM_ZP:
  801. vertex.rotateXZBy( 90); break;
  802. case DWM_ZN:
  803. vertex.rotateXZBy(-90); break;
  804. }
  805. }
  806. drawQuad(vertices);
  807. }
  808. void MapblockMeshGenerator::drawPlantlikeQuad(float rotation, float quad_offset,
  809. bool offset_top_only)
  810. {
  811. v3f vertices[4] = {
  812. v3f(-scale, -BS / 2 + 2.0 * scale * plant_height, 0),
  813. v3f( scale, -BS / 2 + 2.0 * scale * plant_height, 0),
  814. v3f( scale, -BS / 2, 0),
  815. v3f(-scale, -BS / 2, 0),
  816. };
  817. if (random_offset_Y) {
  818. PseudoRandom yrng(face_num++ | p.X << 16 | p.Z << 8 | p.Y << 24);
  819. offset.Y = -BS * ((yrng.next() % 16 / 16.0) * 0.125);
  820. }
  821. int offset_count = offset_top_only ? 2 : 4;
  822. for (int i = 0; i < offset_count; i++)
  823. vertices[i].Z += quad_offset;
  824. for (v3f &vertex : vertices) {
  825. vertex.rotateXZBy(rotation + rotate_degree);
  826. vertex += offset;
  827. }
  828. drawQuad(vertices, v3s16(0, 0, 0), plant_height);
  829. }
  830. void MapblockMeshGenerator::drawPlantlike()
  831. {
  832. draw_style = PLANT_STYLE_CROSS;
  833. scale = BS / 2 * f->visual_scale;
  834. offset = v3f(0, 0, 0);
  835. rotate_degree = 0;
  836. random_offset_Y = false;
  837. face_num = 0;
  838. plant_height = 1.0;
  839. switch (f->param_type_2) {
  840. case CPT2_MESHOPTIONS:
  841. draw_style = PlantlikeStyle(n.param2 & MO_MASK_STYLE);
  842. if (n.param2 & MO_BIT_SCALE_SQRT2)
  843. scale *= 1.41421;
  844. if (n.param2 & MO_BIT_RANDOM_OFFSET) {
  845. PseudoRandom rng(p.X << 8 | p.Z | p.Y << 16);
  846. offset.X = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145);
  847. offset.Z = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145);
  848. }
  849. if (n.param2 & MO_BIT_RANDOM_OFFSET_Y)
  850. random_offset_Y = true;
  851. break;
  852. case CPT2_DEGROTATE:
  853. rotate_degree = n.param2 * 2;
  854. break;
  855. case CPT2_LEVELED:
  856. plant_height = n.param2 / 16.0;
  857. break;
  858. default:
  859. break;
  860. }
  861. switch (draw_style) {
  862. case PLANT_STYLE_CROSS:
  863. drawPlantlikeQuad(46);
  864. drawPlantlikeQuad(-44);
  865. break;
  866. case PLANT_STYLE_CROSS2:
  867. drawPlantlikeQuad(91);
  868. drawPlantlikeQuad(1);
  869. break;
  870. case PLANT_STYLE_STAR:
  871. drawPlantlikeQuad(121);
  872. drawPlantlikeQuad(241);
  873. drawPlantlikeQuad(1);
  874. break;
  875. case PLANT_STYLE_HASH:
  876. drawPlantlikeQuad( 1, BS / 4);
  877. drawPlantlikeQuad( 91, BS / 4);
  878. drawPlantlikeQuad(181, BS / 4);
  879. drawPlantlikeQuad(271, BS / 4);
  880. break;
  881. case PLANT_STYLE_HASH2:
  882. drawPlantlikeQuad( 1, -BS / 2, true);
  883. drawPlantlikeQuad( 91, -BS / 2, true);
  884. drawPlantlikeQuad(181, -BS / 2, true);
  885. drawPlantlikeQuad(271, -BS / 2, true);
  886. break;
  887. }
  888. }
  889. void MapblockMeshGenerator::drawPlantlikeNode()
  890. {
  891. useTile();
  892. drawPlantlike();
  893. }
  894. void MapblockMeshGenerator::drawPlantlikeRootedNode()
  895. {
  896. useTile(0, MATERIAL_FLAG_CRACK_OVERLAY, 0, true);
  897. origin += v3f(0.0, BS, 0.0);
  898. p.Y++;
  899. if (data->m_smooth_lighting) {
  900. getSmoothLightFrame();
  901. } else {
  902. MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
  903. light = LightPair(getInteriorLight(ntop, 1, nodedef));
  904. }
  905. drawPlantlike();
  906. p.Y--;
  907. }
  908. void MapblockMeshGenerator::drawFirelikeQuad(float rotation, float opening_angle,
  909. float offset_h, float offset_v)
  910. {
  911. v3f vertices[4] = {
  912. v3f(-scale, -BS / 2 + scale * 2, 0),
  913. v3f( scale, -BS / 2 + scale * 2, 0),
  914. v3f( scale, -BS / 2, 0),
  915. v3f(-scale, -BS / 2, 0),
  916. };
  917. for (v3f &vertex : vertices) {
  918. vertex.rotateYZBy(opening_angle);
  919. vertex.Z += offset_h;
  920. vertex.rotateXZBy(rotation);
  921. vertex.Y += offset_v;
  922. }
  923. drawQuad(vertices);
  924. }
  925. void MapblockMeshGenerator::drawFirelikeNode()
  926. {
  927. useTile();
  928. scale = BS / 2 * f->visual_scale;
  929. // Check for adjacent nodes
  930. bool neighbors = false;
  931. bool neighbor[6] = {0, 0, 0, 0, 0, 0};
  932. content_t current = n.getContent();
  933. for (int i = 0; i < 6; i++) {
  934. v3s16 n2p = blockpos_nodes + p + g_6dirs[i];
  935. MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
  936. content_t n2c = n2.getContent();
  937. if (n2c != CONTENT_IGNORE && n2c != CONTENT_AIR && n2c != current) {
  938. neighbor[i] = true;
  939. neighbors = true;
  940. }
  941. }
  942. bool drawBasicFire = neighbor[D6D_YN] || !neighbors;
  943. bool drawBottomFire = neighbor[D6D_YP];
  944. if (drawBasicFire || neighbor[D6D_ZP])
  945. drawFirelikeQuad(0, -10, 0.4 * BS);
  946. else if (drawBottomFire)
  947. drawFirelikeQuad(0, 70, 0.47 * BS, 0.484 * BS);
  948. if (drawBasicFire || neighbor[D6D_XN])
  949. drawFirelikeQuad(90, -10, 0.4 * BS);
  950. else if (drawBottomFire)
  951. drawFirelikeQuad(90, 70, 0.47 * BS, 0.484 * BS);
  952. if (drawBasicFire || neighbor[D6D_ZN])
  953. drawFirelikeQuad(180, -10, 0.4 * BS);
  954. else if (drawBottomFire)
  955. drawFirelikeQuad(180, 70, 0.47 * BS, 0.484 * BS);
  956. if (drawBasicFire || neighbor[D6D_XP])
  957. drawFirelikeQuad(270, -10, 0.4 * BS);
  958. else if (drawBottomFire)
  959. drawFirelikeQuad(270, 70, 0.47 * BS, 0.484 * BS);
  960. if (drawBasicFire) {
  961. drawFirelikeQuad(45, 0, 0.0);
  962. drawFirelikeQuad(-45, 0, 0.0);
  963. }
  964. }
  965. void MapblockMeshGenerator::drawFencelikeNode()
  966. {
  967. useTile(0, 0, 0);
  968. TileSpec tile_nocrack = tile;
  969. for (auto &layer : tile_nocrack.layers)
  970. layer.material_flags &= ~MATERIAL_FLAG_CRACK;
  971. // Put wood the right way around in the posts
  972. TileSpec tile_rot = tile;
  973. tile_rot.rotation = 1;
  974. static const f32 post_rad = BS / 8;
  975. static const f32 bar_rad = BS / 16;
  976. static const f32 bar_len = BS / 2 - post_rad;
  977. // The post - always present
  978. static const aabb3f post(-post_rad, -BS / 2, -post_rad,
  979. post_rad, BS / 2, post_rad);
  980. static const f32 postuv[24] = {
  981. 0.375, 0.375, 0.625, 0.625,
  982. 0.375, 0.375, 0.625, 0.625,
  983. 0.000, 0.000, 0.250, 1.000,
  984. 0.250, 0.000, 0.500, 1.000,
  985. 0.500, 0.000, 0.750, 1.000,
  986. 0.750, 0.000, 1.000, 1.000,
  987. };
  988. tile = tile_rot;
  989. drawAutoLightedCuboid(post, postuv);
  990. tile = tile_nocrack;
  991. // Now a section of fence, +X, if there's a post there
  992. v3s16 p2 = p;
  993. p2.X++;
  994. MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  995. const ContentFeatures *f2 = &nodedef->get(n2);
  996. if (f2->drawtype == NDT_FENCELIKE) {
  997. static const aabb3f bar_x1(BS / 2 - bar_len, BS / 4 - bar_rad, -bar_rad,
  998. BS / 2 + bar_len, BS / 4 + bar_rad, bar_rad);
  999. static const aabb3f bar_x2(BS / 2 - bar_len, -BS / 4 - bar_rad, -bar_rad,
  1000. BS / 2 + bar_len, -BS / 4 + bar_rad, bar_rad);
  1001. static const f32 xrailuv[24] = {
  1002. 0.000, 0.125, 1.000, 0.250,
  1003. 0.000, 0.250, 1.000, 0.375,
  1004. 0.375, 0.375, 0.500, 0.500,
  1005. 0.625, 0.625, 0.750, 0.750,
  1006. 0.000, 0.500, 1.000, 0.625,
  1007. 0.000, 0.875, 1.000, 1.000,
  1008. };
  1009. drawAutoLightedCuboid(bar_x1, xrailuv);
  1010. drawAutoLightedCuboid(bar_x2, xrailuv);
  1011. }
  1012. // Now a section of fence, +Z, if there's a post there
  1013. p2 = p;
  1014. p2.Z++;
  1015. n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  1016. f2 = &nodedef->get(n2);
  1017. if (f2->drawtype == NDT_FENCELIKE) {
  1018. static const aabb3f bar_z1(-bar_rad, BS / 4 - bar_rad, BS / 2 - bar_len,
  1019. bar_rad, BS / 4 + bar_rad, BS / 2 + bar_len);
  1020. static const aabb3f bar_z2(-bar_rad, -BS / 4 - bar_rad, BS / 2 - bar_len,
  1021. bar_rad, -BS / 4 + bar_rad, BS / 2 + bar_len);
  1022. static const f32 zrailuv[24] = {
  1023. 0.1875, 0.0625, 0.3125, 0.3125, // cannot rotate; stretch
  1024. 0.2500, 0.0625, 0.3750, 0.3125, // for wood texture instead
  1025. 0.0000, 0.5625, 1.0000, 0.6875,
  1026. 0.0000, 0.3750, 1.0000, 0.5000,
  1027. 0.3750, 0.3750, 0.5000, 0.5000,
  1028. 0.6250, 0.6250, 0.7500, 0.7500,
  1029. };
  1030. drawAutoLightedCuboid(bar_z1, zrailuv);
  1031. drawAutoLightedCuboid(bar_z2, zrailuv);
  1032. }
  1033. }
  1034. bool MapblockMeshGenerator::isSameRail(v3s16 dir)
  1035. {
  1036. MapNode node2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
  1037. if (node2.getContent() == n.getContent())
  1038. return true;
  1039. const ContentFeatures &def2 = nodedef->get(node2);
  1040. return ((def2.drawtype == NDT_RAILLIKE) &&
  1041. (def2.getGroup(raillike_groupname) == raillike_group));
  1042. }
  1043. void MapblockMeshGenerator::drawRaillikeNode()
  1044. {
  1045. static const v3s16 direction[4] = {
  1046. v3s16( 0, 0, 1),
  1047. v3s16( 0, 0, -1),
  1048. v3s16(-1, 0, 0),
  1049. v3s16( 1, 0, 0),
  1050. };
  1051. static const int slope_angle[4] = {0, 180, 90, -90};
  1052. enum RailTile {
  1053. straight,
  1054. curved,
  1055. junction,
  1056. cross,
  1057. };
  1058. struct RailDesc {
  1059. int tile_index;
  1060. int angle;
  1061. };
  1062. static const RailDesc rail_kinds[16] = {
  1063. // +x -x -z +z
  1064. //-------------
  1065. {straight, 0}, // . . . .
  1066. {straight, 0}, // . . . +Z
  1067. {straight, 0}, // . . -Z .
  1068. {straight, 0}, // . . -Z +Z
  1069. {straight, 90}, // . -X . .
  1070. { curved, 180}, // . -X . +Z
  1071. { curved, 270}, // . -X -Z .
  1072. {junction, 180}, // . -X -Z +Z
  1073. {straight, 90}, // +X . . .
  1074. { curved, 90}, // +X . . +Z
  1075. { curved, 0}, // +X . -Z .
  1076. {junction, 0}, // +X . -Z +Z
  1077. {straight, 90}, // +X -X . .
  1078. {junction, 90}, // +X -X . +Z
  1079. {junction, 270}, // +X -X -Z .
  1080. { cross, 0}, // +X -X -Z +Z
  1081. };
  1082. raillike_group = nodedef->get(n).getGroup(raillike_groupname);
  1083. int code = 0;
  1084. int angle;
  1085. int tile_index;
  1086. bool sloped = false;
  1087. for (int dir = 0; dir < 4; dir++) {
  1088. bool rail_above = isSameRail(direction[dir] + v3s16(0, 1, 0));
  1089. if (rail_above) {
  1090. sloped = true;
  1091. angle = slope_angle[dir];
  1092. }
  1093. if (rail_above ||
  1094. isSameRail(direction[dir]) ||
  1095. isSameRail(direction[dir] + v3s16(0, -1, 0)))
  1096. code |= 1 << dir;
  1097. }
  1098. if (sloped) {
  1099. tile_index = straight;
  1100. } else {
  1101. tile_index = rail_kinds[code].tile_index;
  1102. angle = rail_kinds[code].angle;
  1103. }
  1104. useTile(tile_index, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  1105. static const float offset = BS / 64;
  1106. static const float size = BS / 2;
  1107. float y2 = sloped ? size : -size;
  1108. v3f vertices[4] = {
  1109. v3f(-size, y2 + offset, size),
  1110. v3f( size, y2 + offset, size),
  1111. v3f( size, -size + offset, -size),
  1112. v3f(-size, -size + offset, -size),
  1113. };
  1114. if (angle)
  1115. for (v3f &vertex : vertices)
  1116. vertex.rotateXZBy(angle);
  1117. drawQuad(vertices);
  1118. }
  1119. void MapblockMeshGenerator::drawNodeboxNode()
  1120. {
  1121. static const v3s16 tile_dirs[6] = {
  1122. v3s16(0, 1, 0),
  1123. v3s16(0, -1, 0),
  1124. v3s16(1, 0, 0),
  1125. v3s16(-1, 0, 0),
  1126. v3s16(0, 0, 1),
  1127. v3s16(0, 0, -1)
  1128. };
  1129. // we have this order for some reason...
  1130. static const v3s16 connection_dirs[6] = {
  1131. v3s16( 0, 1, 0), // top
  1132. v3s16( 0, -1, 0), // bottom
  1133. v3s16( 0, 0, -1), // front
  1134. v3s16(-1, 0, 0), // left
  1135. v3s16( 0, 0, 1), // back
  1136. v3s16( 1, 0, 0), // right
  1137. };
  1138. TileSpec tiles[6];
  1139. for (int face = 0; face < 6; face++) {
  1140. // Handles facedir rotation for textures
  1141. getTile(tile_dirs[face], &tiles[face]);
  1142. }
  1143. // locate possible neighboring nodes to connect to
  1144. int neighbors_set = 0;
  1145. if (f->node_box.type == NODEBOX_CONNECTED) {
  1146. for (int dir = 0; dir != 6; dir++) {
  1147. int flag = 1 << dir;
  1148. v3s16 p2 = blockpos_nodes + p + connection_dirs[dir];
  1149. MapNode n2 = data->m_vmanip.getNodeNoEx(p2);
  1150. if (nodedef->nodeboxConnects(n, n2, flag))
  1151. neighbors_set |= flag;
  1152. }
  1153. }
  1154. std::vector<aabb3f> boxes;
  1155. n.getNodeBoxes(nodedef, &boxes, neighbors_set);
  1156. for (const auto &box : boxes)
  1157. drawAutoLightedCuboid(box, nullptr, tiles, 6);
  1158. }
  1159. void MapblockMeshGenerator::drawMeshNode()
  1160. {
  1161. u8 facedir = 0;
  1162. scene::IMesh* mesh;
  1163. bool private_mesh; // as a grab/drop pair is not thread-safe
  1164. if (f->param_type_2 == CPT2_FACEDIR ||
  1165. f->param_type_2 == CPT2_COLORED_FACEDIR) {
  1166. facedir = n.getFaceDir(nodedef);
  1167. } else if (f->param_type_2 == CPT2_WALLMOUNTED ||
  1168. f->param_type_2 == CPT2_COLORED_WALLMOUNTED) {
  1169. // Convert wallmounted to 6dfacedir.
  1170. // When cache enabled, it is already converted.
  1171. facedir = n.getWallMounted(nodedef);
  1172. if (!enable_mesh_cache)
  1173. facedir = wallmounted_to_facedir[facedir];
  1174. }
  1175. if (!data->m_smooth_lighting && f->mesh_ptr[facedir]) {
  1176. // use cached meshes
  1177. private_mesh = false;
  1178. mesh = f->mesh_ptr[facedir];
  1179. } else if (f->mesh_ptr[0]) {
  1180. // no cache, clone and rotate mesh
  1181. private_mesh = true;
  1182. mesh = cloneMesh(f->mesh_ptr[0]);
  1183. rotateMeshBy6dFacedir(mesh, facedir);
  1184. recalculateBoundingBox(mesh);
  1185. meshmanip->recalculateNormals(mesh, true, false);
  1186. } else
  1187. return;
  1188. int mesh_buffer_count = mesh->getMeshBufferCount();
  1189. for (int j = 0; j < mesh_buffer_count; j++) {
  1190. useTile(j);
  1191. scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
  1192. video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();
  1193. int vertex_count = buf->getVertexCount();
  1194. if (data->m_smooth_lighting) {
  1195. // Mesh is always private here. So the lighting is applied to each
  1196. // vertex right here.
  1197. for (int k = 0; k < vertex_count; k++) {
  1198. video::S3DVertex &vertex = vertices[k];
  1199. vertex.Color = blendLightColor(vertex.Pos, vertex.Normal);
  1200. vertex.Pos += origin;
  1201. }
  1202. collector->append(tile, vertices, vertex_count,
  1203. buf->getIndices(), buf->getIndexCount());
  1204. } else {
  1205. // Don't modify the mesh, it may not be private here.
  1206. // Instead, let the collector process colors, etc.
  1207. collector->append(tile, vertices, vertex_count,
  1208. buf->getIndices(), buf->getIndexCount(), origin,
  1209. color, f->light_source);
  1210. }
  1211. }
  1212. if (private_mesh)
  1213. mesh->drop();
  1214. }
  1215. // also called when the drawtype is known but should have been pre-converted
  1216. void MapblockMeshGenerator::errorUnknownDrawtype()
  1217. {
  1218. infostream << "Got drawtype " << f->drawtype << std::endl;
  1219. FATAL_ERROR("Unknown drawtype");
  1220. }
  1221. void MapblockMeshGenerator::drawNode()
  1222. {
  1223. // skip some drawtypes early
  1224. switch (f->drawtype) {
  1225. case NDT_NORMAL: // Drawn by MapBlockMesh
  1226. case NDT_AIRLIKE: // Not drawn at all
  1227. case NDT_LIQUID: // Drawn by MapBlockMesh
  1228. return;
  1229. default:
  1230. break;
  1231. }
  1232. origin = intToFloat(p, BS);
  1233. if (data->m_smooth_lighting)
  1234. getSmoothLightFrame();
  1235. else
  1236. light = LightPair(getInteriorLight(n, 1, nodedef));
  1237. switch (f->drawtype) {
  1238. case NDT_FLOWINGLIQUID: drawLiquidNode(); break;
  1239. case NDT_GLASSLIKE: drawGlasslikeNode(); break;
  1240. case NDT_GLASSLIKE_FRAMED: drawGlasslikeFramedNode(); break;
  1241. case NDT_ALLFACES: drawAllfacesNode(); break;
  1242. case NDT_TORCHLIKE: drawTorchlikeNode(); break;
  1243. case NDT_SIGNLIKE: drawSignlikeNode(); break;
  1244. case NDT_PLANTLIKE: drawPlantlikeNode(); break;
  1245. case NDT_PLANTLIKE_ROOTED: drawPlantlikeRootedNode(); break;
  1246. case NDT_FIRELIKE: drawFirelikeNode(); break;
  1247. case NDT_FENCELIKE: drawFencelikeNode(); break;
  1248. case NDT_RAILLIKE: drawRaillikeNode(); break;
  1249. case NDT_NODEBOX: drawNodeboxNode(); break;
  1250. case NDT_MESH: drawMeshNode(); break;
  1251. default: errorUnknownDrawtype(); break;
  1252. }
  1253. }
  1254. /*
  1255. TODO: Fix alpha blending for special nodes
  1256. Currently only the last element rendered is blended correct
  1257. */
  1258. void MapblockMeshGenerator::generate()
  1259. {
  1260. for (p.Z = 0; p.Z < MAP_BLOCKSIZE; p.Z++)
  1261. for (p.Y = 0; p.Y < MAP_BLOCKSIZE; p.Y++)
  1262. for (p.X = 0; p.X < MAP_BLOCKSIZE; p.X++) {
  1263. n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
  1264. f = &nodedef->get(n);
  1265. drawNode();
  1266. }
  1267. }
  1268. void MapblockMeshGenerator::renderSingle(content_t node)
  1269. {
  1270. p = {0, 0, 0};
  1271. n = MapNode(node, 0xff, 0x00);
  1272. f = &nodedef->get(n);
  1273. drawNode();
  1274. }