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/tile.h"
32 #include "util/numeric.h"
34 #include <IMeshManipulator.h>
36 #define WIELD_SCALE_FACTOR 30.0
37 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
39 #define MIN_EXTRUSION_MESH_RESOLUTION 16
40 #define MAX_EXTRUSION_MESH_RESOLUTION 512
42 static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
46 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
47 video::SColor c(255,255,255,255);
48 v3f scale(1.0, 1.0, 0.1);
52 video::S3DVertex vertices[8] = {
54 video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
55 video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
56 video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
57 video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
59 video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
60 video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
61 video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
62 video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
64 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
65 buf->append(vertices, 8, indices, 12);
68 f32 pixelsize_x = 1 / (f32) resolution_x;
69 f32 pixelsize_y = 1 / (f32) resolution_y;
71 for (int i = 0; i < resolution_x; ++i) {
72 f32 pixelpos_x = i * pixelsize_x - 0.5;
74 f32 x1 = pixelpos_x + pixelsize_x;
75 f32 tex0 = (i + 0.1) * pixelsize_x;
76 f32 tex1 = (i + 0.9) * pixelsize_x;
77 video::S3DVertex vertices[8] = {
79 video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
80 video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
81 video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
82 video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
84 video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
85 video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
86 video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
87 video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
89 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
90 buf->append(vertices, 8, indices, 12);
92 for (int i = 0; i < resolution_y; ++i) {
93 f32 pixelpos_y = i * pixelsize_y - 0.5;
94 f32 y0 = -pixelpos_y - pixelsize_y;
96 f32 tex0 = (i + 0.1) * pixelsize_y;
97 f32 tex1 = (i + 0.9) * pixelsize_y;
98 video::S3DVertex vertices[8] = {
100 video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
101 video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
102 video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
103 video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
105 video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
106 video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
107 video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
108 video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
110 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
111 buf->append(vertices, 8, indices, 12);
114 // Create mesh object
115 scene::SMesh *mesh = new scene::SMesh();
116 mesh->addMeshBuffer(buf);
118 scaleMesh(mesh, scale); // also recalculates bounding box
123 Caches extrusion meshes so that only one of them per resolution
124 is needed. Also caches one cube (for convenience).
126 E.g. there is a single extrusion mesh that is used for all
127 16x16 px images, another for all 256x256 px images, and so on.
129 WARNING: Not thread safe. This should not be a problem since
130 rendering related classes (such as WieldMeshSceneNode) will be
131 used from the rendering thread only.
133 class ExtrusionMeshCache: public IReferenceCounted
139 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
140 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
142 m_extrusion_meshes[resolution] =
143 createExtrusionMesh(resolution, resolution);
145 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
148 virtual ~ExtrusionMeshCache()
150 for (auto &extrusion_meshe : m_extrusion_meshes) {
151 extrusion_meshe.second->drop();
155 // Get closest extrusion mesh for given image dimensions
156 // Caller must drop the returned pointer
157 scene::IMesh* create(core::dimension2d<u32> dim)
159 // handle non-power of two textures inefficiently without cache
160 if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) {
161 return createExtrusionMesh(dim.Width, dim.Height);
164 int maxdim = MYMAX(dim.Width, dim.Height);
166 std::map<int, scene::IMesh*>::iterator
167 it = m_extrusion_meshes.lower_bound(maxdim);
169 if (it == m_extrusion_meshes.end()) {
170 // no viable resolution found; use largest one
171 it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION);
172 sanity_check(it != m_extrusion_meshes.end());
175 scene::IMesh *mesh = it->second;
179 // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
180 // Caller must drop the returned pointer
181 scene::IMesh* createCube()
188 std::map<int, scene::IMesh*> m_extrusion_meshes;
189 scene::IMesh *m_cube;
192 ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
195 WieldMeshSceneNode::WieldMeshSceneNode(scene::ISceneManager *mgr, s32 id, bool lighting):
196 scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
197 m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
200 m_enable_shaders = g_settings->getBool("enable_shaders");
201 m_anisotropic_filter = g_settings->getBool("anisotropic_filter");
202 m_bilinear_filter = g_settings->getBool("bilinear_filter");
203 m_trilinear_filter = g_settings->getBool("trilinear_filter");
205 // If this is the first wield mesh scene node, create a cache
206 // for extrusion meshes (and a cube mesh), otherwise reuse it
207 if (!g_extrusion_mesh_cache)
208 g_extrusion_mesh_cache = new ExtrusionMeshCache();
210 g_extrusion_mesh_cache->grab();
212 // Disable bounding box culling for this scene node
213 // since we won't calculate the bounding box.
214 setAutomaticCulling(scene::EAC_OFF);
216 // Create the child scene node
217 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
218 m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1);
219 m_meshnode->setReadOnlyMaterials(false);
220 m_meshnode->setVisible(false);
221 dummymesh->drop(); // m_meshnode grabbed it
224 WieldMeshSceneNode::~WieldMeshSceneNode()
226 sanity_check(g_extrusion_mesh_cache);
227 if (g_extrusion_mesh_cache->drop())
228 g_extrusion_mesh_cache = nullptr;
231 void WieldMeshSceneNode::setCube(const ContentFeatures &f,
234 scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
235 scene::SMesh *copy = cloneMesh(cubemesh);
237 postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors, true);
240 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
243 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
244 const std::string &overlay_name, v3f wield_scale, ITextureSource *tsrc,
247 video::ITexture *texture = tsrc->getTexture(imagename);
249 changeToMesh(nullptr);
252 video::ITexture *overlay_texture =
253 overlay_name.empty() ? NULL : tsrc->getTexture(overlay_name);
255 core::dimension2d<u32> dim = texture->getSize();
256 // Detect animation texture and pull off top frame instead of using entire thing
257 if (num_frames > 1) {
258 u32 frame_height = dim.Height / num_frames;
259 dim = core::dimension2d<u32>(dim.Width, frame_height);
261 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
262 scene::SMesh *mesh = cloneMesh(original);
265 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
266 tsrc->getTexture(imagename));
267 if (overlay_texture) {
268 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
269 copy->getMaterial().setTexture(0, overlay_texture);
270 mesh->addMeshBuffer(copy);
276 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
278 // Customize materials
279 for (u32 layer = 0; layer < m_meshnode->getMaterialCount(); layer++) {
280 video::SMaterial &material = m_meshnode->getMaterial(layer);
281 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
282 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
283 material.MaterialType = m_material_type;
284 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
285 // Enable bi/trilinear filtering only for high resolution textures
286 if (dim.Width > 32) {
287 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
288 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
290 material.setFlag(video::EMF_BILINEAR_FILTER, false);
291 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
293 material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
294 // mipmaps cause "thin black line" artifacts
295 #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
296 material.setFlag(video::EMF_USE_MIP_MAPS, false);
298 if (m_enable_shaders) {
299 material.setTexture(2, tsrc->getShaderFlagsTexture(false));
304 scene::SMesh *createSpecialNodeMesh(Client *client, content_t id, std::vector<ItemPartColor> *colors)
306 MeshMakeData mesh_make_data(client, false, false);
307 MeshCollector collector(false);
308 mesh_make_data.setSmoothLighting(false);
309 MapblockMeshGenerator gen(&mesh_make_data, &collector);
310 gen.renderSingle(id);
312 scene::SMesh *mesh = new scene::SMesh();
313 for (auto &prebuffers : collector.prebuffers)
314 for (PreMeshBuffer &p : prebuffers) {
315 if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
316 const FrameSpec &frame = (*p.layer.frames)[0];
317 p.layer.texture = frame.texture;
318 p.layer.normal_texture = frame.normal_texture;
320 for (video::S3DVertex &v : p.vertices)
321 v.Color.setAlpha(255);
322 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
323 buf->Material.setTexture(0, p.layer.texture);
324 p.layer.applyMaterialOptions(buf->Material);
325 mesh->addMeshBuffer(buf);
326 buf->append(&p.vertices[0], p.vertices.size(),
327 &p.indices[0], p.indices.size());
330 ItemPartColor(p.layer.has_color, p.layer.color));
335 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
337 ITextureSource *tsrc = client->getTextureSource();
338 IItemDefManager *idef = client->getItemDefManager();
339 IShaderSource *shdrsrc = client->getShaderSource();
340 const NodeDefManager *ndef = client->getNodeDefManager();
341 const ItemDefinition &def = item.getDefinition(idef);
342 const ContentFeatures &f = ndef->get(def.name);
343 content_t id = ndef->getId(def.name);
345 scene::SMesh *mesh = nullptr;
347 if (m_enable_shaders) {
348 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
349 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
354 m_base_color = idef->getItemstackColor(item, client);
356 // If wield_image is defined, it overrides everything else
357 if (!def.wield_image.empty()) {
358 setExtruded(def.wield_image, def.wield_overlay, def.wield_scale, tsrc,
360 m_colors.emplace_back();
361 // overlay is white, if present
362 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
367 // See also CItemDefManager::createClientCached()
368 if (def.type == ITEM_NODE) {
370 // e.g. mesh nodes and nodeboxes
371 mesh = cloneMesh(f.mesh_ptr[0]);
372 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
373 &m_material_type, &m_colors);
376 // mesh is pre-scaled by BS * f->visual_scale
377 m_meshnode->setScale(
378 def.wield_scale * WIELD_SCALE_FACTOR
379 / (BS * f.visual_scale));
381 switch (f.drawtype) {
383 changeToMesh(nullptr);
386 case NDT_PLANTLIKE: {
387 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
388 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
389 def.wield_scale, tsrc,
390 f.tiles[0].layers[0].animation_frame_count);
392 const TileLayer &l0 = f.tiles[0].layers[0];
393 m_colors.emplace_back(l0.has_color, l0.color);
394 const TileLayer &l1 = f.tiles[0].layers[1];
395 m_colors.emplace_back(l1.has_color, l1.color);
398 case NDT_PLANTLIKE_ROOTED: {
399 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
400 "", def.wield_scale, tsrc,
401 f.special_tiles[0].layers[0].animation_frame_count);
403 const TileLayer &l0 = f.special_tiles[0].layers[0];
404 m_colors.emplace_back(l0.has_color, l0.color);
410 case NDT_FLOWINGLIQUID: {
411 setCube(f, def.wield_scale);
415 mesh = createSpecialNodeMesh(client, id, &m_colors);
418 m_meshnode->setScale(
419 def.wield_scale * WIELD_SCALE_FACTOR
420 / (BS * f.visual_scale));
424 u32 material_count = m_meshnode->getMaterialCount();
425 for (u32 i = 0; i < material_count; ++i) {
426 video::SMaterial &material = m_meshnode->getMaterial(i);
427 material.MaterialType = m_material_type;
428 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
429 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
430 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
434 else if (!def.inventory_image.empty()) {
435 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
437 m_colors.emplace_back();
438 // overlay is white, if present
439 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
443 // no wield mesh found
444 changeToMesh(nullptr);
447 void WieldMeshSceneNode::setColor(video::SColor c)
450 scene::IMesh *mesh = m_meshnode->getMesh();
455 u8 green = c.getGreen();
456 u8 blue = c.getBlue();
457 u32 mc = mesh->getMeshBufferCount();
458 for (u32 j = 0; j < mc; j++) {
459 video::SColor bc(m_base_color);
460 if ((m_colors.size() > j) && (m_colors[j].override_base))
461 bc = m_colors[j].color;
462 video::SColor buffercolor(255,
463 bc.getRed() * red / 255,
464 bc.getGreen() * green / 255,
465 bc.getBlue() * blue / 255);
466 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
467 colorizeMeshBuffer(buf, &buffercolor);
471 void WieldMeshSceneNode::render()
473 // note: if this method is changed to actually do something,
474 // you probably should implement OnRegisterSceneNode as well
477 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
480 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
481 m_meshnode->setVisible(false);
482 m_meshnode->setMesh(dummymesh);
483 dummymesh->drop(); // m_meshnode grabbed it
485 m_meshnode->setMesh(mesh);
488 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
489 // need to normalize normals when lighting is enabled (because of setScale())
490 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
491 m_meshnode->setVisible(true);
494 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
496 ITextureSource *tsrc = client->getTextureSource();
497 IItemDefManager *idef = client->getItemDefManager();
498 const NodeDefManager *ndef = client->getNodeDefManager();
499 const ItemDefinition &def = item.getDefinition(idef);
500 const ContentFeatures &f = ndef->get(def.name);
501 content_t id = ndef->getId(def.name);
503 if (!g_extrusion_mesh_cache) {
504 g_extrusion_mesh_cache = new ExtrusionMeshCache();
506 g_extrusion_mesh_cache->grab();
509 scene::SMesh *mesh = nullptr;
511 // Shading is on by default
512 result->needs_shading = true;
514 // If inventory_image is defined, it overrides everything else
515 if (!def.inventory_image.empty()) {
516 mesh = getExtrudedMesh(tsrc, def.inventory_image,
517 def.inventory_overlay);
518 result->buffer_colors.emplace_back();
519 // overlay is white, if present
520 result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
521 // Items with inventory images do not need shading
522 result->needs_shading = false;
523 } else if (def.type == ITEM_NODE) {
525 mesh = cloneMesh(f.mesh_ptr[0]);
526 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
527 postProcessNodeMesh(mesh, f, false, false, nullptr,
528 &result->buffer_colors);
530 switch (f.drawtype) {
531 case NDT_PLANTLIKE: {
532 mesh = getExtrudedMesh(tsrc,
533 tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
534 tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
536 const TileLayer &l0 = f.tiles[0].layers[0];
537 result->buffer_colors.emplace_back(l0.has_color, l0.color);
538 const TileLayer &l1 = f.tiles[0].layers[1];
539 result->buffer_colors.emplace_back(l1.has_color, l1.color);
542 case NDT_PLANTLIKE_ROOTED: {
543 mesh = getExtrudedMesh(tsrc,
544 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
546 const TileLayer &l0 = f.special_tiles[0].layers[0];
547 result->buffer_colors.emplace_back(l0.has_color, l0.color);
553 case NDT_FLOWINGLIQUID: {
554 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
555 mesh = cloneMesh(cube);
557 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
559 postProcessNodeMesh(mesh, f, false, false, nullptr,
560 &result->buffer_colors);
564 mesh = createSpecialNodeMesh(client, id, &result->buffer_colors);
565 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
570 u32 mc = mesh->getMeshBufferCount();
571 for (u32 i = 0; i < mc; ++i) {
572 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
573 video::SMaterial &material = buf->getMaterial();
574 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
575 material.setFlag(video::EMF_BILINEAR_FILTER, false);
576 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
577 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
578 material.setFlag(video::EMF_LIGHTING, false);
581 rotateMeshXZby(mesh, -45);
582 rotateMeshYZby(mesh, -30);
589 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
590 const std::string &imagename, const std::string &overlay_name)
593 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
597 video::ITexture *overlay_texture =
598 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
601 core::dimension2d<u32> dim = texture->getSize();
602 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
603 scene::SMesh *mesh = cloneMesh(original);
607 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
608 tsrc->getTexture(imagename));
609 if (overlay_texture) {
610 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
611 copy->getMaterial().setTexture(0, overlay_texture);
612 mesh->addMeshBuffer(copy);
615 // Customize materials
616 for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
617 video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
618 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
619 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
620 material.setFlag(video::EMF_BILINEAR_FILTER, false);
621 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
622 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
623 material.setFlag(video::EMF_LIGHTING, false);
624 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
626 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
631 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
632 bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
633 std::vector<ItemPartColor> *colors, bool apply_scale)
635 u32 mc = mesh->getMeshBufferCount();
636 // Allocate colors for existing buffers
638 for (u32 i = 0; i < mc; ++i)
639 colors->push_back(ItemPartColor());
641 for (u32 i = 0; i < mc; ++i) {
642 const TileSpec *tile = &(f.tiles[i]);
643 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
644 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
645 const TileLayer *layer = &tile->layers[layernum];
646 if (layer->texture_id == 0)
649 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
650 copy->getMaterial() = buf->getMaterial();
651 mesh->addMeshBuffer(copy);
655 ItemPartColor(layer->has_color, layer->color));
657 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
659 video::SMaterial &material = buf->getMaterial();
661 layer->applyMaterialOptions(material);
663 material.MaterialType = *mattype;
665 if (layer->animation_frame_count > 1) {
666 const FrameSpec &animation_frame = (*layer->frames)[0];
667 material.setTexture(0, animation_frame.texture);
669 material.setTexture(0, layer->texture);
672 if (layer->normal_texture) {
673 if (layer->animation_frame_count > 1) {
674 const FrameSpec &animation_frame = (*layer->frames)[0];
675 material.setTexture(1, animation_frame.normal_texture);
677 material.setTexture(1, layer->normal_texture);
679 material.setTexture(2, layer->flags_texture);
681 if (apply_scale && tile->world_aligned) {
682 u32 n = buf->getVertexCount();
683 for (u32 k = 0; k != n; ++k)
684 buf->getTCoords(k) /= layer->scale;