Three.js:如何在负标度后翻转法线

时间:2013-05-29 22:03:11

标签: three.js flip normals scaletransform

我克隆并使用负刻度翻转对象,这会导致我的单面朝向反转。我的问题是,我怎样才能翻转法线?

我不想使用material.side = THREE.DoubleSide,原因是:1)没有正常工作(一些阴影是从内部绘制的)和2)想保持尽可能多的性能。所以DoubleSide对我来说不是一个选择。

这就是我的物体如何翻转。

mesh.scale.x = - scale_width;

提前致谢!

5 个答案:

答案 0 :(得分:5)

我会根据一系列原因建议反对负面比例,如以下链接所述:Transforming vertex normals in three.js

您可以改为将几何反转矩阵应用于几何体

geometry.scale( - 1, 1, 1 );

正如链接中所解释的那样,这样做的结果是,几何面将不再具有逆时针绕组顺序,而是顺时针方向。

您可以手动遍历几何体并翻转每个面的缠绕顺序。这可能适合您 - 如果您没有应用纹理并且没有使用UV。如果要对几何体进行纹理处理,则还需要校正UV。

实际上,几何反演实用程序将是three.js的一个很好的补充。目前,图书馆不支持您想要做的事情。

three.js r.72

答案 1 :(得分:3)

这个问题是两年了,但万一有人经过。这是一种非破坏性的方式:

您可以输入"脏顶点/法线"模式,并手动翻转法线:

mesh.geometry.dynamic = true
mesh.geometry.__dirtyVertices = true;
mesh.geometry.__dirtyNormals = true;

mesh.flipSided = true;

//flip every vertex normal in mesh by multiplying normal by -1
for(var i = 0; i<mesh.geometry.faces.length; i++) {
    mesh.geometry.faces[i].normal.x = -1*mesh.geometry.faces[i].normal.x;
    mesh.geometry.faces[i].normal.y = -1*mesh.geometry.faces[i].normal.y;
    mesh.geometry.faces[i].normal.z = -1*mesh.geometry.faces[i].normal.z;
}

mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();

+1 @WestLangley,我建议你永远不要使用负面比例。

答案 2 :(得分:2)

固定!!

具有负刻度object.scale.x = -1的对象的翻转也会反转自three.js r89以来的法线(请参阅:Support reflection matrices. #12787)。

(但我必须升级到r91以解决我的正常问题。)

答案 3 :(得分:0)

将其倾倒在这里。我在某个地方找到了flipNormals,并将其翻译为BufferGeometry

翻转法线,翻转的UV,逆工作面绕组

有BufferGeometry版

export function flipBufferGeometryNormals(geometry) {
  const tempXYZ = [0, 0, 0];

  // flip normals
  for (let i = 0; i < geometry.attributes.normal.array.length / 9; i++) {
    // cache a coordinates
    tempXYZ[0] = geometry.attributes.normal.array[i * 9];
    tempXYZ[1] = geometry.attributes.normal.array[i * 9 + 1];
    tempXYZ[2] = geometry.attributes.normal.array[i * 9 + 2];

    // overwrite a with c
    geometry.attributes.normal.array[i * 9] =
      geometry.attributes.normal.array[i * 9 + 6];
    geometry.attributes.normal.array[i * 9 + 1] =
      geometry.attributes.normal.array[i * 9 + 7];
    geometry.attributes.normal.array[i * 9 + 2] =
      geometry.attributes.normal.array[i * 9 + 8];

    // overwrite c with stored a values
    geometry.attributes.normal.array[i * 9 + 6] = tempXYZ[0];
    geometry.attributes.normal.array[i * 9 + 7] = tempXYZ[1];
    geometry.attributes.normal.array[i * 9 + 8] = tempXYZ[2];
  }

  // change face winding order
  for (let i = 0; i < geometry.attributes.position.array.length / 9; i++) {
    // cache a coordinates
    tempXYZ[0] = geometry.attributes.position.array[i * 9];
    tempXYZ[1] = geometry.attributes.position.array[i * 9 + 1];
    tempXYZ[2] = geometry.attributes.position.array[i * 9 + 2];

    // overwrite a with c
    geometry.attributes.position.array[i * 9] =
      geometry.attributes.position.array[i * 9 + 6];
    geometry.attributes.position.array[i * 9 + 1] =
      geometry.attributes.position.array[i * 9 + 7];
    geometry.attributes.position.array[i * 9 + 2] =
      geometry.attributes.position.array[i * 9 + 8];

    // overwrite c with stored a values
    geometry.attributes.position.array[i * 9 + 6] = tempXYZ[0];
    geometry.attributes.position.array[i * 9 + 7] = tempXYZ[1];
    geometry.attributes.position.array[i * 9 + 8] = tempXYZ[2];
  }

  // flip UV coordinates
  for (let i = 0; i < geometry.attributes.uv.array.length / 6; i++) {
    // cache a coordinates
    tempXYZ[0] = geometry.attributes.uv.array[i * 6];
    tempXYZ[1] = geometry.attributes.uv.array[i * 6 + 1];

    // overwrite a with c
    geometry.attributes.uv.array[i * 6] =
      geometry.attributes.uv.array[i * 6 + 4];
    geometry.attributes.uv.array[i * 6 + 1] =
      geometry.attributes.uv.array[i * 6 + 5];

    // overwrite c with stored a values
    geometry.attributes.uv.array[i * 6 + 4] = tempXYZ[0];
    geometry.attributes.uv.array[i * 6 + 5] = tempXYZ[1];
  }

  geometry.attributes.normal.needsUpdate = true;
  geometry.attributes.position.needsUpdate = true;
  geometry.attributes.uv.needsUpdate = true;
}

有关老式几何

export function flipNormals (geometry) {
  let temp = 0;
  let face;

  // flip every vertex normal in geometry by multiplying normal by -1
  for (let i = 0; i < geometry.faces.length; i++) {
    face = geometry.faces[i];
    face.normal.x = -1 * face.normal.x;
    face.normal.y = -1 * face.normal.y;
    face.normal.z = -1 * face.normal.z;
  }

  // change face winding order
  for (let i = 0; i < geometry.faces.length; i++) {
    const face = geometry.faces[i];
    temp = face.a;
    face.a = face.c;
    face.c = temp;
  }

  // flip UV coordinates
  const faceVertexUvs = geometry.faceVertexUvs[0];
  for (let i = 0; i < faceVertexUvs.length; i++) {
    temp = faceVertexUvs[i][0];
    faceVertexUvs[i][0] = faceVertexUvs[i][2];
    faceVertexUvs[i][2] = temp;
  }

  geometry.verticesNeedUpdate = true;
  geometry.normalsNeedUpdate = true;

  geometry.computeFaceNormals();
  geometry.computeVertexNormals();
  geometry.computeBoundingSphere();
}

答案 4 :(得分:0)

如果您有索引BufferGeometry,则已经足以重新排序这样的索引:

    let temp;
    for ( let i = 0; i < geometry.index.array.length; i += 3 ) {
      // swap the first and third values
      temp = geometry.index.array[ i ];
      geometry.index.array[ i ] = geometry.index.array[ i + 2 ];
      geometry.index.array[ i + 2 ] = temp;
    }