-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes.js
More file actions
345 lines (297 loc) · 14.3 KB
/
Copy pathshapes.js
File metadata and controls
345 lines (297 loc) · 14.3 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
window.Square = window.classes.Square = class Square extends Shape {
constructor() {
super("positions", "normals", "texture_coords");
this.positions.push( ...Vec.cast([-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, 1, 0] ));
this.normals.push( ...Vec.cast([ 0, 0, 1], [0, 0, 1], [ 0, 0, 1], [0, 0, 1] ));
this.texture_coords.push(...Vec.cast([ 0, 0], [1, 0], [ 0, 1], [1, 1] ));
this.indices.push(0, 1, 2, 1, 3, 2);
}
}
window.Circle = window.classes.Circle = class Circle extends Shape {
constructor(sections) {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast([0, 0, 0], [1, 0, 0]));
this.normals.push(...Vec.cast( [0, 0, 1], [0, 0, 1]));
this.texture_coords.push(...Vec.cast([0.5, 0.5], [1, 0.5]));
for (let i = 0; i < sections; ++i) {
const angle = 2 * Math.PI * (i + 1) / sections,
v = Vec.of(Math.cos(angle), Math.sin(angle)),
id = i + 2;
this.positions.push(...Vec.cast([v[0], v[1], 0]));
this.normals.push(...Vec.cast( [0, 0, 1]));
this.texture_coords.push(...Vec.cast([(v[0] + 1) / 2, (v[1] + 1) / 2]));
this.indices.push(
0, id - 1, id);
}
}
}
window.Cube = window.classes.Cube = class Cube extends Shape {
constructor() {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast(
[-1, 1, -1], [-1, -1, -1], [ 1, 1, -1], [ 1, -1, -1],
[-1, -1, 1], [ 1, -1, 1], [-1, 1, 1], [ 1, 1, 1],
[-1, 1, 1], [ 1, 1, 1], [-1, 1, -1], [ 1, 1, -1],
[-1, -1, -1], [ 1, -1, -1], [-1, -1, 1], [ 1, -1, 1],
[-1, -1, -1], [-1, -1, 1], [-1, 1, -1], [-1, 1, 1],
[ 1, -1, -1], [ 1, -1, 1], [ 1, 1, -1], [ 1, 1, 1]
));
this.texture_coords.push(...Vec.cast(
[0, 2/3], [0.25, 2/3], [0, 1/3], [0.25, 1/3],
[0.5, 2/3], [0.5, 1/3], [0.75, 2/3], [0.75, 1/3],
[0.75, 2/3], [0.75, 1/3], [1, 2/3], [1, 1/3],
[0.25, 2/3], [0.25, 1/3], [0.5, 2/3], [0.5, 1/3],
[0.25, 2/3], [0.5, 2/3], [0.25, 1 ], [0.5, 1 ],
[0.25, 1/3], [0.5, 1/3], [0.25, 0 ], [0.5, 0 ]
));
this.normals.push(...Vec.cast(
...Array(4).fill([ 0, 0, -1]),
...Array(4).fill([ 0, 0, 1]),
...Array(4).fill([ 0, 1, 0]),
...Array(4).fill([ 0, -1, 0]),
...Array(4).fill([-1, 0, 0]),
...Array(4).fill([ 1, 0, 0])
));
this.indices.push(
0, 2, 1, 1, 2, 3,
4, 5, 6, 5, 7, 6,
8, 9, 10, 9, 11, 10,
12, 13, 14, 13, 15, 14,
16, 19, 18, 16, 17, 19,
20, 22, 21, 21, 22, 23
);
}
}
window.TriangularPrism = window.classes.TriangularPrism = class TriangularPrism extends Shape {
constructor() {
// Name the values we'll define per each vertex.
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast(
[0, 0, 1], [ 0, 1, 1], [1, 0, 1],
[0, 0, -1], [ 0, 1, -1], [1, 0, -1],
[0, 0, 1], [ 0, 0, -1], [1, 0, -1], [1, 0, 1],
[0, 0, 1], [ 0, 0, -1], [0, 1, -1], [0, 1, 1],
[1, 0, 1], [ 1, 0, -1], [0, 1, -1], [0, 1, 1]));
// Supply vectors that point away from eace face of the cube. They should match up with the points in the above list
// Normal vectors are needed so the graphics engine can know if the shape is pointed at light or not, and color it accordingly.
const r2 = Math.sqrt(2);
this.normals.push(...Vec.cast(
[ 0, 0, 1], [ 0, 0, 1], [ 0, 0, 1],
[ 0, 0, -1], [ 0, 0, -1], [ 0, 0, -1],
[ 0, -1, 0], [ 0, -1, 0], [ 0, -1, 0], [ 0, -1, 0],
[-1, 0, 0], [-1, 0, 0], [-1, 0, 0], [-1, 0, 0],
[r2, r2, 0], [r2, r2, 0], [r2, r2, 0], [r2, r2, 0]));
// Those two lists, positions and normals, fully describe the "vertices". What's the "i"th vertex? Simply the combined
// data you get if you look up index "i" of both lists above -- a position and a normal vector, together. Now let's
// tell it how to connect vertex entries into triangles. Every three indices in this list makes one triangle:
this.indices.push(
0, 1, 2,
3, 4, 5,
6, 7, 8, 6, 8, 9,
10, 11, 12, 10, 13, 12,
14, 15, 16, 14, 17, 16);
}
}
window.SimpleCube = window.classes.SimpleCube = class SimpleCube extends Shape {
constructor() {
super( "positions", "normals", "texture_coords" );
for( var i = 0; i < 3; i++ )
for( var j = 0; j < 2; j++ ) {
var square_transform = Mat4.rotation( i == 0 ? Math.PI/2 : 0, Vec.of(1, 0, 0) )
.times( Mat4.rotation( Math.PI * j - ( i == 1 ? Math.PI/2 : 0 ), Vec.of( 0, 1, 0 ) ) )
.times( Mat4.translation([ 0, 0, 1 ]) );
Square.insert_transformed_copy_into( this, [], square_transform );
}
}
}
window.Tetrahedron = window.classes.Tetrahedron = class Tetrahedron extends Shape {
constructor(using_flat_shading) {
super("positions", "normals", "texture_coords");
const s3 = Math.sqrt(3) / 4,
v1 = Vec.of(Math.sqrt(8/9), -1/3, 0),
v2 = Vec.of(-Math.sqrt(2/9), -1/3, Math.sqrt(2/3)),
v3 = Vec.of(-Math.sqrt(2/9), -1/3, -Math.sqrt(2/3)),
v4 = Vec.of(0, 1, 0);
this.positions.push(...Vec.cast(
v1, v2, v3,
v1, v3, v4,
v1, v2, v4,
v2, v3, v4));
this.normals.push(...Vec.cast(
...Array(3).fill(v1.plus(v2).plus(v3).normalized()),
...Array(3).fill(v1.plus(v3).plus(v4).normalized()),
...Array(3).fill(v1.plus(v2).plus(v4).normalized()),
...Array(3).fill(v2.plus(v3).plus(v4).normalized())));
this.texture_coords.push(...Vec.cast(
[0.25, s3], [0.75, s3], [0.5, 0],
[0.25, s3], [0.5, 0 ], [0, 0],
[0.25, s3], [0.75, s3], [0.5, 2 * s3],
[0.75, s3], [0.5, 0 ], [1, 0]));
this.indices.push(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
}
}
//Our special shape is a circular trapeziod that has a flat shaded seam on roof
window.myShape = window.classes.myShape = class myShape extends Shape {
constructor(sections, radius) {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast([1, 0, 0]));
this.normals.push(...Vec.cast( [0, 0, 1]));
this.texture_coords.push(...Vec.cast([1, 0.5]));
var t;
var num;
for (let i = 0; i < sections; ++i) {
const angle = 2*Math.PI * (i + 1) / sections,
v = Vec.of(Math.cos(angle), Math.sin(angle), 0),
id = 2 * i + 1;
t = Vec.of(Math.cos(angle)/1.5, Math.sin(angle)/1.5, 1);
if(i < sections - 1)
{
num = i * 2 + 1;
}
this.positions.push(...Vec.cast(t, v));
this.normals.push(...Vec.cast( [0, 0, 1], [0 , 0, -1]));
this.normals.push(...Vec.cast(
v.mix(this.positions[id - 1], 0.5).plus(t).normalized(),
v.plus(t).normalized()));
this.texture_coords.push(...Vec.cast([0.5, 0.5], [(v[0] + 1) / 2, (v[1] + 1) / 2]));
this.indices.push(
id - 1, id, id + 1,
num, num + 1, num + 2,
2 * sections - 1, 0, 1,
num, num + 2, 1,
num - 1, num + 1, 0);
}
}
}
//Special shape with no roof
window.myShapeTwo = window.classes.myShapeTwo = class myShapeTwo extends Shape {
constructor(sections, radius) {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast([1, 0, 0]));
this.normals.push(...Vec.cast( [0, 0, 1]));
this.texture_coords.push(...Vec.cast([1, 0.5]));
var t;
var num;
for (let i = 0; i < sections; ++i) {
const angle = 2*Math.PI * (i + 1) / sections,
v = Vec.of(Math.cos(angle), Math.sin(angle), 0),
id = 2 * i + 1;
t = Vec.of(Math.cos(angle)/1.5, Math.sin(angle)/1.5, 1);
if(i < sections - 1)
{
num = i * 2 + 1;
}
this.positions.push(...Vec.cast(t, v));
this.normals.push(...Vec.cast( [0, 0, 1], [0 , 0, -1]));
this.normals.push(...Vec.cast(
v.mix(this.positions[id - 1], 0.5).plus(t).normalized(),
v.plus(t).normalized()));
this.texture_coords.push(...Vec.cast([0.5, 0.5], [(v[0] + 1) / 2, (v[1] + 1) / 2]));
this.indices.push(
id - 1, id, id + 1,
num, num + 1, num + 2,
2 * sections - 1, 0, 1,
num - 1, num + 1, 0);
}
}
}
window.Cylinder = window.classes.Cylinder = class Cylinder extends Shape {
constructor(sections) {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast([1, 0, 1], [1, 0, -1]));
this.normals.push(...Vec.cast( [1, 0, 0], [1, 0, 0]));
this.texture_coords.push(...Vec.cast([0, 1], [0, 0]));
for (let i = 0; i < sections; ++i) {
const ratio = (i + 1) / sections,
angle = 2 * Math.PI * ratio,
v = Vec.of(Math.cos(angle), Math.sin(angle)),
id = 2 * i + 2;
this.positions.push(...Vec.cast([v[0], v[1], 1], [v[0], v[1], -1]));
this.normals.push(...Vec.cast( [v[0], v[1], 0], [v[0], v[1], 0]));
this.texture_coords.push(...Vec.cast([ratio, 1], [ratio, 0]));
this.indices.push(
id, id - 1, id + 1,
id, id - 1, id - 2);
}
}
}
window.Cone = window.classes.Cone = class Cone extends Shape {
constructor(sections) {
super("positions", "normals", "texture_coords");
this.positions.push(...Vec.cast([1, 0, 0]));
this.normals.push(...Vec.cast( [0, 0, 1]));
this.texture_coords.push(...Vec.cast([1, 0.5]));
let t = Vec.of(0, 0, 1);
for (let i = 0; i < sections; ++i) {
const angle = 2 * Math.PI * (i + 1) / sections,
v = Vec.of(Math.cos(angle), Math.sin(angle), 0),
id = 2 * i + 1;
this.positions.push(...Vec.cast(t, v));
this.normals.push(...Vec.cast(
v.mix(this.positions[id - 1], 0.5).plus(t).normalized(),
v.plus(t).normalized()));
this.texture_coords.push(...Vec.cast([0.5, 0.5], [(v[0] + 1) / 2, (v[1] + 1) / 2]));
this.indices.push(
id - 1, id, id + 1);
}
}
}
// This Shape defines a Sphere surface, with nice (mostly) uniform triangles. A subdivision surface
// (see) Wikipedia article on those) is initially simple, then builds itself into a more and more
// detailed shape of the same layout. Each act of subdivision makes it a better approximation of
// some desired mathematical surface by projecting each new point onto that surface's known
// implicit equation. For a sphere, we begin with a closed 3-simplex (a tetrahedron). For each
// face, connect the midpoints of each edge together to make more faces. Repeat recursively until
// the desired level of detail is obtained. Project all new vertices to unit vectors (onto the
// unit sphere) and group them into triangles by following the predictable pattern of the recursion.
window.Subdivision_Sphere = window.classes.Subdivision_Sphere = class Subdivision_Sphere extends Shape {
constructor(max_subdivisions) {
super("positions", "normals", "texture_coords");
// Start from the following equilateral tetrahedron:
this.positions.push(...Vec.cast([0, 0, -1], [0, .9428, .3333], [-.8165, -.4714, .3333], [.8165, -.4714, .3333]));
// Begin recursion.
this.subdivideTriangle(0, 1, 2, max_subdivisions);
this.subdivideTriangle(3, 2, 1, max_subdivisions);
this.subdivideTriangle(1, 0, 3, max_subdivisions);
this.subdivideTriangle(0, 2, 3, max_subdivisions);
for (let p of this.positions) {
this.normals.push(p.copy());
this.texture_coords.push(Vec.of(
0.5 + Math.atan2(p[2], p[0]) / (2 * Math.PI),
0.5 - Math.asin(p[1]) / Math.PI));
}
// Fix the UV seam by duplicating vertices with offset UV
let tex = this.texture_coords;
for (let i = 0; i < this.indices.length; i += 3) {
const a = this.indices[i], b = this.indices[i + 1], c = this.indices[i + 2];
if ([[a, b], [a, c], [b, c]].some(x => (Math.abs(tex[x[0]][0] - tex[x[1]][0]) > 0.5))
&& [a, b, c].some(x => tex[x][0] < 0.5))
{
for (let q of [[a, i], [b, i + 1], [c, i + 2]]) {
if (tex[q[0]][0] < 0.5) {
this.indices[q[1]] = this.positions.length;
this.positions.push(this.positions[q[0]].copy());
this.normals.push(this.normals[q[0]].copy());
tex.push(tex[q[0]].plus(Vec.of(1, 0)));
}
}
}
}
}
subdivideTriangle(a, b, c, count) {
if (count <= 0) {
this.indices.push(a, b, c);
return;
}
let ab_vert = this.positions[a].mix(this.positions[b], 0.5).normalized(),
ac_vert = this.positions[a].mix(this.positions[c], 0.5).normalized(),
bc_vert = this.positions[b].mix(this.positions[c], 0.5).normalized();
let ab = this.positions.push(ab_vert) - 1,
ac = this.positions.push(ac_vert) - 1,
bc = this.positions.push(bc_vert) - 1;
this.subdivideTriangle( a, ab, ac, count - 1);
this.subdivideTriangle(ab, b, bc, count - 1);
this.subdivideTriangle(ac, bc, c, count - 1);
this.subdivideTriangle(ab, bc, ac, count - 1);
}
}