Three.js得到立方体的4个角坐标?

时间:2013-03-08 20:20:07

标签: three.js

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

这是一个完整的实现:

// 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)

};