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));
389 // initialize the color
391 setColor(video::SColor(0xFFFFFFFF));
396 // See also CItemDefManager::createClientCached()
397 if (def.type == ITEM_NODE) {
398 bool cull_backface = f.needsBackfaceCulling();
400 // Select rendering method
401 switch (f.drawtype) {
403 setExtruded("no_texture_airlike.png", "",
404 v3f(1.0, 1.0, 1.0), tsrc, 1);
410 case NDT_FLOWINGLIQUID: {
411 v3f wscale = def.wield_scale;
412 if (f.drawtype == NDT_FLOWINGLIQUID)
414 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
415 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
417 f.tiles[0].layers[0].animation_frame_count);
419 const TileLayer &l0 = f.tiles[0].layers[0];
420 m_colors.emplace_back(l0.has_color, l0.color);
421 const TileLayer &l1 = f.tiles[0].layers[1];
422 m_colors.emplace_back(l1.has_color, l1.color);
425 case NDT_PLANTLIKE_ROOTED: {
426 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
427 "", def.wield_scale, tsrc,
428 f.special_tiles[0].layers[0].animation_frame_count);
430 const TileLayer &l0 = f.special_tiles[0].layers[0];
431 m_colors.emplace_back(l0.has_color, l0.color);
437 setCube(f, def.wield_scale);
440 // Render non-trivial drawtypes like the actual node
442 n.setParam2(def.place_param2);
444 mesh = createSpecialNodeMesh(client, n, &m_colors, f);
447 m_meshnode->setScale(
448 def.wield_scale * WIELD_SCALE_FACTOR
449 / (BS * f.visual_scale));
454 u32 material_count = m_meshnode->getMaterialCount();
455 for (u32 i = 0; i < material_count; ++i) {
456 video::SMaterial &material = m_meshnode->getMaterial(i);
457 material.MaterialType = m_material_type;
458 material.MaterialTypeParam = 0.5f;
459 material.setFlag(video::EMF_BACK_FACE_CULLING, cull_backface);
460 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
461 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
464 // initialize the color
466 setColor(video::SColor(0xFFFFFFFF));
469 if (!def.inventory_image.empty()) {
470 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
473 setExtruded("no_texture.png", "", def.wield_scale, tsrc, 1);
476 m_colors.emplace_back();
477 // overlay is white, if present
478 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
480 // initialize the color
482 setColor(video::SColor(0xFFFFFFFF));
486 // no wield mesh found
487 changeToMesh(nullptr);
490 void WieldMeshSceneNode::setColor(video::SColor c)
493 scene::IMesh *mesh = m_meshnode->getMesh();
498 u8 green = c.getGreen();
499 u8 blue = c.getBlue();
500 u32 mc = mesh->getMeshBufferCount();
501 for (u32 j = 0; j < mc; j++) {
502 video::SColor bc(m_base_color);
503 if ((m_colors.size() > j) && (m_colors[j].override_base))
504 bc = m_colors[j].color;
505 video::SColor buffercolor(255,
506 bc.getRed() * red / 255,
507 bc.getGreen() * green / 255,
508 bc.getBlue() * blue / 255);
509 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
511 if (m_enable_shaders)
512 setMeshBufferColor(buf, buffercolor);
514 colorizeMeshBuffer(buf, &buffercolor);
518 void WieldMeshSceneNode::setNodeLightColor(video::SColor color)
523 if (m_enable_shaders) {
524 for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
525 video::SMaterial &material = m_meshnode->getMaterial(i);
526 material.EmissiveColor = color;
534 void WieldMeshSceneNode::render()
536 // note: if this method is changed to actually do something,
537 // you probably should implement OnRegisterSceneNode as well
540 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
543 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
544 m_meshnode->setVisible(false);
545 m_meshnode->setMesh(dummymesh);
546 dummymesh->drop(); // m_meshnode grabbed it
548 m_meshnode->setMesh(mesh);
551 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
552 // need to normalize normals when lighting is enabled (because of setScale())
553 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
554 m_meshnode->setVisible(true);
557 // Add mesh to shadow caster
558 m_shadow->addNodeToShadowList(m_meshnode);
560 // Set shadow texture
561 for (u32 i = 0; i < m_meshnode->getMaterialCount(); i++)
562 m_meshnode->setMaterialTexture(3, m_shadow->get_texture());
566 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
568 ITextureSource *tsrc = client->getTextureSource();
569 IItemDefManager *idef = client->getItemDefManager();
570 const NodeDefManager *ndef = client->getNodeDefManager();
571 const ItemDefinition &def = item.getDefinition(idef);
572 const ContentFeatures &f = ndef->get(def.name);
573 content_t id = ndef->getId(def.name);
575 FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
577 scene::SMesh *mesh = nullptr;
579 // Shading is on by default
580 result->needs_shading = true;
582 bool cull_backface = f.needsBackfaceCulling();
584 // If inventory_image is defined, it overrides everything else
585 if (!def.inventory_image.empty()) {
586 mesh = getExtrudedMesh(tsrc, def.inventory_image,
587 def.inventory_overlay);
588 result->buffer_colors.emplace_back();
589 // overlay is white, if present
590 result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
591 // Items with inventory images do not need shading
592 result->needs_shading = false;
593 } else if (def.type == ITEM_NODE && f.drawtype == NDT_AIRLIKE) {
594 // Fallback image for airlike node
595 mesh = getExtrudedMesh(tsrc, "no_texture_airlike.png",
596 def.inventory_overlay);
597 result->needs_shading = false;
598 } else if (def.type == ITEM_NODE) {
599 switch (f.drawtype) {
603 case NDT_FLOWINGLIQUID: {
604 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
605 mesh = cloneMesh(cube);
607 if (f.drawtype == NDT_FLOWINGLIQUID) {
608 scaleMesh(mesh, v3f(1.2, 0.03, 1.2));
609 translateMesh(mesh, v3f(0, -0.57, 0));
611 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
613 postProcessNodeMesh(mesh, f, false, false, nullptr,
614 &result->buffer_colors, true);
615 if (f.drawtype == NDT_ALLFACES)
616 scaleMesh(mesh, v3f(f.visual_scale));
619 case NDT_PLANTLIKE: {
620 mesh = getExtrudedMesh(tsrc,
621 tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
622 tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
624 const TileLayer &l0 = f.tiles[0].layers[0];
625 result->buffer_colors.emplace_back(l0.has_color, l0.color);
626 const TileLayer &l1 = f.tiles[0].layers[1];
627 result->buffer_colors.emplace_back(l1.has_color, l1.color);
630 case NDT_PLANTLIKE_ROOTED: {
631 mesh = getExtrudedMesh(tsrc,
632 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
634 const TileLayer &l0 = f.special_tiles[0].layers[0];
635 result->buffer_colors.emplace_back(l0.has_color, l0.color);
639 // Render non-trivial drawtypes like the actual node
641 n.setParam2(def.place_param2);
643 mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
644 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
649 u32 mc = mesh->getMeshBufferCount();
650 for (u32 i = 0; i < mc; ++i) {
651 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
652 video::SMaterial &material = buf->getMaterial();
653 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
654 material.MaterialTypeParam = 0.5f;
655 material.setFlag(video::EMF_BILINEAR_FILTER, false);
656 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
657 material.setFlag(video::EMF_BACK_FACE_CULLING, cull_backface);
658 material.setFlag(video::EMF_LIGHTING, false);
661 rotateMeshXZby(mesh, -45);
662 rotateMeshYZby(mesh, -30);
669 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
670 const std::string &imagename, const std::string &overlay_name)
673 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
677 video::ITexture *overlay_texture =
678 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
681 core::dimension2d<u32> dim = texture->getSize();
682 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
683 scene::SMesh *mesh = cloneMesh(original);
687 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
688 tsrc->getTexture(imagename));
689 if (overlay_texture) {
690 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
691 copy->getMaterial().setTexture(0, overlay_texture);
692 mesh->addMeshBuffer(copy);
695 // Customize materials
696 for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
697 video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
698 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
699 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
700 material.setFlag(video::EMF_BILINEAR_FILTER, false);
701 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
702 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
703 material.setFlag(video::EMF_LIGHTING, false);
704 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
705 material.MaterialTypeParam = 0.5f;
707 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
712 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
713 bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
714 std::vector<ItemPartColor> *colors, bool apply_scale)
716 u32 mc = mesh->getMeshBufferCount();
717 // Allocate colors for existing buffers
719 for (u32 i = 0; i < mc; ++i)
720 colors->push_back(ItemPartColor());
722 for (u32 i = 0; i < mc; ++i) {
723 const TileSpec *tile = &(f.tiles[i]);
724 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
725 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
726 const TileLayer *layer = &tile->layers[layernum];
727 if (layer->texture_id == 0)
730 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
731 copy->getMaterial() = buf->getMaterial();
732 mesh->addMeshBuffer(copy);
736 ItemPartColor(layer->has_color, layer->color));
738 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
740 video::SMaterial &material = buf->getMaterial();
742 layer->applyMaterialOptions(material);
744 material.MaterialType = *mattype;
746 if (layer->animation_frame_count > 1) {
747 const FrameSpec &animation_frame = (*layer->frames)[0];
748 material.setTexture(0, animation_frame.texture);
750 material.setTexture(0, layer->texture);
753 if (layer->normal_texture) {
754 if (layer->animation_frame_count > 1) {
755 const FrameSpec &animation_frame = (*layer->frames)[0];
756 material.setTexture(1, animation_frame.normal_texture);
758 material.setTexture(1, layer->normal_texture);
760 material.setTexture(2, layer->flags_texture);
762 if (apply_scale && tile->world_aligned) {
763 u32 n = buf->getVertexCount();
764 for (u32 k = 0; k != n; ++k)
765 buf->getTCoords(k) /= layer->scale;