【问题标题】:Three.js get the 4 corner coordinates of a cube?Three.js 获取立方体的 4 个角坐标?
【发布时间】:2014-10-12 02:26:08
【问题描述】:

如何获得立方体角的 4 个坐标?

【问题讨论】:

    标签: three.js


    【解决方案1】:

    如果您使用 CubeGeometry(宽度、高度、深度)并将立方体放置在某个位置,那么您的八个角在

    position.x + width/2, position.y + height/2, position.z + depth/2
    position.x + width/2, position.y + height/2, position.z - depth/2
    position.x + width/2, position.y - height/2, position.z + depth/2
    position.x + width/2, position.y - height/2, position.z - depth/2
    position.x - width/2, position.y + height/2, position.z + depth/2
    position.x - width/2, position.y + height/2, position.z - depth/2
    position.x - width/2, position.y - height/2, position.z + depth/2
    position.x - width/2, position.y - height/2, position.z - depth/2
    

    【讨论】:

    • 我尝试设置 cube2 的位置。 cube 是一个立方体数组: cube2.position.set(cube.last().position.x,60,70);但得到错误 Uncaught TypeError: Object [object Array] has no method 'last'
    【解决方案2】:

    这是一个完整的实现:

    // Returns the positions of all the corners of the box
    // Uses CSS ordering conventions: CW from TL.  First front face corners, then back.
    THREE.BoxGeometry.prototype.corners = function(position){
      this._corners || (this._corners = [
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3,
        new THREE.Vector3
      ]);
    
      var halfWidth = this.parameters.width / 2, halfHeight = this.parameters.height / 2, halfDepth = this.parameters.depth / 2;
    
      this._corners[0].set(position.x - halfWidth, position.y + halfHeight, position.z + halfDepth);
      this._corners[1].set(position.x + halfWidth, position.y + halfHeight, position.z + halfDepth);
      this._corners[2].set(position.x + halfWidth, position.y - halfHeight, position.z + halfDepth);
      this._corners[3].set(position.x - halfWidth, position.y - halfHeight, position.z + halfDepth);
      this._corners[4].set(position.x - halfWidth, position.y + halfHeight, position.z - halfDepth);
      this._corners[5].set(position.x + halfWidth, position.y + halfHeight, position.z - halfDepth);
      this._corners[6].set(position.x + halfWidth, position.y - halfHeight, position.z - halfDepth);
      this._corners[7].set(position.x - halfWidth, position.y - halfHeight, position.z - halfDepth);
    
      return this._corners
    
    }
    
    THREE.Mesh.prototype.corners = function(){
    
      if (!this.geometry instanceof THREE.BoxGeometry){
        console.warn('Unsupported geometry for #corners()')
        return
      }
    
      return this.geometry.corners(this.position)
    
    };
    

    【讨论】:

    • 请注意,在定义 BoxGeometry 形状时必须提供宽度、高度和深度。
    • 假设您有一个立方体在 x 和 z 轴上移动,并且立方体在所有轴上旋转,这不会返回 y 角的正确位置。如果不设置y位置,则默认为0,但为角值添加半高。
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 2020-07-05
    • 2021-12-24
    • 2021-03-26
    • 2012-10-11
    • 2019-12-26
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多