+
+void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
+{
+ ITextureSource *tsrc = client->getTextureSource();
+ IItemDefManager *idef = client->getItemDefManager();
+ INodeDefManager *ndef = client->getNodeDefManager();
+ const ItemDefinition &def = item.getDefinition(idef);
+ const ContentFeatures &f = ndef->get(def.name);
+ content_t id = ndef->getId(def.name);
+
+ if (!g_extrusion_mesh_cache) {
+ g_extrusion_mesh_cache = new ExtrusionMeshCache();
+ } else {
+ g_extrusion_mesh_cache->grab();
+ }
+
+ scene::SMesh *mesh = nullptr;
+
+ // Shading is on by default
+ result->needs_shading = true;
+
+ // If inventory_image is defined, it overrides everything else
+ if (!def.inventory_image.empty()) {
+ mesh = getExtrudedMesh(tsrc, def.inventory_image,
+ def.inventory_overlay);
+ result->buffer_colors.emplace_back();
+ // overlay is white, if present
+ result->buffer_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
+ // Items with inventory images do not need shading
+ result->needs_shading = false;
+ } else if (def.type == ITEM_NODE) {
+ if (f.mesh_ptr[0]) {
+ mesh = cloneMesh(f.mesh_ptr[0]);
+ scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
+ postProcessNodeMesh(mesh, f, false, false, nullptr,
+ &result->buffer_colors);
+ } else {
+ switch (f.drawtype) {
+ case NDT_PLANTLIKE: {
+ mesh = getExtrudedMesh(tsrc,
+ tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
+ tsrc->getTextureName(f.tiles[0].layers[1].texture_id));
+ // Add color
+ const TileLayer &l0 = f.tiles[0].layers[0];
+ result->buffer_colors.emplace_back(l0.has_color, l0.color);
+ const TileLayer &l1 = f.tiles[0].layers[1];
+ result->buffer_colors.emplace_back(l1.has_color, l1.color);
+ break;
+ }
+ case NDT_PLANTLIKE_ROOTED: {
+ mesh = getExtrudedMesh(tsrc,
+ tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id), "");
+ // Add color
+ const TileLayer &l0 = f.special_tiles[0].layers[0];
+ result->buffer_colors.emplace_back(l0.has_color, l0.color);
+ break;
+ }
+ case NDT_NORMAL:
+ case NDT_ALLFACES:
+ case NDT_LIQUID:
+ case NDT_FLOWINGLIQUID: {
+ scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
+ mesh = cloneMesh(cube);
+ cube->drop();
+ scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
+ // add overlays
+ postProcessNodeMesh(mesh, f, false, false, nullptr,
+ &result->buffer_colors);
+ break;
+ }
+ default: {
+ MeshMakeData mesh_make_data(client, false);
+ MapNode mesh_make_node(id, 255, 0);
+ mesh_make_data.fillSingleNode(&mesh_make_node);
+ MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
+ mesh = cloneMesh(mapblock_mesh.getMesh());
+ translateMesh(mesh, v3f(-BS, -BS, -BS));
+ scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
+
+ u32 mc = mesh->getMeshBufferCount();
+ for (u32 i = 0; i < mc; ++i) {
+ video::SMaterial &material1 =
+ mesh->getMeshBuffer(i)->getMaterial();
+ video::SMaterial &material2 =
+ mapblock_mesh.getMesh()->getMeshBuffer(i)->getMaterial();
+ material1.setTexture(0, material2.getTexture(0));
+ material1.setTexture(1, material2.getTexture(1));
+ material1.setTexture(2, material2.getTexture(2));
+ material1.setTexture(3, material2.getTexture(3));
+ material1.MaterialType = material2.MaterialType;
+ }
+ // add overlays (since getMesh() returns
+ // the base layer only)
+ postProcessNodeMesh(mesh, f, false, false, nullptr,
+ &result->buffer_colors);
+ }
+ }
+ }
+
+ u32 mc = mesh->getMeshBufferCount();
+ for (u32 i = 0; i < mc; ++i) {
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
+ video::SMaterial &material = buf->getMaterial();
+ material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ material.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material.setFlag(video::EMF_TRILINEAR_FILTER, false);
+ material.setFlag(video::EMF_BACK_FACE_CULLING, true);
+ material.setFlag(video::EMF_LIGHTING, false);
+ }
+
+ rotateMeshXZby(mesh, -45);
+ rotateMeshYZby(mesh, -30);
+ }
+ result->mesh = mesh;
+}
+
+
+
+scene::SMesh *getExtrudedMesh(ITextureSource *tsrc,
+ const std::string &imagename, const std::string &overlay_name)
+{
+ // check textures
+ video::ITexture *texture = tsrc->getTextureForMesh(imagename);
+ if (!texture) {
+ return NULL;
+ }
+ video::ITexture *overlay_texture =
+ (overlay_name.empty()) ? NULL : tsrc->getTexture(overlay_name);
+
+ // get mesh
+ core::dimension2d<u32> dim = texture->getSize();
+ scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
+ scene::SMesh *mesh = cloneMesh(original);
+ original->drop();
+
+ //set texture
+ mesh->getMeshBuffer(0)->getMaterial().setTexture(0,
+ tsrc->getTexture(imagename));
+ if (overlay_texture) {
+ scene::IMeshBuffer *copy = cloneMeshBuffer(mesh->getMeshBuffer(0));
+ copy->getMaterial().setTexture(0, overlay_texture);
+ mesh->addMeshBuffer(copy);
+ copy->drop();
+ }
+ // Customize materials
+ for (u32 layer = 0; layer < mesh->getMeshBufferCount(); layer++) {
+ video::SMaterial &material = mesh->getMeshBuffer(layer)->getMaterial();
+ material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
+ material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
+ material.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material.setFlag(video::EMF_TRILINEAR_FILTER, false);
+ material.setFlag(video::EMF_BACK_FACE_CULLING, true);
+ material.setFlag(video::EMF_LIGHTING, false);
+ material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ }
+ scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
+
+ return mesh;
+}
+
+void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
+ bool use_shaders, bool set_material, const video::E_MATERIAL_TYPE *mattype,
+ std::vector<ItemPartColor> *colors)
+{
+ u32 mc = mesh->getMeshBufferCount();
+ // Allocate colors for existing buffers
+ colors->clear();
+ for (u32 i = 0; i < mc; ++i)
+ colors->push_back(ItemPartColor());
+
+ for (u32 i = 0; i < mc; ++i) {
+ const TileSpec *tile = &(f.tiles[i]);
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
+ for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
+ const TileLayer *layer = &tile->layers[layernum];
+ if (layer->texture_id == 0)
+ continue;
+ if (layernum != 0) {
+ scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
+ copy->getMaterial() = buf->getMaterial();
+ mesh->addMeshBuffer(copy);
+ copy->drop();
+ buf = copy;
+ colors->push_back(
+ ItemPartColor(layer->has_color, layer->color));
+ } else {
+ (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
+ }
+ video::SMaterial &material = buf->getMaterial();
+ if (set_material)
+ layer->applyMaterialOptions(material);
+ if (mattype) {
+ material.MaterialType = *mattype;
+ }
+ if (layer->animation_frame_count > 1) {
+ const FrameSpec &animation_frame = (*layer->frames)[0];
+ material.setTexture(0, animation_frame.texture);
+ } else {
+ material.setTexture(0, layer->texture);
+ }
+ if (use_shaders) {
+ if (layer->normal_texture) {
+ if (layer->animation_frame_count > 1) {
+ const FrameSpec &animation_frame = (*layer->frames)[0];
+ material.setTexture(1, animation_frame.normal_texture);
+ } else
+ material.setTexture(1, layer->normal_texture);
+ }
+ material.setTexture(2, layer->flags_texture);
+ }
+ }
+ }
+}