三个js:如何规范化由顶点生成的网格

时间:2018-04-12 19:15:32

标签: three.js linear-algebra

我对Three js有点新鲜,而我的线性代数时代早在90年代就已存在,所以我不记得很多关于四分之一的事情了。我的问题是我有一个立方体的8个顶点,我可以用它来创建自定义几何网格,但它没有设置其世界矩阵的位置/旋转/比例信息。因此,它不能像其他三个js模块一样干净利用。我可以查看数学并计​​算什么位置/比例/旋转(旋转有点毛茸茸和一些有趣的acos东西)应该是并从中创建一个标准的boxgeometry。但似乎应该有一些方法通过三个js对象来做它,如果我可以生成适当的矩阵来应用它。四分之一setFromUnitVectors看起来很有趣,但我仍然需要做一些工作来生成向量。任何想法将不胜感激

编辑:) :)让我试着简化一下。我有8个顶点,我想创建一个盒子几何体。但是盒子几何体不需要顶点。它需要宽度,高度,深度(相对容易计算),然后设置位置/比例/旋转。所以这是我迄今为止的代码:

  5____4
1/___0/|
| 6__|_7
2/___3/

const box = new Box3();
box.setFromPoints(points);

const width = points[1].distanceTo(points[0]);
const height = points[3].distanceTo(points[0]);
const depth = points[4].distanceTo(points[0]);

const geometry = new BoxGeometry(width, height, depth);
mesh = new Mesh(geometry, material);

const center = box.getCenter(new Vector3());
const normalizedCorner = points[0].clone().sub(center);
const quarterian = new Quaternion();
quarterian.setFromUnitVectors(geometry.vertices[0], normalizedCorner);
mesh.setRotationFromQuaternion(quarterian);
mesh.position.copy(center);

问题是我的旋转元素是错误的(除了我的向量不是单位向量)。我显然没有得到正确的四分之一来正确旋转我的网格。

编辑:根据WestLangley的建议,我正在创建一个旋转矩阵。但是,当它在正确的平面中旋转时,角度关闭。这是我添加的内容:

const matrix = new Matrix4();
const widthVector = new Vector3().subVectors(points[6], points[7]).normalize();
const heightVector = new Vector3().subVectors(points[6], points[5]).normalize();
const depthVector = new Vector3().subVectors(points[6], points[2]).normalize();

matrix.set(
  widthVector.x, heightVector.x, depthVector.x, 0,
  widthVector.y, heightVector.y, depthVector.y, 0,
  widthVector.z, heightVector.z, depthVector.z, 0,
  0, 0, 0, 1,
);
mesh.quaternion.setFromRotationMatrix(matrix);

1 个答案:

答案 0 :(得分:0)

Per WestLangley的评论我没有正确地创建我的矩阵。正确的矩阵如下:

const matrix = new Matrix4();
const widthVector = new Vector3().subVectors(points[7], points[6]).normalize();
const heightVector = new Vector3().subVectors(points[5], points[6]).normalize();
const depthVector = new Vector3().subVectors(points[2], points[6]).normalize();

matrix.set(
  widthVector.x, heightVector.x, depthVector.x, 0,
  widthVector.y, heightVector.y, depthVector.y, 0,
  widthVector.z, heightVector.z, depthVector.z, 0,
  0, 0, 0, 1,
);
mesh.quaternion.setFromRotationMatrix(matrix);