0

我正在使用 Three.js。找到了Benpurdy编写的一个非常好的贴花库。它很容易修改,也使用了这里描述的技术

但是,该技术使用几何。我正在进行的项目使用 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;
}

感谢是否有人可以阐明如何继续追求这一目标?谢谢。

4

3 回答 3

0

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);

UPD。你可以反过来使用。

var bufGeom = new THREE.BoxBufferGeometry(1,1,1);
var geom = new THREE.Geometry().fromBufferGeometry(bufGeom);
于 2016-11-14T05:53:02.147 回答
0

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

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;
}
于 2016-11-15T10:03:42.117 回答
0

快速而肮脏的解决方案是从 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();
    }
}
于 2016-12-24T01:09:30.917 回答