从直接几何转换为缓冲几何的几何交集

Geometry intersection converting from direct geometry to buffergeometry

我正在使用 Three.js。发现 Benpurdy. It's very easily modifiable and also used the techniques described here

写的一个非常好的 Decal 库

但是,该技术使用几何。我正在进行的项目使用 BufferGeometry。我追踪了几何相交的代码,但无法弄清楚从面和顶点到属性的转换。

this.createGeometry = function(matrix, mesh) {

  var geom = mesh.geometry;

  var decalGeometry = new THREE.Geometry(); 

  var projectorInverse = matrix.clone().getInverse(matrix);
  var meshInverse = mesh.matrixWorld.clone().getInverse(mesh.matrixWorld);
  var faces = [];

  for(var i = 0; i < geom.faces.length; i++){

    var verts = [geom.faces[i].a, geom.faces[i].b, geom.faces[i].c];

    var pts = [];
    var valid = false;

    for(var v = 0; v < 3; v++) {

      var vec = geom.vertices[verts[v]].clone();

      vec.applyMatrix4(mesh.matrixWorld);
      vec.applyMatrix4(matrix);

      if((vec.z > 1) || (vec.z < -1) || (vec.x > 1) || (vec.x < -1) || (vec.y > 1) || (vec.y < -1)) {
      } else {
        valid = true;
      }

      pts.push(vec);
    }

    if(valid) {

      var uv = [];
      for(var n = 0; n < 3; n++){
        uv.push(new THREE.Vector2( (pts[n].x + 1) / 2, (pts[n].y + 1) / 2));

        pts[n].applyMatrix4(projectorInverse);
        pts[n].applyMatrix4(meshInverse);

        decalGeometry.vertices.push( pts[n] );
      }

      // update UV's
      decalGeometry.faceVertexUvs[0].push(uv);

      var newFace = geom.faces[i].clone();

      newFace.a = decalGeometry.vertices.length - 3;
      newFace.b = decalGeometry.vertices.length - 2;
      newFace.c = decalGeometry.vertices.length - 1;

      decalGeometry.faces.push(newFace);
    }

  }

  return decalGeometry;
}

感谢是否有人可以阐明如何进行此操作?谢谢

BufferGeometry() 有一个方法 .fromGeometry()Populates this BufferGeometry with data from a Geometry object.

var geom = new THREE.BoxGeometry(1,1,1);
var bufGeom = new THREE.BufferGeometry().fromGeometry(geom);

更新。你可以反过来使用。

var bufGeom = new THREE.BoxBufferGeometry(1,1,1);
var geom = new THREE.Geometry().fromBufferGeometry(bufGeom);

我最终通过编写另一个函数来计算与 buffergeometry 的交集来解决问题。我花了一些时间试图理解原始缓冲区几何代码。

this.createGeometryFromBufferGeometry = function(matrix, mesh) {

  var geom = mesh.geometry;

  var decalGeometry = new THREE.Geometry(); 

  var projectorInverse = matrix.clone().getInverse(matrix);
  var meshInverse = mesh.matrixWorld.clone().getInverse(mesh.matrixWorld);
  var faces = [];

  for(var i = 0; i < geom.attributes.position.array.length; i+=9){

    var pts = [];
    var valid = false;

    for(var v = 0; v < 9; v+=3) {


      var vec = new THREE.Vector3(geom.attributes.position.array[i+v],geom.attributes.position.array[i+v+1],geom.attributes.position.array[i+v+2]);
      console.log((i+v) + " " + (i+v+1) + " " + (i+v+2) );
      console.log(vec);

      vec.applyMatrix4(mesh.matrixWorld);
      vec.applyMatrix4(matrix);

      if((vec.z > 1) || (vec.z < -1) || (vec.x > 1) || (vec.x < -1) || (vec.y > 1) || (vec.y < -1)) {
      } else {
        valid = true;
      }

      pts.push(vec);
    }


    if(valid) {

      var uv = [];
      for(var n = 0; n < 3; n++){
        uv.push(new THREE.Vector2( (pts[n].x + 1) / 2, (pts[n].y + 1) / 2));

        pts[n].applyMatrix4(projectorInverse);
        pts[n].applyMatrix4(meshInverse);

        decalGeometry.vertices.push( pts[n] );
      }

      decalGeometry.faceVertexUvs[0].push(uv);

      var newFace = new THREE.Face3()

      newFace.a = decalGeometry.vertices.length - 3;
      newFace.b = decalGeometry.vertices.length - 2;
      newFace.c = decalGeometry.vertices.length - 1;

      decalGeometry.faces.push(newFace);
    }

  }
  return decalGeometry;
}

快速而肮脏的解决方案是从 bufferGeometry 创建几何体,并在计算后处理创建的几何体

this.compute = function()
{
   this.geometry = mesh.geometry
   if(this.geometry.attributes)
   {
        this.geometry = new THREE.Geometry().fromBufferGeometry(this.geometry);
        this.computeDecal();
        this.geometry.dispose();
    }
    else
    {
        this.computeDecal();
    }
}