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 "mapblock_mesh.h"
29 #include "client/tile.h"
31 #include "util/numeric.h"
33 #include <IMeshManipulator.h>
35 #define WIELD_SCALE_FACTOR 30.0
36 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
38 #define MIN_EXTRUSION_MESH_RESOLUTION 16
39 #define MAX_EXTRUSION_MESH_RESOLUTION 512
41 static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
45 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
46 video::SColor c(255,255,255,255);
47 v3f scale(1.0, 1.0, 0.1);
51 video::S3DVertex vertices[8] = {
53 video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
54 video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
55 video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
56 video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
58 video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
59 video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
60 video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
61 video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
63 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
64 buf->append(vertices, 8, indices, 12);
67 f32 pixelsize_x = 1 / (f32) resolution_x;
68 f32 pixelsize_y = 1 / (f32) resolution_y;
70 for (int i = 0; i < resolution_x; ++i) {
71 f32 pixelpos_x = i * pixelsize_x - 0.5;
73 f32 x1 = pixelpos_x + pixelsize_x;
74 f32 tex0 = (i + 0.1) * pixelsize_x;
75 f32 tex1 = (i + 0.9) * pixelsize_x;
76 video::S3DVertex vertices[8] = {
78 video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
79 video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
80 video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
81 video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
83 video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
84 video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
85 video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
86 video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
88 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
89 buf->append(vertices, 8, indices, 12);
91 for (int i = 0; i < resolution_y; ++i) {
92 f32 pixelpos_y = i * pixelsize_y - 0.5;
93 f32 y0 = -pixelpos_y - pixelsize_y;
95 f32 tex0 = (i + 0.1) * pixelsize_y;
96 f32 tex1 = (i + 0.9) * pixelsize_y;
97 video::S3DVertex vertices[8] = {
99 video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
100 video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
101 video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
102 video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
104 video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
105 video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
106 video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
107 video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
109 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
110 buf->append(vertices, 8, indices, 12);
113 // Create mesh object
114 scene::SMesh *mesh = new scene::SMesh();
115 mesh->addMeshBuffer(buf);
117 scaleMesh(mesh, scale); // also recalculates bounding box
122 Caches extrusion meshes so that only one of them per resolution
123 is needed. Also caches one cube (for convenience).
125 E.g. there is a single extrusion mesh that is used for all
126 16x16 px images, another for all 256x256 px images, and so on.
128 WARNING: Not thread safe. This should not be a problem since
129 rendering related classes (such as WieldMeshSceneNode) will be
130 used from the rendering thread only.
132 class ExtrusionMeshCache: public IReferenceCounted
138 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
139 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
141 m_extrusion_meshes[resolution] =
142 createExtrusionMesh(resolution, resolution);
144 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
147 virtual ~ExtrusionMeshCache()
149 for (std::map<int, scene::IMesh*>::iterator
150 it = m_extrusion_meshes.begin();
151 it != m_extrusion_meshes.end(); ++it) {
156 // Get closest extrusion mesh for given image dimensions
157 // Caller must drop the returned pointer
158 scene::IMesh* create(core::dimension2d<u32> dim)
160 // handle non-power of two textures inefficiently without cache
161 if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) {
162 return createExtrusionMesh(dim.Width, dim.Height);
165 int maxdim = MYMAX(dim.Width, dim.Height);
167 std::map<int, scene::IMesh*>::iterator
168 it = m_extrusion_meshes.lower_bound(maxdim);
170 if (it == m_extrusion_meshes.end()) {
171 // no viable resolution found; use largest one
172 it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION);
173 sanity_check(it != m_extrusion_meshes.end());
176 scene::IMesh *mesh = it->second;
180 // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
181 // Caller must drop the returned pointer
182 scene::IMesh* createCube()
189 std::map<int, scene::IMesh*> m_extrusion_meshes;
190 scene::IMesh *m_cube;
193 ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
196 WieldMeshSceneNode::WieldMeshSceneNode(scene::ISceneManager *mgr, s32 id, bool lighting):
197 scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
198 m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
201 m_enable_shaders = g_settings->getBool("enable_shaders");
202 m_anisotropic_filter = g_settings->getBool("anisotropic_filter");
203 m_bilinear_filter = g_settings->getBool("bilinear_filter");
204 m_trilinear_filter = g_settings->getBool("trilinear_filter");
206 // If this is the first wield mesh scene node, create a cache
207 // for extrusion meshes (and a cube mesh), otherwise reuse it
208 if (!g_extrusion_mesh_cache)
209 g_extrusion_mesh_cache = new ExtrusionMeshCache();
211 g_extrusion_mesh_cache->grab();
213 // Disable bounding box culling for this scene node
214 // since we won't calculate the bounding box.
215 setAutomaticCulling(scene::EAC_OFF);
217 // Create the child scene node
218 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
219 m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1);
220 m_meshnode->setReadOnlyMaterials(false);
221 m_meshnode->setVisible(false);
222 dummymesh->drop(); // m_meshnode grabbed it
225 WieldMeshSceneNode::~WieldMeshSceneNode()
227 sanity_check(g_extrusion_mesh_cache);
228 if (g_extrusion_mesh_cache->drop())
229 g_extrusion_mesh_cache = nullptr;
232 void WieldMeshSceneNode::setCube(const ContentFeatures &f,
235 scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
236 scene::SMesh *copy = cloneMesh(cubemesh);
238 postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors);
241 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
244 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
245 v3f wield_scale, ITextureSource *tsrc, u8 num_frames)
247 video::ITexture *texture = tsrc->getTexture(imagename);
249 changeToMesh(nullptr);
253 core::dimension2d<u32> dim = texture->getSize();
254 // Detect animation texture and pull off top frame instead of using entire thing
255 if (num_frames > 1) {
256 u32 frame_height = dim.Height / num_frames;
257 dim = core::dimension2d<u32>(dim.Width, frame_height);
259 scene::IMesh *mesh = g_extrusion_mesh_cache->create(dim);
260 scene::SMesh *copy = cloneMesh(mesh);
265 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
267 // Customize material
268 video::SMaterial &material = m_meshnode->getMaterial(0);
269 material.setTexture(0, tsrc->getTextureForMesh(imagename));
270 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
271 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
272 material.MaterialType = m_material_type;
273 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
274 // Enable bi/trilinear filtering only for high resolution textures
275 if (dim.Width > 32) {
276 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
277 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
279 material.setFlag(video::EMF_BILINEAR_FILTER, false);
280 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
282 material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
283 // mipmaps cause "thin black line" artifacts
284 #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
285 material.setFlag(video::EMF_USE_MIP_MAPS, false);
287 if (m_enable_shaders) {
288 material.setTexture(2, tsrc->getShaderFlagsTexture(false));
292 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
294 ITextureSource *tsrc = client->getTextureSource();
295 IItemDefManager *idef = client->getItemDefManager();
296 IShaderSource *shdrsrc = client->getShaderSource();
297 INodeDefManager *ndef = client->getNodeDefManager();
298 const ItemDefinition &def = item.getDefinition(idef);
299 const ContentFeatures &f = ndef->get(def.name);
300 content_t id = ndef->getId(def.name);
302 if (m_enable_shaders) {
303 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
304 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
309 m_base_color = idef->getItemstackColor(item, client);
311 // If wield_image is defined, it overrides everything else
312 if (def.wield_image != "") {
313 setExtruded(def.wield_image, def.wield_scale, tsrc, 1);
314 m_colors.push_back(ItemPartColor());
318 // See also CItemDefManager::createClientCached()
319 else if (def.type == ITEM_NODE) {
321 // e.g. mesh nodes and nodeboxes
322 scene::SMesh *mesh = cloneMesh(f.mesh_ptr[0]);
323 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
324 &m_material_type, &m_colors);
327 // mesh is pre-scaled by BS * f->visual_scale
328 m_meshnode->setScale(
329 def.wield_scale * WIELD_SCALE_FACTOR
330 / (BS * f.visual_scale));
332 switch (f.drawtype) {
334 changeToMesh(nullptr);
337 case NDT_PLANTLIKE: {
338 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
339 def.wield_scale, tsrc,
340 f.tiles[0].layers[0].animation_frame_count);
343 case NDT_PLANTLIKE_ROOTED: {
344 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
345 def.wield_scale, tsrc,
346 f.special_tiles[0].layers[0].animation_frame_count);
351 setCube(f, def.wield_scale);
355 MeshMakeData mesh_make_data(client, false);
356 MapNode mesh_make_node(id, 255, 0);
357 mesh_make_data.fillSingleNode(&mesh_make_node);
358 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
359 scene::SMesh *mesh = cloneMesh(mapblock_mesh.getMesh());
360 translateMesh(mesh, v3f(-BS, -BS, -BS));
361 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
362 &m_material_type, &m_colors);
365 m_meshnode->setScale(
366 def.wield_scale * WIELD_SCALE_FACTOR
367 / (BS * f.visual_scale));
371 u32 material_count = m_meshnode->getMaterialCount();
372 for (u32 i = 0; i < material_count; ++i) {
373 video::SMaterial &material = m_meshnode->getMaterial(i);
374 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
375 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
376 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
380 else if (def.inventory_image != "") {
381 setExtruded(def.inventory_image, def.wield_scale, tsrc, 1);
382 m_colors.push_back(ItemPartColor());
386 // no wield mesh found
387 changeToMesh(nullptr);
390 void WieldMeshSceneNode::setColor(video::SColor c)
393 scene::IMesh *mesh = m_meshnode->getMesh();
398 u8 green = c.getGreen();
399 u8 blue = c.getBlue();
400 u32 mc = mesh->getMeshBufferCount();
401 for (u32 j = 0; j < mc; j++) {
402 video::SColor bc(m_base_color);
403 if ((m_colors.size() > j) && (m_colors[j].override_base))
404 bc = m_colors[j].color;
405 video::SColor buffercolor(255,
406 bc.getRed() * red / 255,
407 bc.getGreen() * green / 255,
408 bc.getBlue() * blue / 255);
409 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
410 colorizeMeshBuffer(buf, &buffercolor);
414 void WieldMeshSceneNode::render()
416 // note: if this method is changed to actually do something,
417 // you probably should implement OnRegisterSceneNode as well
420 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
423 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
424 m_meshnode->setVisible(false);
425 m_meshnode->setMesh(dummymesh);
426 dummymesh->drop(); // m_meshnode grabbed it
428 m_meshnode->setMesh(mesh);
431 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
432 // need to normalize normals when lighting is enabled (because of setScale())
433 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
434 m_meshnode->setVisible(true);
437 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
439 ITextureSource *tsrc = client->getTextureSource();
440 IItemDefManager *idef = client->getItemDefManager();
441 INodeDefManager *ndef = client->getNodeDefManager();
442 const ItemDefinition &def = item.getDefinition(idef);
443 const ContentFeatures &f = ndef->get(def.name);
444 content_t id = ndef->getId(def.name);
446 if (!g_extrusion_mesh_cache) {
447 g_extrusion_mesh_cache = new ExtrusionMeshCache();
449 g_extrusion_mesh_cache->grab();
452 scene::SMesh *mesh = nullptr;
454 // Shading is on by default
455 result->needs_shading = true;
457 // If inventory_image is defined, it overrides everything else
458 if (def.inventory_image != "") {
459 mesh = getExtrudedMesh(tsrc, def.inventory_image);
460 result->buffer_colors.push_back(ItemPartColor());
461 // Items with inventory images do not need shading
462 result->needs_shading = false;
463 } else if (def.type == ITEM_NODE) {
465 mesh = cloneMesh(f.mesh_ptr[0]);
466 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
468 switch (f.drawtype) {
469 case NDT_PLANTLIKE: {
470 mesh = getExtrudedMesh(tsrc,
471 tsrc->getTextureName(f.tiles[0].layers[0].texture_id));
474 case NDT_PLANTLIKE_ROOTED: {
475 mesh = getExtrudedMesh(tsrc,
476 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id));
482 case NDT_FLOWINGLIQUID: {
483 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
484 mesh = cloneMesh(cube);
486 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
490 MeshMakeData mesh_make_data(client, false);
491 MapNode mesh_make_node(id, 255, 0);
492 mesh_make_data.fillSingleNode(&mesh_make_node);
493 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
494 mesh = cloneMesh(mapblock_mesh.getMesh());
495 translateMesh(mesh, v3f(-BS, -BS, -BS));
496 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
498 u32 mc = mesh->getMeshBufferCount();
499 for (u32 i = 0; i < mc; ++i) {
500 video::SMaterial &material1 =
501 mesh->getMeshBuffer(i)->getMaterial();
502 video::SMaterial &material2 =
503 mapblock_mesh.getMesh()->getMeshBuffer(i)->getMaterial();
504 material1.setTexture(0, material2.getTexture(0));
505 material1.setTexture(1, material2.getTexture(1));
506 material1.setTexture(2, material2.getTexture(2));
507 material1.setTexture(3, material2.getTexture(3));
508 material1.MaterialType = material2.MaterialType;
514 u32 mc = mesh->getMeshBufferCount();
515 for (u32 i = 0; i < mc; ++i) {
516 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
517 video::SMaterial &material = buf->getMaterial();
518 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
519 material.setFlag(video::EMF_BILINEAR_FILTER, false);
520 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
521 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
522 material.setFlag(video::EMF_LIGHTING, false);
525 rotateMeshXZby(mesh, -45);
526 rotateMeshYZby(mesh, -30);
528 postProcessNodeMesh(mesh, f, false, false, nullptr, &result->buffer_colors);
535 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename)
537 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
542 core::dimension2d<u32> dim = texture->getSize();
543 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
544 scene::SMesh *mesh = cloneMesh(original);
547 // Customize material
548 video::SMaterial &material = mesh->getMeshBuffer(0)->getMaterial();
549 material.setTexture(0, tsrc->getTexture(imagename));
550 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
551 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
552 material.setFlag(video::EMF_BILINEAR_FILTER, false);
553 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
554 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
555 material.setFlag(video::EMF_LIGHTING, false);
556 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
557 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
562 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
563 bool use_shaders, bool set_material, video::E_MATERIAL_TYPE *mattype,
564 std::vector<ItemPartColor> *colors)
566 u32 mc = mesh->getMeshBufferCount();
567 // Allocate colors for existing buffers
569 for (u32 i = 0; i < mc; ++i)
570 colors->push_back(ItemPartColor());
572 for (u32 i = 0; i < mc; ++i) {
573 const TileSpec *tile = &(f.tiles[i]);
574 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
575 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
576 const TileLayer *layer = &tile->layers[layernum];
577 if (layer->texture_id == 0)
580 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
581 copy->getMaterial() = buf->getMaterial();
582 mesh->addMeshBuffer(copy);
586 ItemPartColor(layer->has_color, layer->color));
588 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
590 video::SMaterial &material = buf->getMaterial();
592 layer->applyMaterialOptions(material);
594 material.MaterialType = *mattype;
596 if (layer->animation_frame_count > 1) {
597 const FrameSpec &animation_frame = (*layer->frames)[0];
598 material.setTexture(0, animation_frame.texture);
600 material.setTexture(0, layer->texture);
603 if (layer->normal_texture) {
604 if (layer->animation_frame_count > 1) {
605 const FrameSpec &animation_frame = (*layer->frames)[0];
606 material.setTexture(1, animation_frame.normal_texture);
608 material.setTexture(1, layer->normal_texture);
610 material.setTexture(2, layer->flags_texture);