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分享者2 天前
学习threejs,交互式神经网络可视化
神经网络·可视化·threejs·filmpass·effectcomposer·unrealbloompass·outputpass
qq_589568108 天前
threejs顶点UV坐标、纹理贴图
threejs·uv
WebGIS开发10 天前
Three.js搭建小米SU7三维汽车实战(2)场景搭建
开发语言·javascript·汽车·threejs
qq_5895681010 天前
threejs模型对象、材质
threejs
gis分享者16 天前
学习threejs,使用Physijs物理引擎,使用DOFConstraint自由度约束,模拟小车移动
汽车·threejs·物理·physijs·physi·dofconstraint
gis分享者22 天前
学习threejs,使用Physijs物理引擎,通过控制重力,实现多米诺骨牌效果
threejs·碰撞·物理·physijs·physi·多米诺
gis分享者24 天前
学习threejs,使用Physijs物理引擎,加载各种几何体网格对象
threejs·shape·物理·physijs·physi·几何体
MossGrower1 个月前
65.Three.js案例-使用 MeshNormalMaterial 和 MeshDepthMaterial 创建 3D 图形
javascript·threejs·spheregeometry·torusknotgeome
醉书生ꦿ℘゜এ1 个月前
threejs学习002-场景中添加几何体
javascript·vue.js·学习·threejs