-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sphere.js
42 lines (33 loc) · 1.35 KB
/
Sphere.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
class Sphere extends Model{
static get DIVISION() {return 10;}
constructor(color) {
super(color);
if (color == null) {
color = [1, 1, 1, 1];
}
this.vertices = this.initVertices(color);
}
initVertices(color) {
let vertices = [];
let d = Math.PI / Sphere.DIVISION;
for (let t = 0; t < Math.PI; t += d) {
for (let r = 0; r < 2 * Math.PI; r += d) {
let p1 = [Math.sin(t) * Math.cos(r), Math.sin(t) * Math.sin(r), Math.cos(t)];
let p2 = [Math.sin(t + d) * Math.cos(r), Math.sin(t + d) * Math.sin(r), Math.cos(t + d)];
let p3 = [Math.sin(t) * Math.cos(r + d), Math.sin(t) * Math.sin(r + d), Math.cos(t)];
let p4 = [Math.sin(t + d) * Math.cos(r + d), Math.sin(t + d) * Math.sin(r + d), Math.cos(t + d)];
let uv1 = [t/Math.PI, r/(2*Math.PI)];
let uv2 = [(t+d)/Math.PI, r/(2*Math.PI)];
let uv3 = [t/Math.PI, (r+d)/(2*Math.PI)];
let uv4 = [(t+d)/Math.PI, (r+d)/(2*Math.PI)];
vertices = vertices.concat(p1, uv1, color, p1);
vertices = vertices.concat(p2, uv2, color, p2);
vertices = vertices.concat(p4, uv4, color, p4);
vertices = vertices.concat(p1, uv1, color, p1);
vertices = vertices.concat(p4, uv4, color, p4);
vertices = vertices.concat(p3, uv3, color, p3);
}
}
return new Float32Array(vertices);
}
}