-
Notifications
You must be signed in to change notification settings - Fork 4
/
makeball.js
72 lines (64 loc) · 2.09 KB
/
makeball.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
function makeBall(){
radius = Math.pow(2, .5)
lats = 11
longs = 17
var geometryData = [];
var normalData = [];
var texCoordData = [];
var indexData = [];
for (var latNumber = 0; latNumber <= lats; ++latNumber) {
for (var longNumber = 0; longNumber <= longs; ++longNumber) {
var theta = latNumber * Math.PI / lats;
var phi = longNumber * 2 * Math.PI / longs;
var sinTheta = Math.sin(theta);
var sinPhi = Math.sin(phi);
var cosTheta = Math.cos(theta);
var cosPhi = Math.cos(phi);
var x = cosPhi * sinTheta;
var y = cosTheta;
var z = sinPhi * sinTheta;
var u = 1 - (longNumber / longs);
var v = latNumber / lats;
normalData.push(x);
normalData.push(y);
normalData.push(z);
// debug("normal, "+x+","+y+","+z+"<br>")
texCoordData.push(u);
texCoordData.push(v);
geometryData.push(radius * x);
geometryData.push(radius * y);
geometryData.push(radius * z);
}
}
for (var latNumber = 0; latNumber < lats; ++latNumber) {
for (var longNumber = 0; longNumber < longs; ++longNumber) {
var first = (latNumber * (longs + 1)) + longNumber;
var second = first + longs + 1;
indexData.push(first);
indexData.push(second);
indexData.push(first + 1);
indexData.push(second);
indexData.push(second + 1);
indexData.push(first + 1);
}
}
var makeLine = function(type, buffer, intflag) {
var s = "<" + type + ">"
for (var i in buffer) {
var p = buffer[i]
if (intflag) {
s += p
} else {
s += p.toFixed(5)
}
if (i!=buffer.length-1) s += ","
}
s += "</" + type + ">"
return s
}
print (makeLine("positions", geometryData))
print (makeLine("normals", normalData))
print (makeLine("uv1", texCoordData))
print (makeLine("faces", indexData, true))
}
makeBall()