wieldmesh.cpp 19 KB

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