-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathp5.Geometry.js
300 lines (270 loc) · 8.36 KB
/
p5.Geometry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @module Lights, Camera
* @submodule Material
* @for p5
* @requires core
* @requires p5.Geometry
*/
//some of the functions are adjusted from Three.js(http://threejs.org)
import p5 from '../core/main';
/**
* p5 Geometry class
* @class p5.Geometry
* @constructor
* @param {Integer} [detailX] number of vertices on horizontal surface
* @param {Integer} [detailY] number of vertices on horizontal surface
* @param {function} [callback] function to call upon object instantiation.
*/
p5.Geometry = function(detailX, detailY, callback) {
//an array containing every vertex
//@type [p5.Vector]
this.vertices = [];
//an array containing every vertex for stroke drawing
this.lineVertices = [];
//an array 1 normal per lineVertex with
//final position representing which direction to
//displace for strokeWeight
//[[0,0,-1,1], [0,1,0,-1] ...];
this.lineNormals = [];
//an array containing 1 normal per vertex
//@type [p5.Vector]
//[p5.Vector, p5.Vector, p5.Vector,p5.Vector, p5.Vector, p5.Vector,...]
this.vertexNormals = [];
//an array containing each three vertex indices that form a face
//[[0, 1, 2], [2, 1, 3], ...]
this.faces = [];
//a 2D array containing uvs for every vertex
//[[0.0,0.0],[1.0,0.0], ...]
this.uvs = [];
// a 2D array containing edge connectivity pattern for create line vertices
//based on faces for most objects;
this.edges = [];
this.vertexColors = [];
this.detailX = detailX !== undefined ? detailX : 1;
this.detailY = detailY !== undefined ? detailY : 1;
this.dirtyFlags = {};
if (callback instanceof Function) {
callback.call(this);
}
return this; // TODO: is this a constructor?
};
p5.Geometry.prototype.reset = function() {
this.lineVertices.length = 0;
this.lineNormals.length = 0;
this.vertices.length = 0;
this.edges.length = 0;
this.vertexColors.length = 0;
this.vertexNormals.length = 0;
this.uvs.length = 0;
this.dirtyFlags = {};
};
/**
* computes faces for geometry objects based on the vertices.
* @method computeFaces
* @chainable
*/
p5.Geometry.prototype.computeFaces = function() {
this.faces.length = 0;
const sliceCount = this.detailX + 1;
let a, b, c, d;
for (let i = 0; i < this.detailY; i++) {
for (let j = 0; j < this.detailX; j++) {
a = i * sliceCount + j; // + offset;
b = i * sliceCount + j + 1; // + offset;
c = (i + 1) * sliceCount + j + 1; // + offset;
d = (i + 1) * sliceCount + j; // + offset;
this.faces.push([a, b, d]);
this.faces.push([d, b, c]);
}
}
return this;
};
p5.Geometry.prototype._getFaceNormal = function(faceId) {
//This assumes that vA->vB->vC is a counter-clockwise ordering
const face = this.faces[faceId];
const vA = this.vertices[face[0]];
const vB = this.vertices[face[1]];
const vC = this.vertices[face[2]];
const ab = p5.Vector.sub(vB, vA);
const ac = p5.Vector.sub(vC, vA);
const n = p5.Vector.cross(ab, ac);
const ln = p5.Vector.mag(n);
let sinAlpha = ln / (p5.Vector.mag(ab) * p5.Vector.mag(ac));
if (sinAlpha === 0 || isNaN(sinAlpha)) {
console.warn(
'p5.Geometry.prototype._getFaceNormal:',
'face has colinear sides or a repeated vertex'
);
return n;
}
if (sinAlpha > 1) sinAlpha = 1; // handle float rounding error
return n.mult(Math.asin(sinAlpha) / ln);
};
/**
* computes smooth normals per vertex as an average of each
* face.
* @method computeNormals
* @chainable
*/
p5.Geometry.prototype.computeNormals = function() {
const vertexNormals = this.vertexNormals;
const vertices = this.vertices;
const faces = this.faces;
let iv;
// initialize the vertexNormals array with empty vectors
vertexNormals.length = 0;
for (iv = 0; iv < vertices.length; ++iv) {
vertexNormals.push(new p5.Vector());
}
// loop through all the faces adding its normal to the normal
// of each of its vertices
for (let f = 0; f < faces.length; ++f) {
const face = faces[f];
const faceNormal = this._getFaceNormal(f);
// all three vertices get the normal added
for (let fv = 0; fv < 3; ++fv) {
const vertexIndex = face[fv];
vertexNormals[vertexIndex].add(faceNormal);
}
}
// normalize the normals
for (iv = 0; iv < vertices.length; ++iv) {
vertexNormals[iv].normalize();
}
return this;
};
/**
* Averages the vertex normals. Used in curved
* surfaces
* @method averageNormals
* @chainable
*/
p5.Geometry.prototype.averageNormals = function() {
for (let i = 0; i <= this.detailY; i++) {
const offset = this.detailX + 1;
let temp = p5.Vector.add(
this.vertexNormals[i * offset],
this.vertexNormals[i * offset + this.detailX]
);
temp = p5.Vector.div(temp, 2);
this.vertexNormals[i * offset] = temp;
this.vertexNormals[i * offset + this.detailX] = temp;
}
return this;
};
/**
* Averages pole normals. Used in spherical primitives
* @method averagePoleNormals
* @chainable
*/
p5.Geometry.prototype.averagePoleNormals = function() {
//average the north pole
let sum = new p5.Vector(0, 0, 0);
for (let i = 0; i < this.detailX; i++) {
sum.add(this.vertexNormals[i]);
}
sum = p5.Vector.div(sum, this.detailX);
for (let i = 0; i < this.detailX; i++) {
this.vertexNormals[i] = sum;
}
//average the south pole
sum = new p5.Vector(0, 0, 0);
for (
let i = this.vertices.length - 1;
i > this.vertices.length - 1 - this.detailX;
i--
) {
sum.add(this.vertexNormals[i]);
}
sum = p5.Vector.div(sum, this.detailX);
for (
let i = this.vertices.length - 1;
i > this.vertices.length - 1 - this.detailX;
i--
) {
this.vertexNormals[i] = sum;
}
return this;
};
/**
* Create a 2D array for establishing stroke connections
* @private
* @chainable
*/
p5.Geometry.prototype._makeTriangleEdges = function() {
this.edges.length = 0;
if (Array.isArray(this.strokeIndices)) {
for (let i = 0, max = this.strokeIndices.length; i < max; i++) {
this.edges.push(this.strokeIndices[i]);
}
} else {
for (let j = 0; j < this.faces.length; j++) {
this.edges.push([this.faces[j][0], this.faces[j][1]]);
this.edges.push([this.faces[j][1], this.faces[j][2]]);
this.edges.push([this.faces[j][2], this.faces[j][0]]);
}
}
return this;
};
/**
* Create 4 vertices for each stroke line, two at the beginning position
* and two at the end position. These vertices are displaced relative to
* that line's normal on the GPU
* @private
* @chainable
*/
p5.Geometry.prototype._edgesToVertices = function() {
this.lineVertices.length = 0;
this.lineNormals.length = 0;
for (let i = 0; i < this.edges.length; i++) {
const begin = this.vertices[this.edges[i][0]];
const end = this.vertices[this.edges[i][1]];
const dir = end
.copy()
.sub(begin)
.normalize();
const a = begin.array();
const b = begin.array();
const c = end.array();
const d = end.array();
const dirAdd = dir.array();
const dirSub = dir.array();
// below is used to displace the pair of vertices at beginning and end
// in opposite directions
dirAdd.push(1);
dirSub.push(-1);
this.lineNormals.push(dirAdd, dirSub, dirAdd, dirAdd, dirSub, dirSub);
this.lineVertices.push(a, b, c, c, b, d);
}
return this;
};
/**
* Modifies all vertices to be centered within the range -100 to 100.
* @method normalize
* @chainable
*/
p5.Geometry.prototype.normalize = function() {
if (this.vertices.length > 0) {
// Find the corners of our bounding box
const maxPosition = this.vertices[0].copy();
const minPosition = this.vertices[0].copy();
for (let i = 0; i < this.vertices.length; i++) {
maxPosition.x = Math.max(maxPosition.x, this.vertices[i].x);
minPosition.x = Math.min(minPosition.x, this.vertices[i].x);
maxPosition.y = Math.max(maxPosition.y, this.vertices[i].y);
minPosition.y = Math.min(minPosition.y, this.vertices[i].y);
maxPosition.z = Math.max(maxPosition.z, this.vertices[i].z);
minPosition.z = Math.min(minPosition.z, this.vertices[i].z);
}
const center = p5.Vector.lerp(maxPosition, minPosition, 0.5);
const dist = p5.Vector.sub(maxPosition, minPosition);
const longestDist = Math.max(Math.max(dist.x, dist.y), dist.z);
const scale = 200 / longestDist;
for (let i = 0; i < this.vertices.length; i++) {
this.vertices[i].sub(center);
this.vertices[i].mult(scale);
}
}
return this;
};
export default p5.Geometry;