【发布时间】:2021-04-23 08:29:22
【问题描述】:
我有一架定制飞机:
JS:
AFRAME.registerGeometry('example', {
schema: {
vertices: {
default: ['-10 10 0', '-10 -10 0', '10 -10 0', '10 -10 0'],
}
},
init: function (data) {
var geometry = new THREE.Geometry();
geometry.vertices = data.vertices.map(function (vertex) {
var points = vertex.split(' ').map(function(x){return parseInt(x);});
return new THREE.Vector3(points[0], points[1], points[2]);
});
geometry.computeBoundingBox();
geometry.faces.push(new THREE.Face3(0, 1, 2));
geometry.faces.push(new THREE.Face3(0, 2, 3));
geometry.mergeVertices();
geometry.computeFaceNormals();
geometry.computeVertexNormals();
this.geometry = geometry;
}
});
HTML:
<a-entity id="myPlane" geometry="primitive: example; vertices: 1 1 -3, 3 1 -3, 2 2 -3, 1 2 -3"></a-entity>
我现在如何在动画循环中操纵顶点位置?
让我们说第一点:
geometry.vertices[0]
我知道我可以通过以下方式访问网格:
document.getElementById("myPlane").object3D;
并改变它的位置,例如:
document.getElementById("myPlane").object3D.position.set(1,0,0)
但是平面网格的几何上没有顶点:
document.getElementById("plane").object3D.children[0]
如何操作该几何体的顶点?
编辑:
我发现你可以像这样更新顶点的位置:
document.getElementById("myPlane").object3D.children[0].geometry.attributes.position.array[0] = 20;
document.getElementById("myPlane").object3D.children[0].geometry.attributes.position.needsUpdate = true;
想在 tick() 函数中完成整个操作,因为我真正想要的是用一条线连接两个对象。
现在平面的顶点如下所示:
Float32Array(18) [-0, 0, -3, 0, 1, -3, 2, 2, -3, 1, 1, -3, 2, 2, -3, 1, 2, -3]
由于飞机有 4 个点,我预计会有 4*3 = 12 个元素,但我们这里有 18 个元素。除了xyz还有什么?
【问题讨论】:
标签: javascript aframe ar.js