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函数计算坐标即可

相关推荐
gis分享者13 天前
学习threejs,使用OrbitControls相机控制器
threejs·相机·相机控制器·orbitcontrols
不系舟17816 天前
threejs 实现镜面反射,只反射指定物体,背景透明
threejs
gis分享者16 天前
学习threejs,使用RollControls相机控制器
threejs·相机控制器·rollcontrols
gis分享者19 天前
学习threejs,使用FlyControls相机控制器
threejs·相机控制器·flycontrols
gis分享者20 天前
学习threejs,使用TrackballControls相机控制器
threejs·trackball·相机控制器
gis分享者1 个月前
学习threejs,导入assimp & assimp2json格式的模型
threejs·三维模型·assimp·assimp2json
gis分享者1 个月前
学习threejs,导入AWD格式的模型
threejs·awd·three.awdloader
gis分享者1 个月前
学习threejs,导入pdb格式的模型
threejs·pdb模型·three.pdbloader
gis分享者1 个月前
学习threejs,THREE.CircleGeometry 二维平面圆形几何体
threejs·圆形几何体·circlegeometry
gis分享者1 个月前
学习threejs,THREE.RingGeometry 二维平面圆环几何体
threejs·圆环几何体·ringgeometry