【问题标题】:Rounded corner box having holes using three.js圆角盒有孔使用three.js
【发布时间】:2020-12-28 16:57:50
【问题描述】:

我需要创建一个在 4 个侧面但在顶部和底部具有圆角的盒子。 Three.js 有一个很好的示例webgl_geometry_shapes,我正在尝试复制它以在绿色圆角框中添加一个孔。在这个例子中为圆添加一个洞:

        // Arc circle

            var arcShape = new THREE.Shape()
                .moveTo( 50, 10 )
                .absarc( 10, 10, 40, 0, Math.PI * 2, false );

            var holePath = new THREE.Path()
                .moveTo( 20, 10 )
                .absarc( 10, 10, 10, 0, Math.PI * 2, true )

            arcShape.holes.push( holePath );

他们创建了一个shape和一个path,然后他们添加了hole。结果是

我对圆角正方形使用了相同的逻辑

原始形状

 // Rounded rectangle

            var roundedRectShape = new THREE.Shape();

            ( function roundedRect( ctx, x, y, width, height, radius ) {

                ctx.moveTo( x, y + radius );
                ctx.lineTo( x, y + height - radius );
                ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
                ctx.lineTo( x + width - radius, y + height );
                ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
                ctx.lineTo( x + width, y + radius );
                ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
                ctx.lineTo( x + radius, y );
                ctx.quadraticCurveTo( x, y, x, y + radius );

            } )( roundedRectShape, 0, 0, 50, 50, 20 );

新孔形状

            var roundedRectShape_small = new THREE.Path();

            ( function roundedRect( ctx, x, y, width, height, radius ) {

                ctx.moveTo( x, y + radius );
                ctx.lineTo( x, y + height - radius );
                ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
                ctx.lineTo( x + width - radius, y + height );
                ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
                ctx.lineTo( x + width, y + radius );
                ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
                ctx.lineTo( x + radius, y );
                ctx.quadraticCurveTo( x, y, x, y + radius );

            } )( roundedRectShape_small, 10, 10, 30, 30, 20 );

然后我添加孔

roundedRectShape.holes.push( roundedRectShape_small );

结果不一样……

您可以看到孔在那里,但内部的 3D 效果是错误的。如何解决?

【问题讨论】:

  • 在同一个例子中,“smileyShape”有几个孔。也许我应该以同样的方式画出我的盒子。你有什么建议吗?
  • 孔的缠绕顺序必须相反

标签: javascript three.js


【解决方案1】:

您必须以相反的缠绕顺序绘制孔。由于外部矩形路径是顺时针绘制的,因此孔路径必须逆时针绘制:

let f_rect = function roundedRect( ctx, x, y, width, height, radius ) {
    ctx.moveTo( x, y + radius );
    ctx.lineTo( x, y + height - radius );
    ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
    ctx.lineTo( x + width - radius, y + height );
    ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
    ctx.lineTo( x + width, y + radius );
    ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
    ctx.lineTo( x + radius, y );
    ctx.quadraticCurveTo( x, y, x, y + radius );
}

let f_rect_reverse = function roundedRect( ctx, x, y, width, height, radius ) { 
    ctx.moveTo( x, y + height - radius );
    ctx.lineTo( x, y + radius ); 
    ctx.quadraticCurveTo( x, y, x + radius, y );
    ctx.lineTo( x + width - radius, y );
    ctx.quadraticCurveTo( x + width, y, x + width, y + radius );
    ctx.lineTo( x + width, y + height - radius );
    ctx.quadraticCurveTo( x + width, y + height, x + width - radius, y + height );
    ctx.lineTo( x + radius, y + height );
    ctx.quadraticCurveTo( x, y + height, x, y + height - radius );       
}

var roundedRectShape = new THREE.Shape();
f_rect( roundedRectShape, -25, -25, 50, 50, 20 );
var roundedRectShape_small = new THREE.Path();
f_rect_reverse( roundedRectShape_small, -15, -15, 30, 30, 10 );
roundedRectShape.holes.push( roundedRectShape_small );

查看完整示例:

(function onLoad() {
  var container, camera, scene, renderer, controls;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    
    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(3, 2, 7);
    scene.add(camera);
    window.onresize = function() {
      renderer.setSize(window.innerWidth, window.innerHeight);
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
    }
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.x = 4;
    directionalLight.position.y = 1;
    directionalLight.position.z = 2;
    scene.add( directionalLight );
    
    controls = new THREE.OrbitControls(camera, renderer.domElement);
        
    addGridHelper();
    createModel();
  }

  function createModel() {

      let f_rect = function roundedRect( ctx, x, y, width, height, radius ) {
          ctx.moveTo( x, y + radius );
          ctx.lineTo( x, y + height - radius );
          ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
          ctx.lineTo( x + width - radius, y + height );
          ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
          ctx.lineTo( x + width, y + radius );
          ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
          ctx.lineTo( x + radius, y );
          ctx.quadraticCurveTo( x, y, x, y + radius );
      }

      let f_rect_reverse = function roundedRect( ctx, x, y, width, height, radius ) { 
          ctx.moveTo( x, y + height - radius );
          ctx.lineTo( x, y + radius ); 
          ctx.quadraticCurveTo( x, y, x + radius, y );
          ctx.lineTo( x + width - radius, y );
          ctx.quadraticCurveTo( x + width, y, x + width, y + radius );
          ctx.lineTo( x + width, y + height - radius );
          ctx.quadraticCurveTo( x + width, y + height, x + width - radius, y + height );
          ctx.lineTo( x + radius, y + height );
          ctx.quadraticCurveTo( x, y + height, x, y + height - radius );       
      }

      var roundedRectShape = new THREE.Shape();
      f_rect( roundedRectShape, -25, -25, 50, 50, 20 );
      var roundedRectShape_small = new THREE.Path();
      f_rect_reverse( roundedRectShape_small, -15, -15, 30, 30, 10 );
      roundedRectShape.holes.push( roundedRectShape_small );

      var material = new THREE.MeshLambertMaterial( { color: 0x00ff00, side: THREE.DoubleSide } );
      var extrudeSettings = { depth: 8, bevelEnabled: true, bevelSegments: 2, steps: 3, bevelSize: 5, bevelThickness: 5 };
      var geometry = new THREE.ExtrudeBufferGeometry( roundedRectShape, extrudeSettings );
      
      let s = 0.1;
      var mesh = new THREE.Mesh( geometry, material );
      mesh.scale.set( s, s, s );

      scene.add( mesh );
  }

  function addGridHelper() {

    var helper = new THREE.GridHelper(10, 10);
    helper.material.opacity = 0.25;
    helper.material.transparent = true;
    scene.add(helper);

    var axis = new THREE.AxesHelper(100);
    scene.add(axis);
  }

  function animate() {
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<!--script src="https://threejs.org/build/three.js"></!--script-->
<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>
<div id="container"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多