-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgl-globe.js
More file actions
229 lines (193 loc) · 7.39 KB
/
webgl-globe.js
File metadata and controls
229 lines (193 loc) · 7.39 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
class WebglGlobeElement extends HTMLElement {
constructor() {
super()
}
static get observedAttributes() {
return []
}
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
default:
this.render()
}
}
async connectedCallback() {
this.scene = new THREE.Scene()
this.camera = new THREE.PerspectiveCamera(45, this.clientWidth / this.clientHeight, 0.01, 1000)
this.camera.position.z = 3
this.renderer = new THREE.WebGLRenderer({alpha: true})
this.renderer.setSize(this.clientWidth, this.clientHeight)
this.appendChild(this.renderer.domElement)
this.sphere = this.getSphere(0.998)
this.circlesOfLatitude = []
for (let latitude = -75; latitude <= 75; latitude += 15) {
this.circlesOfLatitude.push(this.getCircleOfLatitude(latitude))
}
this.circlesOfLongitude = []
for (let longitude = 0; longitude < 180; longitude += 15) {
this.circlesOfLongitude.push(this.getCircleOfLongitude(longitude))
}
this.geojson = await this.getGeoJSON('countries.json')
this.glow = new THREE.AmbientLight(0xffffff, 1)
this.sun = new THREE.DirectionalLight(0xffffff, 1)
this.sun.position.set(5, 3, 5)
this.scene.add(this.sphere)
this.circlesOfLatitude.forEach(curve => this.scene.add(curve))
this.circlesOfLongitude.forEach(curve => this.scene.add(curve))
this.geojson.forEach(line => this.scene.add(line))
this.scene.add(this.glow)
this.scene.add(this.sun)
this.resizeObserver = new ResizeObserver(entries => {
this.camera = new THREE.PerspectiveCamera(45, this.clientWidth / this.clientHeight, 0.01, 1000)
this.camera.position.z = 3
this.renderer.setSize(this.clientWidth, this.clientHeight)
})
this.resizeObserver.observe(this)
this.render()
}
disconnectedCallback() {
this.scene = null
this.camera = null
this.renderer = null
this.sphere = null
this.circlesOfLatitude = null
this.circlesOfLongitude = null
this.geojson = null
this.glow = null
this.sun = null
this.resizeObserver.unobserve(this)
this.resizeObserver = null
this.innerHTML = ''
}
getSphere(radius) {
const geometry = new THREE.SphereGeometry(radius, 48, 24)
const material = new THREE.MeshLambertMaterial({color: 0xFFFFFF})
return new THREE.Mesh(geometry, material)
}
getCircleOfLatitude(latitude) {
// angle in radians
const angle = latitude * Math.PI / 180
// calculate radius from angle
const radius = Math.cos(angle)
// circle in same plane as prime meridian
const curve = new THREE.EllipseCurve(0, 0, radius, radius)
const points = curve.getSpacedPoints(48)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({color: 0xEEEEEE, linewidth: 3})
const line = new THREE.Line(geometry, material)
// rotate circle to be in same plane as equator
const quaternion = new THREE.Quaternion()
line.rotation.x += Math.PI / 2
// translate circle towards north or south pole
line.position.y += Math.sin(angle)
return line
}
getCircleOfLongitude(longitude) {
// angle in radians
const angle = longitude * Math.PI / 180
// great circle of 0 degrees (prime meridian) and 180 degrees
const curve = new THREE.EllipseCurve()
const points = curve.getSpacedPoints(48)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({color: 0xEEEEEE, linewidth: 3})
const line = new THREE.Line(geometry, material)
// rotate great circle about poles
line.rotation.y += angle
return line
}
async getGeoJSON(url) {
const lines = []
const response = await fetch(url)
const geojson = await response.json()
switch (geojson.type) {
case 'FeatureCollection':
geojson.features.forEach(feature => lines.push.apply(lines, this.getGeoJSONFeature(feature)))
break
}
return lines
}
/**
* @typedef {[Number, Number]} Coordinates
* @param {Object} feature
* @param {Object} feature.geometry
* @param {String} feature.geometry.type
* @param {Coordinates[]|Coordinates[][]} feature.geometry.coordinates
* @returns {THREE.Object3D[]}
*/
getGeoJSONFeature(feature) {
const objects = []
let points, geometry, material
switch (feature.geometry.type) {
case 'LineString':
points = feature.geometry.coordinates.map(this.getUnitSphereCoordinates)
geometry = new THREE.BufferGeometry().setFromPoints(points)
material = new THREE.LineBasicMaterial({color: 0x888888, linewidth: 3})
objects.push(new THREE.Line(geometry, material))
break
case 'Point':
points = [this.getUnitSphereCoordinates(feature.geometry.coordinates)]
geometry = new THREE.BufferGeometry().setFromPoints(points)
material = new THREE.PointsMaterial({color: 0x888888, size: 3})
objects.push(new THREE.Points(geometry, material))
break
case 'Polygon':
case 'MultiLineString':
feature.geometry.coordinates.forEach(coordinates => {
const points = coordinates.map(this.getUnitSphereCoordinates)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({color: 0x888888, linewidth: 3})
objects.push(new THREE.Line(geometry, material))
})
break
case 'MultiPoint':
points = feature.geometry.coordinates.map(this.getUnitSphereCoordinates)
geometry = new THREE.BufferGeometry().setFromPoints(points)
material = new THREE.PointsMaterial({color: 0x888888, size: 3})
objects.push(new THREE.Points(geometry, material))
break
case 'MultiPolygon':
feature.geometry.coordinates.forEach(polygonCoordinates => {
polygonCoordinates.forEach(coordinates => {
const points = coordinates.map(this.getUnitSphereCoordinates)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({color: 0x888888, linewidth: 3})
objects.push(new THREE.Line(geometry, material))
})
})
break
case 'GeometryCollection':
feature.geometry.geometries.forEach(geometry => {
objects.push.apply(objects, this.getGeoJSONFeature({geometry}))
})
break
}
return objects
}
/**
* @param {Number} longitude
* @param {Number} latitude
* @returns {THREE.Vector3}
*/
getUnitSphereCoordinates([longitude, latitude]) {
longitude *= Math.PI / 180
latitude *= Math.PI / 180
// positive x-axis intersects prime meridian and equator (0°E 0°N)
const x = Math.cos(latitude) * Math.cos(longitude)
// positive y-axis intersects north pole (0°E 90°N)
const y = Math.sin(latitude)
// positive z-axis intersects 90°W and equator (90°W 0°N)
const z = -Math.cos(latitude) * Math.sin(longitude)
return new THREE.Vector3(x, y, z)
}
render() {
// animate globe
requestAnimationFrame(this.render.bind(this))
// rotate around poles
this.circlesOfLongitude.forEach(curve => curve.rotation.y += 0.01)
this.geojson.forEach(line => line.rotation.y += 0.01)
this.renderer.render(this.scene, this.camera)
}
}
if (!window.customElements.get('webgl-globe')) {
window.customElements.define('webgl-globe', WebglGlobeElement)
}