Three.js如何获取一个立方体的4个角坐标?

4

我怎样获取立方体四个角的坐标?

2个回答

3

如果您正在使用立方体几何体(宽度、高度、深度)并将立方体放置在某个位置,则其八个角位于

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'。 - Rails beginner

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形状时,必须提供宽度、高度和深度。 - Chewie The Chorkie
如果你有一个沿着x和z轴移动的立方体,并且该立方体在所有轴上旋转,这将不能正确返回y轴角落的位置。如果它的y位置没有设置,它将默认为0,但是需要添加一半高度来获取角落值。 - Chewie The Chorkie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接