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分享者5 天前
学习threejs,实现山谷奔跑效果
threejs·着色器·glsl·shadermaterial·unrealbloompass·山谷奔跑·simplex
ct9786 天前
ThreeJs材质、模型加载、核心API
webgl·材质·threejs
a1117767 天前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs
a1117767 天前
3D赛车躲避游戏(html threeJS开源)
前端·游戏·3d·开源·html·threejs
a1117768 天前
水体渲染系统(html开源)
前端·开源·threejs·水体渲染
三年模拟五年烧烤24 天前
easy-threesdk快速一键搭建threejs3d可视化场景
3d·threejs
loriloy24 天前
Three.js 光照教程 - 第四部分:Lighting详解
threejs
loriloy1 个月前
Three.js 材质教程 - 第三部分:Materials详解
threejs
gis分享者1 个月前
学习threejs,打造原生3D高斯溅落实时渲染器
spark·threejs·ply·高斯·splat·溅落·实时渲染器
loriloy1 个月前
Three.js 几何体教程 - 第二部分:Geometry详解
javascript·threejs