-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprimitive.js
172 lines (160 loc) · 5.94 KB
/
primitive.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
PGGL.createSphereVertices = function(radius, subAlpha, subTheta) {
var numVertices = (subAlpha + 1) * (subTheta + 1);
var positions = new Float32Array(3 * numVertices);
var normals = new Float32Array(3 * numVertices);
var texcoords = new Float32Array(3 * numVertices);
asignPush(positions);
asignPush(normals);
asignPush(texcoords);
for (var y = 0; y <= subTheta; y++) {
for(var x = 0; x <= subAlpha; x ++) {
var u = x / subAlpha;
var v = y / subTheta;
var alpha = 2 * Math.PI * u;
var theta = Math.PI * v;
var sinAlpha = Math.sin(alpha);
var cosAlpha = Math.cos(alpha);
var sinTheta = Math.sin(theta);
var cosTheta = Math.cos(theta);
var ux = cosAlpha * sinTheta;
var uy = cosTheta;
var uz = sinAlpha * sinTheta;
positions.push(radius * ux, radius * uy, radius * uz);
normals.push(ux, uy, uz);
texcoords.push(1 - u, v);
}
}
var numVertAlpha = subAlpha + 1;
var indices = new Uint16Array(6 * subAlpha * subTheta);
asignPush(indices);
for(var x = 0; x < subAlpha; x++) {
for(var y = 0; y < subTheta; y++) {
indices.push(
(y + 0) * numVertAlpha + x,
(y + 0) * numVertAlpha + x + 1,
(y + 1) * numVertAlpha + x
);
indices.push(
(y + 1) * numVertAlpha + x,
(y + 0) * numVertAlpha + x + 1,
(y + 1) * numVertAlpha + x + 1
);
}
}
return {
position: positions,
normal: normals,
texcoord: texcoords,
indices: indices
};
}
function asignPush(array) {
var cursor = 0;
array.push = function () {
for (var i = 0; i < arguments.length; ++i) {
var value = arguments[i];
if (value instanceof Array) {
for (var j = 0; j < value.length; j++) {
array[cursor++] = value[j];
}
}
else {
array[cursor++] = value;
}
}
};
}
PGGL.geojsonProvider = function(url) {
return fetch(url).then(function(response) {
return response.json();
}).then(function(json) {
var features = json.features;
var featuresLength = features.length;
var i;
var polygons = [];
for(i = 0; i < featuresLength; i++) {
var feature = features[i];
if(feature.geometry.type === 'Polygon') {
var polygon = feature.geometry.coordinates[0];
var length = polygon.length;
if(length < 3) {return;}
if(polygon[0][0] === polygon[length - 1][0] && polygon[0][1] === polygon[length - 1][1]) {
polygon.pop();
if(polygon.length < 3) {return;}
}
polygons.push(polygon);
}
else if (feature.geometry.type === 'MultiPolygon') {
feature.geometry.coordinates.forEach(function(polygon) {
polygon = polygon[0];
if(!polygon || !polygon instanceof Array) {return;}
var length = polygon.length;
if(length < 3) {return;}
if(polygon[0] && polygon[length - 1] && polygon[0][0] === polygon[length - 1][0] && polygon[0][1] === polygon[length - 1][1]) {
polygon.pop();
if(polygon.length < 3) {return;}
}
polygons.push(polygon);
});
}
}
var polygonCount = polygons.length;
var vertexCount = 0;
var indexCount = 0;
for(i = 0; i < polygonCount; i++) {
vertexCount += polygons[i].length;
indexCount += 3 * (polygons[i].length - 2);
}
var positions = new Float32Array(2 * vertexCount);
var colors = new Float32Array(3 * vertexCount)
var independColors = new Float32Array(3 * vertexCount)
var indices = new Uint16Array(indexCount);
var boundaryIndices = new Uint16Array(2 * vertexCount);
asignPush(positions);
asignPush(colors);
asignPush(independColors);
asignPush(indices);
asignPush(boundaryIndices);
var cursor = 0;
var getColorWithHue = function(hue, percentage) {
var u = percentage;
var h = (360 + hue + (Math.abs(u - 0.5) * 150)) % 360;
var s = Math.sin(u * Math.PI * 2) * 0.25 + 0.75;
var v = 1.0;
var color = chroma.hsv(h, s, v).gl();
color.pop();
return color;
}
var hue = Math.random() * 360;
for(i = 0; i < polygonCount; i++) {
var color = getColorWithHue(hue, i / polygonCount);
for(var j = 0; j < polygons[i].length; j++) {
colors.push(color);
var newColor = getColorWithHue((360 + hue + (Math.abs(i / polygonCount - 0.5) * 100)) % 360, j / polygons[i].length);
independColors.push(newColor);
}
var polygon = [].concat.apply([], polygons[i]);
positions.push(polygon);
var index = earcut(polygon);
index.forEach(function(value, i) {
index[i] += cursor;
});
indices.push(index);
var vCount = polygons[i].length;
for(var l = 0; l < vCount; l++) {
var next = (l == vCount - 1) ? cursor : cursor + l + 1;
boundaryIndices.push(cursor + l, next);
}
cursor += polygons[i].length;
}
return {
position: positions,
color: colors,
independColors: independColors,
indices: indices,
boundaryIndices: boundaryIndices
}
}).catch(function(err) {
console.error(err);
});
}