8 constructor(x, y, z, mass) {
\r
9 this.position = new THREE.Vector3(x, y, z);
\r
10 this.previous = new THREE.Vector3(x, y, z);
\r
11 this.acceleration = new THREE.Vector3(0, 0, 0);
\r
23 constructor(width, height, numPointsWidth, numPointsHeight) {
\r
25 this.height = height;
\r
26 this.numPointsWidth = numPointsWidth;
\r
27 this.numPointsHeight = numPointsHeight;
\r
30 * distance between two vertices horizontally/vertically
\r
31 * divide by the number of points minus one
\r
32 * because there are (n - 1) lines between n vertices
\r
34 let stepWidth = width / (numPointsWidth - 1);
\r
35 let stepHeight = height / (numPointsHeight - 1);
\r
38 * iterate over the number of vertices in x/y axis
\r
39 * and add a new Particle to "particles"
\r
41 this.particles = [];
\r
42 for (let y = 0; y < numPointsHeight; y++) {
\r
43 for (let x = 0; x < numPointsWidth; x++) {
\r
44 this.particles.push(
\r
46 (x - ((numPointsWidth-1)/2)) * stepWidth,
\r
47 height - (y + ((numPointsHeight-1)/2)) * stepHeight,
\r
54 generateGeometry() {
\r
55 const geometry = new THREE.BufferGeometry();
\r
57 const vertices = [];
\r
61 for (let particle of this.particles) {
\r
63 particle.position.x,
\r
64 particle.position.y,
\r
65 particle.position.z);
\r
68 const numPointsWidth = this.numPointsWidth;
\r
69 const numPointsHeight = this.numPointsHeight;
\r
72 * helper function to calculate index of vertex
\r
73 * in "vertices" array based on its x and y positions
\r
75 * @param {number} x - x index of vertex
\r
76 * @param {number} y - y index of vertex
\r
78 function getVertexIndex(x, y) {
\r
79 return y * numPointsWidth + x;
\r
83 * generate faces based on 4 vertices
\r
84 * and 6 springs each
\r
86 for (let y = 0; y < numPointsHeight - 1; y++) {
\r
87 for (let x = 0; x < numPointsWidth - 1; x++) {
\r
89 getVertexIndex(x, y),
\r
90 getVertexIndex(x+1, y),
\r
91 getVertexIndex(x+1, y+1)
\r
94 getVertexIndex(x, y),
\r
95 getVertexIndex(x+1, y+1),
\r
96 getVertexIndex(x, y+1)
\r
101 geometry.setIndex(indices);
\r
102 geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
\r
103 //geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
\r
104 geometry.computeBoundingSphere();
\r
105 geometry.computeVertexNormals();
\r
109 updateGeometry(geometry) {
\r