-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere_latlong.js
executable file
·60 lines (55 loc) · 2.38 KB
/
sphere_latlong.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
///// CUBE DEFINTION
/////
///// Cube is defined to be centered at the origin of the coordinate reference system.
///// Cube size is assumed to be 2.0 x 2.0 x 2.0 .
function SphereLatLong (v, h) {
this.name = "sphere";
let pi = Math.PI;
let verticalLevels = v; // + 2 dots on top & bottom
let horizontalSegments = h;
var vertices = [];
var indices = [];
// Build circles
for (var v = 0; v<verticalLevels; v++){
// Why 1.5? I don't know, but 1 was too little and 2 was too much
var elevationAngle = pi/(verticalLevels+2)*(v+1.5)-pi/2.0;
let z = Math.sin(elevationAngle);
var horizontalRadius = Math.cos(elevationAngle);
// Build a circle in the x-y plane
for (var a = 0; a<horizontalSegments; a++){
let angle = 2*pi/horizontalSegments*a;
let x = Math.cos(angle)*horizontalRadius;
let y = Math.sin(angle)*horizontalRadius;
vertices = vertices.concat([x, y, z]);
}
// Link the circle to the previous one
if (v>0){
let levelStart = v * horizontalSegments;
let prevLevelStart = (v-1) * horizontalSegments;
// Link triangles
for (var i = 0; i<horizontalSegments-1; i++){
indices = indices.concat([levelStart+i+1, levelStart+i, prevLevelStart+i]);
indices = indices.concat([prevLevelStart+i, prevLevelStart+i+1, levelStart+i+1]);
}
indices = indices.concat([levelStart, levelStart+horizontalSegments-1, prevLevelStart+horizontalSegments-1]);
indices = indices.concat([prevLevelStart+horizontalSegments-1, prevLevelStart, levelStart]);
}
}
// Add pole vertices
vertices = vertices.concat([0, 0, -1]);
vertices = vertices.concat([0, 0, 1]);
let southPole = vertices.length/3-2;
let northPole = vertices.length/3-1;
// Link the first circle with the pole
let lastLevel = (verticalLevels-1)*horizontalSegments;
for (var i = 0; i<horizontalSegments-1; i++){
indices = indices.concat(i, i+1, southPole);
indices = indices.concat(lastLevel+i, lastLevel+i+1, northPole);
}
indices = indices.concat(0, horizontalSegments-1, southPole);
indices = indices.concat(lastLevel, northPole, lastLevel+horizontalSegments-1);
this.vertices = new Float32Array(vertices);
this.triangleIndices = new Uint16Array(indices);
this.numVertices = this.vertices.length/3;
this.numTriangles = this.triangleIndices.length/3;
}