3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "wieldmesh.h"
23 #include "inventory.h"
28 #include "content_mapblock.h"
29 #include "mapblock_mesh.h"
30 #include "client/meshgen/collector.h"
31 #include "client/tile.h"
33 #include "util/numeric.h"
35 #include <IMeshManipulator.h>
36 #include "client/renderingengine.h"
38 #define WIELD_SCALE_FACTOR 30.0
39 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
41 #define MIN_EXTRUSION_MESH_RESOLUTION 16
42 #define MAX_EXTRUSION_MESH_RESOLUTION 512
44 static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
48 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
49 video::SColor c(255,255,255,255);
50 v3f scale(1.0, 1.0, 0.1);
54 video::S3DVertex vertices[8] = {
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),
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),
66 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
67 buf->append(vertices, 8, indices, 12);
70 f32 pixelsize_x = 1 / (f32) resolution_x;
71 f32 pixelsize_y = 1 / (f32) resolution_y;
73 for (int i = 0; i < resolution_x; ++i) {
74 f32 pixelpos_x = i * pixelsize_x - 0.5;
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] = {
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),
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),
91 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
92 buf->append(vertices, 8, indices, 12);
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;
98 f32 tex0 = (i + 0.1) * pixelsize_y;
99 f32 tex1 = (i + 0.9) * pixelsize_y;
100 video::S3DVertex vertices[8] = {
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),
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),
112 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
113 buf->append(vertices, 8, indices, 12);
116 // Create mesh object
117 scene::SMesh *mesh = new scene::SMesh();
118 mesh->addMeshBuffer(buf);
120 scaleMesh(mesh, scale); // also recalculates bounding box
125 Caches extrusion meshes so that only one of them per resolution
126 is needed. Also caches one cube (for convenience).
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.
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.
135 class ExtrusionMeshCache: public IReferenceCounted
141 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
142 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
144 m_extrusion_meshes[resolution] =
145 createExtrusionMesh(resolution, resolution);
147 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
150 virtual ~ExtrusionMeshCache()
152 for (auto &extrusion_meshe : m_extrusion_meshes) {
153 extrusion_meshe.second->drop();
157 // Get closest extrusion mesh for given image dimensions
158 // Caller must drop the returned pointer
159 scene::IMesh* create(core::dimension2d<u32> dim)
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);
166 int maxdim = MYMAX(dim.Width, dim.Height);
168 std::map<int, scene::IMesh*>::iterator
169 it = m_extrusion_meshes.lower_bound(maxdim);
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());
177 scene::IMesh *mesh = it->second;
181 // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
182 // Caller must drop the returned pointer
183 scene::IMesh* createCube()
190 std::map<int, scene::IMesh*> m_extrusion_meshes;
191 scene::IMesh *m_cube;
194 ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
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),
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");
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();
212 g_extrusion_mesh_cache->grab();
214 // Disable bounding box culling for this scene node
215 // since we won't calculate the bounding box.
216 setAutomaticCulling(scene::EAC_OFF);
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
225 m_shadow = RenderingEngine::get_shadow_renderer();
228 // Add mesh to shadow caster
229 m_shadow->addNodeToShadowList(m_meshnode);
233 WieldMeshSceneNode::~WieldMeshSceneNode()
235 sanity_check(g_extrusion_mesh_cache);
237 // Remove node from shadow casters. m_shadow might be an invalid pointer!
239 m_shadow->removeNodeFromShadowList(m_meshnode);
241 if (g_extrusion_mesh_cache->drop())
242 g_extrusion_mesh_cache = nullptr;
245 void WieldMeshSceneNode::setCube(const ContentFeatures &f,
248 scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
249 scene::SMesh *copy = cloneMesh(cubemesh);
251 postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors, true);
254 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
257 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
258 const std::string &overlay_name, v3f wield_scale, ITextureSource *tsrc,
261 video::ITexture *texture = tsrc->getTexture(imagename);
263 changeToMesh(nullptr);
266 video::ITexture *overlay_texture =
267 overlay_name.empty() ? NULL : tsrc->getTexture(overlay_name);
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);
275 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
276 scene::SMesh *mesh = cloneMesh(original);
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);
290 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
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);
305 material.setFlag(video::EMF_BILINEAR_FILTER, false);
306 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
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));
317 static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
318 std::vector<ItemPartColor> *colors, const ContentFeatures &f)
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());
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) {
336 } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE) {
339 gen.renderSingle(n.getContent(), n.getParam2());
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;
350 for (video::S3DVertex &v : p.vertices) {
351 v.Color.setAlpha(255);
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());
361 ItemPartColor(p.layer.has_color, p.layer.color));
366 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool check_wield_image)
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);
376 scene::SMesh *mesh = nullptr;
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;
385 m_base_color = idef->getItemstackColor(item, client);
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,
391 m_colors.emplace_back();
392 // overlay is white, if present
393 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
394 // initialize the color
396 setColor(video::SColor(0xFFFFFFFF));
401 // See also CItemDefManager::createClientCached()
402 if (def.type == ITEM_NODE) {
403 bool cull_backface = f.needsBackfaceCulling();
405 // Select rendering method
406 switch (f.drawtype) {
408 setExtruded("no_texture_airlike.png", "",
409 v3f(1.0, 1.0, 1.0), tsrc, 1);
415 case NDT_FLOWINGLIQUID: {
416 v3f wscale = def.wield_scale;
417 if (f.drawtype == NDT_FLOWINGLIQUID)
419 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
420 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
422 f.tiles[0].layers[0].animation_frame_count);
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);
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);
435 const TileLayer &l0 = f.special_tiles[0].layers[0];
436 m_colors.emplace_back(l0.has_color, l0.color);
442 setCube(f, def.wield_scale);
445 // Render non-trivial drawtypes like the actual node
447 n.setParam2(def.place_param2);
449 mesh = createSpecialNodeMesh(client, n, &m_colors, f);
452 m_meshnode->setScale(
453 def.wield_scale * WIELD_SCALE_FACTOR
454 / (BS * f.visual_scale));
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);
469 // initialize the color
471 setColor(video::SColor(0xFFFFFFFF));
474 if (!def.inventory_image.empty()) {
475 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
478 setExtruded("no_texture.png", "", def.wield_scale, tsrc, 1);
481 m_colors.emplace_back();
482 // overlay is white, if present
483 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
485 // initialize the color
487 setColor(video::SColor(0xFFFFFFFF));
491 // no wield mesh found
492 changeToMesh(nullptr);
495 void WieldMeshSceneNode::setColor(video::SColor c)
498 scene::IMesh *mesh = m_meshnode->getMesh();
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);
516 if (m_enable_shaders)
517 setMeshBufferColor(buf, buffercolor);
519 colorizeMeshBuffer(buf, &buffercolor);
523 void WieldMeshSceneNode::setNodeLightColor(video::SColor color)
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;
539 void WieldMeshSceneNode::render()
541 // note: if this method is changed to actually do something,
542 // you probably should implement OnRegisterSceneNode as well
545 void WieldMeshSceneNode::changeToMesh(scene::IMesh *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
553 m_meshnode->setMesh(mesh);
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);
562 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
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);
571 FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
573 scene::SMesh *mesh = nullptr;
575 // Shading is on by default
576 result->needs_shading = true;
578 bool cull_backface = f.needsBackfaceCulling();
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) {
599 case NDT_FLOWINGLIQUID: {
600 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
601 mesh = cloneMesh(cube);
603 if (f.drawtype == NDT_FLOWINGLIQUID) {
604 scaleMesh(mesh, v3f(1.2, 0.03, 1.2));
605 translateMesh(mesh, v3f(0, -0.57, 0));
607 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
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));
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));
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);
626 case NDT_PLANTLIKE_ROOTED: {
627 mesh = getExtrudedMesh(tsrc,
628 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
630 const TileLayer &l0 = f.special_tiles[0].layers[0];
631 result->buffer_colors.emplace_back(l0.has_color, l0.color);
635 // Render non-trivial drawtypes like the actual node
637 n.setParam2(def.place_param2);
639 mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
640 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
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);
657 rotateMeshXZby(mesh, -45);
658 rotateMeshYZby(mesh, -30);
665 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
666 const std::string &imagename, const std::string &overlay_name)
669 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
673 video::ITexture *overlay_texture =
674 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
677 core::dimension2d<u32> dim = texture->getSize();
678 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
679 scene::SMesh *mesh = cloneMesh(original);
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);
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;
703 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
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)
712 u32 mc = mesh->getMeshBufferCount();
713 // Allocate colors for existing buffers
715 for (u32 i = 0; i < mc; ++i)
716 colors->push_back(ItemPartColor());
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)
726 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
727 copy->getMaterial() = buf->getMaterial();
728 mesh->addMeshBuffer(copy);
732 ItemPartColor(layer->has_color, layer->color));
734 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
736 video::SMaterial &material = buf->getMaterial();
738 layer->applyMaterialOptions(material);
740 material.MaterialType = *mattype;
742 if (layer->animation_frame_count > 1) {
743 const FrameSpec &animation_frame = (*layer->frames)[0];
744 material.setTexture(0, animation_frame.texture);
746 material.setTexture(0, layer->texture);
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);
754 material.setTexture(1, layer->normal_texture);
756 material.setTexture(2, layer->flags_texture);
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;