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

相关推荐
陶甜也3 天前
ThreeJS曲线动画:打造炫酷3D路径运动
前端·vue·threejs
gis分享者10 天前
学习threejs,打造交互式花卉生成器
交互·threejs·生成·shadermaterial·花卉·planegeometry
患得患失94914 天前
【ThreeJs】【性能优化】从渲染底层到业务逻辑的系统性提速方案
优化·threejs
接着奏乐接着舞。16 天前
3D地球可视化教程 - 第3篇:地球动画与相机控制
前端·vue.js·3d·threejs
gis分享者16 天前
学习threejs,实现粒子化交互文字
threejs·文字·shadermaterial·粒子化·icosahedron
患得患失9491 个月前
【Threejs】【工具类】Raycaster实现 3D 交互(如鼠标拾取、碰撞检测)的核心工具
3d·交互·threejs·raycaster
gis分享者1 个月前
学习threejs,使用自定义GLSL 着色器,实现水面、粒子特效
threejs·着色器·glsl·粒子·shadermaterial·unrealbloompass·水面
陶甜也2 个月前
threeJS 实现开花的效果
前端·vue·blender·threejs
二川bro2 个月前
第25节:VR基础与WebXR API入门
前端·3d·vr·threejs
哈哈地图2 个月前
three.js手机端的4种旋转方式
threejs·手机交互