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