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 // always set all textures
324 // with no shaders only texture 0 is ever actually used
325 buf->Material.setTexture(0, p.layer.texture);
326 buf->Material.setTexture(1, p.layer.normal_texture);
327 buf->Material.setTexture(2, p.layer.flags_texture);
328 p.layer.applyMaterialOptions(buf->Material);
329 mesh->addMeshBuffer(buf);
330 buf->append(&p.vertices[0], p.vertices.size(),
331 &p.indices[0], p.indices.size());
334 ItemPartColor(p.layer.has_color, p.layer.color));
339 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
341 ITextureSource *tsrc = client->getTextureSource();
342 IItemDefManager *idef = client->getItemDefManager();
343 IShaderSource *shdrsrc = client->getShaderSource();
344 INodeDefManager *ndef = client->getNodeDefManager();
345 const ItemDefinition &def = item.getDefinition(idef);
346 const ContentFeatures &f = ndef->get(def.name);
347 content_t id = ndef->getId(def.name);
349 scene::SMesh *mesh = nullptr;
351 if (m_enable_shaders) {
352 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
353 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
358 m_base_color = idef->getItemstackColor(item, client);
360 // If wield_image is defined, it overrides everything else
361 if (!def.wield_image.empty()) {
362 setExtruded(def.wield_image, def.wield_overlay, def.wield_scale, tsrc,
364 m_colors.emplace_back();
365 // overlay is white, if present
366 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
371 // See also CItemDefManager::createClientCached()
372 if (def.type == ITEM_NODE) {
374 // e.g. mesh nodes and nodeboxes
375 mesh = cloneMesh(f.mesh_ptr[0]);
376 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
377 &m_material_type, &m_colors);
380 // mesh is pre-scaled by BS * f->visual_scale
381 m_meshnode->setScale(
382 def.wield_scale * WIELD_SCALE_FACTOR
383 / (BS * f.visual_scale));
385 switch (f.drawtype) {
387 changeToMesh(nullptr);
390 case NDT_PLANTLIKE: {
391 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
392 tsrc->getTextureName(f.tiles[0].layers[1].texture_id),
393 def.wield_scale, tsrc,
394 f.tiles[0].layers[0].animation_frame_count);
396 const TileLayer &l0 = f.tiles[0].layers[0];
397 m_colors.emplace_back(l0.has_color, l0.color);
398 const TileLayer &l1 = f.tiles[0].layers[1];
399 m_colors.emplace_back(l1.has_color, l1.color);
402 case NDT_PLANTLIKE_ROOTED: {
403 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
404 "", def.wield_scale, tsrc,
405 f.special_tiles[0].layers[0].animation_frame_count);
407 const TileLayer &l0 = f.special_tiles[0].layers[0];
408 m_colors.emplace_back(l0.has_color, l0.color);
414 case NDT_FLOWINGLIQUID: {
415 setCube(f, def.wield_scale);
419 mesh = createSpecialNodeMesh(client, id, &m_colors);
422 m_meshnode->setScale(
423 def.wield_scale * WIELD_SCALE_FACTOR
424 / (BS * f.visual_scale));
428 u32 material_count = m_meshnode->getMaterialCount();
429 for (u32 i = 0; i < material_count; ++i) {
430 video::SMaterial &material = m_meshnode->getMaterial(i);
431 material.MaterialType = m_material_type;
432 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
433 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
434 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
438 else if (!def.inventory_image.empty()) {
439 setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
441 m_colors.emplace_back();
442 // overlay is white, if present
443 m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
447 // no wield mesh found
448 changeToMesh(nullptr);
451 void WieldMeshSceneNode::setColor(video::SColor c)
454 scene::IMesh *mesh = m_meshnode->getMesh();
459 u8 green = c.getGreen();
460 u8 blue = c.getBlue();
461 u32 mc = mesh->getMeshBufferCount();
462 for (u32 j = 0; j < mc; j++) {
463 video::SColor bc(m_base_color);
464 if ((m_colors.size() > j) && (m_colors[j].override_base))
465 bc = m_colors[j].color;
466 video::SColor buffercolor(255,
467 bc.getRed() * red / 255,
468 bc.getGreen() * green / 255,
469 bc.getBlue() * blue / 255);
470 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
471 colorizeMeshBuffer(buf, &buffercolor);
475 void WieldMeshSceneNode::render()
477 // note: if this method is changed to actually do something,
478 // you probably should implement OnRegisterSceneNode as well
481 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
484 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
485 m_meshnode->setVisible(false);
486 m_meshnode->setMesh(dummymesh);
487 dummymesh->drop(); // m_meshnode grabbed it
489 m_meshnode->setMesh(mesh);
492 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
493 // need to normalize normals when lighting is enabled (because of setScale())
494 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
495 m_meshnode->setVisible(true);
498 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
500 ITextureSource *tsrc = client->getTextureSource();
501 IItemDefManager *idef = client->getItemDefManager();
502 INodeDefManager *ndef = client->getNodeDefManager();
503 const ItemDefinition &def = item.getDefinition(idef);
504 const ContentFeatures &f = ndef->get(def.name);
505 content_t id = ndef->getId(def.name);
507 if (!g_extrusion_mesh_cache) {
508 g_extrusion_mesh_cache = new ExtrusionMeshCache();
510 g_extrusion_mesh_cache->grab();
513 scene::SMesh *mesh = nullptr;
515 // Shading is on by default
516 result->needs_shading = true;
518 // If inventory_image is defined, it overrides everything else
519 if (!def.inventory_image.empty()) {
520 mesh = getExtrudedMesh(tsrc, def.inventory_image,
521 def.inventory_overlay);
522 result->buffer_colors.emplace_back();
523 // overlay is white, if present
524 result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
525 // Items with inventory images do not need shading
526 result->needs_shading = false;
527 } else if (def.type == ITEM_NODE) {
529 mesh = cloneMesh(f.mesh_ptr[0]);
530 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
531 postProcessNodeMesh(mesh, f, false, false, nullptr,
532 &result->buffer_colors);
534 switch (f.drawtype) {
535 case NDT_PLANTLIKE: {
536 mesh = getExtrudedMesh(tsrc,
537 tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
538 tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
540 const TileLayer &l0 = f.tiles[0].layers[0];
541 result->buffer_colors.emplace_back(l0.has_color, l0.color);
542 const TileLayer &l1 = f.tiles[0].layers[1];
543 result->buffer_colors.emplace_back(l1.has_color, l1.color);
546 case NDT_PLANTLIKE_ROOTED: {
547 mesh = getExtrudedMesh(tsrc,
548 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
550 const TileLayer &l0 = f.special_tiles[0].layers[0];
551 result->buffer_colors.emplace_back(l0.has_color, l0.color);
557 case NDT_FLOWINGLIQUID: {
558 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
559 mesh = cloneMesh(cube);
561 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
563 postProcessNodeMesh(mesh, f, false, false, nullptr,
564 &result->buffer_colors);
568 mesh = createSpecialNodeMesh(client, id, &result->buffer_colors);
569 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
574 u32 mc = mesh->getMeshBufferCount();
575 for (u32 i = 0; i < mc; ++i) {
576 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
577 video::SMaterial &material = buf->getMaterial();
578 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
579 material.setFlag(video::EMF_BILINEAR_FILTER, false);
580 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
581 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
582 material.setFlag(video::EMF_LIGHTING, false);
585 rotateMeshXZby(mesh, -45);
586 rotateMeshYZby(mesh, -30);
593 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
594 const std::string &imagename, const std::string &overlay_name)
597 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
601 video::ITexture *overlay_texture =
602 (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
605 core::dimension2d<u32> dim = texture->getSize();
606 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
607 scene::SMesh *mesh = cloneMesh(original);
611 mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
612 tsrc->getTexture(imagename));
613 if (overlay_texture) {
614 scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
615 copy->getMaterial().setTexture(0, overlay_texture);
616 mesh->addMeshBuffer(copy);
619 // Customize materials
620 for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
621 video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
622 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
623 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
624 material.setFlag(video::EMF_BILINEAR_FILTER, false);
625 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
626 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
627 material.setFlag(video::EMF_LIGHTING, false);
628 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
630 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
635 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
636 bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
637 std::vector<ItemPartColor> *colors, bool apply_scale)
639 u32 mc = mesh->getMeshBufferCount();
640 // Allocate colors for existing buffers
642 for (u32 i = 0; i < mc; ++i)
643 colors->push_back(ItemPartColor());
645 for (u32 i = 0; i < mc; ++i) {
646 const TileSpec *tile = &(f.tiles[i]);
647 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
648 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
649 const TileLayer *layer = &tile->layers[layernum];
650 if (layer->texture_id == 0)
653 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
654 copy->getMaterial() = buf->getMaterial();
655 mesh->addMeshBuffer(copy);
659 ItemPartColor(layer->has_color, layer->color));
661 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
663 video::SMaterial &material = buf->getMaterial();
665 layer->applyMaterialOptions(material);
667 material.MaterialType = *mattype;
669 if (layer->animation_frame_count > 1) {
670 const FrameSpec &animation_frame = (*layer->frames)[0];
671 material.setTexture(0, animation_frame.texture);
673 material.setTexture(0, layer->texture);
676 if (layer->normal_texture) {
677 if (layer->animation_frame_count > 1) {
678 const FrameSpec &animation_frame = (*layer->frames)[0];
679 material.setTexture(1, animation_frame.normal_texture);
681 material.setTexture(1, layer->normal_texture);
683 material.setTexture(2, layer->flags_texture);
685 if (apply_scale && tile->world_aligned) {
686 u32 n = buf->getVertexCount();
687 for (u32 k = 0; k != n; ++k)
688 buf->getTCoords(k) /= layer->scale;