I have a custom plane:
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>
How could I now manipulate the vertices position in an animation loop?
Lets say the first point:
geometry.vertices[0]
I know I can access the mesh with following:
document.getElementById("myPlane").object3D;
and change its position for example:
document.getElementById("myPlane").object3D.position.set(1,0,0)
but there is no vertices on the geometry of the plane mesh:
document.getElementById("plane").object3D.children[0]
how to manipulate the vertices of that geometry?
EDIT:
I found out you can update the position of the vertices like this:
document.getElementById("myPlane").object3D.children[0].geometry.attributes.position.array[0] = 20;
document.getElementById("myPlane").object3D.children[0].geometry.attributes.position.needsUpdate = true;
would like to do the whole manipulation in the tick() function, because what I really want is to connect two objects with a line.
Now the vertices of the plane looks like this:
Float32Array(18) [-0, 0, -3, 0, 1, -3, 2, 2, -3, 1, 1, -3, 2, 2, -3, 1, 2, -3]
since the plane has 4 points I expected to have 4*3 = 12 elements, but we got here 18 elements. What is the rest beside xyz?
The vertices are available as an attribute of the objects geometry:
And you can simply change anything you want, and confirm the updates with:
The big complication comes with the difference between indexed and non-indexed geometry. In a nutshell:
The code looks identical, but a-frame versions are different - you should get the same results when changing geometries (geometry.toNonIndexed())