scene::IMesh * getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename) { video::ITexture *texture = tsrc->getTextureForMesh(imagename); if (!texture) { return NULL; } core::dimension2d<u32> dim = texture->getSize(); scene::IMesh *mesh = cloneMesh(g_extrusion_mesh_cache->create(dim)); // Customize material video::SMaterial &material = mesh->getMeshBuffer(0)->getMaterial(); material.setTexture(0, tsrc->getTexture(imagename)); 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; }
scene::IAnimatedMesh* createCubeMesh(v3f scale) { video::SColor c(255,255,255,255); video::S3DVertex vertices[24] = { // Up video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1), video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0), video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0), video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1), // Down video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0), video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0), video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1), video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1), // Right video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1), video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0), video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0), video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1), // Left video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1), video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1), video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0), video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0), // Back video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1), video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1), video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0), video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0), // Front video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1), video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0), video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0), video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1), }; u16 indices[6] = {0,1,2,2,3,0}; scene::SMesh *mesh = new scene::SMesh(); for (u32 i=0; i<6; ++i) { scene::IMeshBuffer *buf = new scene::SMeshBuffer(); buf->append(vertices + 4 * i, 4, indices, 6); // Set default material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; // Add mesh buffer to mesh mesh->addMeshBuffer(buf); buf->drop(); } scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh); mesh->drop(); scaleMesh(anim_mesh, scale); // also recalculates bounding box return anim_mesh; }
TrianglesList scaleAndCenterMesh(TrianglesList &meshTriangles, double scaleFactor) { //Build MeshData TrianglesInfoList cutInfo = createNewTriangleInfoList(meshTriangles); MeshData meshData(meshTriangles, cutInfo); //Center mesh std::list<MeshData> listMeshData; listMeshData.push_back(meshData); std::list<TranslatedMeshData> translatedMeshData = centerMeshesInBarycenter(listMeshData); //Scale mesh MeshData scaledMeshData = scaleMesh(translatedMeshData.front().first, scaleFactor); return scaledMeshData.first; }
scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture, video::IVideoDriver *driver, v3f scale) { scene::IAnimatedMesh *mesh = NULL; core::dimension2d<u32> size = texture->getSize(); video::ECOLOR_FORMAT format = texture->getColorFormat(); if (format == video::ECF_A8R8G8B8) { // Texture is in the correct color format, we can pass it // to extrudeARGB right away. void *data = texture->lock(MY_ETLM_READ_ONLY); if (data == NULL) return NULL; mesh = extrudeARGB(size.Width, size.Height, (u8*) data); texture->unlock(); } else { video::IImage *img1 = driver->createImageFromData(format, size, texture->lock(MY_ETLM_READ_ONLY)); if (img1 == NULL) return NULL; // img1 is in the texture's color format, convert to 8-bit ARGB video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, size); if (img2 == NULL) { img1->drop(); return NULL; } img1->copyTo(img2); img1->drop(); mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock()); img2->unlock(); img2->drop(); } // Set default material mesh->getMeshBuffer(0)->getMaterial().setTexture(0, texture); mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_LIGHTING, false); mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); mesh->getMeshBuffer(0)->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; scaleMesh(mesh, scale); // also recalculates bounding box return 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 getItemMesh(Client *client, const ItemStack &item, ItemMesh *result) { ITextureSource *tsrc = client->getTextureSource(); IItemDefManager *idef = client->getItemDefManager(); const NodeDefManager *ndef = client->getNodeDefManager(); const ItemDefinition &def = item.getDefinition(idef); const ContentFeatures &f = ndef->get(def.name); content_t id = ndef->getId(def.name); FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized"); 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: { mesh = createSpecialNodeMesh(client, id, &result->buffer_colors); scaleMesh(mesh, v3f(0.12, 0.12, 0.12)); } } } 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; }
static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y) { const f32 r = 0.5; scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); v3f scale(1.0, 1.0, 0.1); // Front and back { video::S3DVertex vertices[8] = { // z- video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0), video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0), video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1), video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1), // z+ video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0), video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1), video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1), video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } f32 pixelsize_x = 1 / (f32) resolution_x; f32 pixelsize_y = 1 / (f32) resolution_y; for (int i = 0; i < resolution_x; ++i) { f32 pixelpos_x = i * pixelsize_x - 0.5; f32 x0 = pixelpos_x; f32 x1 = pixelpos_x + pixelsize_x; f32 tex0 = (i + 0.1) * pixelsize_x; f32 tex1 = (i + 0.9) * pixelsize_x; video::S3DVertex vertices[8] = { // x- video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1), video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1), video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0), video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0), // x+ video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1), video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0), video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0), video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } for (int i = 0; i < resolution_y; ++i) { f32 pixelpos_y = i * pixelsize_y - 0.5; f32 y0 = -pixelpos_y - pixelsize_y; f32 y1 = -pixelpos_y; f32 tex0 = (i + 0.1) * pixelsize_y; f32 tex1 = (i + 0.9) * pixelsize_y; video::S3DVertex vertices[8] = { // y- video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0), video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0), video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1), video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1), // y+ video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0), video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1), video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1), video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } // Create mesh object scene::SMesh *mesh = new scene::SMesh(); mesh->addMeshBuffer(buf); buf->drop(); scaleMesh(mesh, scale); // also recalculates bounding box return mesh; }
MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): clearHardwareBuffer(false), step(data->step), timestamp(data->timestamp), m_mesh(new scene::SMesh()), m_gamedef(data->m_gamedef), m_animation_force_timer(0), // force initial animation m_last_crack(-1), m_crack_materials(), m_highlighted_materials(), m_last_daynight_ratio((u32) -1), m_daynight_diffs(), m_usage_timer(0) { m_enable_shaders = g_settings->getBool("enable_shaders"); m_enable_highlighting = g_settings->getBool("enable_node_highlighting"); // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated) // 24-155ms for MAP_BLOCKSIZE=32 (NOTE: probably outdated) //TimeTaker timer1("MapBlockMesh()"); data->fill_data(); std::vector<FastFace> fastfaces_new; /* We are including the faces of the trailing edges of the block. This means that when something changes, the caller must also update the meshes of the blocks at the leading edges. NOTE: This is the slowest part of this method. */ { // 4-23ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated) //TimeTaker timer2("updateAllFastFaceRows()"); updateAllFastFaceRows(data, fastfaces_new, step); } // End of slow part //if (data->debug) infostream<<" step="<<step<<" fastfaces_new.size="<<fastfaces_new.size()<<std::endl; /* Convert FastFaces to MeshCollector */ MeshCollector collector; { // avg 0ms (100ms spikes when loading textures the first time) // (NOTE: probably outdated) //TimeTaker timer2("MeshCollector building"); for(u32 i=0; i<fastfaces_new.size(); i++) { FastFace &f = fastfaces_new[i]; const u16 indices[] = {0,1,2,2,3,0}; const u16 indices_alternate[] = {0,1,3,2,3,1}; if(f.tile.texture == NULL) continue; const u16 *indices_p = indices; /* Revert triangles for nicer looking gradient if vertices 1 and 3 have same color or 0 and 2 have different color. getRed() is the day color. */ if(f.vertices[0].Color.getRed() != f.vertices[2].Color.getRed() || f.vertices[1].Color.getRed() == f.vertices[3].Color.getRed()) indices_p = indices_alternate; collector.append(f.tile, f.vertices, 4, indices_p, 6); } } /* Add special graphics: - torches - flowing water - fences - whatever */ if(step <= 1) mapblock_mesh_generate_special(data, collector); m_highlight_mesh_color = data->m_highlight_mesh_color; /* Convert MeshCollector to SMesh */ ITextureSource *tsrc = m_gamedef->tsrc(); IShaderSource *shdrsrc = m_gamedef->getShaderSource(); for(u32 i = 0; i < collector.prebuffers.size(); i++) { PreMeshBuffer &p = collector.prebuffers[i]; if (step <= data->draw_control.farmesh || !data->draw_control.farmesh) { // Generate animation data // - Cracks if(p.tile.material_flags & MATERIAL_FLAG_CRACK) { // Find the texture name plus ^[crack:N: std::ostringstream os(std::ios::binary); os<<tsrc->getTextureName(p.tile.texture_id)<<"^[crack"; if(p.tile.material_flags & MATERIAL_FLAG_CRACK_OVERLAY) os<<"o"; // use ^[cracko os<<":"<<(u32)p.tile.animation_frame_count<<":"; m_crack_materials.insert(std::make_pair(i, os.str())); // Replace tile texture with the cracked one p.tile.texture = tsrc->getTexture( os.str()+"0", &p.tile.texture_id); } } // - Texture animation if(p.tile.material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) { // Add to MapBlockMesh in order to animate these tiles m_animation_tiles[i] = p.tile; m_animation_frames[i] = 0; if(g_settings->getBool("desynchronize_mapblock_texture_animation")){ // Get starting position from noise m_animation_frame_offsets[i] = 100000 * (2.0 + noise3d( data->m_blockpos.X, data->m_blockpos.Y, data->m_blockpos.Z, 0)); } else { // Play all synchronized m_animation_frame_offsets[i] = 0; } // Replace tile texture with the first animation frame FrameSpec animation_frame = p.tile.frames.find(0)->second; p.tile.texture = animation_frame.texture; } if(m_enable_highlighting && p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED) m_highlighted_materials.push_back(i); for(u32 j = 0; j < p.vertices.size(); j++) { // Note applyFacesShading second parameter is precalculated sqrt // value for speed improvement // Skip it for lightsources and top faces. video::SColor &vc = p.vertices[j].Color; if (!vc.getBlue()) { if (p.vertices[j].Normal.Y < -0.5) { applyFacesShading (vc, 0.447213); } else if (p.vertices[j].Normal.X > 0.5) { applyFacesShading (vc, 0.670820); } else if (p.vertices[j].Normal.X < -0.5) { applyFacesShading (vc, 0.670820); } else if (p.vertices[j].Normal.Z > 0.5) { applyFacesShading (vc, 0.836660); } else if (p.vertices[j].Normal.Z < -0.5) { applyFacesShading (vc, 0.836660); } } if(!m_enable_shaders) { // - Classic lighting (shaders handle this by themselves) // Set initial real color and store for later updates u8 day = vc.getRed(); u8 night = vc.getGreen(); finalColorBlend(vc, day, night, 1000); if(day != night) m_daynight_diffs[i][j] = std::make_pair(day, night); } } // Create material video::SMaterial material; material.setFlag(video::EMF_LIGHTING, false); material.setFlag(video::EMF_BACK_FACE_CULLING, true); material.setFlag(video::EMF_BILINEAR_FILTER, false); material.setFlag(video::EMF_FOG_ENABLE, true); //material.setFlag(video::EMF_WIREFRAME, true); material.setTexture(0, p.tile.texture); if (p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED) { material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR; } else { if (m_enable_shaders) { material.MaterialType = shdrsrc->getShaderInfo(p.tile.shader_id).material; p.tile.applyMaterialOptionsWithShaders(material); if (p.tile.normal_texture) { material.setTexture(1, p.tile.normal_texture); material.setTexture(2, tsrc->getTexture("enable_img.png")); } else { material.setTexture(2, tsrc->getTexture("disable_img.png")); } } else { p.tile.applyMaterialOptions(material); } } // Create meshbuffer // This is a "Standard MeshBuffer", // it's a typedeffed CMeshBuffer<video::S3DVertex> scene::SMeshBuffer *buf = new scene::SMeshBuffer(); // Set material buf->Material = material; // Add to mesh m_mesh->addMeshBuffer(buf); // Mesh grabbed it buf->drop(); buf->append(&p.vertices[0], p.vertices.size(), &p.indices[0], p.indices.size()); } m_camera_offset = camera_offset; /* Do some stuff to the mesh */ v3f t = v3f(0,0,0); if (step>1) { scaleMesh(m_mesh, v3f(step,step,step)); // TODO: remove this wrong numbers, find formula good test: fly above ocean if (step == 2) t = v3f(BS/2, BS/2, BS/2); if (step == 4) t = v3f(BS*1.666, -BS/3.0, BS*1.666); if (step == 8) t = v3f(BS*2.666, -BS*2.4, BS*2.666); if (step == 16) t = v3f(BS*6.4, -BS*6.4, BS*6.4); } translateMesh(m_mesh, intToFloat(data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS) + t); if(m_mesh) { #if 0 // Usually 1-700 faces and 1-7 materials infostream<<"Updated MapBlock mesh p="<<data->m_blockpos<<" has "<<fastfaces_new.size()<<" faces " <<"and uses "<<m_mesh->getMeshBufferCount() <<" materials "<<" step="<<step<<" range="<<data->range<< " mesh="<<m_mesh<<std::endl; #endif } //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl; // Check if animation is required for this mesh m_has_animation = !m_crack_materials.empty() || !m_daynight_diffs.empty() || !m_animation_tiles.empty() || !m_highlighted_materials.empty(); }
void CNodeDefManager::updateTextures(IGameDef *gamedef, void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress), void *progress_callback_args) { #ifndef SERVER infostream << "CNodeDefManager::updateTextures(): Updating " "textures in node definitions" << std::endl; ITextureSource *tsrc = gamedef->tsrc(); IShaderSource *shdsrc = gamedef->getShaderSource(); scene::ISceneManager* smgr = gamedef->getSceneManager(); scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator(); bool new_style_water = g_settings->getBool("new_style_water"); bool new_style_leaves = g_settings->getBool("new_style_leaves"); bool connected_glass = g_settings->getBool("connected_glass"); bool opaque_water = g_settings->getBool("opaque_water"); bool enable_shaders = g_settings->getBool("enable_shaders"); bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping"); bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion"); bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache"); bool use_normal_texture = enable_shaders && (enable_bumpmapping || enable_parallax_occlusion); u32 size = m_content_features.size(); for (u32 i = 0; i < size; i++) { ContentFeatures *f = &m_content_features[i]; // Figure out the actual tiles to use TileDef tiledef[6]; for (u32 j = 0; j < 6; j++) { tiledef[j] = f->tiledef[j]; if (tiledef[j].name == "") tiledef[j].name = "unknown_node.png"; } bool is_liquid = false; bool is_water_surface = false; u8 material_type = (f->alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; switch (f->drawtype) { default: case NDT_NORMAL: f->solidness = 2; break; case NDT_AIRLIKE: f->solidness = 0; break; case NDT_LIQUID: assert(f->liquid_type == LIQUID_SOURCE); if (opaque_water) f->alpha = 255; if (new_style_water){ f->solidness = 0; } else { f->solidness = 1; f->backface_culling = false; } is_liquid = true; break; case NDT_FLOWINGLIQUID: assert(f->liquid_type == LIQUID_FLOWING); f->solidness = 0; if (opaque_water) f->alpha = 255; is_liquid = true; break; case NDT_GLASSLIKE: f->solidness = 0; f->visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED: f->solidness = 0; f->visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED_OPTIONAL: f->solidness = 0; f->visual_solidness = 1; f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE; break; case NDT_ALLFACES: f->solidness = 0; f->visual_solidness = 1; break; case NDT_ALLFACES_OPTIONAL: if (new_style_leaves) { f->drawtype = NDT_ALLFACES; f->solidness = 0; f->visual_solidness = 1; } else { f->drawtype = NDT_NORMAL; f->solidness = 2; for (u32 i = 0; i < 6; i++) tiledef[i].name += std::string("^[noalpha"); } if (f->waving == 1) material_type = TILE_MATERIAL_WAVING_LEAVES; break; case NDT_PLANTLIKE: f->solidness = 0; f->backface_culling = false; if (f->waving == 1) material_type = TILE_MATERIAL_WAVING_PLANTS; break; case NDT_FIRELIKE: f->backface_culling = false; f->solidness = 0; break; case NDT_MESH: f->solidness = 0; f->backface_culling = false; break; case NDT_TORCHLIKE: case NDT_SIGNLIKE: case NDT_FENCELIKE: case NDT_RAILLIKE: case NDT_NODEBOX: f->solidness = 0; break; } if (is_liquid) { material_type = (f->alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT; if (f->name == "default:water_source") is_water_surface = true; } u32 tile_shader[6]; for (u16 j = 0; j < 6; j++) { tile_shader[j] = shdsrc->getShader("nodes_shader", material_type, f->drawtype); } if (is_water_surface) { tile_shader[0] = shdsrc->getShader("water_surface_shader", material_type, f->drawtype); } // Tiles (fill in f->tiles[]) for (u16 j = 0; j < 6; j++) { fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j], use_normal_texture, f->backface_culling, f->alpha, material_type); } // Special tiles (fill in f->special_tiles[]) for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) { fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j], tile_shader[j], use_normal_texture, f->tiledef_special[j].backface_culling, f->alpha, material_type); } if ((f->drawtype == NDT_MESH) && (f->mesh != "")) { // Meshnode drawtype // Read the mesh and apply scale f->mesh_ptr[0] = gamedef->getMesh(f->mesh); if (f->mesh_ptr[0]){ v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale; scaleMesh(f->mesh_ptr[0], scale); recalculateBoundingBox(f->mesh_ptr[0]); meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); } } else if ((f->drawtype == NDT_NODEBOX) && ((f->node_box.type == NODEBOX_REGULAR) || (f->node_box.type == NODEBOX_FIXED)) && (!f->node_box.fixed.empty())) { //Convert regular nodebox nodes to meshnodes //Change the drawtype and apply scale f->drawtype = NDT_MESH; f->mesh_ptr[0] = convertNodeboxNodeToMesh(f); v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale; scaleMesh(f->mesh_ptr[0], scale); recalculateBoundingBox(f->mesh_ptr[0]); meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); } //Cache 6dfacedir and wallmounted rotated clones of meshes if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) { for (u16 j = 1; j < 24; j++) { f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]); rotateMeshBy6dFacedir(f->mesh_ptr[j], j); recalculateBoundingBox(f->mesh_ptr[j]); meshmanip->recalculateNormals(f->mesh_ptr[j], true, false); } } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) { static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2}; for (u16 j = 1; j < 6; j++) { f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]); rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]); recalculateBoundingBox(f->mesh_ptr[j]); meshmanip->recalculateNormals(f->mesh_ptr[j], true, false); } rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]); recalculateBoundingBox(f->mesh_ptr[0]); meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); } progress_callback(progress_callback_args, i, size); } #endif }
static scene::IMesh* createExtrusionMesh(int resolution_x, int resolution_y) { const f32 r = 0.5; scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); v3f scale(1.0, 1.0, 0.1); // Front and back { video::S3DVertex vertices[8] = { // z- video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0), video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0), video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1), video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1), // z+ video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0), video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1), video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1), video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } f32 pixelsize_x = 1 / (f32) resolution_x; f32 pixelsize_y = 1 / (f32) resolution_y; for (int i = 0; i < resolution_x; ++i) { f32 pixelpos_x = i * pixelsize_x - 0.5; f32 x0 = pixelpos_x; f32 x1 = pixelpos_x + pixelsize_x; f32 tex0 = (i + 0.1) * pixelsize_x; f32 tex1 = (i + 0.9) * pixelsize_x; video::S3DVertex vertices[8] = { // x- video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1), video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1), video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0), video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0), // x+ video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1), video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0), video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0), video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } for (int i = 0; i < resolution_y; ++i) { f32 pixelpos_y = i * pixelsize_y - 0.5; f32 y0 = -pixelpos_y - pixelsize_y; f32 y1 = -pixelpos_y; f32 tex0 = (i + 0.1) * pixelsize_y; f32 tex1 = (i + 0.9) * pixelsize_y; video::S3DVertex vertices[8] = { // y- video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0), video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0), video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1), video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1), // y+ video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0), video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1), video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1), video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } // Define default material video::SMaterial *material = &buf->getMaterial(); material->MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; material->BackfaceCulling = true; material->setFlag(video::EMF_LIGHTING, false); material->setFlag(video::EMF_BILINEAR_FILTER, false); material->setFlag(video::EMF_TRILINEAR_FILTER, false); // anisotropic filtering removes "thin black line" artifacts material->setFlag(video::EMF_ANISOTROPIC_FILTER, true); material->setFlag(video::EMF_TEXTURE_WRAP, false); // Create mesh object scene::SMesh *mesh = new scene::SMesh(); mesh->addMeshBuffer(buf); buf->drop(); scaleMesh(mesh, scale); // also recalculates bounding box return mesh; }
ClientCached* createClientCachedDirect(const std::string &name, IGameDef *gamedef) const { infostream<<"Lazily creating item texture and mesh for \"" <<name<<"\""<<std::endl; // This is not thread-safe sanity_check(get_current_thread_id() == m_main_thread); // Skip if already in cache ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; ITextureSource *tsrc = gamedef->getTextureSource(); INodeDefManager *nodedef = gamedef->getNodeDefManager(); const ItemDefinition &def = get(name); // Create new ClientCached cc = new ClientCached(); // Create an inventory texture cc->inventory_texture = NULL; if(def.inventory_image != "") cc->inventory_texture = tsrc->getTexture(def.inventory_image); // Additional processing for nodes: // - Create a wield mesh if WieldMeshSceneNode can't render // the node on its own. // - If inventory_texture isn't set yet, create one using // render-to-texture. if (def.type == ITEM_NODE) { // Get node properties content_t id = nodedef->getId(name); const ContentFeatures &f = nodedef->get(id); bool need_rtt_mesh = cc->inventory_texture == NULL; // Keep this in sync with WieldMeshSceneNode::setItem() bool need_wield_mesh = !(f.mesh_ptr[0] || f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES || f.drawtype == NDT_AIRLIKE); scene::IMesh *node_mesh = NULL; if (need_rtt_mesh || need_wield_mesh) { u8 param1 = 0; if (f.param_type == CPT_LIGHT) param1 = 0xee; /* Make a mesh from the node */ MeshMakeData mesh_make_data(gamedef, false); u8 param2 = 0; if (f.param_type_2 == CPT2_WALLMOUNTED) param2 = 1; MapNode mesh_make_node(id, param1, param2); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0)); node_mesh = mapblock_mesh.getMesh(); node_mesh->grab(); video::SColor c(255, 255, 255, 255); setMeshColor(node_mesh, c); // scale and translate the mesh so it's a // unit cube centered on the origin scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS)); translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0)); } /* Draw node mesh into a render target texture */ if (need_rtt_mesh) { TextureFromMeshParams params; params.mesh = node_mesh; params.dim.set(64, 64); params.rtt_texture_name = "INVENTORY_" + def.name + "_RTT"; params.delete_texture_on_shutdown = true; params.camera_position.set(0, 1.0, -1.5); params.camera_position.rotateXZBy(45); params.camera_lookat.set(0, 0, 0); // Set orthogonal projection params.camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); params.ambient_light.set(1.0, 0.2, 0.2, 0.2); params.light_position.set(10, 100, -50); params.light_color.set(1.0, 0.5, 0.5, 0.5); params.light_radius = 1000; #ifdef __ANDROID__ params.camera_position.set(0, -1.0, -1.5); params.camera_position.rotateXZBy(45); params.light_position.set(10, -100, -50); #endif cc->inventory_texture = tsrc->generateTextureFromMesh(params); // render-to-target didn't work if (cc->inventory_texture == NULL) { cc->inventory_texture = tsrc->getTexture(f.tiledef[0].name); } } /* Use the node mesh as the wield mesh */ if (need_wield_mesh) { cc->wield_mesh = node_mesh; cc->wield_mesh->grab(); // no way reference count can be smaller than 2 in this place! assert(cc->wield_mesh->getReferenceCount() >= 2); } if (node_mesh) node_mesh->drop(); } // Put in cache m_clientcached.set(name, cc); return cc; }
virtual void updateTexturesAndMeshes(IGameDef *gamedef) { #ifndef SERVER infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating " <<"textures and meshes in item definitions"<<std::endl; ITextureSource *tsrc = gamedef->getTextureSource(); INodeDefManager *nodedef = gamedef->getNodeDefManager(); IrrlichtDevice *device = tsrc->getDevice(); video::IVideoDriver *driver = device->getVideoDriver(); for(std::map<std::string, ItemDefinition*>::iterator i = m_item_definitions.begin(); i != m_item_definitions.end(); i++) { ItemDefinition *def = i->second; bool need_node_mesh = false; // Create an inventory texture def->inventory_texture = NULL; if(def->inventory_image != "") { def->inventory_texture = tsrc->getTextureRaw(def->inventory_image); } else if(def->type == ITEM_NODE) { need_node_mesh = true; } // Create a wield mesh if(def->wield_mesh != NULL) { def->wield_mesh->drop(); def->wield_mesh = NULL; } if(def->type == ITEM_NODE && def->wield_image == "") { need_node_mesh = true; } else if(def->wield_image != "" || def->inventory_image != "") { // Extrude the wield image into a mesh std::string imagename; if(def->wield_image != "") imagename = def->wield_image; else imagename = def->inventory_image; def->wield_mesh = createExtrudedMesh( tsrc->getTextureRaw(imagename), driver, def->wield_scale * v3f(40.0, 40.0, 4.0)); if(def->wield_mesh == NULL) { infostream<<"ItemDefManager: WARNING: " <<"updateTexturesAndMeshes(): " <<"Unable to create extruded mesh for item " <<def->name<<std::endl; } } if(need_node_mesh) { /* Get node properties */ content_t id = nodedef->getId(def->name); const ContentFeatures &f = nodedef->get(id); u8 param1 = 0; if(f.param_type == CPT_LIGHT) param1 = 0xee; /* Make a mesh from the node */ MeshMakeData mesh_make_data(gamedef); MapNode mesh_make_node(id, param1, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data); scene::IMesh *node_mesh = mapblock_mesh.getMesh(); assert(node_mesh); setMeshColor(node_mesh, video::SColor(255, 255, 255, 255)); /* Scale and translate the mesh so it's a unit cube centered on the origin */ scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS)); translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0)); /* Draw node mesh into a render target texture */ if(def->inventory_texture == NULL) { core::dimension2d<u32> dim(64,64); std::string rtt_texture_name = "INVENTORY_" + def->name + "_RTT"; v3f camera_position(0, 1.0, -1.5); camera_position.rotateXZBy(45); v3f camera_lookat(0, 0, 0); core::CMatrix4<f32> camera_projection_matrix; // Set orthogonal projection camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); video::SColorf ambient_light(0.2,0.2,0.2); v3f light_position(10, 100, -50); video::SColorf light_color(0.5,0.5,0.5); f32 light_radius = 1000; def->inventory_texture = generateTextureFromMesh( node_mesh, device, dim, rtt_texture_name, camera_position, camera_lookat, camera_projection_matrix, ambient_light, light_position, light_color, light_radius); // render-to-target didn't work if(def->inventory_texture == NULL) { def->inventory_texture = tsrc->getTextureRaw(f.tname_tiles[0]); } } /* Use the node mesh as the wield mesh */ if(def->wield_mesh == NULL) { // Scale to proper wield mesh proportions scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0) * def->wield_scale); def->wield_mesh = node_mesh; def->wield_mesh->grab(); } // falling outside of here deletes node_mesh } } #endif }
int MSHMET_mshmet(int intopt[7], double fopt[4], pMesh mesh, pSol sol){ Info *info; fprintf(stdout," -- MSHMET, Release %s (%s) \n",MS_VER,MS_REL); fprintf(stdout," %s\n",MS_CPY); fprintf(stdout," %s\n",COMPIL); /* trap exceptions */ signal(SIGABRT,mshmet_excfun); signal(SIGFPE,mshmet_excfun); signal(SIGILL,mshmet_excfun); signal(SIGSEGV,mshmet_excfun); signal(SIGTERM,mshmet_excfun); signal(SIGINT,mshmet_excfun); //atexit(mshmet_endcod); tminit(mshmet_ctim,TIMEMAX); chrono(ON,&mshmet_ctim[0]); chrono(ON,&mshmet_ctim[1]); /* default */ info = &mesh->info; info->hmin = (float) fopt[0]; // 0.01; info->hmax = (float) fopt[1]; // 1.0; info->eps = (float) fopt[2]; // 0.01; info->width = (float) fopt[3]; // 0.05; info->nnu = intopt[0]; // 0; info->iso = intopt[1]; // 0; info->ls = intopt[2]; // 0; info->ddebug = intopt[3]; // 0; info->imprim = intopt[4]; // 10; info->nlis = intopt[5]; // 0; info->bin = 1; // pas besoin c'est pour le fichier info->nsol = -1; //-1; // pas besoin ==> on peut prendre plusieurs solutions en meme temps ??? info->metric = intopt[6]; // 0; // metric given besoin ??? MSHMET_setfunc(mesh); chrono(OFF,&mshmet_ctim[1]); if ( mesh->info.imprim ) mshmet_stats(mesh,sol); fprintf(stdout," -- DATA READING COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[1])); fprintf(stdout,"\n %s\n MODULE MSHMET-LJLL : %s (%s)\n %s\n", MS_STR,MS_VER,MS_REL,MS_STR); /* analysis */ chrono(ON,&mshmet_ctim[2]); if ( mesh->info.imprim ) fprintf(stdout,"\n -- PHASE 1 : ANALYSIS\n"); if ( abs(mesh->info.imprim) > 4 ) { fprintf(stdout," ** SETTING ADJACENCIES\n"); fflush(stdout); } if ( !scaleMesh(mesh,sol) ) return(1); if ( !hashel(mesh) ) exit(1); chrono(OFF,&mshmet_ctim[2]); if ( mesh->info.imprim ) fprintf(stdout," -- PHASE 1 COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[2])); /* metric */ chrono(ON,&mshmet_ctim[3]); if ( mesh->info.imprim ) fprintf(stdout,"\n -- PHASE 2 : METRIC\n"); if ( !mshme1(mesh,sol) ) exit(1); chrono(OFF,&mshmet_ctim[3]); if ( mesh->info.imprim ) fprintf(stdout," -- PHASE 2 COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[3])); fprintf(stdout,"\n %s\n END OF MODULE MSHMET \n %s\n",MS_STR,MS_STR); /* sol->outn="zzzz"; if ( !saveMet(sol,&mesh->info,sol->outn) ) exit(1); */ if ( mesh->info.imprim ) mshmet_endcod(); fprintf(stdout,"\n %s\n END OF MODULE MSHMET \n %s\n",MS_STR,MS_STR); if ( mesh->info.imprim < -4 || mesh->info.ddebug ) M_memDump(); return(0); }
void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc, scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings) { // minimap pixel color - the average color of a texture if (tsettings.enable_minimap && !tiledef[0].name.empty()) minimap_color = tsrc->getTextureAverageColor(tiledef[0].name); // Figure out the actual tiles to use TileDef tdef[6]; for (u32 j = 0; j < 6; j++) { tdef[j] = tiledef[j]; if (tdef[j].name.empty()) tdef[j].name = "unknown_node.png"; } // also the overlay tiles TileDef tdef_overlay[6]; for (u32 j = 0; j < 6; j++) tdef_overlay[j] = tiledef_overlay[j]; // also the special tiles TileDef tdef_spec[6]; for (u32 j = 0; j < CF_SPECIAL_COUNT; j++) tdef_spec[j] = tiledef_special[j]; bool is_liquid = false; u8 material_type = (alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; switch (drawtype) { default: case NDT_NORMAL: material_type = (alpha == 255) ? TILE_MATERIAL_OPAQUE : TILE_MATERIAL_ALPHA; solidness = 2; break; case NDT_AIRLIKE: solidness = 0; break; case NDT_LIQUID: assert(liquid_type == LIQUID_SOURCE); if (tsettings.opaque_water) alpha = 255; solidness = 1; is_liquid = true; break; case NDT_FLOWINGLIQUID: assert(liquid_type == LIQUID_FLOWING); solidness = 0; if (tsettings.opaque_water) alpha = 255; is_liquid = true; break; case NDT_GLASSLIKE: solidness = 0; visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED: solidness = 0; visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED_OPTIONAL: solidness = 0; visual_solidness = 1; drawtype = tsettings.connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE; break; case NDT_ALLFACES: solidness = 0; visual_solidness = 1; break; case NDT_ALLFACES_OPTIONAL: if (tsettings.leaves_style == LEAVES_FANCY) { drawtype = NDT_ALLFACES; solidness = 0; visual_solidness = 1; } else if (tsettings.leaves_style == LEAVES_SIMPLE) { for (u32 j = 0; j < 6; j++) { if (!tdef_spec[j].name.empty()) tdef[j].name = tdef_spec[j].name; } drawtype = NDT_GLASSLIKE; solidness = 0; visual_solidness = 1; } else { drawtype = NDT_NORMAL; solidness = 2; for (TileDef &td : tdef) td.name += std::string("^[noalpha"); } if (waving >= 1) material_type = TILE_MATERIAL_WAVING_LEAVES; break; case NDT_PLANTLIKE: solidness = 0; if (waving >= 1) material_type = TILE_MATERIAL_WAVING_PLANTS; break; case NDT_FIRELIKE: solidness = 0; break; case NDT_MESH: case NDT_NODEBOX: solidness = 0; if (waving == 1) material_type = TILE_MATERIAL_WAVING_PLANTS; else if (waving == 2) material_type = TILE_MATERIAL_WAVING_LEAVES; break; case NDT_TORCHLIKE: case NDT_SIGNLIKE: case NDT_FENCELIKE: case NDT_RAILLIKE: solidness = 0; break; case NDT_PLANTLIKE_ROOTED: solidness = 2; break; } if (is_liquid) { // Vertex alpha is no longer supported, correct if necessary. correctAlpha(tdef, 6); correctAlpha(tdef_overlay, 6); correctAlpha(tdef_spec, CF_SPECIAL_COUNT); material_type = (alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT; } u32 tile_shader = shdsrc->getShader("nodes_shader", material_type, drawtype); u8 overlay_material = material_type; if (overlay_material == TILE_MATERIAL_OPAQUE) overlay_material = TILE_MATERIAL_BASIC; else if (overlay_material == TILE_MATERIAL_LIQUID_OPAQUE) overlay_material = TILE_MATERIAL_LIQUID_TRANSPARENT; u32 overlay_shader = shdsrc->getShader("nodes_shader", overlay_material, drawtype); // Tiles (fill in f->tiles[]) for (u16 j = 0; j < 6; j++) { tiles[j].world_aligned = isWorldAligned(tdef[j].align_style, tsettings.world_aligned_mode, drawtype); fillTileAttribs(tsrc, &tiles[j].layers[0], tiles[j], tdef[j], color, material_type, tile_shader, tdef[j].backface_culling, tsettings); if (!tdef_overlay[j].name.empty()) fillTileAttribs(tsrc, &tiles[j].layers[1], tiles[j], tdef_overlay[j], color, overlay_material, overlay_shader, tdef[j].backface_culling, tsettings); } u8 special_material = material_type; if (drawtype == NDT_PLANTLIKE_ROOTED) { if (waving == 1) special_material = TILE_MATERIAL_WAVING_PLANTS; else if (waving == 2) special_material = TILE_MATERIAL_WAVING_LEAVES; } u32 special_shader = shdsrc->getShader("nodes_shader", special_material, drawtype); // Special tiles (fill in f->special_tiles[]) for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) fillTileAttribs(tsrc, &special_tiles[j].layers[0], special_tiles[j], tdef_spec[j], color, special_material, special_shader, tdef_spec[j].backface_culling, tsettings); if (param_type_2 == CPT2_COLOR || param_type_2 == CPT2_COLORED_FACEDIR || param_type_2 == CPT2_COLORED_WALLMOUNTED) palette = tsrc->getPalette(palette_name); if (drawtype == NDT_MESH && !mesh.empty()) { // Meshnode drawtype // Read the mesh and apply scale mesh_ptr[0] = client->getMesh(mesh); if (mesh_ptr[0]){ v3f scale = v3f(1.0, 1.0, 1.0) * BS * visual_scale; scaleMesh(mesh_ptr[0], scale); recalculateBoundingBox(mesh_ptr[0]); meshmanip->recalculateNormals(mesh_ptr[0], true, false); } } //Cache 6dfacedir and wallmounted rotated clones of meshes if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_FACEDIR || param_type_2 == CPT2_COLORED_FACEDIR)) { for (u16 j = 1; j < 24; j++) { mesh_ptr[j] = cloneMesh(mesh_ptr[0]); rotateMeshBy6dFacedir(mesh_ptr[j], j); recalculateBoundingBox(mesh_ptr[j]); meshmanip->recalculateNormals(mesh_ptr[j], true, false); } } else if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_WALLMOUNTED || param_type_2 == CPT2_COLORED_WALLMOUNTED)) { static const u8 wm_to_6d[6] = { 20, 0, 16 + 1, 12 + 3, 8, 4 + 2 }; for (u16 j = 1; j < 6; j++) { mesh_ptr[j] = cloneMesh(mesh_ptr[0]); rotateMeshBy6dFacedir(mesh_ptr[j], wm_to_6d[j]); recalculateBoundingBox(mesh_ptr[j]); meshmanip->recalculateNormals(mesh_ptr[j], true, false); } rotateMeshBy6dFacedir(mesh_ptr[0], wm_to_6d[0]); recalculateBoundingBox(mesh_ptr[0]); meshmanip->recalculateNormals(mesh_ptr[0], true, false); } }
void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc, scene::ISceneManager *smgr, scene::IMeshManipulator *meshmanip, IGameDef *gamedef, const TextureSettings &tsettings) { // minimap pixel color - the average color of a texture if (tsettings.enable_minimap && tiledef[0].name != "") minimap_color = tsrc->getTextureAverageColor(tiledef[0].name); // Figure out the actual tiles to use TileDef tdef[6]; for (u32 j = 0; j < 6; j++) { tdef[j] = tiledef[j]; if (tdef[j].name == "") tdef[j].name = "unknown_node.png"; } bool is_liquid = false; bool is_water_surface = false; u8 material_type = (alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; switch (drawtype) { default: case NDT_NORMAL: solidness = 2; break; case NDT_AIRLIKE: solidness = 0; break; case NDT_LIQUID: assert(liquid_type == LIQUID_SOURCE); if (tsettings.opaque_water) alpha = 255; solidness = 1; is_liquid = true; break; case NDT_FLOWINGLIQUID: assert(liquid_type == LIQUID_FLOWING); solidness = 0; if (tsettings.opaque_water) alpha = 255; is_liquid = true; break; case NDT_GLASSLIKE: solidness = 0; visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED: solidness = 0; visual_solidness = 1; break; case NDT_GLASSLIKE_FRAMED_OPTIONAL: solidness = 0; visual_solidness = 1; drawtype = tsettings.connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE; break; case NDT_ALLFACES: solidness = 0; visual_solidness = 1; break; case NDT_ALLFACES_OPTIONAL: if (tsettings.leaves_style == LEAVES_FANCY) { drawtype = NDT_ALLFACES; solidness = 0; visual_solidness = 1; } else if (tsettings.leaves_style == LEAVES_SIMPLE) { for (u32 j = 0; j < 6; j++) { if (tiledef_special[j].name != "") tdef[j].name = tiledef_special[j].name; } drawtype = NDT_GLASSLIKE; solidness = 0; visual_solidness = 1; } else { drawtype = NDT_NORMAL; solidness = 2; for (u32 i = 0; i < 6; i++) tdef[i].name += std::string("^[noalpha"); } if (waving == 1) material_type = TILE_MATERIAL_WAVING_LEAVES; break; case NDT_PLANTLIKE: solidness = 0; if (waving == 1) material_type = TILE_MATERIAL_WAVING_PLANTS; break; case NDT_FIRELIKE: solidness = 0; break; case NDT_MESH: solidness = 0; break; case NDT_TORCHLIKE: case NDT_SIGNLIKE: case NDT_FENCELIKE: case NDT_RAILLIKE: case NDT_NODEBOX: solidness = 0; break; } if (is_liquid) { material_type = (alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT; if (name == "default:water_source") is_water_surface = true; } u32 tile_shader[6]; for (u16 j = 0; j < 6; j++) { tile_shader[j] = shdsrc->getShader("nodes_shader", material_type, drawtype); } if (is_water_surface) { tile_shader[0] = shdsrc->getShader("water_surface_shader", material_type, drawtype); } // Tiles (fill in f->tiles[]) for (u16 j = 0; j < 6; j++) { fillTileAttribs(tsrc, &tiles[j], &tdef[j], tile_shader[j], tsettings.use_normal_texture, tiledef[j].backface_culling, alpha, material_type); } // Special tiles (fill in f->special_tiles[]) for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) { fillTileAttribs(tsrc, &special_tiles[j], &tiledef_special[j], tile_shader[j], tsettings.use_normal_texture, tiledef_special[j].backface_culling, alpha, material_type); } if ((drawtype == NDT_MESH) && (mesh != "")) { // Meshnode drawtype // Read the mesh and apply scale mesh_ptr[0] = gamedef->getMesh(mesh); if (mesh_ptr[0]){ v3f scale = v3f(1.0, 1.0, 1.0) * BS * visual_scale; scaleMesh(mesh_ptr[0], scale); recalculateBoundingBox(mesh_ptr[0]); meshmanip->recalculateNormals(mesh_ptr[0], true, false); } } else if ((drawtype == NDT_NODEBOX) && ((node_box.type == NODEBOX_REGULAR) || (node_box.type == NODEBOX_FIXED)) && (!node_box.fixed.empty())) { //Convert regular nodebox nodes to meshnodes //Change the drawtype and apply scale drawtype = NDT_MESH; mesh_ptr[0] = convertNodeboxesToMesh(node_box.fixed); v3f scale = v3f(1.0, 1.0, 1.0) * visual_scale; scaleMesh(mesh_ptr[0], scale); recalculateBoundingBox(mesh_ptr[0]); meshmanip->recalculateNormals(mesh_ptr[0], true, false); } //Cache 6dfacedir and wallmounted rotated clones of meshes if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_FACEDIR)) { for (u16 j = 1; j < 24; j++) { mesh_ptr[j] = cloneMesh(mesh_ptr[0]); rotateMeshBy6dFacedir(mesh_ptr[j], j); recalculateBoundingBox(mesh_ptr[j]); meshmanip->recalculateNormals(mesh_ptr[j], true, false); } } else if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_WALLMOUNTED)) { static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2}; for (u16 j = 1; j < 6; j++) { mesh_ptr[j] = cloneMesh(mesh_ptr[0]); rotateMeshBy6dFacedir(mesh_ptr[j], wm_to_6d[j]); recalculateBoundingBox(mesh_ptr[j]); meshmanip->recalculateNormals(mesh_ptr[j], true, false); } rotateMeshBy6dFacedir(mesh_ptr[0], wm_to_6d[0]); recalculateBoundingBox(mesh_ptr[0]); meshmanip->recalculateNormals(mesh_ptr[0], true, false); } }
scene::IMesh *getItemMesh(Client *client, const ItemStack &item) { 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::IMesh *mesh; // If inventory_image is defined, it overrides everything else if (def.inventory_image != "") { mesh = getExtrudedMesh(tsrc, def.inventory_image); return mesh; } 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)); setMeshColor(mesh, video::SColor (255, 255, 255, 255)); } else if (f.drawtype == NDT_PLANTLIKE) { mesh = getExtrudedMesh(tsrc, tsrc->getTextureName(f.tiles[0].texture_id)); } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES || f.drawtype == NDT_LIQUID || f.drawtype == NDT_FLOWINGLIQUID) { mesh = cloneMesh(g_extrusion_mesh_cache->createCube()); scaleMesh(mesh, v3f(1.2, 1.2, 1.2)); } else { 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; } } u32 mc = mesh->getMeshBufferCount(); for (u32 i = 0; i < mc; ++i) { const TileSpec *tile = &(f.tiles[i]); scene::IMeshBuffer *buf = mesh->getMeshBuffer(i); colorizeMeshBuffer(buf, &tile->color); 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); if (tile->animation_frame_count > 1) { FrameSpec animation_frame = tile->frames[0]; material.setTexture(0, animation_frame.texture); } else { material.setTexture(0, tile->texture); } } rotateMeshXZby(mesh, -45); rotateMeshYZby(mesh, -30); return mesh; } return NULL; }
ClientCached* createClientCachedDirect(const std::string &name, IGameDef *gamedef) const { infostream<<"Lazily creating item texture and mesh for \"" <<name<<"\""<<std::endl; // This is not thread-safe assert(get_current_thread_id() == m_main_thread); // Skip if already in cache ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; ITextureSource *tsrc = gamedef->getTextureSource(); INodeDefManager *nodedef = gamedef->getNodeDefManager(); IrrlichtDevice *device = tsrc->getDevice(); video::IVideoDriver *driver = device->getVideoDriver(); const ItemDefinition *def = &get(name); // Create new ClientCached cc = new ClientCached(); bool need_node_mesh = false; // Create an inventory texture cc->inventory_texture = NULL; if(def->inventory_image != "") { cc->inventory_texture = tsrc->getTextureRaw(def->inventory_image); } else if(def->type == ITEM_NODE) { need_node_mesh = true; } // Create a wield mesh assert(cc->wield_mesh == NULL); if(def->type == ITEM_NODE && def->wield_image == "") { need_node_mesh = true; } else if(def->wield_image != "" || def->inventory_image != "") { // Extrude the wield image into a mesh std::string imagename; if(def->wield_image != "") imagename = def->wield_image; else imagename = def->inventory_image; cc->wield_mesh = createExtrudedMesh( tsrc->getTextureRaw(imagename), driver, def->wield_scale * v3f(40.0, 40.0, 4.0)); if(cc->wield_mesh == NULL) { infostream<<"ItemDefManager: WARNING: " <<"updateTexturesAndMeshes(): " <<"Unable to create extruded mesh for item " <<def->name<<std::endl; } } if(need_node_mesh) { /* Get node properties */ content_t id = nodedef->getId(def->name); const ContentFeatures &f = nodedef->get(id); u8 param1 = 0; if(f.param_type == CPT_LIGHT) param1 = 0xee; /* Make a mesh from the node */ MeshMakeData mesh_make_data(gamedef); MapNode mesh_make_node(id, param1, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data); scene::IMesh *node_mesh = mapblock_mesh.getMesh(); assert(node_mesh); video::SColor c(255, 255, 255, 255); if(g_settings->getS32("enable_shaders") != 0) c = MapBlock_LightColor(255, 0xffff, decode_light(f.light_source)); setMeshColor(node_mesh, c); /* Scale and translate the mesh so it's a unit cube centered on the origin */ scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS)); translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0)); /* Draw node mesh into a render target texture */ if(cc->inventory_texture == NULL) { core::dimension2d<u32> dim(64,64); std::string rtt_texture_name = "INVENTORY_" + def->name + "_RTT"; v3f camera_position(0, 1.0, -1.5); camera_position.rotateXZBy(45); v3f camera_lookat(0, 0, 0); core::CMatrix4<f32> camera_projection_matrix; // Set orthogonal projection camera_projection_matrix.buildProjectionMatrixOrthoLH( 1.65, 1.65, 0, 100); video::SColorf ambient_light(0.2,0.2,0.2); v3f light_position(10, 100, -50); video::SColorf light_color(0.5,0.5,0.5); f32 light_radius = 1000; cc->inventory_texture = generateTextureFromMesh( node_mesh, device, dim, rtt_texture_name, camera_position, camera_lookat, camera_projection_matrix, ambient_light, light_position, light_color, light_radius); // render-to-target didn't work if(cc->inventory_texture == NULL) { cc->inventory_texture = tsrc->getTextureRaw(f.tiledef[0].name); } } else { if (m_driver == 0) m_driver = driver; m_extruded_textures.push_back(cc->inventory_texture); } /* Use the node mesh as the wield mesh */ // Scale to proper wield mesh proportions scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0) * def->wield_scale); cc->wield_mesh = node_mesh; cc->wield_mesh->grab(); //no way reference count can be smaller than 2 in this place! assert(cc->wield_mesh->getReferenceCount() >= 2); } // Put in cache m_clientcached.set(name, cc); return cc; }