threejs

1.场景清空,释放内容

javascript 复制代码
// 假设你已经有一个Three.js的场景对象scene
 
// 函数:清空场景中的所有对象
function clearScene(scene) {
    while(scene.children.length > 0){
        const object = scene.children[0];
        
        if(object.isMesh) {
            // 如果有几何体和材质,也进行清理
            object.geometry.dispose();
            if(object.material.isMaterial) {
                cleanMaterial(object.material);
            } else {
                // 材质组是数组,所以我们需要遍历它
                for(const material of object.material) cleanMaterial(material);
            }
        }
        
        scene.remove(object);  // 从场景中移除对象
    }
}
 
// 辅助函数:清理材质资源
function cleanMaterial(material){
    material.dispose(); // 清理材质
    if(material.map) material.map.dispose(); // 清理贴图
    if(material.lightMap) material.lightMap.dispose();
    if(material.bumpMap) material.bumpMap.dispose();
    if(material.normalMap) material.normalMap.dispose();
    if(material.specularMap) material.specularMap.dispose();
    if(material.envMap) material.envMap.dispose();
    // ... 清理其他你使用的材质资源
}
 
// 使用方法:
clearScene(scene);

2.选中物体

如果放在像若依这种前端框架中,会有菜单栏导致计算坐标的位置发生偏移

使用clientX配合getBoundingClientRect函数计算坐标即可

相关推荐
DragonBallSuper13 天前
在Cesium中使用ThreeJs材质(不是场景融合哦)
webgl·材质·threejs·cesium·可视化渲染
gis分享者22 天前
学习threejs,使用MeshFaceMaterial面材质容器
threejs·facematerial·面材质容器·不同面不同材质·替代方案
gis分享者22 天前
学习threejs,使用MeshLambertMaterial漫反射材质
材质·threejs·漫反射·lambertmaterial
gis分享者1 个月前
学习threejs,Animation、Core、CustomBlendingEquation、Renderer常量汇总
threejs·core·animation·常量·renderer·equation
gis分享者1 个月前
学习threejs,使用LineBasicMaterial基础线材质
threejs·line·basicmaterial·线材质·gosper·高斯帕曲线
gis分享者1 个月前
学习threejs,使用ShaderMaterial自定义着色器材质
材质·threejs·着色器·shadermaterial