BufferGeometry的点交点



我试图调整这个例子或这个讨论,但当我使用新库时,几何属性中不再有面和顶点。

结构不同,所以我混淆了使用索引->阵列和Attrirube->更改此代码的位置:

`

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

`知道怎么做吗?请帮忙。。提前感谢~

自r125以来,不再存在Geometry类。现在所有的几何图形都是BufferGeometry。因此,顶点被存储在geometry.attributes.position中。

BufferGeometry可以被索引或不被索引:

  • Indexed表示用顶点
  • 非索引意味着用三元组定义的面顶点

所以,这部分代码:

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

需要一些更改。

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尚未测试此片段,从头开始更改。可能是打字错误。

最新更新