-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
296 lines (237 loc) · 9.53 KB
/
script.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Scene, Camera, Renderer
let renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas') });
let scene = new THREE.Scene();
let aspect = window.innerWidth / window.innerHeight;
let camera = new THREE.PerspectiveCamera(45, aspect, 0.1, 1500);
let cameraRotation = 0;
let cameraRotationSpeed = 0.001;
let cameraAutoRotation = true;
let orbitControls = new THREE.OrbitControls(camera);
// Lights
let spotLight = new THREE.SpotLight(0xffffff, 1, 0, 10, 2);
// Texture Loader
let textureLoader = new THREE.TextureLoader();
// Planet Proto
let planetProto = {
sphere: function (size) {
let sphere = new THREE.SphereGeometry(size, 32, 32);
return sphere;
},
material: function (options) {
let material = new THREE.MeshPhongMaterial();
if (options) {
for (var property in options) {
material[property] = options[property];
}
}
return material;
},
glowMaterial: function (intensity, fade, color) {
// Custom glow shader from https://github.com/stemkoski/stemkoski.github.com/tree/master/Three.js
let glowMaterial = new THREE.ShaderMaterial({
uniforms: {
'c': {
type: 'f',
value: intensity },
'p': {
type: 'f',
value: fade },
glowColor: {
type: 'c',
value: new THREE.Color(color) },
viewVector: {
type: 'v3',
value: camera.position } },
vertexShader: `
uniform vec3 viewVector;
uniform float c;
uniform float p;
varying float intensity;
void main() {
vec3 vNormal = normalize( normalMatrix * normal );
vec3 vNormel = normalize( normalMatrix * viewVector );
intensity = pow( c - dot(vNormal, vNormel), p );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `
uniform vec3 glowColor;
varying float intensity;
void main()
{
vec3 glow = glowColor * intensity;
gl_FragColor = vec4( glow, 1.0 );
}`,
side: THREE.BackSide,
blending: THREE.AdditiveBlending,
transparent: true });
return glowMaterial;
},
texture: function (material, property, uri) {
let textureLoader = new THREE.TextureLoader();
textureLoader.crossOrigin = true;
textureLoader.load(
uri,
function (texture) {
material[property] = texture;
material.needsUpdate = true;
});
} };
let createPlanet = function (options) {
// Create the planet's Surface
let surfaceGeometry = planetProto.sphere(options.surface.size);
let surfaceMaterial = planetProto.material(options.surface.material);
let surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
// Create the planet's Atmosphere
let atmosphereGeometry = planetProto.sphere(options.surface.size + options.atmosphere.size);
let atmosphereMaterialDefaults = {
side: THREE.DoubleSide,
transparent: true };
let atmosphereMaterialOptions = Object.assign(atmosphereMaterialDefaults, options.atmosphere.material);
let atmosphereMaterial = planetProto.material(atmosphereMaterialOptions);
let atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
// Create the planet's Atmospheric glow
let atmosphericGlowGeometry = planetProto.sphere(options.surface.size + options.atmosphere.size + options.atmosphere.glow.size);
let atmosphericGlowMaterial = planetProto.glowMaterial(options.atmosphere.glow.intensity, options.atmosphere.glow.fade, options.atmosphere.glow.color);
let atmosphericGlow = new THREE.Mesh(atmosphericGlowGeometry, atmosphericGlowMaterial);
// Nest the planet's Surface and Atmosphere into a planet object
let planet = new THREE.Object3D();
surface.name = 'surface';
atmosphere.name = 'atmosphere';
atmosphericGlow.name = 'atmosphericGlow';
planet.add(surface);
planet.add(atmosphere);
planet.add(atmosphericGlow);
// Load the Surface's textures
for (let textureProperty in options.surface.textures) {
planetProto.texture(
surfaceMaterial,
textureProperty,
options.surface.textures[textureProperty]);
}
// Load the Atmosphere's texture
for (let textureProperty in options.atmosphere.textures) {
planetProto.texture(
atmosphereMaterial,
textureProperty,
options.atmosphere.textures[textureProperty]);
}
return planet;
};
let mars = createPlanet({
surface: {
size: 0.5,
material: {
bumpScale: 0.05,
specular: new THREE.Color('grey'),
shininess: 10 },
textures: {
map: './assets/map.jpg',
bumpMap: './assets/bump_map.png',
specularMap: './assets/rgb_sketchmap.jpg' } },
atmosphere: {
size: 0.003,
material: {
opacity: 0.8 },
textures: {
map: './assets/cloud_map.png',
alphaMap: './assets/cloud_map.png' },
glow: {
size: 0.02,
intensity: 0.7,
fade: 7,
color: 0x9C2E35 } } });
// Marker Proto
let markerProto = {
latLongToVector3: function latLongToVector3(latitude, longitude, radius, height) {
var phi = latitude * Math.PI / 180;
var theta = (longitude - 180) * Math.PI / 180;
var x = -(radius + height) * Math.cos(phi) * Math.cos(theta);
var y = (radius + height) * Math.sin(phi);
var z = (radius + height) * Math.cos(phi) * Math.sin(theta);
return new THREE.Vector3(x, y, z);
},
marker: function marker(size, color, vector3Position) {
let markerGeometry = new THREE.SphereGeometry(size);
let markerMaterial = new THREE.MeshLambertMaterial({
color: color });
let markerMesh = new THREE.Mesh(markerGeometry, markerMaterial);
markerMesh.position.copy(vector3Position);
return markerMesh;
} };
// Place Marker
let placeMarker = function (object, options) {
let position = markerProto.latLongToVector3(options.latitude, options.longitude, options.radius, options.height);
let marker = markerProto.marker(options.size, options.color, position);
object.add(marker);
};
// Place Marker At Address
let placeMarkerAtAddress = function (address, color) {
let encodedLocation = address.replace(/\s/g, '+');
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + encodedLocation);
httpRequest.send(null);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
let result = JSON.parse(httpRequest.responseText);
if (result.results.length > 0) {
let latitude = result.results[0].geometry.location.lat;
let longitude = result.results[0].geometry.location.lng;
placeMarker(mars.getObjectByName('surface'), {
latitude: latitude,
longitude: longitude,
radius: 0.5,
height: 0,
size: 0.01,
color: color });
}
}
};
};
// Galaxy
let galaxyGeometry = new THREE.SphereGeometry(100, 32, 32);
let galaxyMaterial = new THREE.MeshBasicMaterial({
side: THREE.BackSide });
let galaxy = new THREE.Mesh(galaxyGeometry, galaxyMaterial);
// Load Galaxy Textures
textureLoader.crossOrigin = true;
textureLoader.load(
'./assets/background.jpg',
function (texture) {
galaxyMaterial.map = texture;
scene.add(galaxy);
});
// Scene, Camera, Renderer Configuration
renderer.setSize(window.innerWidth, 800);
document.body.appendChild(renderer.domElement);
camera.position.set(1, 1, 1);
orbitControls.enabled = !cameraAutoRotation;
scene.add(camera);
scene.add(spotLight);
scene.add(mars);
// Light Configurations
spotLight.position.set(2, 0, 1);
// Mesh Configurations
mars.receiveShadow = true;
mars.castShadow = true;
mars.getObjectByName('surface').geometry.center();
// On window resize, adjust camera aspect ratio and renderer size
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Main render function
let render = function () {
mars.getObjectByName('surface').rotation.y += 1 / 32 * 0.01;
mars.getObjectByName('atmosphere').rotation.y += 1 / 16 * 0.01;
if (cameraAutoRotation) {
cameraRotation += cameraRotationSpeed;
camera.position.y = 0;
camera.position.x = 2 * Math.sin(cameraRotation);
camera.position.z = 2 * Math.cos(cameraRotation);
camera.lookAt(mars.position);
}
requestAnimationFrame(render);
renderer.render(scene, camera);
};
render();