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