// 创建第一个长方体模型
    const geometry1 = new THREE.BoxGeometry(39, 10, 24);
    const material1 = new THREE.MeshBasicMaterial({ color: 0x00ff00, opacity: 0, transparent: true });
    const mesh1 = new THREE.Mesh(geometry1, material1);
    mesh1.position.set(55.5, 0, -38);
    scene.add(mesh1);

    // 创建第一个长方体模型
    const geometry2 = new THREE.BoxGeometry(34, 10, 20);
    const material2 = new THREE.MeshBasicMaterial({ color: 0x00ff00, opacity: 0, transparent: true });
    const mesh2 = new THREE.Mesh(geometry2, material2);
    mesh2.position.set(99, 0, -40);
    scene.add(mesh2);

    // 创建第二个正方体模型
    const geometry = new THREE.BoxGeometry(2, 4, 2);
    const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(79, 0, -22);
    scene.add(mesh);

    // 创建两个 Box3 对象,用于边界盒检测
    const mesh1BoundingBox = new THREE.Box3().setFromObject(mesh1);
    const meshBoundingBox = new THREE.Box3().setFromObject(mesh);
    const mesh2BoundingBox = new THREE.Box3().setFromObject(mesh2);

    const walls = [longWall3, longWall4, shortWall2]; // 目标墙壁
    const walls1 = [longWall1, longWall2, shortWall1]; // 目标墙壁
    // 定义一个函数来检测是否相交
    function checkIntersection() {
        // 更新每个物体的边界盒位置
        mesh1BoundingBox.setFromObject(mesh1);
        meshBoundingBox.setFromObject(mesh);
        mesh2BoundingBox.setFromObject(mesh2);

        // 检测边界盒是否相交
        if (meshBoundingBox.intersectsBox(mesh1BoundingBox)) {
            walls.forEach(wall => {
                wall.material.color.set(0xff0000); // 改为红色
                wall.material.emissive.set(0xff0000); // 改为红色
                wall.material.opacity = 0.8;
            });
        } else if (meshBoundingBox.intersectsBox(mesh2BoundingBox)) {
            walls1.forEach(wall => {
                wall.material.color.set(0xff0000); // 改为红色
                wall.material.emissive.set(0xff0000); // 改为红色
                wall.material.opacity = 0.8;
            });
        }
    }
    // 定义移动路径的三个点
    const points = [
        new THREE.Vector3(79, 0, -22),
        new THREE.Vector3(79, 0, -35),
        new THREE.Vector3(72, 0, -35),
        // new THREE.Vector3(82, 0, -35),
    ];
    // 定义移动到下一个点的逻辑
    let tweenIndex = 0;
    moveToNextPoint = () => {
        if (tweenIndex < points.length) {
            const targetPosition = points[tweenIndex];

            // 创建 Tween 动画
            new TWEEN.Tween(mesh.position)
                .to({ x: targetPosition.x, y: targetPosition.y, z: targetPosition.z }, 3000) // 动画持续时间为3秒
                .easing(TWEEN.Easing.Quadratic.Out) // 缓动函数
                .onUpdate(() => {
                    checkIntersection(); // 在每次更新时检查是否相交
                })
                .onComplete(() => {
                    tweenIndex++; // 增加索引
                    moveToNextPoint(); // 移动到下一个点
                })
                .start();
        }
    }

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐