three.js:在自定义Geometry上计算faceVertexUvs以进行纹理映射

时间:2018-09-14 12:55:08

标签: three.js geometry textures

我在这里有这个例子: https://jsfiddle.net/NiklasKnaack/L1cqbdr9/82/

function createPlanetFace( radiusX, radiusY, radiusZ, localUp, resolution ) {

    const face = {};
  face.geometry = new THREE.Geometry();
  face.geometry.faceVertexUvs[ 0 ] = [];
  face.verticesOriginal = [];
  face.verticesNormalized = [];

    const axisA = new THREE.Vector3( localUp.y, localUp.z, localUp.x );
  const axisB = new THREE.Vector3().crossVectors( localUp, axisA );

  for ( let y = 0; y < resolution; y++ ) {

    for ( let x = 0; x < resolution; x++ ) {

        const index = x + y * resolution;

        const percent = new THREE.Vector2( x, y );
      percent.x /= ( resolution - 1 );
      percent.y /= ( resolution - 1 );

      const vertex = new THREE.Vector3();
      vertex.x = ( localUp.x + ( percent.x - 0.5 ) * 2 * axisA.x + ( percent.y - 0.5 ) * 2 * axisB.x ) * radiusX;
      vertex.y = ( localUp.y + ( percent.x - 0.5 ) * 2 * axisA.y + ( percent.y - 0.5 ) * 2 * axisB.y ) * radiusY;
      vertex.z = ( localUp.z + ( percent.x - 0.5 ) * 2 * axisA.z + ( percent.y - 0.5 ) * 2 * axisB.z ) * radiusZ;

            face.verticesOriginal[ index ] = new THREE.Vector3( vertex.x, vertex.y, vertex.z );

      vertex.normalize();//create a sphere

      vertex.x += vertex.x * radiusX / 2;
      vertex.y += vertex.y * radiusY / 2;
      vertex.z += vertex.z * radiusZ / 2;

      face.verticesNormalized[ index ] = new THREE.Vector3( vertex.x, vertex.y, vertex.z );

      face.geometry.vertices[ index ] = vertex;

      //if ( index % 6 === 0 && index > 0 && x !== resolution - 1 && y !== resolution - 1 ) {
      if ( x !== resolution - 1 && y !== resolution - 1 ) {

        const triangle1 = new THREE.Face3( index, index + resolution + 1, index + resolution );
        const triangle2 = new THREE.Face3( index, index + 1, index + resolution + 1 );

        face.geometry.faces.push( triangle1, triangle2 );

      }

    }

  }

  //face.geometry.computeBoundingSphere();
  //face.geometry.computeVertexNormals();
  //face.geometry.computeFaceNormals();

  return face;

};

为此,我想计算UV,以便可以正确显示加载的纹理。

原则上,createPlanetFace函数创建一个平面。从这6个平面中创建一个立方体或球体。 (在示例中查看) 到目前为止,由于UV缺失,仅纹理不显示。 经过大量研究和尝试,我发现控制台中出现错误,纹理完全失真或根本没有纹理。这就是为什么我消除了计算UV的痛苦尝试。 我在此主题上找到的示例都是不同的。至少其中大多数。现在,我已经无法采取任何行动,需要您的帮助。

谢谢。

0 个答案:

没有答案