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