]> git.lizzy.rs Git - minetest.git/blob - src/wieldmesh.cpp
CollisionMoveSimple: Collide with 'ignore' nodes
[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                         buf->Material.setTexture(0, p.layer.texture);
324                         p.layer.applyMaterialOptions(buf->Material);
325                         mesh->addMeshBuffer(buf);
326                         buf->append(&p.vertices[0], p.vertices.size(),
327                                         &p.indices[0], p.indices.size());
328                         buf->drop();
329                         colors->push_back(
330                                 ItemPartColor(p.layer.has_color, p.layer.color));
331                 }
332         return mesh;
333 }
334
335 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
336 {
337         ITextureSource *tsrc = client->getTextureSource();
338         IItemDefManager *idef = client->getItemDefManager();
339         IShaderSource *shdrsrc = client->getShaderSource();
340         const NodeDefManager *ndef = client->getNodeDefManager();
341         const ItemDefinition &def = item.getDefinition(idef);
342         const ContentFeatures &f = ndef->get(def.name);
343         content_t id = ndef->getId(def.name);
344
345         scene::SMesh *mesh = nullptr;
346
347         if (m_enable_shaders) {
348                 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
349                 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
350         }
351
352         // Color-related
353         m_colors.clear();
354         m_base_color = idef->getItemstackColor(item, client);
355
356         // If wield_image is defined, it overrides everything else
357         if (!def.wield_image.empty()) {
358                 setExtruded(def.wield_image, def.wield_overlay, def.wield_scale, tsrc,
359                         1);
360                 m_colors.emplace_back();
361                 // overlay is white, if present
362                 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
363                 return;
364         }
365
366         // Handle nodes
367         // See also CItemDefManager::createClientCached()
368         if (def.type == ITEM_NODE) {
369                 if (f.mesh_ptr[0]) {
370                         // e.g. mesh nodes and nodeboxes
371                         mesh = cloneMesh(f.mesh_ptr[0]);
372                         postProcessNodeMesh(mesh, f, m_enable_shaders, true,
373                                 &m_material_type, &m_colors);
374                         changeToMesh(mesh);
375                         mesh->drop();
376                         // mesh is pre-scaled by BS * f->visual_scale
377                         m_meshnode->setScale(
378                                         def.wield_scale * WIELD_SCALE_FACTOR
379                                         / (BS * f.visual_scale));
380                 } else {
381                         switch (f.drawtype) {
382                                 case NDT_AIRLIKE: {
383                                         changeToMesh(nullptr);
384                                         break;
385                                 }
386                                 case NDT_PLANTLIKE: {
387                                         setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
388                                                 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
389                                                 def.wield_scale, tsrc,
390                                                 f.tiles[0].layers[0].animation_frame_count);
391                                         // Add color
392                                         const TileLayer &l0 = f.tiles[0].layers[0];
393                                         m_colors.emplace_back(l0.has_color, l0.color);
394                                         const TileLayer &l1 = f.tiles[0].layers[1];
395                                         m_colors.emplace_back(l1.has_color, l1.color);
396                                         break;
397                                 }
398                                 case NDT_PLANTLIKE_ROOTED: {
399                                         setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
400                                                 "", def.wield_scale, tsrc,
401                                                 f.special_tiles[0].layers[0].animation_frame_count);
402                                         // Add color
403                                         const TileLayer &l0 = f.special_tiles[0].layers[0];
404                                         m_colors.emplace_back(l0.has_color, l0.color);
405                                         break;
406                                 }
407                                 case NDT_NORMAL:
408                                 case NDT_ALLFACES:
409                                 case NDT_LIQUID:
410                                 case NDT_FLOWINGLIQUID: {
411                                         setCube(f, def.wield_scale);
412                                         break;
413                                 }
414                                 default: {
415                                         mesh = createSpecialNodeMesh(client, id, &m_colors);
416                                         changeToMesh(mesh);
417                                         mesh->drop();
418                                         m_meshnode->setScale(
419                                                         def.wield_scale * WIELD_SCALE_FACTOR
420                                                         / (BS * f.visual_scale));
421                                 }
422                         }
423                 }
424                 u32 material_count = m_meshnode->getMaterialCount();
425                 for (u32 i = 0; i < material_count; ++i) {
426                         video::SMaterial &material = m_meshnode->getMaterial(i);
427                         material.MaterialType = m_material_type;
428                         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
429                         material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
430                         material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
431                 }
432                 return;
433         }
434         else if (!def.inventory_image.empty()) {
435                 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
436                         tsrc, 1);
437                 m_colors.emplace_back();
438                 // overlay is white, if present
439                 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
440                 return;
441         }
442
443         // no wield mesh found
444         changeToMesh(nullptr);
445 }
446
447 void WieldMeshSceneNode::setColor(video::SColor c)
448 {
449         assert(!m_lighting);
450         scene::IMesh *mesh = m_meshnode->getMesh();
451         if (!mesh)
452                 return;
453
454         u8 red = c.getRed();
455         u8 green = c.getGreen();
456         u8 blue = c.getBlue();
457         u32 mc = mesh->getMeshBufferCount();
458         for (u32 j = 0; j < mc; j++) {
459                 video::SColor bc(m_base_color);
460                 if ((m_colors.size() > j) && (m_colors[j].override_base))
461                         bc = m_colors[j].color;
462                 video::SColor buffercolor(255,
463                         bc.getRed() * red / 255,
464                         bc.getGreen() * green / 255,
465                         bc.getBlue() * blue / 255);
466                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
467                 colorizeMeshBuffer(buf, &buffercolor);
468         }
469 }
470
471 void WieldMeshSceneNode::render()
472 {
473         // note: if this method is changed to actually do something,
474         // you probably should implement OnRegisterSceneNode as well
475 }
476
477 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
478 {
479         if (!mesh) {
480                 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
481                 m_meshnode->setVisible(false);
482                 m_meshnode->setMesh(dummymesh);
483                 dummymesh->drop();  // m_meshnode grabbed it
484         } else {
485                 m_meshnode->setMesh(mesh);
486         }
487
488         m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
489         // need to normalize normals when lighting is enabled (because of setScale())
490         m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
491         m_meshnode->setVisible(true);
492 }
493
494 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
495 {
496         ITextureSource *tsrc = client->getTextureSource();
497         IItemDefManager *idef = client->getItemDefManager();
498         const NodeDefManager *ndef = client->getNodeDefManager();
499         const ItemDefinition &def = item.getDefinition(idef);
500         const ContentFeatures &f = ndef->get(def.name);
501         content_t id = ndef->getId(def.name);
502
503         if (!g_extrusion_mesh_cache) {
504                 g_extrusion_mesh_cache = new ExtrusionMeshCache();
505         } else {
506                 g_extrusion_mesh_cache->grab();
507         }
508
509         scene::SMesh *mesh = nullptr;
510
511         // Shading is on by default
512         result->needs_shading = true;
513
514         // If inventory_image is defined, it overrides everything else
515         if (!def.inventory_image.empty()) {
516                 mesh = getExtrudedMesh(tsrc, def.inventory_image,
517                         def.inventory_overlay);
518                 result->buffer_colors.emplace_back();
519                 // overlay is white, if present
520                 result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
521                 // Items with inventory images do not need shading
522                 result->needs_shading = false;
523         } else if (def.type == ITEM_NODE) {
524                 if (f.mesh_ptr[0]) {
525                         mesh = cloneMesh(f.mesh_ptr[0]);
526                         scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
527                         postProcessNodeMesh(mesh, f, false, false, nullptr,
528                                 &result->buffer_colors);
529                 } else {
530                         switch (f.drawtype) {
531                                 case NDT_PLANTLIKE: {
532                                         mesh = getExtrudedMesh(tsrc,
533                                                 tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
534                                                 tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
535                                         // Add color
536                                         const TileLayer &l0 = f.tiles[0].layers[0];
537                                         result->buffer_colors.emplace_back(l0.has_color, l0.color);
538                                         const TileLayer &l1 = f.tiles[0].layers[1];
539                                         result->buffer_colors.emplace_back(l1.has_color, l1.color);
540                                         break;
541                                 }
542                                 case NDT_PLANTLIKE_ROOTED: {
543                                         mesh = getExtrudedMesh(tsrc,
544                                                 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
545                                         // Add color
546                                         const TileLayer &l0 = f.special_tiles[0].layers[0];
547                                         result->buffer_colors.emplace_back(l0.has_color, l0.color);
548                                         break;
549                                 }
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                                         scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
558                                         // add overlays
559                                         postProcessNodeMesh(mesh, f, false, false, nullptr,
560                                                 &result->buffer_colors);
561                                         break;
562                                 }
563                                 default: {
564                                         mesh = createSpecialNodeMesh(client, id, &result->buffer_colors);
565                                         scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
566                                 }
567                         }
568                 }
569
570                 u32 mc = mesh->getMeshBufferCount();
571                 for (u32 i = 0; i < mc; ++i) {
572                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
573                         video::SMaterial &material = buf->getMaterial();
574                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
575                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
576                         material.setFlag(video::EMF_TRILINEAR_FILTER, false);
577                         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
578                         material.setFlag(video::EMF_LIGHTING, false);
579                 }
580
581                 rotateMeshXZby(mesh, -45);
582                 rotateMeshYZby(mesh, -30);
583         }
584         result->mesh = mesh;
585 }
586
587
588
589 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
590         const std::string &imagename, const std::string &overlay_name)
591 {
592         // check textures
593         video::ITexture *texture = tsrc->getTextureForMesh(imagename);
594         if (!texture) {
595                 return NULL;
596         }
597         video::ITexture *overlay_texture =
598                 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
599
600         // get mesh
601         core::dimension2d<u32> dim = texture->getSize();
602         scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
603         scene::SMesh *mesh = cloneMesh(original);
604         original->drop();
605
606         //set texture
607         mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
608                 tsrc->getTexture(imagename));
609         if (overlay_texture) {
610                 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
611                 copy->getMaterial().setTexture(0, overlay_texture);
612                 mesh->addMeshBuffer(copy);
613                 copy->drop();
614         }
615         // Customize materials
616         for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
617                 video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
618                 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
619                 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
620                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
621                 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
622                 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
623                 material.setFlag(video::EMF_LIGHTING, false);
624                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
625         }
626         scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
627
628         return mesh;
629 }
630
631 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
632         bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
633         std::vector<ItemPartColor> *colors, bool apply_scale)
634 {
635         u32 mc = mesh->getMeshBufferCount();
636         // Allocate colors for existing buffers
637         colors->clear();
638         for (u32 i = 0; i < mc; ++i)
639                 colors->push_back(ItemPartColor());
640
641         for (u32 i = 0; i < mc; ++i) {
642                 const TileSpec *tile = &(f.tiles[i]);
643                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
644                 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
645                         const TileLayer *layer = &tile->layers[layernum];
646                         if (layer->texture_id == 0)
647                                 continue;
648                         if (layernum != 0) {
649                                 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
650                                 copy->getMaterial() = buf->getMaterial();
651                                 mesh->addMeshBuffer(copy);
652                                 copy->drop();
653                                 buf = copy;
654                                 colors->push_back(
655                                         ItemPartColor(layer->has_color, layer->color));
656                         } else {
657                                 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
658                         }
659                         video::SMaterial &material = buf->getMaterial();
660                         if (set_material)
661                                 layer->applyMaterialOptions(material);
662                         if (mattype) {
663                                 material.MaterialType = *mattype;
664                         }
665                         if (layer->animation_frame_count > 1) {
666                                 const FrameSpec &animation_frame = (*layer->frames)[0];
667                                 material.setTexture(0, animation_frame.texture);
668                         } else {
669                                 material.setTexture(0, layer->texture);
670                         }
671                         if (use_shaders) {
672                                 if (layer->normal_texture) {
673                                         if (layer->animation_frame_count > 1) {
674                                                 const FrameSpec &animation_frame = (*layer->frames)[0];
675                                                 material.setTexture(1, animation_frame.normal_texture);
676                                         } else
677                                                 material.setTexture(1, layer->normal_texture);
678                                 }
679                                 material.setTexture(2, layer->flags_texture);
680                         }
681                         if (apply_scale && tile->world_aligned) {
682                                 u32 n = buf->getVertexCount();
683                                 for (u32 k = 0; k != n; ++k)
684                                         buf->getTCoords(k) /= layer->scale;
685                         }
686                 }
687         }
688 }