在three.js添加包围框和标签

一、添加包围框

      我添加包围框的作用是为了分割模型,使模型的每个部分看起来更立体。通过一段数据来定义这个包围框,这段数据包括(模型的最大x、y、z坐标和最小x、y、z坐标,以及模型的中心位置)。

add_box(data, scene) {
    for (let i = 0; i < data.maxxyz.length; i++) {
        const maxX = data.maxxyz[i][0], maxY = data.maxxyz[i][1],
            maxZ = data.maxxyz[i][2];
        const minX = data.minxyz[i][0], minY = data.minxyz[i][1],
            minZ = data.minxyz[i][2];
        const location = new THREE.Vector3(data.location[i][0], -data.location[i][2], data.location[i][1]);
        // 计算包围盒的尺寸
        const width = Math.abs(maxX - minX);
        const height = Math.abs(maxY - minY);
        const depth = Math.abs(maxZ - minZ);
        // 创建长方体的几何体
        const boxGeometry = new THREE.BoxGeometry(width, depth, height);
        // 创建包围盒的材质,这里使用线框材质以展示边框
        const boxMaterial = new THREE.LineBasicMaterial({color: 0xff0000});
        const boundingBoxMesh = new THREE.LineSegments(new THREE.EdgesGeometry(boxGeometry), boxMaterial);
        // 设置包围盒的位置为指定位置
        boundingBoxMesh.position.copy(location);
        // 将包围盒添加到场景中
        scene.add(boundingBoxMesh);
    }
}

 const location = new THREE.Vector3(data.location[i][0], -data.location[i][2], data.location[i][1]);和const boxGeometry = new THREE.BoxGeometry(width, depth, height);这两段代码中原本的顺序是width, height, depth,但因为模型的坐标系与three中的坐标系不同,所以做了调整。顺便说一下,这种方法画包围框不会连接对角线。

二、添加文字 

      添加文字的作用是标明模型每个部分具体的名称 ,所以就要使用数据中的name数据

 

for (let i = 0; i < data.location.length; i++) {
    let height = data.location[i]
    // 创建文本标签
    const labelCanvas = document.createElement('canvas');
    const context = labelCanvas.getContext('2d');
    const location = new THREE.Vector3(data.location[i][0], -data.location[i][2], data.location[i][1]);
    context.font = 'Bold 20px Arial';
    context.fillStyle = 'rgba(0, 255, 0, 0.8)';
    context.fillText(data.name[i], 0, 20);
    const labelTexture = new THREE.CanvasTexture(labelCanvas);
    const labelMaterial = new THREE.SpriteMaterial({map: labelTexture});
    const labelSprite = new THREE.Sprite(labelMaterial);
    // 设置标签的位置为包围盒的位置偏移一些距离,可以根据需求调整偏移量
    labelSprite.position.copy(location.clone().add(new THREE.Vector3(0.6, -(height[2] / 2 + 0.5)*6, 0)));
    labelSprite.scale.set(6, 5, 5); // 调整标签的大小
    scene.add(labelSprite);
}

 const location = new THREE.Vector3(data.location[i][0], -data.location[i][2], data.location[i][1]);和 labelSprite.position.copy(location.clone().add(new THREE.Vector3(0.6, -(height[2] / 2 + 0.5)*6, 0)));这两段代码这样写的原因和上面的相同。

three.js是一个基于WebGL的3D图形库,它提供了一种简单的方式来在Web浏览器创建和显示3D图形。要使用three.js添加标签文字和指示线,你可以采用以下步骤: 1. 创建文本(标签): three.js提供了TextGeometry类,可以用来创建3D文字。你需要提供字体数据(可以使用像`font.json`这样的Three.js支持的字体文件),以及你想要显示的字符串。然后,你可以使用这个几何体和材质来创建一个Mesh对象,这样文字就可以作为场景的一部分渲染出来。 2. 创建指示线: 指示线可以通过绘制线条来实现。在three.js,你可以使用Line或者LineSegments对象来创建线条。你需要定义线条的顶点坐标,并将这些坐标传递给BufferGeometry或Geometry对象。然后创建一个LineBasicMaterial或LineDashedMaterial,并将这些材质和几何体结合起来创建出线条的Mesh对象。 3. 将标签和指示线添加到场景: 创建好TextGeometry和Line对象后,你需要将它们添加到你的场景。这意味着你需要将它们的Mesh对象添加到场景的子对象列表。 示例代码: ```javascript // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建3D文字 const loader = new THREE.FontLoader(); loader.load('path/to/your/font.json', function (font) { const textGeometry = new THREE.TextGeometry('标签文字', { font: font, size: 2, height: 0.5, curveSegments: 12, bevelEnabled: true, bevelThickness: 0.2, bevelSize: 0.1, bevelSegments: 5 }); const textMaterial = new THREE.MeshBasicMaterial({color: 0x000000}); const textMesh = new THREE.Mesh(textGeometry, textMaterial); scene.add(textMesh); }); // 创建指示线 const lineGeometry = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(0, 0, 0), new THREE.Vector3(10, 10, 10)]); const lineMaterial = new THREE.LineBasicMaterial({color: 0xff0000}); const line = new THREE.Line(lineGeometry, lineMaterial); scene.add(line); // 设置相机位置和渲染场景 camera.position.z = 5; function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); ``` 注意替换 `'path/to/your/font.json'` 为你的字体文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值