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 WieldMeshSceneNode::~WieldMeshSceneNode()
230 sanity_check(g_extrusion_mesh_cache);
232 // Remove node from shadow casters. m_shadow might be an invalid pointer!
233 if (auto shadow = RenderingEngine::get_shadow_renderer())
234 shadow->removeNodeFromShadowList(m_meshnode);
236 if (g_extrusion_mesh_cache->drop())
237 g_extrusion_mesh_cache = nullptr;
240 void WieldMeshSceneNode::setCube(const ContentFeatures &f,
243 scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
244 scene::SMesh *copy = cloneMesh(cubemesh);
246 postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors, true);
249 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
252 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
253 const std::string &overlay_name, v3f wield_scale, ITextureSource *tsrc,
256 video::ITexture *texture = tsrc->getTexture(imagename);
258 changeToMesh(nullptr);
261 video::ITexture *overlay_texture =
262 overlay_name.empty() ? NULL : tsrc->getTexture(overlay_name);
264 core::dimension2d<u32> dim = texture->getSize();
265 // Detect animation texture and pull off top frame instead of using entire thing
266 if (num_frames > 1) {
267 u32 frame_height = dim.Height / num_frames;
268 dim = core::dimension2d<u32>(dim.Width, frame_height);
270 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
271 scene::SMesh *mesh = cloneMesh(original);
274 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
275 tsrc->getTexture(imagename));
276 if (overlay_texture) {
277 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
278 copy->getMaterial().setTexture(0, overlay_texture);
279 mesh->addMeshBuffer(copy);
285 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
287 // Customize materials
288 for (u32 layer = 0; layer < m_meshnode->getMaterialCount(); layer++) {
289 video::SMaterial &material = m_meshnode->getMaterial(layer);
290 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
291 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
292 material.MaterialType = m_material_type;
293 material.MaterialTypeParam = 0.5f;
294 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
295 // Enable bi/trilinear filtering only for high resolution textures
296 if (dim.Width > 32) {
297 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
298 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
300 material.setFlag(video::EMF_BILINEAR_FILTER, false);
301 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
303 material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
304 // mipmaps cause "thin black line" artifacts
305 material.setFlag(video::EMF_USE_MIP_MAPS, false);
306 if (m_enable_shaders) {
307 material.setTexture(2, tsrc->getShaderFlagsTexture(false));
312 static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
313 std::vector<ItemPartColor> *colors, const ContentFeatures &f)
315 MeshMakeData mesh_make_data(client, false);
316 MeshCollector collector;
317 mesh_make_data.setSmoothLighting(false);
318 MapblockMeshGenerator gen(&mesh_make_data, &collector,
319 client->getSceneManager()->getMeshManipulator());
323 } else if (f.param_type_2 == CPT2_WALLMOUNTED ||
324 f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
325 if (f.drawtype == NDT_TORCHLIKE ||
326 f.drawtype == NDT_SIGNLIKE ||
327 f.drawtype == NDT_NODEBOX ||
328 f.drawtype == NDT_MESH) {
331 } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE) {
334 gen.renderSingle(n.getContent(), n.getParam2());
337 scene::SMesh *mesh = new scene::SMesh();
338 for (auto &prebuffers : collector.prebuffers)
339 for (PreMeshBuffer &p : prebuffers) {
340 if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
341 const FrameSpec &frame = (*p.layer.frames)[0];
342 p.layer.texture = frame.texture;
343 p.layer.normal_texture = frame.normal_texture;
345 for (video::S3DVertex &v : p.vertices) {
346 v.Color.setAlpha(255);
348 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
349 buf->Material.setTexture(0, p.layer.texture);
350 p.layer.applyMaterialOptions(buf->Material);
351 mesh->addMeshBuffer(buf);
352 buf->append(&p.vertices[0], p.vertices.size(),
353 &p.indices[0], p.indices.size());
356 ItemPartColor(p.layer.has_color, p.layer.color));
361 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool check_wield_image)
363 ITextureSource *tsrc = client->getTextureSource();
364 IItemDefManager *idef = client->getItemDefManager();
365 IShaderSource *shdrsrc = client->getShaderSource();
366 const NodeDefManager *ndef = client->getNodeDefManager();
367 const ItemDefinition &def = item.getDefinition(idef);
368 const ContentFeatures &f = ndef->get(def.name);
369 content_t id = ndef->getId(def.name);
371 scene::SMesh *mesh = nullptr;
373 if (m_enable_shaders) {
374 u32 shader_id = shdrsrc->getShader("object_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
375 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
380 m_base_color = idef->getItemstackColor(item, client);
382 // If wield_image needs to be checked and is defined, it overrides everything else
383 if (!def.wield_image.empty() && check_wield_image) {
384 setExtruded(def.wield_image, def.wield_overlay, def.wield_scale, tsrc,
386 m_colors.emplace_back();
387 // overlay is white, if present
388 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
393 // See also CItemDefManager::createClientCached()
394 if (def.type == ITEM_NODE) {
395 bool cull_backface = f.needsBackfaceCulling();
397 // Select rendering method
398 switch (f.drawtype) {
400 setExtruded("no_texture_airlike.png", "",
401 v3f(1.0, 1.0, 1.0), tsrc, 1);
407 case NDT_FLOWINGLIQUID: {
408 v3f wscale = def.wield_scale;
409 if (f.drawtype == NDT_FLOWINGLIQUID)
411 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
412 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
414 f.tiles[0].layers[0].animation_frame_count);
416 const TileLayer &l0 = f.tiles[0].layers[0];
417 m_colors.emplace_back(l0.has_color, l0.color);
418 const TileLayer &l1 = f.tiles[0].layers[1];
419 m_colors.emplace_back(l1.has_color, l1.color);
422 case NDT_PLANTLIKE_ROOTED: {
423 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
424 "", def.wield_scale, tsrc,
425 f.special_tiles[0].layers[0].animation_frame_count);
427 const TileLayer &l0 = f.special_tiles[0].layers[0];
428 m_colors.emplace_back(l0.has_color, l0.color);
434 setCube(f, def.wield_scale);
437 // Render non-trivial drawtypes like the actual node
439 n.setParam2(def.place_param2);
441 mesh = createSpecialNodeMesh(client, n, &m_colors, f);
444 m_meshnode->setScale(
445 def.wield_scale * WIELD_SCALE_FACTOR
446 / (BS * f.visual_scale));
451 u32 material_count = m_meshnode->getMaterialCount();
452 for (u32 i = 0; i < material_count; ++i) {
453 video::SMaterial &material = m_meshnode->getMaterial(i);
454 material.MaterialType = m_material_type;
455 material.MaterialTypeParam = 0.5f;
456 material.setFlag(video::EMF_BACK_FACE_CULLING, cull_backface);
457 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
458 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
461 // initialize the color
463 setColor(video::SColor(0xFFFFFFFF));
466 if (!def.inventory_image.empty()) {
467 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
470 setExtruded("no_texture.png", "", def.wield_scale, tsrc, 1);
473 m_colors.emplace_back();
474 // overlay is white, if present
475 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
477 // initialize the color
479 setColor(video::SColor(0xFFFFFFFF));
483 // no wield mesh found
484 changeToMesh(nullptr);
487 void WieldMeshSceneNode::setColor(video::SColor c)
490 scene::IMesh *mesh = m_meshnode->getMesh();
495 u8 green = c.getGreen();
496 u8 blue = c.getBlue();
497 u32 mc = mesh->getMeshBufferCount();
498 for (u32 j = 0; j < mc; j++) {
499 video::SColor bc(m_base_color);
500 if ((m_colors.size() > j) && (m_colors[j].override_base))
501 bc = m_colors[j].color;
502 video::SColor buffercolor(255,
503 bc.getRed() * red / 255,
504 bc.getGreen() * green / 255,
505 bc.getBlue() * blue / 255);
506 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
508 if (m_enable_shaders)
509 setMeshBufferColor(buf, buffercolor);
511 colorizeMeshBuffer(buf, &buffercolor);
515 void WieldMeshSceneNode::setNodeLightColor(video::SColor color)
520 if (m_enable_shaders) {
521 for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
522 video::SMaterial &material = m_meshnode->getMaterial(i);
523 material.EmissiveColor = color;
531 void WieldMeshSceneNode::render()
533 // note: if this method is changed to actually do something,
534 // you probably should implement OnRegisterSceneNode as well
537 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
540 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
541 m_meshnode->setVisible(false);
542 m_meshnode->setMesh(dummymesh);
543 dummymesh->drop(); // m_meshnode grabbed it
545 m_meshnode->setMesh(mesh);
548 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
549 // need to normalize normals when lighting is enabled (because of setScale())
550 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
551 m_meshnode->setVisible(true);
554 // Add mesh to shadow caster
555 m_shadow->addNodeToShadowList(m_meshnode);
557 // Set shadow texture
558 for (u32 i = 0; i < m_meshnode->getMaterialCount(); i++)
559 m_meshnode->setMaterialTexture(3, m_shadow->get_texture());
563 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
565 ITextureSource *tsrc = client->getTextureSource();
566 IItemDefManager *idef = client->getItemDefManager();
567 const NodeDefManager *ndef = client->getNodeDefManager();
568 const ItemDefinition &def = item.getDefinition(idef);
569 const ContentFeatures &f = ndef->get(def.name);
570 content_t id = ndef->getId(def.name);
572 FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
574 scene::SMesh *mesh = nullptr;
576 // Shading is on by default
577 result->needs_shading = true;
579 bool cull_backface = f.needsBackfaceCulling();
581 // If inventory_image is defined, it overrides everything else
582 if (!def.inventory_image.empty()) {
583 mesh = getExtrudedMesh(tsrc, def.inventory_image,
584 def.inventory_overlay);
585 result->buffer_colors.emplace_back();
586 // overlay is white, if present
587 result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
588 // Items with inventory images do not need shading
589 result->needs_shading = false;
590 } else if (def.type == ITEM_NODE && f.drawtype == NDT_AIRLIKE) {
591 // Fallback image for airlike node
592 mesh = getExtrudedMesh(tsrc, "no_texture_airlike.png",
593 def.inventory_overlay);
594 result->needs_shading = false;
595 } else if (def.type == ITEM_NODE) {
596 switch (f.drawtype) {
600 case NDT_FLOWINGLIQUID: {
601 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
602 mesh = cloneMesh(cube);
604 if (f.drawtype == NDT_FLOWINGLIQUID) {
605 scaleMesh(mesh, v3f(1.2, 0.03, 1.2));
606 translateMesh(mesh, v3f(0, -0.57, 0));
608 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
610 postProcessNodeMesh(mesh, f, false, false, nullptr,
611 &result->buffer_colors, true);
612 if (f.drawtype == NDT_ALLFACES)
613 scaleMesh(mesh, v3f(f.visual_scale));
616 case NDT_PLANTLIKE: {
617 mesh = getExtrudedMesh(tsrc,
618 tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
619 tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
621 const TileLayer &l0 = f.tiles[0].layers[0];
622 result->buffer_colors.emplace_back(l0.has_color, l0.color);
623 const TileLayer &l1 = f.tiles[0].layers[1];
624 result->buffer_colors.emplace_back(l1.has_color, l1.color);
627 case NDT_PLANTLIKE_ROOTED: {
628 mesh = getExtrudedMesh(tsrc,
629 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
631 const TileLayer &l0 = f.special_tiles[0].layers[0];
632 result->buffer_colors.emplace_back(l0.has_color, l0.color);
636 // Render non-trivial drawtypes like the actual node
638 n.setParam2(def.place_param2);
640 mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
641 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
646 u32 mc = mesh->getMeshBufferCount();
647 for (u32 i = 0; i < mc; ++i) {
648 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
649 video::SMaterial &material = buf->getMaterial();
650 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
651 material.MaterialTypeParam = 0.5f;
652 material.setFlag(video::EMF_BILINEAR_FILTER, false);
653 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
654 material.setFlag(video::EMF_BACK_FACE_CULLING, cull_backface);
655 material.setFlag(video::EMF_LIGHTING, false);
658 rotateMeshXZby(mesh, -45);
659 rotateMeshYZby(mesh, -30);
666 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
667 const std::string &imagename, const std::string &overlay_name)
670 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
674 video::ITexture *overlay_texture =
675 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
678 core::dimension2d<u32> dim = texture->getSize();
679 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
680 scene::SMesh *mesh = cloneMesh(original);
684 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
685 tsrc->getTexture(imagename));
686 if (overlay_texture) {
687 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
688 copy->getMaterial().setTexture(0, overlay_texture);
689 mesh->addMeshBuffer(copy);
692 // Customize materials
693 for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
694 video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
695 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
696 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
697 material.setFlag(video::EMF_BILINEAR_FILTER, false);
698 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
699 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
700 material.setFlag(video::EMF_LIGHTING, false);
701 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
702 material.MaterialTypeParam = 0.5f;
704 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
709 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
710 bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
711 std::vector<ItemPartColor> *colors, bool apply_scale)
713 u32 mc = mesh->getMeshBufferCount();
714 // Allocate colors for existing buffers
716 for (u32 i = 0; i < mc; ++i)
717 colors->push_back(ItemPartColor());
719 for (u32 i = 0; i < mc; ++i) {
720 const TileSpec *tile = &(f.tiles[i]);
721 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
722 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
723 const TileLayer *layer = &tile->layers[layernum];
724 if (layer->texture_id == 0)
727 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
728 copy->getMaterial() = buf->getMaterial();
729 mesh->addMeshBuffer(copy);
733 ItemPartColor(layer->has_color, layer->color));
735 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
737 video::SMaterial &material = buf->getMaterial();
739 layer->applyMaterialOptions(material);
741 material.MaterialType = *mattype;
743 if (layer->animation_frame_count > 1) {
744 const FrameSpec &animation_frame = (*layer->frames)[0];
745 material.setTexture(0, animation_frame.texture);
747 material.setTexture(0, layer->texture);
750 if (layer->normal_texture) {
751 if (layer->animation_frame_count > 1) {
752 const FrameSpec &animation_frame = (*layer->frames)[0];
753 material.setTexture(1, animation_frame.normal_texture);
755 material.setTexture(1, layer->normal_texture);
757 material.setTexture(2, layer->flags_texture);
759 if (apply_scale && tile->world_aligned) {
760 u32 n = buf->getVertexCount();
761 for (u32 k = 0; k != n; ++k)
762 buf->getTCoords(k) /= layer->scale;