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