wieldmesh.cpp 22 KB

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