首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Three.js:无法使用THREE.ExtrudeGeometry打孔(剪切)线条形状

Three.js:无法使用THREE.ExtrudeGeometry打孔(剪切)线条形状
EN

Stack Overflow用户
提问于 2021-09-28 10:18:42
回答 1查看 102关注 0票数 2

大家好,我是Three.js新手

我想用THREE.ExtrudeGeometry在素板上冲压(切割)一些形状。

下面是我的代码片段。

代码语言:javascript
复制
    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;
    }
代码语言:javascript
复制
<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>

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

我可以冲孔为圆形,但对于线形,它不起作用。

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

当前输出:

预期输出:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-07 11:50:41

您将需要跟踪带有圆角的矩形和路径。对于两个点作为线的端点(x1,y1)和(x2,y2),可以获得对应于矩形的8个顶点作为路径:

代码语言:javascript
复制
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;
  };

因此,

代码语言:javascript
复制
var hole1 = getPath(10, 200, 400, 300, 40, 20);
boardShape.holes.push(hole1);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69359913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档