content_mapblock.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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 <cmath>
  17. #include "content_mapblock.h"
  18. #include "util/numeric.h"
  19. #include "util/directiontables.h"
  20. #include "mapblock_mesh.h"
  21. #include "settings.h"
  22. #include "nodedef.h"
  23. #include "client/tile.h"
  24. #include "mesh.h"
  25. #include <IMeshManipulator.h>
  26. #include "client/meshgen/collector.h"
  27. #include "client/renderingengine.h"
  28. #include "client.h"
  29. #include "noise.h"
  30. // Distance of light extrapolation (for oversized nodes)
  31. // After this distance, it gives up and considers light level constant
  32. #define SMOOTH_LIGHTING_OVERSIZE 1.0
  33. // Node edge count (for glasslike-framed)
  34. #define FRAMED_EDGE_COUNT 12
  35. // Node neighbor count, including edge-connected, but not vertex-connected
  36. // (for glasslike-framed)
  37. // Corresponding offsets are listed in g_27dirs
  38. #define FRAMED_NEIGHBOR_COUNT 18
  39. // Maps light index to corner direction
  40. static const v3s16 light_dirs[8] = {
  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. v3s16( 1, 1, -1),
  48. v3s16( 1, 1, 1),
  49. };
  50. // Maps cuboid face and vertex indices to the corresponding light index
  51. static const u8 light_indices[6][4] = {
  52. {3, 7, 6, 2},
  53. {0, 4, 5, 1},
  54. {6, 7, 5, 4},
  55. {3, 2, 0, 1},
  56. {7, 3, 1, 5},
  57. {2, 6, 4, 0},
  58. };
  59. // Standard index set to make a quad on 4 vertices
  60. static constexpr u16 quad_indices_02[] = {0, 1, 2, 2, 3, 0};
  61. static constexpr u16 quad_indices_13[] = {0, 1, 3, 3, 1, 2};
  62. static const auto &quad_indices = quad_indices_02;
  63. const std::string MapblockMeshGenerator::raillike_groupname = "connect_to_raillike";
  64. MapblockMeshGenerator::MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output,
  65. scene::IMeshManipulator *mm):
  66. data(input),
  67. collector(output),
  68. nodedef(data->nodedef),
  69. meshmanip(mm),
  70. blockpos_nodes(data->m_blockpos * MAP_BLOCKSIZE),
  71. enable_mesh_cache(g_settings->getBool("enable_mesh_cache") &&
  72. !data->m_smooth_lighting) // Mesh cache is not supported with smooth lighting
  73. {
  74. }
  75. void MapblockMeshGenerator::useTile(int index, u8 set_flags, u8 reset_flags, bool special)
  76. {
  77. if (special)
  78. getSpecialTile(index, &cur_node.tile, cur_node.p == data->m_crack_pos_relative);
  79. else
  80. getTile(index, &cur_node.tile);
  81. if (!data->m_smooth_lighting)
  82. cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);
  83. for (auto &layer : cur_node.tile.layers) {
  84. layer.material_flags |= set_flags;
  85. layer.material_flags &= ~reset_flags;
  86. }
  87. }
  88. // Returns a tile, ready for use, non-rotated.
  89. void MapblockMeshGenerator::getTile(int index, TileSpec *tile)
  90. {
  91. getNodeTileN(cur_node.n, cur_node.p, index, data, *tile);
  92. }
  93. // Returns a tile, ready for use, rotated according to the node facedir.
  94. void MapblockMeshGenerator::getTile(v3s16 direction, TileSpec *tile)
  95. {
  96. getNodeTile(cur_node.n, cur_node.p, direction, data, *tile);
  97. }
  98. // Returns a special tile, ready for use, non-rotated.
  99. void MapblockMeshGenerator::getSpecialTile(int index, TileSpec *tile, bool apply_crack)
  100. {
  101. *tile = cur_node.f->special_tiles[index];
  102. TileLayer *top_layer = nullptr;
  103. for (auto &layernum : tile->layers) {
  104. TileLayer *layer = &layernum;
  105. if (layer->texture_id == 0)
  106. continue;
  107. top_layer = layer;
  108. if (!layer->has_color)
  109. cur_node.n.getColor(*cur_node.f, &layer->color);
  110. }
  111. if (apply_crack)
  112. top_layer->material_flags |= MATERIAL_FLAG_CRACK;
  113. }
  114. void MapblockMeshGenerator::drawQuad(v3f *coords, const v3s16 &normal,
  115. float vertical_tiling)
  116. {
  117. const v2f tcoords[4] = {v2f(0.0, 0.0), v2f(1.0, 0.0),
  118. v2f(1.0, vertical_tiling), v2f(0.0, vertical_tiling)};
  119. video::S3DVertex vertices[4];
  120. bool shade_face = !cur_node.f->light_source && (normal != v3s16(0, 0, 0));
  121. v3f normal2(normal.X, normal.Y, normal.Z);
  122. for (int j = 0; j < 4; j++) {
  123. vertices[j].Pos = coords[j] + cur_node.origin;
  124. vertices[j].Normal = normal2;
  125. if (data->m_smooth_lighting)
  126. vertices[j].Color = blendLightColor(coords[j]);
  127. else
  128. vertices[j].Color = cur_node.color;
  129. if (shade_face)
  130. applyFacesShading(vertices[j].Color, normal2);
  131. vertices[j].TCoords = tcoords[j];
  132. }
  133. collector->append(cur_node.tile, vertices, 4, quad_indices, 6);
  134. }
  135. static std::array<video::S3DVertex, 24> setupCuboidVertices(const aabb3f &box, const f32 *txc, TileSpec *tiles, int tilecount) {
  136. v3f min = box.MinEdge;
  137. v3f max = box.MaxEdge;
  138. std::array<video::S3DVertex, 24> vertices = {{
  139. // top
  140. video::S3DVertex(min.X, max.Y, max.Z, 0, 1, 0, {}, txc[0], txc[1]),
  141. video::S3DVertex(max.X, max.Y, max.Z, 0, 1, 0, {}, txc[2], txc[1]),
  142. video::S3DVertex(max.X, max.Y, min.Z, 0, 1, 0, {}, txc[2], txc[3]),
  143. video::S3DVertex(min.X, max.Y, min.Z, 0, 1, 0, {}, txc[0], txc[3]),
  144. // bottom
  145. video::S3DVertex(min.X, min.Y, min.Z, 0, -1, 0, {}, txc[4], txc[5]),
  146. video::S3DVertex(max.X, min.Y, min.Z, 0, -1, 0, {}, txc[6], txc[5]),
  147. video::S3DVertex(max.X, min.Y, max.Z, 0, -1, 0, {}, txc[6], txc[7]),
  148. video::S3DVertex(min.X, min.Y, max.Z, 0, -1, 0, {}, txc[4], txc[7]),
  149. // right
  150. video::S3DVertex(max.X, max.Y, min.Z, 1, 0, 0, {}, txc[ 8], txc[9]),
  151. video::S3DVertex(max.X, max.Y, max.Z, 1, 0, 0, {}, txc[10], txc[9]),
  152. video::S3DVertex(max.X, min.Y, max.Z, 1, 0, 0, {}, txc[10], txc[11]),
  153. video::S3DVertex(max.X, min.Y, min.Z, 1, 0, 0, {}, txc[ 8], txc[11]),
  154. // left
  155. video::S3DVertex(min.X, max.Y, max.Z, -1, 0, 0, {}, txc[12], txc[13]),
  156. video::S3DVertex(min.X, max.Y, min.Z, -1, 0, 0, {}, txc[14], txc[13]),
  157. video::S3DVertex(min.X, min.Y, min.Z, -1, 0, 0, {}, txc[14], txc[15]),
  158. video::S3DVertex(min.X, min.Y, max.Z, -1, 0, 0, {}, txc[12], txc[15]),
  159. // back
  160. video::S3DVertex(max.X, max.Y, max.Z, 0, 0, 1, {}, txc[16], txc[17]),
  161. video::S3DVertex(min.X, max.Y, max.Z, 0, 0, 1, {}, txc[18], txc[17]),
  162. video::S3DVertex(min.X, min.Y, max.Z, 0, 0, 1, {}, txc[18], txc[19]),
  163. video::S3DVertex(max.X, min.Y, max.Z, 0, 0, 1, {}, txc[16], txc[19]),
  164. // front
  165. video::S3DVertex(min.X, max.Y, min.Z, 0, 0, -1, {}, txc[20], txc[21]),
  166. video::S3DVertex(max.X, max.Y, min.Z, 0, 0, -1, {}, txc[22], txc[21]),
  167. video::S3DVertex(max.X, min.Y, min.Z, 0, 0, -1, {}, txc[22], txc[23]),
  168. video::S3DVertex(min.X, min.Y, min.Z, 0, 0, -1, {}, txc[20], txc[23]),
  169. }};
  170. for (int face = 0; face < 6; face++) {
  171. int tileindex = MYMIN(face, tilecount - 1);
  172. const TileSpec &tile = tiles[tileindex];
  173. for (int j = 0; j < 4; j++) {
  174. video::S3DVertex &vertex = vertices[face * 4 + j];
  175. v2f &tcoords = vertex.TCoords;
  176. switch (tile.rotation) {
  177. case TileRotation::None:
  178. break;
  179. case TileRotation::R90:
  180. tcoords.set(-tcoords.Y, tcoords.X);
  181. break;
  182. case TileRotation::R180:
  183. tcoords.set(-tcoords.X, -tcoords.Y);
  184. break;
  185. case TileRotation::R270:
  186. tcoords.set(tcoords.Y, -tcoords.X);
  187. break;
  188. }
  189. }
  190. }
  191. return vertices;
  192. }
  193. enum class QuadDiagonal {
  194. Diag02,
  195. Diag13,
  196. };
  197. // Create a cuboid with custom lighting.
  198. // tiles - the tiles (materials) to use (for all 6 faces)
  199. // tilecount - number of entries in tiles, 1<=tilecount<=6
  200. // txc - texture coordinates - this is a list of texture coordinates
  201. // for the opposite corners of each face - therefore, there
  202. // should be (2+2)*6=24 values in the list. The order of
  203. // the faces in the list is up-down-right-left-back-front
  204. // (compatible with ContentFeatures).
  205. // mask - a bit mask that suppresses drawing of tiles.
  206. // tile i will not be drawn if mask & (1 << i) is 1
  207. // face_lighter(int face, video::S3DVertex vertices[4]) -> QuadDiagonal -
  208. // a callback that will be called for each face drawn to setup vertex colors,
  209. // and to choose diagonal to split the quad at.
  210. template <typename Fn>
  211. void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
  212. TileSpec *tiles, int tilecount, const f32 *txc, u8 mask, Fn &&face_lighter)
  213. {
  214. assert(tilecount >= 1 && tilecount <= 6); // pre-condition
  215. auto vertices = setupCuboidVertices(box, txc, tiles, tilecount);
  216. for (int k = 0; k < 6; ++k) {
  217. if (mask & (1 << k))
  218. continue;
  219. QuadDiagonal diagonal = face_lighter(k, &vertices[4 * k]);
  220. const u16 *indices = diagonal == QuadDiagonal::Diag13 ? quad_indices_13 : quad_indices_02;
  221. int tileindex = MYMIN(k, tilecount - 1);
  222. collector->append(tiles[tileindex], &vertices[4 * k], 4, indices, 6);
  223. }
  224. }
  225. // Gets the base lighting values for a node
  226. void MapblockMeshGenerator::getSmoothLightFrame()
  227. {
  228. for (int k = 0; k < 8; ++k)
  229. cur_node.frame.sunlight[k] = false;
  230. for (int k = 0; k < 8; ++k) {
  231. LightPair light(getSmoothLightTransparent(blockpos_nodes + cur_node.p, light_dirs[k], data));
  232. cur_node.frame.lightsDay[k] = light.lightDay;
  233. cur_node.frame.lightsNight[k] = light.lightNight;
  234. // If there is direct sunlight and no ambient occlusion at some corner,
  235. // mark the vertical edge (top and bottom corners) containing it.
  236. if (light.lightDay == 255) {
  237. cur_node.frame.sunlight[k] = true;
  238. cur_node.frame.sunlight[k ^ 2] = true;
  239. }
  240. }
  241. }
  242. // Calculates vertex light level
  243. // vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
  244. LightInfo MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
  245. {
  246. // Light levels at (logical) node corners are known. Here,
  247. // trilinear interpolation is used to calculate light level
  248. // at a given point in the node.
  249. f32 x = core::clamp(vertex_pos.X / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  250. f32 y = core::clamp(vertex_pos.Y / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  251. f32 z = core::clamp(vertex_pos.Z / BS + 0.5, 0.0 - SMOOTH_LIGHTING_OVERSIZE, 1.0 + SMOOTH_LIGHTING_OVERSIZE);
  252. f32 lightDay = 0.0; // daylight
  253. f32 lightNight = 0.0;
  254. f32 lightBoosted = 0.0; // daylight + direct sunlight, if any
  255. for (int k = 0; k < 8; ++k) {
  256. f32 dx = (k & 4) ? x : 1 - x;
  257. f32 dy = (k & 2) ? y : 1 - y;
  258. f32 dz = (k & 1) ? z : 1 - z;
  259. // Use direct sunlight (255), if any; use daylight otherwise.
  260. f32 light_boosted = cur_node.frame.sunlight[k] ? 255 : cur_node.frame.lightsDay[k];
  261. lightDay += dx * dy * dz * cur_node.frame.lightsDay[k];
  262. lightNight += dx * dy * dz * cur_node.frame.lightsNight[k];
  263. lightBoosted += dx * dy * dz * light_boosted;
  264. }
  265. return LightInfo{lightDay, lightNight, lightBoosted};
  266. }
  267. // Calculates vertex color to be used in mapblock mesh
  268. // vertex_pos - vertex position in the node (coordinates are clamped to [0.0, 1.0] or so)
  269. // tile_color - node's tile color
  270. video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos)
  271. {
  272. LightInfo light = blendLight(vertex_pos);
  273. return encode_light(light.getPair(), cur_node.f->light_source);
  274. }
  275. video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos,
  276. const v3f &vertex_normal)
  277. {
  278. LightInfo light = blendLight(vertex_pos);
  279. video::SColor color = encode_light(light.getPair(MYMAX(0.0f, vertex_normal.Y)),
  280. cur_node.f->light_source);
  281. if (!cur_node.f->light_source)
  282. applyFacesShading(color, vertex_normal);
  283. return color;
  284. }
  285. void MapblockMeshGenerator::generateCuboidTextureCoords(const aabb3f &box, f32 *coords)
  286. {
  287. f32 tx1 = (box.MinEdge.X / BS) + 0.5;
  288. f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
  289. f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
  290. f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
  291. f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
  292. f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
  293. f32 txc[24] = {
  294. tx1, 1 - tz2, tx2, 1 - tz1, // up
  295. tx1, tz1, tx2, tz2, // down
  296. tz1, 1 - ty2, tz2, 1 - ty1, // right
  297. 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1, // left
  298. 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1, // back
  299. tx1, 1 - ty2, tx2, 1 - ty1, // front
  300. };
  301. for (int i = 0; i != 24; ++i)
  302. coords[i] = txc[i];
  303. }
  304. static inline int lightDiff(LightPair a, LightPair b)
  305. {
  306. return abs(a.lightDay - b.lightDay) + abs(a.lightNight - b.lightNight);
  307. }
  308. void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
  309. TileSpec *tiles, int tile_count, u8 mask)
  310. {
  311. bool scale = std::fabs(cur_node.f->visual_scale - 1.0f) > 1e-3f;
  312. f32 texture_coord_buf[24];
  313. f32 dx1 = box.MinEdge.X;
  314. f32 dy1 = box.MinEdge.Y;
  315. f32 dz1 = box.MinEdge.Z;
  316. f32 dx2 = box.MaxEdge.X;
  317. f32 dy2 = box.MaxEdge.Y;
  318. f32 dz2 = box.MaxEdge.Z;
  319. if (scale) {
  320. if (!txc) { // generate texture coords before scaling
  321. generateCuboidTextureCoords(box, texture_coord_buf);
  322. txc = texture_coord_buf;
  323. }
  324. box.MinEdge *= cur_node.f->visual_scale;
  325. box.MaxEdge *= cur_node.f->visual_scale;
  326. }
  327. box.MinEdge += cur_node.origin;
  328. box.MaxEdge += cur_node.origin;
  329. if (!txc) {
  330. generateCuboidTextureCoords(box, texture_coord_buf);
  331. txc = texture_coord_buf;
  332. }
  333. if (!tiles) {
  334. tiles = &cur_node.tile;
  335. tile_count = 1;
  336. }
  337. if (data->m_smooth_lighting) {
  338. LightInfo lights[8];
  339. for (int j = 0; j < 8; ++j) {
  340. v3f d;
  341. d.X = (j & 4) ? dx2 : dx1;
  342. d.Y = (j & 2) ? dy2 : dy1;
  343. d.Z = (j & 1) ? dz2 : dz1;
  344. lights[j] = blendLight(d);
  345. }
  346. drawCuboid(box, tiles, tile_count, txc, mask, [&] (int face, video::S3DVertex vertices[4]) {
  347. LightPair final_lights[4];
  348. for (int j = 0; j < 4; j++) {
  349. video::S3DVertex &vertex = vertices[j];
  350. final_lights[j] = lights[light_indices[face][j]].getPair(MYMAX(0.0f, vertex.Normal.Y));
  351. vertex.Color = encode_light(final_lights[j], cur_node.f->light_source);
  352. if (!cur_node.f->light_source)
  353. applyFacesShading(vertex.Color, vertex.Normal);
  354. }
  355. if (lightDiff(final_lights[1], final_lights[3]) < lightDiff(final_lights[0], final_lights[2]))
  356. return QuadDiagonal::Diag13;
  357. return QuadDiagonal::Diag02;
  358. });
  359. } else {
  360. drawCuboid(box, tiles, tile_count, txc, mask, [&] (int face, video::S3DVertex vertices[4]) {
  361. video::SColor color = encode_light(cur_node.light, cur_node.f->light_source);
  362. if (!cur_node.f->light_source)
  363. applyFacesShading(color, vertices[0].Normal);
  364. for (int j = 0; j < 4; j++) {
  365. video::S3DVertex &vertex = vertices[j];
  366. vertex.Color = color;
  367. }
  368. return QuadDiagonal::Diag02;
  369. });
  370. }
  371. }
  372. void MapblockMeshGenerator::drawSolidNode()
  373. {
  374. u8 faces = 0; // k-th bit will be set if k-th face is to be drawn.
  375. static const v3s16 tile_dirs[6] = {
  376. v3s16(0, 1, 0),
  377. v3s16(0, -1, 0),
  378. v3s16(1, 0, 0),
  379. v3s16(-1, 0, 0),
  380. v3s16(0, 0, 1),
  381. v3s16(0, 0, -1)
  382. };
  383. TileSpec tiles[6];
  384. u16 lights[6];
  385. content_t n1 = cur_node.n.getContent();
  386. for (int face = 0; face < 6; face++) {
  387. v3s16 p2 = blockpos_nodes + cur_node.p + tile_dirs[face];
  388. MapNode neighbor = data->m_vmanip.getNodeNoEx(p2);
  389. content_t n2 = neighbor.getContent();
  390. bool backface_culling = cur_node.f->drawtype == NDT_NORMAL;
  391. if (n2 == n1)
  392. continue;
  393. if (n2 == CONTENT_IGNORE)
  394. continue;
  395. if (n2 != CONTENT_AIR) {
  396. const ContentFeatures &f2 = nodedef->get(n2);
  397. if (f2.solidness == 2)
  398. continue;
  399. if (cur_node.f->drawtype == NDT_LIQUID) {
  400. if (cur_node.f->sameLiquidRender(f2))
  401. continue;
  402. backface_culling = f2.solidness || f2.visual_solidness;
  403. }
  404. }
  405. faces |= 1 << face;
  406. getTile(tile_dirs[face], &tiles[face]);
  407. for (auto &layer : tiles[face].layers) {
  408. if (backface_culling)
  409. layer.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
  410. layer.material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
  411. layer.material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
  412. }
  413. if (!data->m_smooth_lighting) {
  414. lights[face] = getFaceLight(cur_node.n, neighbor, nodedef);
  415. }
  416. }
  417. if (!faces)
  418. return;
  419. u8 mask = faces ^ 0b0011'1111; // k-th bit is set if k-th face is to be *omitted*, as expected by cuboid drawing functions.
  420. cur_node.origin = intToFloat(cur_node.p, BS);
  421. auto box = aabb3f(v3f(-0.5 * BS), v3f(0.5 * BS));
  422. f32 texture_coord_buf[24];
  423. box.MinEdge += cur_node.origin;
  424. box.MaxEdge += cur_node.origin;
  425. generateCuboidTextureCoords(box, texture_coord_buf);
  426. if (data->m_smooth_lighting) {
  427. LightPair lights[6][4];
  428. for (int face = 0; face < 6; ++face) {
  429. for (int k = 0; k < 4; k++) {
  430. v3s16 corner = light_dirs[light_indices[face][k]];
  431. lights[face][k] = LightPair(getSmoothLightSolid(
  432. blockpos_nodes + cur_node.p, tile_dirs[face], corner, data));
  433. }
  434. }
  435. drawCuboid(box, tiles, 6, texture_coord_buf, mask, [&] (int face, video::S3DVertex vertices[4]) {
  436. auto final_lights = lights[face];
  437. for (int j = 0; j < 4; j++) {
  438. video::S3DVertex &vertex = vertices[j];
  439. vertex.Color = encode_light(final_lights[j], cur_node.f->light_source);
  440. if (!cur_node.f->light_source)
  441. applyFacesShading(vertex.Color, vertex.Normal);
  442. }
  443. if (lightDiff(final_lights[1], final_lights[3]) < lightDiff(final_lights[0], final_lights[2]))
  444. return QuadDiagonal::Diag13;
  445. return QuadDiagonal::Diag02;
  446. });
  447. } else {
  448. drawCuboid(box, tiles, 6, texture_coord_buf, mask, [&] (int face, video::S3DVertex vertices[4]) {
  449. video::SColor color = encode_light(lights[face], cur_node.f->light_source);
  450. if (!cur_node.f->light_source)
  451. applyFacesShading(color, vertices[0].Normal);
  452. for (int j = 0; j < 4; j++) {
  453. video::S3DVertex &vertex = vertices[j];
  454. vertex.Color = color;
  455. }
  456. return QuadDiagonal::Diag02;
  457. });
  458. }
  459. }
  460. u8 MapblockMeshGenerator::getNodeBoxMask(aabb3f box, u8 solid_neighbors, u8 sametype_neighbors) const
  461. {
  462. const f32 NODE_BOUNDARY = 0.5 * BS;
  463. // For an oversized nodebox, return immediately
  464. if (box.MaxEdge.X > NODE_BOUNDARY ||
  465. box.MinEdge.X < -NODE_BOUNDARY ||
  466. box.MaxEdge.Y > NODE_BOUNDARY ||
  467. box.MinEdge.Y < -NODE_BOUNDARY ||
  468. box.MaxEdge.Z > NODE_BOUNDARY ||
  469. box.MinEdge.Z < -NODE_BOUNDARY)
  470. return 0;
  471. // We can skip faces at node boundary if the matching neighbor is solid
  472. u8 solid_mask =
  473. (box.MaxEdge.Y == NODE_BOUNDARY ? 1 : 0) |
  474. (box.MinEdge.Y == -NODE_BOUNDARY ? 2 : 0) |
  475. (box.MaxEdge.X == NODE_BOUNDARY ? 4 : 0) |
  476. (box.MinEdge.X == -NODE_BOUNDARY ? 8 : 0) |
  477. (box.MaxEdge.Z == NODE_BOUNDARY ? 16 : 0) |
  478. (box.MinEdge.Z == -NODE_BOUNDARY ? 32 : 0);
  479. u8 sametype_mask = 0;
  480. if (cur_node.f->alpha == AlphaMode::ALPHAMODE_OPAQUE) {
  481. // In opaque nodeboxes, faces on opposite sides can cancel
  482. // each other out if there is a matching neighbor of the same type
  483. sametype_mask =
  484. ((solid_mask & 3) == 3 ? 3 : 0) |
  485. ((solid_mask & 12) == 12 ? 12 : 0) |
  486. ((solid_mask & 48) == 48 ? 48 : 0);
  487. }
  488. // Combine masks with actual neighbors to get the faces to be skipped
  489. return (solid_mask & solid_neighbors) | (sametype_mask & sametype_neighbors);
  490. }
  491. void MapblockMeshGenerator::prepareLiquidNodeDrawing()
  492. {
  493. getSpecialTile(0, &cur_liquid.tile_top);
  494. getSpecialTile(1, &cur_liquid.tile);
  495. MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p + v3s16(0, 1, 0));
  496. MapNode nbottom = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p + v3s16(0, -1, 0));
  497. cur_liquid.c_flowing = cur_node.f->liquid_alternative_flowing_id;
  498. cur_liquid.c_source = cur_node.f->liquid_alternative_source_id;
  499. cur_liquid.top_is_same_liquid = (ntop.getContent() == cur_liquid.c_flowing)
  500. || (ntop.getContent() == cur_liquid.c_source);
  501. cur_liquid.draw_bottom = (nbottom.getContent() != cur_liquid.c_flowing)
  502. && (nbottom.getContent() != cur_liquid.c_source);
  503. if (cur_liquid.draw_bottom) {
  504. const ContentFeatures &f2 = nodedef->get(nbottom.getContent());
  505. if (f2.solidness > 1)
  506. cur_liquid.draw_bottom = false;
  507. }
  508. if (data->m_smooth_lighting)
  509. return; // don't need to pre-compute anything in this case
  510. if (cur_node.f->light_source != 0) {
  511. // If this liquid emits light and doesn't contain light, draw
  512. // it at what it emits, for an increased effect
  513. u8 e = decode_light(cur_node.f->light_source);
  514. cur_node.light = LightPair(std::max(e, cur_node.light.lightDay),
  515. std::max(e, cur_node.light.lightNight));
  516. } else if (nodedef->getLightingFlags(ntop).has_light) {
  517. // Otherwise, use the light of the node on top if possible
  518. cur_node.light = LightPair(getInteriorLight(ntop, 0, nodedef));
  519. }
  520. cur_liquid.color_top = encode_light(cur_node.light, cur_node.f->light_source);
  521. cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);
  522. }
  523. void MapblockMeshGenerator::getLiquidNeighborhood()
  524. {
  525. u8 range = rangelim(nodedef->get(cur_liquid.c_flowing).liquid_range, 1, 8);
  526. for (int w = -1; w <= 1; w++)
  527. for (int u = -1; u <= 1; u++) {
  528. LiquidData::NeighborData &neighbor = cur_liquid.neighbors[w + 1][u + 1];
  529. v3s16 p2 = cur_node.p + v3s16(u, 0, w);
  530. MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  531. neighbor.content = n2.getContent();
  532. neighbor.level = -0.5f;
  533. neighbor.is_same_liquid = false;
  534. neighbor.top_is_same_liquid = false;
  535. if (neighbor.content == CONTENT_IGNORE)
  536. continue;
  537. if (neighbor.content == cur_liquid.c_source) {
  538. neighbor.is_same_liquid = true;
  539. neighbor.level = 0.5f;
  540. } else if (neighbor.content == cur_liquid.c_flowing) {
  541. neighbor.is_same_liquid = true;
  542. u8 liquid_level = (n2.param2 & LIQUID_LEVEL_MASK);
  543. if (liquid_level <= LIQUID_LEVEL_MAX + 1 - range)
  544. liquid_level = 0;
  545. else
  546. liquid_level -= (LIQUID_LEVEL_MAX + 1 - range);
  547. neighbor.level = (-0.5f + (liquid_level + 0.5f) / range);
  548. }
  549. // Check node above neighbor.
  550. // NOTE: This doesn't get executed if neighbor
  551. // doesn't exist
  552. p2.Y++;
  553. n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  554. if (n2.getContent() == cur_liquid.c_source || n2.getContent() == cur_liquid.c_flowing)
  555. neighbor.top_is_same_liquid = true;
  556. }
  557. }
  558. void MapblockMeshGenerator::calculateCornerLevels()
  559. {
  560. for (int k = 0; k < 2; k++)
  561. for (int i = 0; i < 2; i++)
  562. cur_liquid.corner_levels[k][i] = getCornerLevel(i, k);
  563. }
  564. f32 MapblockMeshGenerator::getCornerLevel(int i, int k) const
  565. {
  566. float sum = 0;
  567. int count = 0;
  568. int air_count = 0;
  569. for (int dk = 0; dk < 2; dk++)
  570. for (int di = 0; di < 2; di++) {
  571. const LiquidData::NeighborData &neighbor_data = cur_liquid.neighbors[k + dk][i + di];
  572. content_t content = neighbor_data.content;
  573. // If top is liquid, draw starting from top of node
  574. if (neighbor_data.top_is_same_liquid)
  575. return 0.5f;
  576. // Source always has the full height
  577. if (content == cur_liquid.c_source)
  578. return 0.5f;
  579. // Flowing liquid has level information
  580. if (content == cur_liquid.c_flowing) {
  581. sum += neighbor_data.level;
  582. count++;
  583. } else if (content == CONTENT_AIR) {
  584. air_count++;
  585. }
  586. }
  587. if (air_count >= 2)
  588. return -0.5f + 0.2f / BS;
  589. if (count > 0)
  590. return sum / count;
  591. return 0;
  592. }
  593. namespace {
  594. struct LiquidFaceDesc {
  595. v3s16 dir; // XZ
  596. v3s16 p[2]; // XZ only; 1 means +, 0 means -
  597. };
  598. struct UV {
  599. int u, v;
  600. };
  601. static const LiquidFaceDesc liquid_base_faces[4] = {
  602. {v3s16( 1, 0, 0), {v3s16(1, 0, 1), v3s16(1, 0, 0)}},
  603. {v3s16(-1, 0, 0), {v3s16(0, 0, 0), v3s16(0, 0, 1)}},
  604. {v3s16( 0, 0, 1), {v3s16(0, 0, 1), v3s16(1, 0, 1)}},
  605. {v3s16( 0, 0, -1), {v3s16(1, 0, 0), v3s16(0, 0, 0)}},
  606. };
  607. static const UV liquid_base_vertices[4] = {
  608. {0, 1},
  609. {1, 1},
  610. {1, 0},
  611. {0, 0}
  612. };
  613. }
  614. void MapblockMeshGenerator::drawLiquidSides()
  615. {
  616. for (const auto &face : liquid_base_faces) {
  617. const LiquidData::NeighborData &neighbor = cur_liquid.neighbors[face.dir.Z + 1][face.dir.X + 1];
  618. // No face between nodes of the same liquid, unless there is node
  619. // at the top to which it should be connected. Again, unless the face
  620. // there would be inside the liquid
  621. if (neighbor.is_same_liquid) {
  622. if (!cur_liquid.top_is_same_liquid)
  623. continue;
  624. if (neighbor.top_is_same_liquid)
  625. continue;
  626. }
  627. const ContentFeatures &neighbor_features = nodedef->get(neighbor.content);
  628. // Don't draw face if neighbor is blocking the view
  629. if (neighbor_features.solidness == 2)
  630. continue;
  631. video::S3DVertex vertices[4];
  632. for (int j = 0; j < 4; j++) {
  633. const UV &vertex = liquid_base_vertices[j];
  634. const v3s16 &base = face.p[vertex.u];
  635. float v = vertex.v;
  636. v3f pos;
  637. pos.X = (base.X - 0.5f) * BS;
  638. pos.Z = (base.Z - 0.5f) * BS;
  639. if (vertex.v) {
  640. pos.Y = (neighbor.is_same_liquid ? cur_liquid.corner_levels[base.Z][base.X] : -0.5f) * BS;
  641. } else if (cur_liquid.top_is_same_liquid) {
  642. pos.Y = 0.5f * BS;
  643. } else {
  644. pos.Y = cur_liquid.corner_levels[base.Z][base.X] * BS;
  645. v += 0.5f - cur_liquid.corner_levels[base.Z][base.X];
  646. }
  647. if (data->m_smooth_lighting)
  648. cur_node.color = blendLightColor(pos);
  649. pos += cur_node.origin;
  650. vertices[j] = video::S3DVertex(pos.X, pos.Y, pos.Z, 0, 0, 0, cur_node.color, vertex.u, v);
  651. };
  652. collector->append(cur_liquid.tile, vertices, 4, quad_indices, 6);
  653. }
  654. }
  655. void MapblockMeshGenerator::drawLiquidTop()
  656. {
  657. // To get backface culling right, the vertices need to go
  658. // clockwise around the front of the face. And we happened to
  659. // calculate corner levels in exact reverse order.
  660. static const int corner_resolve[4][2] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  661. video::S3DVertex vertices[4] = {
  662. video::S3DVertex(-BS / 2, 0, BS / 2, 0, 0, 0, cur_liquid.color_top, 0, 1),
  663. video::S3DVertex( BS / 2, 0, BS / 2, 0, 0, 0, cur_liquid.color_top, 1, 1),
  664. video::S3DVertex( BS / 2, 0, -BS / 2, 0, 0, 0, cur_liquid.color_top, 1, 0),
  665. video::S3DVertex(-BS / 2, 0, -BS / 2, 0, 0, 0, cur_liquid.color_top, 0, 0),
  666. };
  667. for (int i = 0; i < 4; i++) {
  668. int u = corner_resolve[i][0];
  669. int w = corner_resolve[i][1];
  670. vertices[i].Pos.Y += cur_liquid.corner_levels[w][u] * BS;
  671. if (data->m_smooth_lighting)
  672. vertices[i].Color = blendLightColor(vertices[i].Pos);
  673. vertices[i].Pos += cur_node.origin;
  674. }
  675. // Default downwards-flowing texture animation goes from
  676. // -Z towards +Z, thus the direction is +Z.
  677. // Rotate texture to make animation go in flow direction
  678. // Positive if liquid moves towards +Z
  679. f32 dz = (cur_liquid.corner_levels[0][0] + cur_liquid.corner_levels[0][1]) -
  680. (cur_liquid.corner_levels[1][0] + cur_liquid.corner_levels[1][1]);
  681. // Positive if liquid moves towards +X
  682. f32 dx = (cur_liquid.corner_levels[0][0] + cur_liquid.corner_levels[1][0]) -
  683. (cur_liquid.corner_levels[0][1] + cur_liquid.corner_levels[1][1]);
  684. v2f tcoord_center(0.5, 0.5);
  685. v2f tcoord_translate(blockpos_nodes.Z + cur_node.p.Z,
  686. blockpos_nodes.X + cur_node.p.X);
  687. v2f dir = v2f(dx, dz).normalize();
  688. if (dir == v2f{0.0f, 0.0f}) // if corners are symmetrical
  689. dir = v2f{1.0f, 0.0f};
  690. // Rotate tcoord_translate around the origin. The X axis turns to dir.
  691. tcoord_translate.set(
  692. dir.X * tcoord_translate.X - dir.Y * tcoord_translate.Y,
  693. dir.Y * tcoord_translate.X + dir.X * tcoord_translate.Y);
  694. tcoord_translate.X -= floor(tcoord_translate.X);
  695. tcoord_translate.Y -= floor(tcoord_translate.Y);
  696. for (video::S3DVertex &vertex : vertices) {
  697. // Rotate vertex.TCoords around tcoord_center. The X axis turns to dir.
  698. vertex.TCoords -= tcoord_center;
  699. vertex.TCoords.set(
  700. dir.X * vertex.TCoords.X - dir.Y * vertex.TCoords.Y,
  701. dir.Y * vertex.TCoords.X + dir.X * vertex.TCoords.Y);
  702. vertex.TCoords += tcoord_center;
  703. vertex.TCoords += tcoord_translate;
  704. }
  705. std::swap(vertices[0].TCoords, vertices[2].TCoords);
  706. collector->append(cur_liquid.tile_top, vertices, 4, quad_indices, 6);
  707. }
  708. void MapblockMeshGenerator::drawLiquidBottom()
  709. {
  710. video::S3DVertex vertices[4] = {
  711. video::S3DVertex(-BS / 2, -BS / 2, -BS / 2, 0, 0, 0, cur_liquid.color_top, 0, 0),
  712. video::S3DVertex( BS / 2, -BS / 2, -BS / 2, 0, 0, 0, cur_liquid.color_top, 1, 0),
  713. video::S3DVertex( BS / 2, -BS / 2, BS / 2, 0, 0, 0, cur_liquid.color_top, 1, 1),
  714. video::S3DVertex(-BS / 2, -BS / 2, BS / 2, 0, 0, 0, cur_liquid.color_top, 0, 1),
  715. };
  716. for (int i = 0; i < 4; i++) {
  717. if (data->m_smooth_lighting)
  718. vertices[i].Color = blendLightColor(vertices[i].Pos);
  719. vertices[i].Pos += cur_node.origin;
  720. }
  721. collector->append(cur_liquid.tile_top, vertices, 4, quad_indices, 6);
  722. }
  723. void MapblockMeshGenerator::drawLiquidNode()
  724. {
  725. prepareLiquidNodeDrawing();
  726. getLiquidNeighborhood();
  727. calculateCornerLevels();
  728. drawLiquidSides();
  729. if (!cur_liquid.top_is_same_liquid)
  730. drawLiquidTop();
  731. if (cur_liquid.draw_bottom)
  732. drawLiquidBottom();
  733. }
  734. void MapblockMeshGenerator::drawGlasslikeNode()
  735. {
  736. useTile(0, 0, 0);
  737. for (int face = 0; face < 6; face++) {
  738. // Check this neighbor
  739. v3s16 dir = g_6dirs[face];
  740. v3s16 neighbor_pos = blockpos_nodes + cur_node.p + dir;
  741. MapNode neighbor = data->m_vmanip.getNodeNoExNoEmerge(neighbor_pos);
  742. // Don't make face if neighbor is of same type
  743. if (neighbor.getContent() == cur_node.n.getContent())
  744. continue;
  745. // Face at Z-
  746. v3f vertices[4] = {
  747. v3f(-BS / 2, BS / 2, -BS / 2),
  748. v3f( BS / 2, BS / 2, -BS / 2),
  749. v3f( BS / 2, -BS / 2, -BS / 2),
  750. v3f(-BS / 2, -BS / 2, -BS / 2),
  751. };
  752. for (v3f &vertex : vertices) {
  753. switch (face) {
  754. case D6D_ZP:
  755. vertex.rotateXZBy(180); break;
  756. case D6D_YP:
  757. vertex.rotateYZBy( 90); break;
  758. case D6D_XP:
  759. vertex.rotateXZBy( 90); break;
  760. case D6D_ZN:
  761. vertex.rotateXZBy( 0); break;
  762. case D6D_YN:
  763. vertex.rotateYZBy(-90); break;
  764. case D6D_XN:
  765. vertex.rotateXZBy(-90); break;
  766. }
  767. }
  768. drawQuad(vertices, dir);
  769. }
  770. }
  771. void MapblockMeshGenerator::drawGlasslikeFramedNode()
  772. {
  773. TileSpec tiles[6];
  774. for (int face = 0; face < 6; face++)
  775. getTile(g_6dirs[face], &tiles[face]);
  776. if (!data->m_smooth_lighting)
  777. cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);
  778. TileSpec glass_tiles[6];
  779. for (auto &glass_tile : glass_tiles)
  780. glass_tile = tiles[4];
  781. // Only respect H/V merge bits when paramtype2 = "glasslikeliquidlevel" (liquid tank)
  782. u8 param2 = (cur_node.f->param_type_2 == CPT2_GLASSLIKE_LIQUID_LEVEL) ?
  783. cur_node.n.getParam2() : 0;
  784. bool H_merge = !(param2 & 128);
  785. bool V_merge = !(param2 & 64);
  786. param2 &= 63;
  787. static const float a = BS / 2.0f;
  788. static const float g = a - 0.03f;
  789. static const float b = 0.876f * (BS / 2.0f);
  790. static const aabb3f frame_edges[FRAMED_EDGE_COUNT] = {
  791. aabb3f( b, b, -a, a, a, a), // y+
  792. aabb3f(-a, b, -a, -b, a, a), // y+
  793. aabb3f( b, -a, -a, a, -b, a), // y-
  794. aabb3f(-a, -a, -a, -b, -b, a), // y-
  795. aabb3f( b, -a, b, a, a, a), // x+
  796. aabb3f( b, -a, -a, a, a, -b), // x+
  797. aabb3f(-a, -a, b, -b, a, a), // x-
  798. aabb3f(-a, -a, -a, -b, a, -b), // x-
  799. aabb3f(-a, b, b, a, a, a), // z+
  800. aabb3f(-a, -a, b, a, -b, a), // z+
  801. aabb3f(-a, -a, -a, a, -b, -b), // z-
  802. aabb3f(-a, b, -a, a, a, -b), // z-
  803. };
  804. // tables of neighbor (connect if same type and merge allowed),
  805. // checked with g_26dirs
  806. // 1 = connect, 0 = face visible
  807. bool nb[FRAMED_NEIGHBOR_COUNT] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  808. // 1 = check
  809. static const bool check_nb_vertical [FRAMED_NEIGHBOR_COUNT] =
  810. {0,1,0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
  811. static const bool check_nb_horizontal [FRAMED_NEIGHBOR_COUNT] =
  812. {1,0,1,1,0,1, 0,0,0,0, 1,1,1,1, 0,0,0,0};
  813. static const bool check_nb_all [FRAMED_NEIGHBOR_COUNT] =
  814. {1,1,1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1};
  815. const bool *check_nb = check_nb_all;
  816. // neighbors checks for frames visibility
  817. if (H_merge || V_merge) {
  818. if (!H_merge)
  819. check_nb = check_nb_vertical; // vertical-only merge
  820. if (!V_merge)
  821. check_nb = check_nb_horizontal; // horizontal-only merge
  822. content_t current = cur_node.n.getContent();
  823. for (int i = 0; i < FRAMED_NEIGHBOR_COUNT; i++) {
  824. if (!check_nb[i])
  825. continue;
  826. v3s16 n2p = blockpos_nodes + cur_node.p + g_26dirs[i];
  827. MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
  828. content_t n2c = n2.getContent();
  829. if (n2c == current)
  830. nb[i] = 1;
  831. }
  832. }
  833. // edge visibility
  834. static const u8 nb_triplet[FRAMED_EDGE_COUNT][3] = {
  835. {1, 2, 7}, {1, 5, 6}, {4, 2, 15}, {4, 5, 14},
  836. {2, 0, 11}, {2, 3, 13}, {5, 0, 10}, {5, 3, 12},
  837. {0, 1, 8}, {0, 4, 16}, {3, 4, 17}, {3, 1, 9},
  838. };
  839. cur_node.tile = tiles[1];
  840. for (int edge = 0; edge < FRAMED_EDGE_COUNT; edge++) {
  841. bool edge_invisible;
  842. if (nb[nb_triplet[edge][2]])
  843. edge_invisible = nb[nb_triplet[edge][0]] & nb[nb_triplet[edge][1]];
  844. else
  845. edge_invisible = nb[nb_triplet[edge][0]] ^ nb[nb_triplet[edge][1]];
  846. if (edge_invisible)
  847. continue;
  848. drawAutoLightedCuboid(frame_edges[edge]);
  849. }
  850. for (int face = 0; face < 6; face++) {
  851. if (nb[face])
  852. continue;
  853. cur_node.tile = glass_tiles[face];
  854. // Face at Z-
  855. v3f vertices[4] = {
  856. v3f(-a, a, -g),
  857. v3f( a, a, -g),
  858. v3f( a, -a, -g),
  859. v3f(-a, -a, -g),
  860. };
  861. for (v3f &vertex : vertices) {
  862. switch (face) {
  863. case D6D_ZP:
  864. vertex.rotateXZBy(180); break;
  865. case D6D_YP:
  866. vertex.rotateYZBy( 90); break;
  867. case D6D_XP:
  868. vertex.rotateXZBy( 90); break;
  869. case D6D_ZN:
  870. vertex.rotateXZBy( 0); break;
  871. case D6D_YN:
  872. vertex.rotateYZBy(-90); break;
  873. case D6D_XN:
  874. vertex.rotateXZBy(-90); break;
  875. }
  876. }
  877. v3s16 dir = g_6dirs[face];
  878. drawQuad(vertices, dir);
  879. }
  880. // Optionally render internal liquid level defined by param2
  881. // Liquid is textured with 1 tile defined in nodedef 'special_tiles'
  882. if (param2 > 0 && cur_node.f->param_type_2 == CPT2_GLASSLIKE_LIQUID_LEVEL &&
  883. cur_node.f->special_tiles[0].layers[0].texture) {
  884. // Internal liquid level has param2 range 0 .. 63,
  885. // convert it to -0.5 .. 0.5
  886. float vlev = (param2 / 63.0f) * 2.0f - 1.0f;
  887. getSpecialTile(0, &cur_node.tile);
  888. drawAutoLightedCuboid(aabb3f(-(nb[5] ? g : b),
  889. -(nb[4] ? g : b),
  890. -(nb[3] ? g : b),
  891. (nb[2] ? g : b),
  892. (nb[1] ? g : b) * vlev,
  893. (nb[0] ? g : b)));
  894. }
  895. }
  896. void MapblockMeshGenerator::drawAllfacesNode()
  897. {
  898. static const aabb3f box(-BS / 2, -BS / 2, -BS / 2, BS / 2, BS / 2, BS / 2);
  899. useTile(0, 0, 0);
  900. drawAutoLightedCuboid(box);
  901. }
  902. void MapblockMeshGenerator::drawTorchlikeNode()
  903. {
  904. u8 wall = cur_node.n.getWallMounted(nodedef);
  905. u8 tileindex = 0;
  906. switch (wall) {
  907. case DWM_YP: tileindex = 1; break; // ceiling
  908. case DWM_YN: tileindex = 0; break; // floor
  909. case DWM_S1: tileindex = 1; break; // ceiling, but rotated
  910. case DWM_S2: tileindex = 0; break; // floor, but rotated
  911. default: tileindex = 2; // side (or invalid, shouldn't happen)
  912. }
  913. useTile(tileindex, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  914. float size = BS / 2 * cur_node.f->visual_scale;
  915. v3f vertices[4] = {
  916. v3f(-size, size, 0),
  917. v3f( size, size, 0),
  918. v3f( size, -size, 0),
  919. v3f(-size, -size, 0),
  920. };
  921. for (v3f &vertex : vertices) {
  922. switch (wall) {
  923. case DWM_YP:
  924. vertex.Y += -size + BS/2;
  925. vertex.rotateXZBy(-45);
  926. break;
  927. case DWM_YN:
  928. vertex.Y += size - BS/2;
  929. vertex.rotateXZBy(45);
  930. break;
  931. case DWM_XP:
  932. vertex.X += -size + BS/2;
  933. break;
  934. case DWM_XN:
  935. vertex.X += -size + BS/2;
  936. vertex.rotateXZBy(180);
  937. break;
  938. case DWM_ZP:
  939. vertex.X += -size + BS/2;
  940. vertex.rotateXZBy(90);
  941. break;
  942. case DWM_ZN:
  943. vertex.X += -size + BS/2;
  944. vertex.rotateXZBy(-90);
  945. break;
  946. case DWM_S1:
  947. // same as DWM_YP, but rotated 90°
  948. vertex.Y += -size + BS/2;
  949. vertex.rotateXZBy(45);
  950. break;
  951. case DWM_S2:
  952. // same as DWM_YN, but rotated -90°
  953. vertex.Y += size - BS/2;
  954. vertex.rotateXZBy(-45);
  955. break;
  956. }
  957. }
  958. drawQuad(vertices);
  959. }
  960. void MapblockMeshGenerator::drawSignlikeNode()
  961. {
  962. u8 wall = cur_node.n.getWallMounted(nodedef);
  963. useTile(0, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  964. static const float offset = BS / 16;
  965. float size = BS / 2 * cur_node.f->visual_scale;
  966. // Wall at X+ of node
  967. v3f vertices[4] = {
  968. v3f(BS / 2 - offset, size, size),
  969. v3f(BS / 2 - offset, size, -size),
  970. v3f(BS / 2 - offset, -size, -size),
  971. v3f(BS / 2 - offset, -size, size),
  972. };
  973. for (v3f &vertex : vertices) {
  974. switch (wall) {
  975. case DWM_YP:
  976. vertex.rotateXYBy( 90); break;
  977. case DWM_YN:
  978. vertex.rotateXYBy(-90); break;
  979. case DWM_XP:
  980. vertex.rotateXZBy( 0); break;
  981. case DWM_XN:
  982. vertex.rotateXZBy(180); break;
  983. case DWM_ZP:
  984. vertex.rotateXZBy( 90); break;
  985. case DWM_ZN:
  986. vertex.rotateXZBy(-90); break;
  987. case DWM_S1:
  988. vertex.rotateXYBy( 90); vertex.rotateXZBy(90); break;
  989. case DWM_S2:
  990. vertex.rotateXYBy(-90); vertex.rotateXZBy(-90); break;
  991. }
  992. }
  993. drawQuad(vertices);
  994. }
  995. void MapblockMeshGenerator::drawPlantlikeQuad(float rotation, float quad_offset,
  996. bool offset_top_only)
  997. {
  998. const f32 scale = cur_node.scale;
  999. v3f vertices[4] = {
  1000. v3f(-scale, -BS / 2 + 2.0 * scale * cur_plant.plant_height, 0),
  1001. v3f( scale, -BS / 2 + 2.0 * scale * cur_plant.plant_height, 0),
  1002. v3f( scale, -BS / 2, 0),
  1003. v3f(-scale, -BS / 2, 0),
  1004. };
  1005. if (cur_plant.random_offset_Y) {
  1006. PseudoRandom yrng(cur_plant.face_num++
  1007. | cur_node.p.X << 16
  1008. | cur_node.p.Z << 8
  1009. | cur_node.p.Y << 24);
  1010. cur_plant.offset.Y = -BS * ((yrng.next() % 16 / 16.0) * 0.125);
  1011. }
  1012. int offset_count = offset_top_only ? 2 : 4;
  1013. for (int i = 0; i < offset_count; i++)
  1014. vertices[i].Z += quad_offset;
  1015. for (v3f &vertex : vertices) {
  1016. vertex.rotateXZBy(rotation + cur_plant.rotate_degree);
  1017. vertex += cur_plant.offset;
  1018. }
  1019. u8 wall = cur_node.n.getWallMounted(nodedef);
  1020. if (wall != DWM_YN) {
  1021. for (v3f &vertex : vertices) {
  1022. switch (wall) {
  1023. case DWM_YP:
  1024. vertex.rotateYZBy(180);
  1025. vertex.rotateXZBy(180);
  1026. break;
  1027. case DWM_XP:
  1028. vertex.rotateXYBy(90);
  1029. break;
  1030. case DWM_XN:
  1031. vertex.rotateXYBy(-90);
  1032. vertex.rotateYZBy(180);
  1033. break;
  1034. case DWM_ZP:
  1035. vertex.rotateYZBy(-90);
  1036. vertex.rotateXYBy(90);
  1037. break;
  1038. case DWM_ZN:
  1039. vertex.rotateYZBy(90);
  1040. vertex.rotateXYBy(90);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. drawQuad(vertices, v3s16(0, 0, 0), cur_plant.plant_height);
  1046. }
  1047. void MapblockMeshGenerator::drawPlantlike(bool is_rooted)
  1048. {
  1049. cur_plant.draw_style = PLANT_STYLE_CROSS;
  1050. cur_node.scale = BS / 2 * cur_node.f->visual_scale;
  1051. cur_plant.offset = v3f(0, 0, 0);
  1052. cur_plant.rotate_degree = 0.0f;
  1053. cur_plant.random_offset_Y = false;
  1054. cur_plant.face_num = 0;
  1055. cur_plant.plant_height = 1.0;
  1056. switch (cur_node.f->param_type_2) {
  1057. case CPT2_MESHOPTIONS:
  1058. cur_plant.draw_style = PlantlikeStyle(cur_node.n.param2 & MO_MASK_STYLE);
  1059. if (cur_node.n.param2 & MO_BIT_SCALE_SQRT2)
  1060. cur_node.scale *= 1.41421;
  1061. if (cur_node.n.param2 & MO_BIT_RANDOM_OFFSET) {
  1062. PseudoRandom rng(cur_node.p.X << 8 | cur_node.p.Z | cur_node.p.Y << 16);
  1063. cur_plant.offset.X = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145);
  1064. cur_plant.offset.Z = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145);
  1065. }
  1066. if (cur_node.n.param2 & MO_BIT_RANDOM_OFFSET_Y)
  1067. cur_plant.random_offset_Y = true;
  1068. break;
  1069. case CPT2_DEGROTATE:
  1070. case CPT2_COLORED_DEGROTATE:
  1071. cur_plant.rotate_degree = 1.5f * cur_node.n.getDegRotate(nodedef);
  1072. break;
  1073. case CPT2_LEVELED:
  1074. cur_plant.plant_height = cur_node.n.param2 / 16.0;
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. if (is_rooted) {
  1080. u8 wall = cur_node.n.getWallMounted(nodedef);
  1081. switch (wall) {
  1082. case DWM_YP:
  1083. cur_plant.offset.Y += BS*2;
  1084. break;
  1085. case DWM_XN:
  1086. case DWM_XP:
  1087. case DWM_ZN:
  1088. case DWM_ZP:
  1089. cur_plant.offset.X += -BS;
  1090. cur_plant.offset.Y += BS;
  1091. break;
  1092. }
  1093. }
  1094. switch (cur_plant.draw_style) {
  1095. case PLANT_STYLE_CROSS:
  1096. drawPlantlikeQuad(46);
  1097. drawPlantlikeQuad(-44);
  1098. break;
  1099. case PLANT_STYLE_CROSS2:
  1100. drawPlantlikeQuad(91);
  1101. drawPlantlikeQuad(1);
  1102. break;
  1103. case PLANT_STYLE_STAR:
  1104. drawPlantlikeQuad(121);
  1105. drawPlantlikeQuad(241);
  1106. drawPlantlikeQuad(1);
  1107. break;
  1108. case PLANT_STYLE_HASH:
  1109. drawPlantlikeQuad( 1, BS / 4);
  1110. drawPlantlikeQuad( 91, BS / 4);
  1111. drawPlantlikeQuad(181, BS / 4);
  1112. drawPlantlikeQuad(271, BS / 4);
  1113. break;
  1114. case PLANT_STYLE_HASH2:
  1115. drawPlantlikeQuad( 1, -BS / 2, true);
  1116. drawPlantlikeQuad( 91, -BS / 2, true);
  1117. drawPlantlikeQuad(181, -BS / 2, true);
  1118. drawPlantlikeQuad(271, -BS / 2, true);
  1119. break;
  1120. }
  1121. }
  1122. void MapblockMeshGenerator::drawPlantlikeNode()
  1123. {
  1124. useTile();
  1125. drawPlantlike();
  1126. }
  1127. void MapblockMeshGenerator::drawPlantlikeRootedNode()
  1128. {
  1129. drawSolidNode();
  1130. useTile(0, MATERIAL_FLAG_CRACK_OVERLAY, 0, true);
  1131. cur_node.origin += v3f(0.0, BS, 0.0);
  1132. cur_node.p.Y++;
  1133. if (data->m_smooth_lighting) {
  1134. getSmoothLightFrame();
  1135. } else {
  1136. MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p);
  1137. cur_node.light = LightPair(getInteriorLight(ntop, 0, nodedef));
  1138. }
  1139. drawPlantlike(true);
  1140. cur_node.p.Y--;
  1141. }
  1142. void MapblockMeshGenerator::drawFirelikeQuad(float rotation, float opening_angle,
  1143. float offset_h, float offset_v)
  1144. {
  1145. const f32 scale = cur_node.scale;
  1146. v3f vertices[4] = {
  1147. v3f(-scale, -BS / 2 + scale * 2, 0),
  1148. v3f( scale, -BS / 2 + scale * 2, 0),
  1149. v3f( scale, -BS / 2, 0),
  1150. v3f(-scale, -BS / 2, 0),
  1151. };
  1152. for (v3f &vertex : vertices) {
  1153. vertex.rotateYZBy(opening_angle);
  1154. vertex.Z += offset_h;
  1155. vertex.rotateXZBy(rotation);
  1156. vertex.Y += offset_v;
  1157. }
  1158. drawQuad(vertices);
  1159. }
  1160. void MapblockMeshGenerator::drawFirelikeNode()
  1161. {
  1162. useTile();
  1163. cur_node.scale = BS / 2 * cur_node.f->visual_scale;
  1164. // Check for adjacent nodes
  1165. bool neighbors = false;
  1166. bool neighbor[6] = {0, 0, 0, 0, 0, 0};
  1167. content_t current = cur_node.n.getContent();
  1168. for (int i = 0; i < 6; i++) {
  1169. v3s16 n2p = blockpos_nodes + cur_node.p + g_6dirs[i];
  1170. MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
  1171. content_t n2c = n2.getContent();
  1172. if (n2c != CONTENT_IGNORE && n2c != CONTENT_AIR && n2c != current) {
  1173. neighbor[i] = true;
  1174. neighbors = true;
  1175. }
  1176. }
  1177. bool drawBasicFire = neighbor[D6D_YN] || !neighbors;
  1178. bool drawBottomFire = neighbor[D6D_YP];
  1179. if (drawBasicFire || neighbor[D6D_ZP])
  1180. drawFirelikeQuad(0, -10, 0.4 * BS);
  1181. else if (drawBottomFire)
  1182. drawFirelikeQuad(0, 70, 0.47 * BS, 0.484 * BS);
  1183. if (drawBasicFire || neighbor[D6D_XN])
  1184. drawFirelikeQuad(90, -10, 0.4 * BS);
  1185. else if (drawBottomFire)
  1186. drawFirelikeQuad(90, 70, 0.47 * BS, 0.484 * BS);
  1187. if (drawBasicFire || neighbor[D6D_ZN])
  1188. drawFirelikeQuad(180, -10, 0.4 * BS);
  1189. else if (drawBottomFire)
  1190. drawFirelikeQuad(180, 70, 0.47 * BS, 0.484 * BS);
  1191. if (drawBasicFire || neighbor[D6D_XP])
  1192. drawFirelikeQuad(270, -10, 0.4 * BS);
  1193. else if (drawBottomFire)
  1194. drawFirelikeQuad(270, 70, 0.47 * BS, 0.484 * BS);
  1195. if (drawBasicFire) {
  1196. drawFirelikeQuad(45, 0, 0.0);
  1197. drawFirelikeQuad(-45, 0, 0.0);
  1198. }
  1199. }
  1200. void MapblockMeshGenerator::drawFencelikeNode()
  1201. {
  1202. useTile(0, 0, 0);
  1203. TileSpec tile_nocrack = cur_node.tile;
  1204. for (auto &layer : tile_nocrack.layers)
  1205. layer.material_flags &= ~MATERIAL_FLAG_CRACK;
  1206. // Put wood the right way around in the posts
  1207. TileSpec tile_rot = cur_node.tile;
  1208. tile_rot.rotation = TileRotation::R90;
  1209. static const f32 post_rad = BS / 8;
  1210. static const f32 bar_rad = BS / 16;
  1211. static const f32 bar_len = BS / 2 - post_rad;
  1212. // The post - always present
  1213. static const aabb3f post(-post_rad, -BS / 2, -post_rad,
  1214. post_rad, BS / 2, post_rad);
  1215. static const f32 postuv[24] = {
  1216. 0.375, 0.375, 0.625, 0.625,
  1217. 0.375, 0.375, 0.625, 0.625,
  1218. 0.000, 0.000, 0.250, 1.000,
  1219. 0.250, 0.000, 0.500, 1.000,
  1220. 0.500, 0.000, 0.750, 1.000,
  1221. 0.750, 0.000, 1.000, 1.000,
  1222. };
  1223. cur_node.tile = tile_rot;
  1224. drawAutoLightedCuboid(post, postuv);
  1225. cur_node.tile = tile_nocrack;
  1226. // Now a section of fence, +X, if there's a post there
  1227. v3s16 p2 = cur_node.p;
  1228. p2.X++;
  1229. MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  1230. const ContentFeatures *f2 = &nodedef->get(n2);
  1231. if (f2->drawtype == NDT_FENCELIKE) {
  1232. static const aabb3f bar_x1(BS / 2 - bar_len, BS / 4 - bar_rad, -bar_rad,
  1233. BS / 2 + bar_len, BS / 4 + bar_rad, bar_rad);
  1234. static const aabb3f bar_x2(BS / 2 - bar_len, -BS / 4 - bar_rad, -bar_rad,
  1235. BS / 2 + bar_len, -BS / 4 + bar_rad, bar_rad);
  1236. static const f32 xrailuv[24] = {
  1237. 0.000, 0.125, 1.000, 0.250,
  1238. 0.000, 0.250, 1.000, 0.375,
  1239. 0.375, 0.375, 0.500, 0.500,
  1240. 0.625, 0.625, 0.750, 0.750,
  1241. 0.000, 0.500, 1.000, 0.625,
  1242. 0.000, 0.875, 1.000, 1.000,
  1243. };
  1244. drawAutoLightedCuboid(bar_x1, xrailuv);
  1245. drawAutoLightedCuboid(bar_x2, xrailuv);
  1246. }
  1247. // Now a section of fence, +Z, if there's a post there
  1248. p2 = cur_node.p;
  1249. p2.Z++;
  1250. n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
  1251. f2 = &nodedef->get(n2);
  1252. if (f2->drawtype == NDT_FENCELIKE) {
  1253. static const aabb3f bar_z1(-bar_rad, BS / 4 - bar_rad, BS / 2 - bar_len,
  1254. bar_rad, BS / 4 + bar_rad, BS / 2 + bar_len);
  1255. static const aabb3f bar_z2(-bar_rad, -BS / 4 - bar_rad, BS / 2 - bar_len,
  1256. bar_rad, -BS / 4 + bar_rad, BS / 2 + bar_len);
  1257. static const f32 zrailuv[24] = {
  1258. 0.1875, 0.0625, 0.3125, 0.3125, // cannot rotate; stretch
  1259. 0.2500, 0.0625, 0.3750, 0.3125, // for wood texture instead
  1260. 0.0000, 0.5625, 1.0000, 0.6875,
  1261. 0.0000, 0.3750, 1.0000, 0.5000,
  1262. 0.3750, 0.3750, 0.5000, 0.5000,
  1263. 0.6250, 0.6250, 0.7500, 0.7500,
  1264. };
  1265. drawAutoLightedCuboid(bar_z1, zrailuv);
  1266. drawAutoLightedCuboid(bar_z2, zrailuv);
  1267. }
  1268. }
  1269. bool MapblockMeshGenerator::isSameRail(v3s16 dir)
  1270. {
  1271. MapNode node2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p + dir);
  1272. if (node2.getContent() == cur_node.n.getContent())
  1273. return true;
  1274. const ContentFeatures &def2 = nodedef->get(node2);
  1275. return ((def2.drawtype == NDT_RAILLIKE) &&
  1276. (def2.getGroup(raillike_groupname) == cur_rail.raillike_group));
  1277. }
  1278. namespace {
  1279. static const v3s16 rail_direction[4] = {
  1280. v3s16( 0, 0, 1),
  1281. v3s16( 0, 0, -1),
  1282. v3s16(-1, 0, 0),
  1283. v3s16( 1, 0, 0),
  1284. };
  1285. static const int rail_slope_angle[4] = {0, 180, 90, -90};
  1286. enum RailTile {
  1287. straight,
  1288. curved,
  1289. junction,
  1290. cross,
  1291. };
  1292. struct RailDesc {
  1293. int tile_index;
  1294. int angle;
  1295. };
  1296. static const RailDesc rail_kinds[16] = {
  1297. // +x -x -z +z
  1298. //-------------
  1299. {straight, 0}, // . . . .
  1300. {straight, 0}, // . . . +Z
  1301. {straight, 0}, // . . -Z .
  1302. {straight, 0}, // . . -Z +Z
  1303. {straight, 90}, // . -X . .
  1304. { curved, 180}, // . -X . +Z
  1305. { curved, 270}, // . -X -Z .
  1306. {junction, 180}, // . -X -Z +Z
  1307. {straight, 90}, // +X . . .
  1308. { curved, 90}, // +X . . +Z
  1309. { curved, 0}, // +X . -Z .
  1310. {junction, 0}, // +X . -Z +Z
  1311. {straight, 90}, // +X -X . .
  1312. {junction, 90}, // +X -X . +Z
  1313. {junction, 270}, // +X -X -Z .
  1314. { cross, 0}, // +X -X -Z +Z
  1315. };
  1316. }
  1317. void MapblockMeshGenerator::drawRaillikeNode()
  1318. {
  1319. cur_rail.raillike_group = cur_node.f->getGroup(raillike_groupname);
  1320. int code = 0;
  1321. int angle;
  1322. int tile_index;
  1323. bool sloped = false;
  1324. for (int dir = 0; dir < 4; dir++) {
  1325. bool rail_above = isSameRail(rail_direction[dir] + v3s16(0, 1, 0));
  1326. if (rail_above) {
  1327. sloped = true;
  1328. angle = rail_slope_angle[dir];
  1329. }
  1330. if (rail_above ||
  1331. isSameRail(rail_direction[dir]) ||
  1332. isSameRail(rail_direction[dir] + v3s16(0, -1, 0)))
  1333. code |= 1 << dir;
  1334. }
  1335. if (sloped) {
  1336. tile_index = straight;
  1337. } else {
  1338. tile_index = rail_kinds[code].tile_index;
  1339. angle = rail_kinds[code].angle;
  1340. }
  1341. useTile(tile_index, MATERIAL_FLAG_CRACK_OVERLAY, MATERIAL_FLAG_BACKFACE_CULLING);
  1342. static const float offset = BS / 64;
  1343. static const float size = BS / 2;
  1344. float y2 = sloped ? size : -size;
  1345. v3f vertices[4] = {
  1346. v3f(-size, y2 + offset, size),
  1347. v3f( size, y2 + offset, size),
  1348. v3f( size, -size + offset, -size),
  1349. v3f(-size, -size + offset, -size),
  1350. };
  1351. if (angle)
  1352. for (v3f &vertex : vertices)
  1353. vertex.rotateXZBy(angle);
  1354. drawQuad(vertices);
  1355. }
  1356. namespace {
  1357. static const v3s16 nodebox_tile_dirs[6] = {
  1358. v3s16(0, 1, 0),
  1359. v3s16(0, -1, 0),
  1360. v3s16(1, 0, 0),
  1361. v3s16(-1, 0, 0),
  1362. v3s16(0, 0, 1),
  1363. v3s16(0, 0, -1)
  1364. };
  1365. // we have this order for some reason...
  1366. static const v3s16 nodebox_connection_dirs[6] = {
  1367. v3s16( 0, 1, 0), // top
  1368. v3s16( 0, -1, 0), // bottom
  1369. v3s16( 0, 0, -1), // front
  1370. v3s16(-1, 0, 0), // left
  1371. v3s16( 0, 0, 1), // back
  1372. v3s16( 1, 0, 0), // right
  1373. };
  1374. }
  1375. void MapblockMeshGenerator::drawNodeboxNode()
  1376. {
  1377. TileSpec tiles[6];
  1378. for (int face = 0; face < 6; face++) {
  1379. // Handles facedir rotation for textures
  1380. getTile(nodebox_tile_dirs[face], &tiles[face]);
  1381. }
  1382. bool param2_is_rotation =
  1383. cur_node.f->param_type_2 == CPT2_COLORED_FACEDIR ||
  1384. cur_node.f->param_type_2 == CPT2_COLORED_WALLMOUNTED ||
  1385. cur_node.f->param_type_2 == CPT2_FACEDIR ||
  1386. cur_node.f->param_type_2 == CPT2_WALLMOUNTED;
  1387. bool param2_is_level =
  1388. cur_node.f->param_type_2 == CPT2_LEVELED;
  1389. // locate possible neighboring nodes to connect to
  1390. u8 neighbors_set = 0;
  1391. u8 solid_neighbors = 0;
  1392. u8 sametype_neighbors = 0;
  1393. for (int dir = 0; dir != 6; dir++) {
  1394. u8 flag = 1 << dir;
  1395. v3s16 p2 = blockpos_nodes + cur_node.p + nodebox_tile_dirs[dir];
  1396. MapNode n2 = data->m_vmanip.getNodeNoEx(p2);
  1397. // mark neighbors that are the same node type
  1398. // and have the same rotation or higher level stored as param2
  1399. if (n2.param0 == cur_node.n.param0 &&
  1400. (!param2_is_rotation || cur_node.n.param2 == n2.param2) &&
  1401. (!param2_is_level || cur_node.n.param2 <= n2.param2))
  1402. sametype_neighbors |= flag;
  1403. // mark neighbors that are simple solid blocks
  1404. if (nodedef->get(n2).drawtype == NDT_NORMAL)
  1405. solid_neighbors |= flag;
  1406. if (cur_node.f->node_box.type == NODEBOX_CONNECTED) {
  1407. p2 = blockpos_nodes + cur_node.p + nodebox_connection_dirs[dir];
  1408. n2 = data->m_vmanip.getNodeNoEx(p2);
  1409. if (nodedef->nodeboxConnects(cur_node.n, n2, flag))
  1410. neighbors_set |= flag;
  1411. }
  1412. }
  1413. std::vector<aabb3f> boxes;
  1414. cur_node.n.getNodeBoxes(nodedef, &boxes, neighbors_set);
  1415. bool isTransparent = false;
  1416. for (const TileSpec &tile : tiles) {
  1417. if (tile.layers[0].isTransparent()) {
  1418. isTransparent = true;
  1419. break;
  1420. }
  1421. }
  1422. if (isTransparent) {
  1423. std::vector<float> sections;
  1424. // Preallocate 8 default splits + Min&Max for each nodebox
  1425. sections.reserve(8 + 2 * boxes.size());
  1426. for (int axis = 0; axis < 3; axis++) {
  1427. // identify sections
  1428. if (axis == 0) {
  1429. // Default split at node bounds, up to 3 nodes in each direction
  1430. for (float s = -3.5f * BS; s < 4.0f * BS; s += 1.0f * BS)
  1431. sections.push_back(s);
  1432. }
  1433. else {
  1434. // Avoid readding the same 8 default splits for Y and Z
  1435. sections.resize(8);
  1436. }
  1437. // Add edges of existing node boxes, rounded to 1E-3
  1438. for (size_t i = 0; i < boxes.size(); i++) {
  1439. sections.push_back(std::floor(boxes[i].MinEdge[axis] * 1E3) * 1E-3);
  1440. sections.push_back(std::floor(boxes[i].MaxEdge[axis] * 1E3) * 1E-3);
  1441. }
  1442. // split the boxes at recorded sections
  1443. // limit splits to avoid runaway crash if inner loop adds infinite splits
  1444. // due to e.g. precision problems.
  1445. // 100 is just an arbitrary, reasonably high number.
  1446. for (size_t i = 0; i < boxes.size() && i < 100; i++) {
  1447. aabb3f *box = &boxes[i];
  1448. for (float section : sections) {
  1449. if (box->MinEdge[axis] < section && box->MaxEdge[axis] > section) {
  1450. aabb3f copy(*box);
  1451. copy.MinEdge[axis] = section;
  1452. box->MaxEdge[axis] = section;
  1453. boxes.push_back(copy);
  1454. box = &boxes[i]; // find new address of the box in case of reallocation
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. for (auto &box : boxes) {
  1461. u8 mask = getNodeBoxMask(box, solid_neighbors, sametype_neighbors);
  1462. drawAutoLightedCuboid(box, nullptr, tiles, 6, mask);
  1463. }
  1464. }
  1465. void MapblockMeshGenerator::drawMeshNode()
  1466. {
  1467. u8 facedir = 0;
  1468. scene::IMesh* mesh;
  1469. bool private_mesh; // as a grab/drop pair is not thread-safe
  1470. int degrotate = 0;
  1471. if (cur_node.f->param_type_2 == CPT2_FACEDIR ||
  1472. cur_node.f->param_type_2 == CPT2_COLORED_FACEDIR ||
  1473. cur_node.f->param_type_2 == CPT2_4DIR ||
  1474. cur_node.f->param_type_2 == CPT2_COLORED_4DIR) {
  1475. facedir = cur_node.n.getFaceDir(nodedef);
  1476. } else if (cur_node.f->param_type_2 == CPT2_WALLMOUNTED ||
  1477. cur_node.f->param_type_2 == CPT2_COLORED_WALLMOUNTED) {
  1478. // Convert wallmounted to 6dfacedir.
  1479. // When cache enabled, it is already converted.
  1480. facedir = cur_node.n.getWallMounted(nodedef);
  1481. if (!enable_mesh_cache)
  1482. facedir = wallmounted_to_facedir[facedir];
  1483. } else if (cur_node.f->param_type_2 == CPT2_DEGROTATE ||
  1484. cur_node.f->param_type_2 == CPT2_COLORED_DEGROTATE) {
  1485. degrotate = cur_node.n.getDegRotate(nodedef);
  1486. }
  1487. if (!data->m_smooth_lighting && cur_node.f->mesh_ptr[facedir] && !degrotate) {
  1488. // use cached meshes
  1489. private_mesh = false;
  1490. mesh = cur_node.f->mesh_ptr[facedir];
  1491. } else if (cur_node.f->mesh_ptr[0]) {
  1492. // no cache, clone and rotate mesh
  1493. private_mesh = true;
  1494. mesh = cloneMesh(cur_node.f->mesh_ptr[0]);
  1495. if (facedir)
  1496. rotateMeshBy6dFacedir(mesh, facedir);
  1497. else if (degrotate)
  1498. rotateMeshXZby(mesh, 1.5f * degrotate);
  1499. recalculateBoundingBox(mesh);
  1500. meshmanip->recalculateNormals(mesh, true, false);
  1501. } else
  1502. return;
  1503. int mesh_buffer_count = mesh->getMeshBufferCount();
  1504. for (int j = 0; j < mesh_buffer_count; j++) {
  1505. useTile(j);
  1506. scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
  1507. video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();
  1508. int vertex_count = buf->getVertexCount();
  1509. if (data->m_smooth_lighting) {
  1510. // Mesh is always private here. So the lighting is applied to each
  1511. // vertex right here.
  1512. for (int k = 0; k < vertex_count; k++) {
  1513. video::S3DVertex &vertex = vertices[k];
  1514. vertex.Color = blendLightColor(vertex.Pos, vertex.Normal);
  1515. vertex.Pos += cur_node.origin;
  1516. }
  1517. collector->append(cur_node.tile, vertices, vertex_count,
  1518. buf->getIndices(), buf->getIndexCount());
  1519. } else {
  1520. // Don't modify the mesh, it may not be private here.
  1521. // Instead, let the collector process colors, etc.
  1522. collector->append(cur_node.tile, vertices, vertex_count,
  1523. buf->getIndices(), buf->getIndexCount(), cur_node.origin,
  1524. cur_node.color, cur_node.f->light_source);
  1525. }
  1526. }
  1527. if (private_mesh)
  1528. mesh->drop();
  1529. }
  1530. // also called when the drawtype is known but should have been pre-converted
  1531. void MapblockMeshGenerator::errorUnknownDrawtype()
  1532. {
  1533. infostream << "Got drawtype " << cur_node.f->drawtype << std::endl;
  1534. FATAL_ERROR("Unknown drawtype");
  1535. }
  1536. void MapblockMeshGenerator::drawNode()
  1537. {
  1538. switch (cur_node.f->drawtype) {
  1539. case NDT_AIRLIKE: // Not drawn at all
  1540. return;
  1541. case NDT_LIQUID:
  1542. case NDT_NORMAL: // solid nodes don’t need the usual setup
  1543. drawSolidNode();
  1544. return;
  1545. default:
  1546. break;
  1547. }
  1548. cur_node.origin = intToFloat(cur_node.p, BS);
  1549. if (data->m_smooth_lighting)
  1550. getSmoothLightFrame();
  1551. else
  1552. cur_node.light = LightPair(getInteriorLight(cur_node.n, 0, nodedef));
  1553. switch (cur_node.f->drawtype) {
  1554. case NDT_FLOWINGLIQUID: drawLiquidNode(); break;
  1555. case NDT_GLASSLIKE: drawGlasslikeNode(); break;
  1556. case NDT_GLASSLIKE_FRAMED: drawGlasslikeFramedNode(); break;
  1557. case NDT_ALLFACES: drawAllfacesNode(); break;
  1558. case NDT_TORCHLIKE: drawTorchlikeNode(); break;
  1559. case NDT_SIGNLIKE: drawSignlikeNode(); break;
  1560. case NDT_PLANTLIKE: drawPlantlikeNode(); break;
  1561. case NDT_PLANTLIKE_ROOTED: drawPlantlikeRootedNode(); break;
  1562. case NDT_FIRELIKE: drawFirelikeNode(); break;
  1563. case NDT_FENCELIKE: drawFencelikeNode(); break;
  1564. case NDT_RAILLIKE: drawRaillikeNode(); break;
  1565. case NDT_NODEBOX: drawNodeboxNode(); break;
  1566. case NDT_MESH: drawMeshNode(); break;
  1567. default: errorUnknownDrawtype(); break;
  1568. }
  1569. }
  1570. void MapblockMeshGenerator::generate()
  1571. {
  1572. for (cur_node.p.Z = 0; cur_node.p.Z < data->side_length; cur_node.p.Z++)
  1573. for (cur_node.p.Y = 0; cur_node.p.Y < data->side_length; cur_node.p.Y++)
  1574. for (cur_node.p.X = 0; cur_node.p.X < data->side_length; cur_node.p.X++) {
  1575. cur_node.n = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p);
  1576. cur_node.f = &nodedef->get(cur_node.n);
  1577. drawNode();
  1578. }
  1579. }
  1580. void MapblockMeshGenerator::renderSingle(content_t node, u8 param2)
  1581. {
  1582. cur_node.p = {0, 0, 0};
  1583. cur_node.n = MapNode(node, 0xff, param2);
  1584. cur_node.f = &nodedef->get(cur_node.n);
  1585. drawNode();
  1586. }