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.
21 #include "wieldmesh.h"
22 #include "inventory.h"
27 #include "mapblock_mesh.h"
28 #include "client/tile.h"
30 #include "util/numeric.h"
32 #include <IMeshManipulator.h>
34 #define WIELD_SCALE_FACTOR 30.0
35 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
37 #define MIN_EXTRUSION_MESH_RESOLUTION 16
38 #define MAX_EXTRUSION_MESH_RESOLUTION 512
40 static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
44 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
45 video::SColor c(255,255,255,255);
46 v3f scale(1.0, 1.0, 0.1);
50 video::S3DVertex vertices[8] = {
52 video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
53 video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
54 video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
55 video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
57 video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
58 video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
59 video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
60 video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
62 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
63 buf->append(vertices, 8, indices, 12);
66 f32 pixelsize_x = 1 / (f32) resolution_x;
67 f32 pixelsize_y = 1 / (f32) resolution_y;
69 for (int i = 0; i < resolution_x; ++i) {
70 f32 pixelpos_x = i * pixelsize_x - 0.5;
72 f32 x1 = pixelpos_x + pixelsize_x;
73 f32 tex0 = (i + 0.1) * pixelsize_x;
74 f32 tex1 = (i + 0.9) * pixelsize_x;
75 video::S3DVertex vertices[8] = {
77 video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
78 video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
79 video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
80 video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
82 video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
83 video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
84 video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
85 video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
87 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
88 buf->append(vertices, 8, indices, 12);
90 for (int i = 0; i < resolution_y; ++i) {
91 f32 pixelpos_y = i * pixelsize_y - 0.5;
92 f32 y0 = -pixelpos_y - pixelsize_y;
94 f32 tex0 = (i + 0.1) * pixelsize_y;
95 f32 tex1 = (i + 0.9) * pixelsize_y;
96 video::S3DVertex vertices[8] = {
98 video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
99 video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
100 video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
101 video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
103 video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
104 video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
105 video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
106 video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
108 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
109 buf->append(vertices, 8, indices, 12);
112 // Create mesh object
113 scene::SMesh *mesh = new scene::SMesh();
114 mesh->addMeshBuffer(buf);
116 scaleMesh(mesh, scale); // also recalculates bounding box
121 Caches extrusion meshes so that only one of them per resolution
122 is needed. Also caches one cube (for convenience).
124 E.g. there is a single extrusion mesh that is used for all
125 16x16 px images, another for all 256x256 px images, and so on.
127 WARNING: Not thread safe. This should not be a problem since
128 rendering related classes (such as WieldMeshSceneNode) will be
129 used from the rendering thread only.
131 class ExtrusionMeshCache: public IReferenceCounted
137 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
138 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
140 m_extrusion_meshes[resolution] =
141 createExtrusionMesh(resolution, resolution);
143 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
146 virtual ~ExtrusionMeshCache()
148 for (std::map<int, scene::IMesh*>::iterator
149 it = m_extrusion_meshes.begin();
150 it != m_extrusion_meshes.end(); ++it) {
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);
240 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
243 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
244 v3f wield_scale, ITextureSource *tsrc, u8 num_frames)
246 video::ITexture *texture = tsrc->getTexture(imagename);
248 changeToMesh(nullptr);
252 core::dimension2d<u32> dim = texture->getSize();
253 // Detect animation texture and pull off top frame instead of using entire thing
254 if (num_frames > 1) {
255 u32 frame_height = dim.Height / num_frames;
256 dim = core::dimension2d<u32>(dim.Width, frame_height);
258 scene::IMesh *mesh = g_extrusion_mesh_cache->create(dim);
259 scene::SMesh *copy = cloneMesh(mesh);
264 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
266 // Customize material
267 video::SMaterial &material = m_meshnode->getMaterial(0);
268 material.setTexture(0, tsrc->getTextureForMesh(imagename));
269 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
270 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
271 material.MaterialType = m_material_type;
272 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
273 // Enable bi/trilinear filtering only for high resolution textures
274 if (dim.Width > 32) {
275 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
276 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
278 material.setFlag(video::EMF_BILINEAR_FILTER, false);
279 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
281 material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
282 // mipmaps cause "thin black line" artifacts
283 #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
284 material.setFlag(video::EMF_USE_MIP_MAPS, false);
286 if (m_enable_shaders) {
287 material.setTexture(2, tsrc->getShaderFlagsTexture(false));
291 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
293 ITextureSource *tsrc = client->getTextureSource();
294 IItemDefManager *idef = client->getItemDefManager();
295 IShaderSource *shdrsrc = client->getShaderSource();
296 INodeDefManager *ndef = client->getNodeDefManager();
297 const ItemDefinition &def = item.getDefinition(idef);
298 const ContentFeatures &f = ndef->get(def.name);
299 content_t id = ndef->getId(def.name);
301 if (m_enable_shaders) {
302 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
303 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
308 m_base_color = idef->getItemstackColor(item, client);
310 // If wield_image is defined, it overrides everything else
311 if (def.wield_image != "") {
312 setExtruded(def.wield_image, def.wield_scale, tsrc, 1);
313 m_colors.push_back(ItemPartColor());
317 // See also CItemDefManager::createClientCached()
318 else if (def.type == ITEM_NODE) {
320 // e.g. mesh nodes and nodeboxes
321 scene::SMesh *mesh = cloneMesh(f.mesh_ptr[0]);
322 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
323 &m_material_type, &m_colors);
326 // mesh is pre-scaled by BS * f->visual_scale
327 m_meshnode->setScale(
328 def.wield_scale * WIELD_SCALE_FACTOR
329 / (BS * f.visual_scale));
331 switch (f.drawtype) {
333 changeToMesh(nullptr);
336 case NDT_PLANTLIKE: {
337 setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
338 def.wield_scale, tsrc,
339 f.tiles[0].layers[0].animation_frame_count);
342 case NDT_PLANTLIKE_ROOTED: {
343 setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
344 def.wield_scale, tsrc,
345 f.special_tiles[0].layers[0].animation_frame_count);
350 setCube(f, def.wield_scale);
354 MeshMakeData mesh_make_data(client, false);
355 MapNode mesh_make_node(id, 255, 0);
356 mesh_make_data.fillSingleNode(&mesh_make_node);
357 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
358 scene::SMesh *mesh = cloneMesh(mapblock_mesh.getMesh());
359 translateMesh(mesh, v3f(-BS, -BS, -BS));
360 postProcessNodeMesh(mesh, f, m_enable_shaders, true,
361 &m_material_type, &m_colors);
364 m_meshnode->setScale(
365 def.wield_scale * WIELD_SCALE_FACTOR
366 / (BS * f.visual_scale));
370 u32 material_count = m_meshnode->getMaterialCount();
371 for (u32 i = 0; i < material_count; ++i) {
372 video::SMaterial &material = m_meshnode->getMaterial(i);
373 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
374 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
375 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
379 else if (def.inventory_image != "") {
380 setExtruded(def.inventory_image, def.wield_scale, tsrc, 1);
381 m_colors.push_back(ItemPartColor());
385 // no wield mesh found
386 changeToMesh(nullptr);
389 void WieldMeshSceneNode::setColor(video::SColor c)
392 scene::IMesh *mesh = m_meshnode->getMesh();
397 u8 green = c.getGreen();
398 u8 blue = c.getBlue();
399 u32 mc = mesh->getMeshBufferCount();
400 for (u32 j = 0; j < mc; j++) {
401 video::SColor bc(m_base_color);
402 if ((m_colors.size() > j) && (m_colors[j].override_base))
403 bc = m_colors[j].color;
404 video::SColor buffercolor(255,
405 bc.getRed() * red / 255,
406 bc.getGreen() * green / 255,
407 bc.getBlue() * blue / 255);
408 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
409 colorizeMeshBuffer(buf, &buffercolor);
413 void WieldMeshSceneNode::render()
415 // note: if this method is changed to actually do something,
416 // you probably should implement OnRegisterSceneNode as well
419 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
422 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
423 m_meshnode->setVisible(false);
424 m_meshnode->setMesh(dummymesh);
425 dummymesh->drop(); // m_meshnode grabbed it
427 m_meshnode->setMesh(mesh);
430 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
431 // need to normalize normals when lighting is enabled (because of setScale())
432 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
433 m_meshnode->setVisible(true);
436 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
438 ITextureSource *tsrc = client->getTextureSource();
439 IItemDefManager *idef = client->getItemDefManager();
440 INodeDefManager *ndef = client->getNodeDefManager();
441 const ItemDefinition &def = item.getDefinition(idef);
442 const ContentFeatures &f = ndef->get(def.name);
443 content_t id = ndef->getId(def.name);
445 if (!g_extrusion_mesh_cache) {
446 g_extrusion_mesh_cache = new ExtrusionMeshCache();
448 g_extrusion_mesh_cache->grab();
451 scene::SMesh *mesh = nullptr;
453 // Shading is on by default
454 result->needs_shading = true;
456 // If inventory_image is defined, it overrides everything else
457 if (def.inventory_image != "") {
458 mesh = getExtrudedMesh(tsrc, def.inventory_image);
459 result->buffer_colors.push_back(ItemPartColor());
460 // Items with inventory images do not need shading
461 result->needs_shading = false;
462 } else if (def.type == ITEM_NODE) {
464 mesh = cloneMesh(f.mesh_ptr[0]);
465 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
467 switch (f.drawtype) {
468 case NDT_PLANTLIKE: {
469 mesh = getExtrudedMesh(tsrc,
470 tsrc->getTextureName(f.tiles[0].layers[0].texture_id));
473 case NDT_PLANTLIKE_ROOTED: {
474 mesh = getExtrudedMesh(tsrc,
475 tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id));
481 case NDT_FLOWINGLIQUID: {
482 scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
483 mesh = cloneMesh(cube);
485 scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
489 MeshMakeData mesh_make_data(client, false);
490 MapNode mesh_make_node(id, 255, 0);
491 mesh_make_data.fillSingleNode(&mesh_make_node);
492 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
493 mesh = cloneMesh(mapblock_mesh.getMesh());
494 translateMesh(mesh, v3f(-BS, -BS, -BS));
495 scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
497 u32 mc = mesh->getMeshBufferCount();
498 for (u32 i = 0; i < mc; ++i) {
499 video::SMaterial &material1 =
500 mesh->getMeshBuffer(i)->getMaterial();
501 video::SMaterial &material2 =
502 mapblock_mesh.getMesh()->getMeshBuffer(i)->getMaterial();
503 material1.setTexture(0, material2.getTexture(0));
504 material1.setTexture(1, material2.getTexture(1));
505 material1.setTexture(2, material2.getTexture(2));
506 material1.setTexture(3, material2.getTexture(3));
507 material1.MaterialType = material2.MaterialType;
513 u32 mc = mesh->getMeshBufferCount();
514 for (u32 i = 0; i < mc; ++i) {
515 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
516 video::SMaterial &material = buf->getMaterial();
517 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
518 material.setFlag(video::EMF_BILINEAR_FILTER, false);
519 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
520 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
521 material.setFlag(video::EMF_LIGHTING, false);
524 rotateMeshXZby(mesh, -45);
525 rotateMeshYZby(mesh, -30);
527 postProcessNodeMesh(mesh, f, false, false, nullptr, &result->buffer_colors);
534 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename)
536 video::ITexture *texture = tsrc->getTextureForMesh(imagename);
541 core::dimension2d<u32> dim = texture->getSize();
542 scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
543 scene::SMesh *mesh = cloneMesh(original);
546 // Customize material
547 video::SMaterial &material = mesh->getMeshBuffer(0)->getMaterial();
548 material.setTexture(0, tsrc->getTexture(imagename));
549 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
550 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
551 material.setFlag(video::EMF_BILINEAR_FILTER, false);
552 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
553 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
554 material.setFlag(video::EMF_LIGHTING, false);
555 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
556 scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
561 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
562 bool use_shaders, bool set_material, video::E_MATERIAL_TYPE *mattype,
563 std::vector<ItemPartColor> *colors)
565 u32 mc = mesh->getMeshBufferCount();
566 // Allocate colors for existing buffers
568 for (u32 i = 0; i < mc; ++i)
569 colors->push_back(ItemPartColor());
571 for (u32 i = 0; i < mc; ++i) {
572 const TileSpec *tile = &(f.tiles[i]);
573 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
574 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
575 const TileLayer *layer = &tile->layers[layernum];
576 if (layer->texture_id == 0)
579 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
580 copy->getMaterial() = buf->getMaterial();
581 mesh->addMeshBuffer(copy);
585 ItemPartColor(layer->has_color, layer->color));
587 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
589 video::SMaterial &material = buf->getMaterial();
591 layer->applyMaterialOptions(material);
593 material.MaterialType = *mattype;
595 if (layer->animation_frame_count > 1) {
596 const FrameSpec &animation_frame = (*layer->frames)[0];
597 material.setTexture(0, animation_frame.texture);
599 material.setTexture(0, layer->texture);
602 if (layer->normal_texture) {
603 if (layer->animation_frame_count > 1) {
604 const FrameSpec &animation_frame = (*layer->frames)[0];
605 material.setTexture(1, animation_frame.normal_texture);
607 material.setTexture(1, layer->normal_texture);
609 material.setTexture(2, layer->flags_texture);