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