wieldmesh.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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.setFlag(video::EMF_BACK_FACE_CULLING, true);
  254. // Enable bi/trilinear filtering only for high resolution textures
  255. if (dim.Width > 32) {
  256. material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
  257. material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
  258. } else {
  259. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  260. material.setFlag(video::EMF_TRILINEAR_FILTER, false);
  261. }
  262. material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
  263. // mipmaps cause "thin black line" artifacts
  264. #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
  265. material.setFlag(video::EMF_USE_MIP_MAPS, false);
  266. #endif
  267. if (m_enable_shaders) {
  268. material.setTexture(2, tsrc->getShaderFlagsTexture(false));
  269. }
  270. }
  271. }
  272. scene::SMesh *createSpecialNodeMesh(Client *client, content_t id, std::vector<ItemPartColor> *colors)
  273. {
  274. MeshMakeData mesh_make_data(client, false, false);
  275. MeshCollector collector;
  276. mesh_make_data.setSmoothLighting(false);
  277. MapblockMeshGenerator gen(&mesh_make_data, &collector);
  278. gen.renderSingle(id);
  279. colors->clear();
  280. scene::SMesh *mesh = new scene::SMesh();
  281. for (auto &prebuffers : collector.prebuffers)
  282. for (PreMeshBuffer &p : prebuffers) {
  283. if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
  284. const FrameSpec &frame = (*p.layer.frames)[0];
  285. p.layer.texture = frame.texture;
  286. p.layer.normal_texture = frame.normal_texture;
  287. }
  288. for (video::S3DVertex &v : p.vertices)
  289. v.Color.setAlpha(255);
  290. scene::SMeshBuffer *buf = new scene::SMeshBuffer();
  291. buf->Material.setTexture(0, p.layer.texture);
  292. p.layer.applyMaterialOptions(buf->Material);
  293. mesh->addMeshBuffer(buf);
  294. buf->append(&p.vertices[0], p.vertices.size(),
  295. &p.indices[0], p.indices.size());
  296. buf->drop();
  297. colors->push_back(
  298. ItemPartColor(p.layer.has_color, p.layer.color));
  299. }
  300. return mesh;
  301. }
  302. void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
  303. {
  304. ITextureSource *tsrc = client->getTextureSource();
  305. IItemDefManager *idef = client->getItemDefManager();
  306. IShaderSource *shdrsrc = client->getShaderSource();
  307. const NodeDefManager *ndef = client->getNodeDefManager();
  308. const ItemDefinition &def = item.getDefinition(idef);
  309. const ContentFeatures &f = ndef->get(def.name);
  310. content_t id = ndef->getId(def.name);
  311. scene::SMesh *mesh = nullptr;
  312. if (m_enable_shaders) {
  313. u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
  314. m_material_type = shdrsrc->getShaderInfo(shader_id).material;
  315. }
  316. // Color-related
  317. m_colors.clear();
  318. m_base_color = idef->getItemstackColor(item, client);
  319. // If wield_image is defined, it overrides everything else
  320. if (!def.wield_image.empty()) {
  321. setExtruded(def.wield_image, def.wield_overlay, def.wield_scale, tsrc,
  322. 1);
  323. m_colors.emplace_back();
  324. // overlay is white, if present
  325. m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  326. return;
  327. }
  328. // Handle nodes
  329. // See also CItemDefManager::createClientCached()
  330. if (def.type == ITEM_NODE) {
  331. if (f.mesh_ptr[0]) {
  332. // e.g. mesh nodes and nodeboxes
  333. mesh = cloneMesh(f.mesh_ptr[0]);
  334. postProcessNodeMesh(mesh, f, m_enable_shaders, true,
  335. &m_material_type, &m_colors);
  336. changeToMesh(mesh);
  337. mesh->drop();
  338. // mesh is pre-scaled by BS * f->visual_scale
  339. m_meshnode->setScale(
  340. def.wield_scale * WIELD_SCALE_FACTOR
  341. / (BS * f.visual_scale));
  342. } else {
  343. switch (f.drawtype) {
  344. case NDT_AIRLIKE: {
  345. changeToMesh(nullptr);
  346. break;
  347. }
  348. case NDT_PLANTLIKE: {
  349. setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
  350. tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
  351. def.wield_scale, tsrc,
  352. f.tiles[0].layers[0].animation_frame_count);
  353. // Add color
  354. const TileLayer &l0 = f.tiles[0].layers[0];
  355. m_colors.emplace_back(l0.has_color, l0.color);
  356. const TileLayer &l1 = f.tiles[0].layers[1];
  357. m_colors.emplace_back(l1.has_color, l1.color);
  358. break;
  359. }
  360. case NDT_PLANTLIKE_ROOTED: {
  361. setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
  362. "", def.wield_scale, tsrc,
  363. f.special_tiles[0].layers[0].animation_frame_count);
  364. // Add color
  365. const TileLayer &l0 = f.special_tiles[0].layers[0];
  366. m_colors.emplace_back(l0.has_color, l0.color);
  367. break;
  368. }
  369. case NDT_NORMAL:
  370. case NDT_ALLFACES:
  371. case NDT_LIQUID:
  372. case NDT_FLOWINGLIQUID: {
  373. setCube(f, def.wield_scale);
  374. break;
  375. }
  376. default: {
  377. mesh = createSpecialNodeMesh(client, id, &m_colors);
  378. changeToMesh(mesh);
  379. mesh->drop();
  380. m_meshnode->setScale(
  381. def.wield_scale * WIELD_SCALE_FACTOR
  382. / (BS * f.visual_scale));
  383. }
  384. }
  385. }
  386. u32 material_count = m_meshnode->getMaterialCount();
  387. for (u32 i = 0; i < material_count; ++i) {
  388. video::SMaterial &material = m_meshnode->getMaterial(i);
  389. material.MaterialType = m_material_type;
  390. material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  391. material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
  392. material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
  393. }
  394. return;
  395. }
  396. else if (!def.inventory_image.empty()) {
  397. setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
  398. tsrc, 1);
  399. m_colors.emplace_back();
  400. // overlay is white, if present
  401. m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  402. return;
  403. }
  404. // no wield mesh found
  405. changeToMesh(nullptr);
  406. }
  407. void WieldMeshSceneNode::setColor(video::SColor c)
  408. {
  409. assert(!m_lighting);
  410. scene::IMesh *mesh = m_meshnode->getMesh();
  411. if (!mesh)
  412. return;
  413. u8 red = c.getRed();
  414. u8 green = c.getGreen();
  415. u8 blue = c.getBlue();
  416. u32 mc = mesh->getMeshBufferCount();
  417. for (u32 j = 0; j < mc; j++) {
  418. video::SColor bc(m_base_color);
  419. if ((m_colors.size() > j) && (m_colors[j].override_base))
  420. bc = m_colors[j].color;
  421. video::SColor buffercolor(255,
  422. bc.getRed() * red / 255,
  423. bc.getGreen() * green / 255,
  424. bc.getBlue() * blue / 255);
  425. scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
  426. colorizeMeshBuffer(buf, &buffercolor);
  427. }
  428. }
  429. void WieldMeshSceneNode::render()
  430. {
  431. // note: if this method is changed to actually do something,
  432. // you probably should implement OnRegisterSceneNode as well
  433. }
  434. void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
  435. {
  436. if (!mesh) {
  437. scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
  438. m_meshnode->setVisible(false);
  439. m_meshnode->setMesh(dummymesh);
  440. dummymesh->drop(); // m_meshnode grabbed it
  441. } else {
  442. m_meshnode->setMesh(mesh);
  443. }
  444. m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
  445. // need to normalize normals when lighting is enabled (because of setScale())
  446. m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
  447. m_meshnode->setVisible(true);
  448. }
  449. void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
  450. {
  451. ITextureSource *tsrc = client->getTextureSource();
  452. IItemDefManager *idef = client->getItemDefManager();
  453. const NodeDefManager *ndef = client->getNodeDefManager();
  454. const ItemDefinition &def = item.getDefinition(idef);
  455. const ContentFeatures &f = ndef->get(def.name);
  456. content_t id = ndef->getId(def.name);
  457. FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
  458. scene::SMesh *mesh = nullptr;
  459. // Shading is on by default
  460. result->needs_shading = true;
  461. // If inventory_image is defined, it overrides everything else
  462. if (!def.inventory_image.empty()) {
  463. mesh = getExtrudedMesh(tsrc, def.inventory_image,
  464. def.inventory_overlay);
  465. result->buffer_colors.emplace_back();
  466. // overlay is white, if present
  467. result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
  468. // Items with inventory images do not need shading
  469. result->needs_shading = false;
  470. } else if (def.type == ITEM_NODE) {
  471. if (f.mesh_ptr[0]) {
  472. mesh = cloneMesh(f.mesh_ptr[0]);
  473. scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
  474. postProcessNodeMesh(mesh, f, false, false, nullptr,
  475. &result->buffer_colors);
  476. } else {
  477. switch (f.drawtype) {
  478. case NDT_PLANTLIKE: {
  479. mesh = getExtrudedMesh(tsrc,
  480. tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
  481. tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
  482. // Add color
  483. const TileLayer &l0 = f.tiles[0].layers[0];
  484. result->buffer_colors.emplace_back(l0.has_color, l0.color);
  485. const TileLayer &l1 = f.tiles[0].layers[1];
  486. result->buffer_colors.emplace_back(l1.has_color, l1.color);
  487. break;
  488. }
  489. case NDT_PLANTLIKE_ROOTED: {
  490. mesh = getExtrudedMesh(tsrc,
  491. tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
  492. // Add color
  493. const TileLayer &l0 = f.special_tiles[0].layers[0];
  494. result->buffer_colors.emplace_back(l0.has_color, l0.color);
  495. break;
  496. }
  497. case NDT_NORMAL:
  498. case NDT_ALLFACES:
  499. case NDT_LIQUID:
  500. case NDT_FLOWINGLIQUID: {
  501. scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
  502. mesh = cloneMesh(cube);
  503. cube->drop();
  504. scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
  505. // add overlays
  506. postProcessNodeMesh(mesh, f, false, false, nullptr,
  507. &result->buffer_colors);
  508. break;
  509. }
  510. default: {
  511. mesh = createSpecialNodeMesh(client, id, &result->buffer_colors);
  512. scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
  513. }
  514. }
  515. }
  516. u32 mc = mesh->getMeshBufferCount();
  517. for (u32 i = 0; i < mc; ++i) {
  518. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  519. video::SMaterial &material = buf->getMaterial();
  520. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  521. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  522. material.setFlag(video::EMF_TRILINEAR_FILTER, false);
  523. material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  524. material.setFlag(video::EMF_LIGHTING, false);
  525. }
  526. rotateMeshXZby(mesh, -45);
  527. rotateMeshYZby(mesh, -30);
  528. }
  529. result->mesh = mesh;
  530. }
  531. scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
  532. const std::string &imagename, const std::string &overlay_name)
  533. {
  534. // check textures
  535. video::ITexture *texture = tsrc->getTextureForMesh(imagename);
  536. if (!texture) {
  537. return NULL;
  538. }
  539. video::ITexture *overlay_texture =
  540. (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
  541. // get mesh
  542. core::dimension2d<u32> dim = texture->getSize();
  543. scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
  544. scene::SMesh *mesh = cloneMesh(original);
  545. original->drop();
  546. //set texture
  547. mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
  548. tsrc->getTexture(imagename));
  549. if (overlay_texture) {
  550. scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
  551. copy->getMaterial().setTexture(0, overlay_texture);
  552. mesh->addMeshBuffer(copy);
  553. copy->drop();
  554. }
  555. // Customize materials
  556. for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
  557. video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
  558. material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  559. material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  560. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  561. material.setFlag(video::EMF_TRILINEAR_FILTER, false);
  562. material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  563. material.setFlag(video::EMF_LIGHTING, false);
  564. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  565. }
  566. scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
  567. return mesh;
  568. }
  569. void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
  570. bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
  571. std::vector<ItemPartColor> *colors, bool apply_scale)
  572. {
  573. u32 mc = mesh->getMeshBufferCount();
  574. // Allocate colors for existing buffers
  575. colors->clear();
  576. for (u32 i = 0; i < mc; ++i)
  577. colors->push_back(ItemPartColor());
  578. for (u32 i = 0; i < mc; ++i) {
  579. const TileSpec *tile = &(f.tiles[i]);
  580. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  581. for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
  582. const TileLayer *layer = &tile->layers[layernum];
  583. if (layer->texture_id == 0)
  584. continue;
  585. if (layernum != 0) {
  586. scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
  587. copy->getMaterial() = buf->getMaterial();
  588. mesh->addMeshBuffer(copy);
  589. copy->drop();
  590. buf = copy;
  591. colors->push_back(
  592. ItemPartColor(layer->has_color, layer->color));
  593. } else {
  594. (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
  595. }
  596. video::SMaterial &material = buf->getMaterial();
  597. if (set_material)
  598. layer->applyMaterialOptions(material);
  599. if (mattype) {
  600. material.MaterialType = *mattype;
  601. }
  602. if (layer->animation_frame_count > 1) {
  603. const FrameSpec &animation_frame = (*layer->frames)[0];
  604. material.setTexture(0, animation_frame.texture);
  605. } else {
  606. material.setTexture(0, layer->texture);
  607. }
  608. if (use_shaders) {
  609. if (layer->normal_texture) {
  610. if (layer->animation_frame_count > 1) {
  611. const FrameSpec &animation_frame = (*layer->frames)[0];
  612. material.setTexture(1, animation_frame.normal_texture);
  613. } else
  614. material.setTexture(1, layer->normal_texture);
  615. }
  616. material.setTexture(2, layer->flags_texture);
  617. }
  618. if (apply_scale && tile->world_aligned) {
  619. u32 n = buf->getVertexCount();
  620. for (u32 k = 0; k != n; ++k)
  621. buf->getTCoords(k) /= layer->scale;
  622. }
  623. }
  624. }
  625. }