javascript - Three.js:无法使用 THREE.ExtrudeGeometry 打洞(切割)线形状

标签 javascript three.js 3d geometry drawing

大家好,我是 Three.js 的新手

我想使用 THREE.ExtrudeGeometry 在平板上打洞(切割)一些形状。

这是我的代码片段。

    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera( 50, window.innerWidth/window.innerHeight, 0.1, 1000 );
    camera.position.z = 10;
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );
    
    var light = new THREE.PointLight(0xffffff, 2, 100);
    light.position.z = 10;
    scene.add(light);
    
    let boardHeight = 100;
    let boardWidth = 100;
    
    const boardShape = new THREE.Shape();
    boardShape.moveTo(0, 0);
    boardShape.lineTo(0, boardHeight);
    boardShape.lineTo(boardWidth, boardHeight);
    boardShape.lineTo(boardWidth, 0);
    boardShape.lineTo(0, 0);
    
    var hole1 = new THREE.Path();
    hole1.moveTo(20, 20); 
    hole1.lineTo(18, 20);
    boardShape.holes.push(hole1);
    hole1 = new THREE.Path(); 
    hole1.moveTo(50, 50);
    hole1.absarc(30, 50, 20, 0.0000, Math.PI*2, true);
    boardShape.holes.push(hole1);
    hole1 = new THREE.Path(); 
    hole1.moveTo(25, 25); 
    hole1.lineTo(27, 25); 
    boardShape.holes.push(hole1);
    
    var material = new THREE.MeshBasicMaterial( { color: 0x005E99 } );
    
    const extrudeSettings = { depth: 1, bevelEnabled: false, bevelSegments: 1, steps: 1, bevelSize: 1, bevelThickness: 1, UVGenerator: THREE.ExtrudeGeometry.WorldUVGenerator };                 
    
    const geometry = new THREE.ExtrudeGeometry(boardShape,  extrudeSettings);
    
    var cube = new THREE.Mesh( geometry, material );
    geometry.scale(0.02, 0.02, 0.02);
    cube.position.set(-boardWidth / 2 * 0.02, -boardHeight / 2 * 0.02, 0.02 * 0.5);
    scene.add( cube );
    
    camera.position.z = 5;
    
    var render = function () {
      requestAnimationFrame( render );    
      //cube.rotation.x += 0.1;
      //cube.rotation.y += 0.1;    
      renderer.render(scene, camera);
    };
    
    render();
    
    // Rotate the mesh based on mouse position
    document.body.onmousemove = function(e){
      //cube.rotation.z = e.pageX / 100;
      //cube.rotation.x = e.pageY / 100;
    }
    
    // Click to toggle wireframe mode
    document.body.onclick = function(e){
      cube.material.wireframe = !cube.material.wireframe;
    }
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/build/three.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/examples/js/controls/OrbitControls.js"></script>
<script src="https://rawgit.com/Wilt/ThreeCSG/develop/ThreeCSG.js"></script>
<div id="container"></div>

我在给定的示例中添加了一个圆形和一条两线形状。

我可以为圆形打洞,但对于线条形状则不起作用。

我还附上了当前输出和预期输出的图像

电流输出:

enter image description here

预期输出:

enter image description here

最佳答案

您需要用路径绘制一个圆 Angular 矩形。 对于直线两端的两个点(x1,y1)和(x2,y2),可以得到矩形对应的8个顶点作为路径:

const getPath = (x1, y1, x2, y2, thickness = 1, radius = 0) => {
    const vector1 = new THREE.Vector2(x1, y1);
    const vector2 = new THREE.Vector2(x2, y2);
    const direction = vector2.clone().sub(vector1).normalize();
    const angle = direction.angle();
    const perpendicularDirection = direction.clone().rotateAround(new THREE.Vector2(0, 0), Math.PI / 2);
    const path = new THREE.Path();
    path.moveTo(...vector2.clone().addScaledVector(perpendicularDirection, thickness - radius).toArray());
    if(radius > 0) {
      path.arc(...direction.clone().multiplyScalar(-radius).toArray(), radius, angle, angle + Math.PI / 2);
    }
    path.lineTo(
      ...vector1.clone()
        .addScaledVector(perpendicularDirection, thickness)
        .addScaledVector(direction, radius)
        .toArray()
    );
    if(radius > 0) {
      path.arc(...perpendicularDirection.clone().multiplyScalar(-radius).toArray(), radius, angle + Math.PI / 2, angle + Math.PI);
    }
    path.lineTo(
      ...vector1.clone()
        .addScaledVector(perpendicularDirection, -(thickness - radius))
        .toArray()
    );
    if(radius > 0) {
      path.arc(...direction.clone().multiplyScalar(radius).toArray(), radius, angle + Math.PI, angle + 1.5 * Math.PI);
    }
    path.lineTo(
      ...vector2.clone()
        .addScaledVector(perpendicularDirection, -(thickness))
        .addScaledVector(direction, -radius)
        .toArray()
    );
    if(radius > 0) {
      path.arc(...perpendicularDirection.clone().multiplyScalar(radius).toArray(), radius, angle + 1.5 * Math.PI, angle + 2 * Math.PI);
    }
    path.lineTo(...vector2.clone().addScaledVector(perpendicularDirection, thickness - radius).toArray());
    return path;
  };

因此

var hole1 = getPath(10, 200, 400, 300, 40, 20);
boardShape.holes.push(hole1);

关于javascript - Three.js:无法使用 THREE.ExtrudeGeometry 打洞(切割)线形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69359913/

相关文章:

javascript - 检查子元素宽度是否超​​过父元素宽度

javascript - 你能在实例化后向 React JSX 元素添加一个键吗?

javascript - 如何使该弹出窗口的宽度占用户屏幕尺寸的百分比?

javascript - Threejs 基于相机旋转的指南针

Three.js 不那么强烈的环境光?

3d - YouTube HTML5 3D上传问题

javascript - 在 three.js 中查找对象尺寸

javascript - setInterval 数组

javascript - A帧图像亮度

javascript - 3d 数学 : screen space to world space