-
Notifications
You must be signed in to change notification settings - Fork 140
/
globe.js
executable file
·147 lines (127 loc) · 4.58 KB
/
globe.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
import {
Color,
LineBasicMaterial,
LineSegments,
Mesh,
MeshPhongMaterial,
SphereGeometry,
TextureLoader
} from 'three';
const THREE = window.THREE
? window.THREE // Prefer consumption from global THREE, if exists
: {
Color,
LineBasicMaterial,
LineSegments,
Mesh,
MeshPhongMaterial,
SphereGeometry,
TextureLoader
};
import { GeoJsonGeometry } from 'three-geojson-geometry';
import { createGlowMesh } from '../utils/three-glow-mesh';
import Kapsule from 'kapsule';
import { geoGraticule10 } from 'd3-geo';
import { emptyObject } from '../utils/gc';
import { GLOBE_RADIUS } from '../constants';
//
export default Kapsule({
props: {
globeImageUrl: {},
bumpImageUrl: {},
showGlobe: { default: true, onChange(showGlobe, state) { state.globeObj.visible = !!showGlobe }, triggerUpdate: false },
showGraticules: { default: false, onChange(showGraticules, state) { state.graticulesObj.visible = !!showGraticules }, triggerUpdate: false },
showAtmosphere: { default: true, onChange(showAtmosphere, state) { state.atmosphereObj && (state.atmosphereObj.visible = !!showAtmosphere) }, triggerUpdate: false },
atmosphereColor: { default: 'lightskyblue' },
atmosphereAltitude: { default: 0.15 },
onReady: { default: () => {}, triggerUpdate: false }
},
methods: {
globeMaterial: function(state, globeMaterial) {
if (globeMaterial !== undefined) {
state.globeObj.material = globeMaterial || state.defaultGlobeMaterial;
return this;
}
return state.globeObj.material;
}
},
stateInit: () => {
// create globe
const globeGeometry = new THREE.SphereGeometry(GLOBE_RADIUS, 75, 75);
const defaultGlobeMaterial = new THREE.MeshPhongMaterial({ color: 0x000000, transparent: true });
const globeObj = new THREE.Mesh(globeGeometry, defaultGlobeMaterial);
globeObj.rotation.y = -Math.PI / 2; // face prime meridian along Z axis
globeObj.__globeObjType = 'globe'; // Add object type
// create graticules
const graticulesObj = new THREE.LineSegments(
new GeoJsonGeometry(geoGraticule10(), GLOBE_RADIUS, 2),
new THREE.LineBasicMaterial({ color: 'lightgrey', transparent: true, opacity: 0.1 })
);
return {
globeObj,
graticulesObj,
defaultGlobeMaterial
}
},
init(threeObj, state) {
// Clear the scene
emptyObject(threeObj);
// Main three object to manipulate
state.scene = threeObj;
state.scene.add(state.globeObj); // add globe
state.scene.add(state.graticulesObj); // add graticules
state.ready = false;
},
update(state, changedProps) {
const globeMaterial = state.globeObj.material;
if (changedProps.hasOwnProperty('globeImageUrl')) {
if (!state.globeImageUrl) {
// Black globe if no image
!globeMaterial.color && (globeMaterial.color = new THREE.Color(0x000000));
} else {
new THREE.TextureLoader().load(state.globeImageUrl, texture => {
globeMaterial.map = texture;
globeMaterial.color = null;
globeMaterial.needsUpdate = true;
// ready when first globe image finishes loading (asynchronously to allow 1 frame to load texture)
!state.ready && (state.ready = true) && setTimeout(state.onReady);
});
}
}
if (changedProps.hasOwnProperty('bumpImageUrl')) {
if (!state.bumpImageUrl) {
globeMaterial.bumpMap = null;
globeMaterial.needsUpdate = true;
} else {
state.bumpImageUrl && new THREE.TextureLoader().load(state.bumpImageUrl, texture => {
globeMaterial.bumpMap = texture;
globeMaterial.needsUpdate = true;
});
}
}
if (changedProps.hasOwnProperty('atmosphereColor') || changedProps.hasOwnProperty('atmosphereAltitude')) {
if (state.atmosphereObj) {
// recycle previous atmosphere object
state.scene.remove(state.atmosphereObj);
emptyObject(state.atmosphereObj);
}
if (state.atmosphereColor && state.atmosphereAltitude) {
const obj = state.atmosphereObj = createGlowMesh(state.globeObj.geometry, {
backside: true,
color: state.atmosphereColor,
size: GLOBE_RADIUS * state.atmosphereAltitude,
power: 3.5, // dispersion
coefficient: 0.1
});
obj.visible = !!state.showAtmosphere;
obj.__globeObjType = 'atmosphere'; // Add object type
state.scene.add(obj);
}
}
if (!state.ready && !state.globeImageUrl) {
// ready immediately if there's no globe image
state.ready = true;
state.onReady();
}
}
});