wieldmesh.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 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 "wieldmesh.h"
  17. #include "settings.h"
  18. #include "shader.h"
  19. #include "inventory.h"
  20. #include "client.h"
  21. #include "itemdef.h"
  22. #include "nodedef.h"
  23. #include "mesh.h"
  24. #include "content_mapblock.h"
  25. #include "mapblock_mesh.h"
  26. #include "client/meshgen/collector.h"
  27. #include "client/tile.h"
  28. #include "log.h"
  29. #include "util/numeric.h"
  30. #include <map>
  31. #include <IMeshManipulator.h>
  32. #include "client/renderingengine.h"
  33. #define WIELD_SCALE_FACTOR 30.0
  34. #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
  35. #define MIN_EXTRUSION_MESH_RESOLUTION 16
  36. #define MAX_EXTRUSION_MESH_RESOLUTION 512
  37. static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
  38. {
  39. const f32 r = 0.5;
  40. scene::IMeshBuffer *buf = new scene::SMeshBuffer();
  41. video::SColor c(255,255,255,255);
  42. v3f scale(1.0, 1.0, 0.1);
  43. // Front and back
  44. {
  45. video::S3DVertex vertices[8] = {
  46. // z-
  47. video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
  48. video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
  49. video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
  50. video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
  51. // z+
  52. video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
  53. video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
  54. video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
  55. video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
  56. };
  57. u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
  58. buf->append(vertices, 8, indices, 12);
  59. }
  60. f32 pixelsize_x = 1 / (f32) resolution_x;
  61. f32 pixelsize_y = 1 / (f32) resolution_y;
  62. for (int i = 0; i < resolution_x; ++i) {
  63. f32 pixelpos_x = i * pixelsize_x - 0.5;
  64. f32 x0 = pixelpos_x;
  65. f32 x1 = pixelpos_x + pixelsize_x;
  66. f32 tex0 = (i + 0.1) * pixelsize_x;
  67. f32 tex1 = (i + 0.9) * pixelsize_x;
  68. video::S3DVertex vertices[8] = {
  69. // x-
  70. video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
  71. video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
  72. video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
  73. video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
  74. // x+
  75. video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
  76. video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
  77. video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
  78. video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
  79. };
  80. u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
  81. buf->append(vertices, 8, indices, 12);
  82. }
  83. for (int i = 0; i < resolution_y; ++i) {
  84. f32 pixelpos_y = i * pixelsize_y - 0.5;
  85. f32 y0 = -pixelpos_y - pixelsize_y;
  86. f32 y1 = -pixelpos_y;
  87. f32 tex0 = (i + 0.1) * pixelsize_y;
  88. f32 tex1 = (i + 0.9) * pixelsize_y;
  89. video::S3DVertex vertices[8] = {
  90. // y-
  91. video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
  92. video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
  93. video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
  94. video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
  95. // y+
  96. video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
  97. video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
  98. video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
  99. video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
  100. };
  101. u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
  102. buf->append(vertices, 8, indices, 12);
  103. }
  104. // Create mesh object
  105. scene::SMesh *mesh = new scene::SMesh();
  106. mesh->addMeshBuffer(buf);
  107. buf->drop();
  108. scaleMesh(mesh, scale); // also recalculates bounding box
  109. return mesh;
  110. }
  111. /*
  112. Caches extrusion meshes so that only one of them per resolution
  113. is needed. Also caches one cube (for convenience).
  114. E.g. there is a single extrusion mesh that is used for all
  115. 16x16 px images, another for all 256x256 px images, and so on.
  116. WARNING: Not thread safe. This should not be a problem since
  117. rendering related classes (such as WieldMeshSceneNode) will be
  118. used from the rendering thread only.
  119. */
  120. class ExtrusionMeshCache: public IReferenceCounted
  121. {
  122. public:
  123. // Constructor
  124. ExtrusionMeshCache()
  125. {
  126. for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
  127. resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
  128. resolution *= 2) {
  129. m_extrusion_meshes[resolution] =
  130. createExtrusionMesh(resolution, resolution);
  131. }
  132. m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
  133. }
  134. // Destructor
  135. virtual ~ExtrusionMeshCache()
  136. {
  137. for (auto &extrusion_meshe : m_extrusion_meshes) {
  138. extrusion_meshe.second->drop();
  139. }
  140. m_cube->drop();
  141. }
  142. // Get closest extrusion mesh for given image dimensions
  143. // Caller must drop the returned pointer
  144. scene::IMesh* create(core::dimension2d<u32> dim)
  145. {
  146. // handle non-power of two textures inefficiently without cache
  147. if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) {
  148. return createExtrusionMesh(dim.Width, dim.Height);
  149. }
  150. int maxdim = MYMAX(dim.Width, dim.Height);
  151. std::map<int, scene::IMesh*>::iterator
  152. it = m_extrusion_meshes.lower_bound(maxdim);
  153. if (it == m_extrusion_meshes.end()) {
  154. // no viable resolution found; use largest one
  155. it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION);
  156. sanity_check(it != m_extrusion_meshes.end());
  157. }
  158. scene::IMesh *mesh = it->second;
  159. mesh->grab();
  160. return mesh;
  161. }
  162. // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
  163. // Caller must drop the returned pointer
  164. scene::IMesh* createCube()
  165. {
  166. m_cube->grab();
  167. return m_cube;
  168. }
  169. private:
  170. std::map<int, scene::IMesh*> m_extrusion_meshes;
  171. scene::IMesh *m_cube;
  172. };
  173. ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
  174. WieldMeshSceneNode::WieldMeshSceneNode(scene::ISceneManager *mgr, s32 id, bool lighting):
  175. scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
  176. m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
  177. m_lighting(lighting)
  178. {
  179. m_enable_shaders = g_settings->getBool("enable_shaders");
  180. m_anisotropic_filter = g_settings->getBool("anisotropic_filter");
  181. m_bilinear_filter = g_settings->getBool("bilinear_filter");
  182. m_trilinear_filter = g_settings->getBool("trilinear_filter");
  183. // If this is the first wield mesh scene node, create a cache
  184. // for extrusion meshes (and a cube mesh), otherwise reuse it
  185. if (!g_extrusion_mesh_cache)
  186. g_extrusion_mesh_cache = new ExtrusionMeshCache();
  187. else
  188. g_extrusion_mesh_cache->grab();
  189. // Disable bounding box culling for this scene node
  190. // since we won't calculate the bounding box.
  191. setAutomaticCulling(scene::EAC_OFF);
  192. // Create the child scene node
  193. scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
  194. m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1);
  195. m_meshnode->setReadOnlyMaterials(false);
  196. m_meshnode->setVisible(false);
  197. dummymesh->drop(); // m_meshnode grabbed it
  198. m_shadow = RenderingEngine::get_shadow_renderer();
  199. if (m_shadow) {
  200. // Add mesh to shadow caster
  201. m_shadow->addNodeToShadowList(m_meshnode);
  202. }
  203. }
  204. WieldMeshSceneNode::~WieldMeshSceneNode()
  205. {
  206. sanity_check(g_extrusion_mesh_cache);
  207. // Remove node from shadow casters. m_shadow might be an invalid pointer!
  208. if (m_shadow)
  209. m_shadow->removeNodeFromShadowList(m_meshnode);
  210. if (g_extrusion_mesh_cache->drop())
  211. g_extrusion_mesh_cache = nullptr;
  212. }
  213. void WieldMeshSceneNode::setCube(const ContentFeatures &f,
  214. v3f wield_scale)
  215. {
  216. scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
  217. scene::SMesh *copy = cloneMesh(cubemesh);
  218. cubemesh->drop();
  219. postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors, true);
  220. changeToMesh(copy);
  221. copy->drop();
  222. m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
  223. }
  224. void WieldMeshSceneNode::setExtruded(const std::string &imagename,
  225. const std::string &overlay_name, v3f wield_scale, ITextureSource *tsrc,
  226. u8 num_frames)
  227. {
  228. video::ITexture *texture = tsrc->getTexture(imagename);
  229. if (!texture) {
  230. changeToMesh(nullptr);
  231. return;
  232. }
  233. video::ITexture *overlay_texture =
  234. overlay_name.empty() ? NULL : tsrc->getTexture(overlay_name);
  235. core::dimension2d<u32> dim = texture->getSize();
  236. // Detect animation texture and pull off top frame instead of using entire thing
  237. if (num_frames > 1) {
  238. u32 frame_height = dim.Height / num_frames;
  239. dim = core::dimension2d<u32>(dim.Width, frame_height);
  240. }
  241. scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
  242. scene::SMesh *mesh = cloneMesh(original);
  243. original->drop();
  244. //set texture
  245. mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
  246. tsrc->getTexture(imagename));
  247. if (overlay_texture) {
  248. scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
  249. copy->getMaterial().setTexture(0, overlay_texture);
  250. mesh->addMeshBuffer(copy);
  251. copy->drop();
  252. }
  253. changeToMesh(mesh);
  254. mesh->drop();
  255. m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
  256. // Customize materials
  257. for (u32 layer = 0; layer < m_meshnode->getMaterialCount(); layer++) {
  258. video::SMaterial &material = m_meshnode->getMaterial(layer);
  259. material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  260. material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  261. material.MaterialType = m_material_type;
  262. material.MaterialTypeParam = 0.5f;
  263. material.BackfaceCulling = true;
  264. // Enable bi/trilinear filtering only for high resolution textures
  265. bool bilinear_filter = dim.Width > 32 && m_bilinear_filter;
  266. bool trilinear_filter = dim.Width > 32 && m_trilinear_filter;
  267. material.forEachTexture([=] (auto &tex) {
  268. setMaterialFilters(tex, bilinear_filter, trilinear_filter,
  269. m_anisotropic_filter);
  270. });
  271. // mipmaps cause "thin black line" artifacts
  272. material.UseMipMaps = false;
  273. if (m_enable_shaders) {
  274. material.setTexture(2, tsrc->getShaderFlagsTexture(false));
  275. }
  276. }
  277. }
  278. static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
  279. std::vector<ItemPartColor> *colors, const ContentFeatures &f)
  280. {
  281. MeshMakeData mesh_make_data(client->ndef(), 1, false);
  282. MeshCollector collector(v3f(0.0f * BS), v3f());
  283. mesh_make_data.setSmoothLighting(false);
  284. MapblockMeshGenerator gen(&mesh_make_data, &collector,
  285. client->getSceneManager()->getMeshManipulator());
  286. if (n.getParam2()) {
  287. // keep it
  288. } else if (f.param_type_2 == CPT2_WALLMOUNTED ||
  289. f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
  290. if (f.drawtype == NDT_TORCHLIKE ||
  291. f.drawtype == NDT_SIGNLIKE ||
  292. f.drawtype == NDT_NODEBOX ||
  293. f.drawtype == NDT_MESH) {
  294. n.setParam2(4);
  295. }
  296. } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE) {
  297. n.setParam2(1);
  298. }
  299. gen.renderSingle(n.getContent(), n.getParam2());
  300. colors->clear();
  301. scene::SMesh *mesh = new scene::SMesh();
  302. for (auto &prebuffers : collector.prebuffers)
  303. for (PreMeshBuffer &p : prebuffers) {
  304. if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
  305. const FrameSpec &frame = (*p.layer.frames)[0];
  306. p.layer.texture = frame.texture;
  307. p.layer.normal_texture = frame.normal_texture;
  308. }
  309. for (video::S3DVertex &v : p.vertices) {
  310. v.Color.setAlpha(255);
  311. }
  312. scene::SMeshBuffer *buf = new scene::SMeshBuffer();
  313. buf->Material.setTexture(0, p.layer.texture);
  314. p.layer.applyMaterialOptions(buf->Material);
  315. mesh->addMeshBuffer(buf);
  316. buf->append(&p.vertices[0], p.vertices.size(),
  317. &p.indices[0], p.indices.size());
  318. buf->drop();
  319. colors->push_back(
  320. ItemPartColor(p.layer.has_color, p.layer.color));
  321. }
  322. return mesh;
  323. }
  324. void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool check_wield_image)
  325. {
  326. ITextureSource *tsrc = client->getTextureSource();
  327. IItemDefManager *idef = client->getItemDefManager();
  328. IShaderSource *shdrsrc = client->getShaderSource();
  329. const NodeDefManager *ndef = client->getNodeDefManager();
  330. const ItemDefinition &def = item.getDefinition(idef);
  331. const ContentFeatures &f = ndef->get(def.name);
  332. content_t id = ndef->getId(def.name);
  333. scene::SMesh *mesh = nullptr;
  334. if (m_enable_shaders) {
  335. u32 shader_id = shdrsrc->getShader("object_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
  336. m_material_type = shdrsrc->getShaderInfo(shader_id).material;
  337. }
  338. // Color-related
  339. m_colors.clear();
  340. m_base_color = idef->getItemstackColor(item, client);
  341. const std::string wield_image = item.getWieldImage(idef);
  342. const std::string wield_overlay = item.getWieldOverlay(idef);
  343. const v3f wield_scale = item.getWieldScale(idef);
  344. // If wield_image needs to be checked and is defined, it overrides everything else
  345. if (!wield_image.empty() && check_wield_image) {
  346. setExtruded(wield_image, wield_overlay, wield_scale, tsrc,
  347. 1);
  348. m_colors.emplace_back();
  349. // overlay is white, if present
  350. m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  351. // initialize the color
  352. if (!m_lighting)
  353. setColor(video::SColor(0xFFFFFFFF));
  354. return;
  355. }
  356. // Handle nodes
  357. // See also CItemDefManager::createClientCached()
  358. if (def.type == ITEM_NODE) {
  359. bool cull_backface = f.needsBackfaceCulling();
  360. // Select rendering method
  361. switch (f.drawtype) {
  362. case NDT_AIRLIKE:
  363. setExtruded("no_texture_airlike.png", "",
  364. v3f(1.0, 1.0, 1.0), tsrc, 1);
  365. break;
  366. case NDT_SIGNLIKE:
  367. case NDT_TORCHLIKE:
  368. case NDT_RAILLIKE:
  369. case NDT_PLANTLIKE:
  370. case NDT_FLOWINGLIQUID: {
  371. v3f wscale = wield_scale;
  372. if (f.drawtype == NDT_FLOWINGLIQUID)
  373. wscale.Z *= 0.1f;
  374. setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
  375. tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
  376. wscale, tsrc,
  377. f.tiles[0].layers[0].animation_frame_count);
  378. // Add color
  379. const TileLayer &l0 = f.tiles[0].layers[0];
  380. m_colors.emplace_back(l0.has_color, l0.color);
  381. const TileLayer &l1 = f.tiles[0].layers[1];
  382. m_colors.emplace_back(l1.has_color, l1.color);
  383. break;
  384. }
  385. case NDT_PLANTLIKE_ROOTED: {
  386. setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
  387. "", wield_scale, tsrc,
  388. f.special_tiles[0].layers[0].animation_frame_count);
  389. // Add color
  390. const TileLayer &l0 = f.special_tiles[0].layers[0];
  391. m_colors.emplace_back(l0.has_color, l0.color);
  392. break;
  393. }
  394. case NDT_NORMAL:
  395. case NDT_ALLFACES:
  396. case NDT_LIQUID:
  397. setCube(f, wield_scale);
  398. break;
  399. default: {
  400. // Render non-trivial drawtypes like the actual node
  401. MapNode n(id);
  402. if (def.place_param2)
  403. n.setParam2(*def.place_param2);
  404. mesh = createSpecialNodeMesh(client, n, &m_colors, f);
  405. changeToMesh(mesh);
  406. mesh->drop();
  407. m_meshnode->setScale(
  408. wield_scale * WIELD_SCALE_FACTOR
  409. / (BS * f.visual_scale));
  410. break;
  411. }
  412. }
  413. u32 material_count = m_meshnode->getMaterialCount();
  414. for (u32 i = 0; i < material_count; ++i) {
  415. video::SMaterial &material = m_meshnode->getMaterial(i);
  416. material.MaterialType = m_material_type;
  417. material.MaterialTypeParam = 0.5f;
  418. material.BackfaceCulling = cull_backface;
  419. material.forEachTexture([this] (auto &tex) {
  420. setMaterialFilters(tex, m_bilinear_filter, m_trilinear_filter,
  421. m_anisotropic_filter);
  422. });
  423. }
  424. // initialize the color
  425. if (!m_lighting)
  426. setColor(video::SColor(0xFFFFFFFF));
  427. return;
  428. } else {
  429. const std::string inventory_image = item.getInventoryImage(idef);
  430. if (!inventory_image.empty()) {
  431. const std::string inventory_overlay = item.getInventoryOverlay(idef);
  432. setExtruded(inventory_image, inventory_overlay, def.wield_scale, tsrc, 1);
  433. } else {
  434. setExtruded("no_texture.png", "", def.wield_scale, tsrc, 1);
  435. }
  436. m_colors.emplace_back();
  437. // overlay is white, if present
  438. m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  439. // initialize the color
  440. if (!m_lighting)
  441. setColor(video::SColor(0xFFFFFFFF));
  442. return;
  443. }
  444. // no wield mesh found
  445. changeToMesh(nullptr);
  446. }
  447. void WieldMeshSceneNode::setColor(video::SColor c)
  448. {
  449. assert(!m_lighting);
  450. scene::IMesh *mesh = m_meshnode->getMesh();
  451. if (!mesh)
  452. return;
  453. u8 red = c.getRed();
  454. u8 green = c.getGreen();
  455. u8 blue = c.getBlue();
  456. const u32 mc = mesh->getMeshBufferCount();
  457. if (mc > m_colors.size())
  458. m_colors.resize(mc);
  459. for (u32 j = 0; j < mc; j++) {
  460. video::SColor bc(m_base_color);
  461. m_colors[j].applyOverride(bc);
  462. video::SColor buffercolor(255,
  463. bc.getRed() * red / 255,
  464. bc.getGreen() * green / 255,
  465. bc.getBlue() * blue / 255);
  466. scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
  467. if (m_colors[j].needColorize(buffercolor)) {
  468. buf->setDirty(scene::EBT_VERTEX);
  469. if (m_enable_shaders)
  470. setMeshBufferColor(buf, buffercolor);
  471. else
  472. colorizeMeshBuffer(buf, &buffercolor);
  473. }
  474. }
  475. }
  476. void WieldMeshSceneNode::setNodeLightColor(video::SColor color)
  477. {
  478. if (!m_meshnode)
  479. return;
  480. if (m_enable_shaders) {
  481. for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
  482. video::SMaterial &material = m_meshnode->getMaterial(i);
  483. material.EmissiveColor = color;
  484. }
  485. } else {
  486. setColor(color);
  487. }
  488. }
  489. void WieldMeshSceneNode::render()
  490. {
  491. // note: if this method is changed to actually do something,
  492. // you probably should implement OnRegisterSceneNode as well
  493. }
  494. void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
  495. {
  496. if (!mesh) {
  497. scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
  498. m_meshnode->setVisible(false);
  499. m_meshnode->setMesh(dummymesh);
  500. dummymesh->drop(); // m_meshnode grabbed it
  501. } else {
  502. m_meshnode->setMesh(mesh);
  503. // without shaders recolored often for lighting
  504. // otherwise only once
  505. if (m_enable_shaders)
  506. mesh->setHardwareMappingHint(scene::EHM_STATIC);
  507. else
  508. mesh->setHardwareMappingHint(scene::EHM_DYNAMIC);
  509. }
  510. m_meshnode->forEachMaterial([this] (auto &mat) {
  511. mat.Lighting = m_lighting;
  512. // need to normalize normals when lighting is enabled (because of setScale())
  513. mat.NormalizeNormals = m_lighting;
  514. });
  515. m_meshnode->setVisible(true);
  516. }
  517. void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
  518. {
  519. ITextureSource *tsrc = client->getTextureSource();
  520. IItemDefManager *idef = client->getItemDefManager();
  521. const NodeDefManager *ndef = client->getNodeDefManager();
  522. const ItemDefinition &def = item.getDefinition(idef);
  523. const ContentFeatures &f = ndef->get(def.name);
  524. content_t id = ndef->getId(def.name);
  525. FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
  526. scene::SMesh *mesh = nullptr;
  527. // Shading is on by default
  528. result->needs_shading = true;
  529. bool cull_backface = f.needsBackfaceCulling();
  530. // If inventory_image is defined, it overrides everything else
  531. const std::string inventory_image = item.getInventoryImage(idef);
  532. const std::string inventory_overlay = item.getInventoryOverlay(idef);
  533. if (!inventory_image.empty()) {
  534. mesh = getExtrudedMesh(tsrc, inventory_image, inventory_overlay);
  535. result->buffer_colors.emplace_back();
  536. // overlay is white, if present
  537. result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  538. // Items with inventory images do not need shading
  539. result->needs_shading = false;
  540. } else if (def.type == ITEM_NODE && f.drawtype == NDT_AIRLIKE) {
  541. // Fallback image for airlike node
  542. mesh = getExtrudedMesh(tsrc, "no_texture_airlike.png", inventory_overlay);
  543. result->needs_shading = false;
  544. } else if (def.type == ITEM_NODE) {
  545. switch (f.drawtype) {
  546. case NDT_NORMAL:
  547. case NDT_ALLFACES:
  548. case NDT_LIQUID:
  549. case NDT_FLOWINGLIQUID: {
  550. scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
  551. mesh = cloneMesh(cube);
  552. cube->drop();
  553. if (f.drawtype == NDT_FLOWINGLIQUID) {
  554. scaleMesh(mesh, v3f(1.2, 0.03, 1.2));
  555. translateMesh(mesh, v3f(0, -0.57, 0));
  556. } else
  557. scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
  558. // add overlays
  559. postProcessNodeMesh(mesh, f, false, false, nullptr,
  560. &result->buffer_colors, true);
  561. if (f.drawtype == NDT_ALLFACES)
  562. scaleMesh(mesh, v3f(f.visual_scale));
  563. break;
  564. }
  565. case NDT_PLANTLIKE: {
  566. mesh = getExtrudedMesh(tsrc,
  567. tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
  568. tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
  569. // Add color
  570. const TileLayer &l0 = f.tiles[0].layers[0];
  571. result->buffer_colors.emplace_back(l0.has_color, l0.color);
  572. const TileLayer &l1 = f.tiles[0].layers[1];
  573. result->buffer_colors.emplace_back(l1.has_color, l1.color);
  574. break;
  575. }
  576. case NDT_PLANTLIKE_ROOTED: {
  577. mesh = getExtrudedMesh(tsrc,
  578. tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
  579. // Add color
  580. const TileLayer &l0 = f.special_tiles[0].layers[0];
  581. result->buffer_colors.emplace_back(l0.has_color, l0.color);
  582. break;
  583. }
  584. default: {
  585. // Render non-trivial drawtypes like the actual node
  586. MapNode n(id);
  587. if (def.place_param2)
  588. n.setParam2(*def.place_param2);
  589. mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
  590. scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
  591. break;
  592. }
  593. }
  594. for (u32 i = 0; i < mesh->getMeshBufferCount(); ++i) {
  595. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  596. video::SMaterial &material = buf->getMaterial();
  597. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  598. material.MaterialTypeParam = 0.5f;
  599. material.forEachTexture([] (auto &tex) {
  600. tex.MinFilter = video::ETMINF_NEAREST_MIPMAP_NEAREST;
  601. tex.MagFilter = video::ETMAGF_NEAREST;
  602. });
  603. material.BackfaceCulling = cull_backface;
  604. material.Lighting = false;
  605. }
  606. rotateMeshXZby(mesh, -45);
  607. rotateMeshYZby(mesh, -30);
  608. }
  609. // might need to be re-colorized, this is done only when needed
  610. if (mesh) {
  611. mesh->setHardwareMappingHint(scene::EHM_DYNAMIC, scene::EBT_VERTEX);
  612. mesh->setHardwareMappingHint(scene::EHM_STATIC, scene::EBT_INDEX);
  613. }
  614. result->mesh = mesh;
  615. }
  616. scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
  617. const std::string &imagename, const std::string &overlay_name)
  618. {
  619. // check textures
  620. video::ITexture *texture = tsrc->getTextureForMesh(imagename);
  621. if (!texture) {
  622. return NULL;
  623. }
  624. video::ITexture *overlay_texture =
  625. (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
  626. // get mesh
  627. core::dimension2d<u32> dim = texture->getSize();
  628. scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
  629. scene::SMesh *mesh = cloneMesh(original);
  630. original->drop();
  631. //set texture
  632. mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
  633. tsrc->getTexture(imagename));
  634. if (overlay_texture) {
  635. scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
  636. copy->getMaterial().setTexture(0, overlay_texture);
  637. mesh->addMeshBuffer(copy);
  638. copy->drop();
  639. }
  640. // Customize materials
  641. for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
  642. video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
  643. material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  644. material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  645. material.forEachTexture([] (auto &tex) {
  646. tex.MinFilter = video::ETMINF_NEAREST_MIPMAP_NEAREST;
  647. tex.MagFilter = video::ETMAGF_NEAREST;
  648. });
  649. material.BackfaceCulling = true;
  650. material.Lighting = false;
  651. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  652. material.MaterialTypeParam = 0.5f;
  653. }
  654. scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
  655. return mesh;
  656. }
  657. void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
  658. bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
  659. std::vector<ItemPartColor> *colors, bool apply_scale)
  660. {
  661. const u32 mc = mesh->getMeshBufferCount();
  662. // Allocate colors for existing buffers
  663. colors->clear();
  664. colors->resize(mc);
  665. for (u32 i = 0; i < mc; ++i) {
  666. const TileSpec *tile = &(f.tiles[i]);
  667. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  668. for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
  669. const TileLayer *layer = &tile->layers[layernum];
  670. if (layer->texture_id == 0)
  671. continue;
  672. if (layernum != 0) {
  673. scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
  674. copy->getMaterial() = buf->getMaterial();
  675. mesh->addMeshBuffer(copy);
  676. copy->drop();
  677. buf = copy;
  678. colors->emplace_back(layer->has_color, layer->color);
  679. } else {
  680. (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
  681. }
  682. video::SMaterial &material = buf->getMaterial();
  683. if (set_material)
  684. layer->applyMaterialOptions(material);
  685. if (mattype) {
  686. material.MaterialType = *mattype;
  687. }
  688. if (layer->animation_frame_count > 1) {
  689. const FrameSpec &animation_frame = (*layer->frames)[0];
  690. material.setTexture(0, animation_frame.texture);
  691. } else {
  692. material.setTexture(0, layer->texture);
  693. }
  694. if (use_shaders) {
  695. if (layer->normal_texture) {
  696. if (layer->animation_frame_count > 1) {
  697. const FrameSpec &animation_frame = (*layer->frames)[0];
  698. material.setTexture(1, animation_frame.normal_texture);
  699. } else
  700. material.setTexture(1, layer->normal_texture);
  701. }
  702. material.setTexture(2, layer->flags_texture);
  703. }
  704. if (apply_scale && tile->world_aligned) {
  705. u32 n = buf->getVertexCount();
  706. for (u32 k = 0; k != n; ++k)
  707. buf->getTCoords(k) /= layer->scale;
  708. }
  709. }
  710. }
  711. }