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