1

I'm trying to adapt this example or this discussion, but when I use new library, there is no more face and vertices in geometry properties.

The structures is differenet, so I confuse to use the Index->array and Attrirube->Position to change this code:

`

  obj.geometry.faces.forEach(function(face, idx) {
    obj.localToWorld(a.copy(obj.geometry.vertices[face.a]));
    obj.localToWorld(b.copy(obj.geometry.vertices[face.b]));
    obj.localToWorld(c.copy(obj.geometry.vertices[face.c]));
    lineAB = new THREE.Line3(a, b);
    lineBC = new THREE.Line3(b, c);
    lineCA = new THREE.Line3(c, a);
    console.log("lineAB", lineAB);
    console.log("lineBC", lineBC);
    console.log("lineCA", lineCA);

    setPointOfIntersection(lineAB, mathPlane, idx);
    setPointOfIntersection(lineBC, mathPlane, idx);
    setPointOfIntersection(lineCA, mathPlane, idx);
  });

` Any idea how to do it? Please help.. Thanks in advance~

Kang Gilar
  • 11
  • 1

1 Answers1

2

Since r125, there is no Geometry class anymore. All geometries are BufferGeometry now. Thus, vertices are stored in geometry.attributes.position.

BufferGeometry can be indexed or non-indexed:

  • Indexed means that faces defined with triplets of incides of vertices.
  • Non-indexed means that faces defined with triplets of vertices.

So, this part of code:

var a = new THREE.Vector3(),
  b = new THREE.Vector3(),
  c = new THREE.Vector3();

  obj.geometry.faces.forEach(function(face) {
    obj.localToWorld(a.copy(obj.geometry.vertices[face.a]));
    obj.localToWorld(b.copy(obj.geometry.vertices[face.b]));
    obj.localToWorld(c.copy(obj.geometry.vertices[face.c]));
    lineAB = new THREE.Line3(a, b);
    lineBC = new THREE.Line3(b, c);
    lineCA = new THREE.Line3(c, a);
    setPointOfIntersection(lineAB, mathPlane);
    setPointOfIntersection(lineBC, mathPlane);
    setPointOfIntersection(lineCA, mathPlane);
  });

needs some changes.

var a = new THREE.Vector3(),
  b = new THREE.Vector3(),
  c = new THREE.Vector3();

var isIndexed = obj.geometry.index != null; // if geometry is indexed or non-indexed
var pos = obj.geometry.attributes.position; // attribute with positions
var idx = obj.geometry.index;  // index

var faceCount = (isIndexed ? idx.count : pos.count) / 3; // amount of faces

  for(let i = 0; i < faceCount; i++) {

    let baseIdx = i * 3;
    let idxA = baseIdx + 0;
    a.fromBufferAttribute(pos, isIndexed ? idx.getX(idxA) : idxA); 
    // .fromBufferAttribute is a method of Vector3
    // .getX is a method of BufferAttribute
    let idxB = baseIdx + 1;
    b.fromBufferAttribute(pos, isIndexed ? idx.getX(idxB) : idxB);
    let idxC = baseIdx + 2;
    c.fromBufferAttribute(pos, isIndexed ? idx.getX(idxC) : idxC);   


    obj.localToWorld(a);
    obj.localToWorld(b);
    obj.localToWorld(c);
    lineAB = new THREE.Line3(a, b);
    lineBC = new THREE.Line3(b, c);
    lineCA = new THREE.Line3(c, a);
    setPointOfIntersection(lineAB, mathPlane);
    setPointOfIntersection(lineBC, mathPlane);
    setPointOfIntersection(lineCA, mathPlane);

  });

PS Haven't tested this snippet, changes from scratch. Possible typos.

prisoner849
  • 16,894
  • 4
  • 34
  • 68