forked from visualgoodness/Google-Snowview
/
skyboxWithParticles4.html
389 lines (264 loc) · 11 KB
/
skyboxWithParticles4.html
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!doctype html>
<html lang="en">
<head>
<title>Google Snowview prototype</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
a {
color:#0078ff;
}
</style>
</head>
<body>
<div id="map_canvas" style="width: 425px; height: 400px;float:left"></div>
<script src="js/Three.js"></script>
<script src="js/Snow.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script src="//maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script>
/* this was built quick and dirty... so forgive the sloppiness and keep an eye out for memory leaks etc */
var map;
//staring location
var startLoc = {lat:40.746895,lng:-73.991005}
var startMap = new google.maps.LatLng(startLoc.lat,startLoc.lng);
/*this is the dimensions for each face of the skybox. It is also the dimensions of the images that are pulled from the gmaps api. Larger dimesions will give a larger, higher res textures etc. I also noticed that the google watermark is the same size, regardless of the image dimensions, so if you pull down large enough images, the watermark appears pretty small and less noticeable. For eg. a 400x400 image will have a huge watermark compared to a 1200x1200 image */
var boxSize = 1200;
var container, stats;
var camera, scene, renderer, snowGroup, particle;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var sceneWidth = 500, sceneHeight = 400;
var snowflakeCount = 1500;
var texture_placeholder,
isUserInteracting = false,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
lon = 90, onMouseDownLon = 0,
lat = 0, onMouseDownLat = 0,
phi = 0, theta = 0,
target = new THREE.Vector3();
var particle;
var particles = [];
var pMaterial;
var initted = false;
var shaking = false;
init();
animate();
function init() {
//build google map
var mapOptions = {
center: startMap,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
// given radius is 50 meters or less.
google.maps.event.addListener(map, 'click', function(event) {
//user clicked the map and picked a location, build the skybox
initSkybox(event.latLng);
});
//end build map
//set up the threejs scene
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
scene = new THREE.Scene();
var PI2 = Math.PI * 2;
/*
Uncomment the line below to turn on webgl. It doesnt seem that it would be too far-fetched to create this with webgl and have it use the canvas
version as a fallback for unsupported browsers. Currently, enabling webgl will throw a browser security error (crossdoman silliness on the gmaps images... probably caused by threejs using getImageData and/or putImageData). But if you end up pulling the images from google to a backend for processing first, this shouldnt be an issue (in theory).
*/
//renderer = new THREE.WebGLRenderer();
renderer = new THREE.CanvasRenderer();
renderer.setSize( sceneWidth, sceneHeight );
container.appendChild( renderer.domElement );
snowGroup = new THREE.Object3D();
scene.add(snowGroup);
//stats
stats = new Stats();
stats.domElement.style.position = 'relative';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
initSkybox(startLoc);
initSnow();
}
function initSkybox(latLng){
try{scene.remove(mesh)} catch (err){};
texture_placeholder = document.createElement( 'canvas' );
texture_placeholder.width = 128;
texture_placeholder.height = 128;
var context = texture_placeholder.getContext( '2d' );
context.fillStyle = 'rgb( 0, 0, 0 )';
context.fillRect( 0, 0, texture_placeholder.width, texture_placeholder.height );
var fov = 90;
var latitude = initted ? latLng.lat() : latLng.lat;
var longitude = initted ? latLng.lng() : latLng.lng;
initted = true;
//headings are [front,left,back,right]
var headings = [1,90,180,270];
var imgSize = boxSize+'x'+boxSize;
//link example
//http://maps.googleapis.com/maps/api/streetview?size=600x600&location=40.720032,%20-73.988354&fov=100&heading=270&pitch=0&sensor=false
var materials = [
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov='+(fov+2)+'&heading='+headings[3]+'&pitch=0&sensor=false' ), // right
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov='+fov+'&heading='+headings[1]+'&pitch=0&sensor=false' ), // left
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov=90&heading=180&pitch=90&sensor=false' ), // top
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov=90&heading=180&pitch=-90&sensor=false' ), // bottom
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov='+fov+'&heading='+headings[2]+'&pitch=0&sensor=false' ), // back
loadTexture( 'http://maps.googleapis.com/maps/api/streetview?size='+imgSize+'&location='+latitude+',%20'+longitude+'&fov='+fov+'&heading='+headings[0]+'&pitch=0&sensor=false' ) // front
];
mesh = new THREE.Mesh( new THREE.CubeGeometry( boxSize, boxSize, boxSize, 7, 7, 7, materials ), new THREE.MeshFaceMaterial() );
mesh.scale.x = - 1;
scene.add( mesh );
}
function initSnow(isShaking){
scene.remove(snowGroup);
snowGroup = null;
particles = [];
snowGroup = new THREE.Object3D();
scene.add(snowGroup);
for ( var i = 0; i < snowflakeCount; i++ ) {
particle = new THREE.Particle( new THREE.ParticleBasicMaterial( pMaterial));
particle.position.x = randomNumber(150,-150);
particle.position.y = randomNumber(150,-150);
particle.position.z = randomNumber(150,-150);
particle.scale.x = particle.scale.y = randomNumber(.1,.25);
//particle properties
if(isShaking){
particle.velocity = new THREE.Vector3(20,-2,20);
} else {
particle.velocity = new THREE.Vector3(0,-2,0);
}
particle.velocity.rotateX(randomRange(-45,45));
particle.velocity.rotateY(randomRange(0,360));
particle.gravity = new THREE.Vector3(0,0,0);
particle.drag = 1;
particles.push(particle);
snowGroup.add(particle);
}
}
//this was a work in progress when i left it. It currently looks like crap, but I think a step in the right direction
function shake(){
initSnow(true);
shaking = true;
setInterval(function(){
target.x = randomNumber(target.x - 100, target.x + 100);
target.y = randomNumber(target.y - 100, target.y + 100);
target.z = randomNumber(target.z - 100, target.z + 100);
camera.lookAt( target );
renderer.render( scene, camera );
}, 200, 10)
setTimeout(function(){
initSnow(false)
},2000)
}
//helper function for pulling the skybox images and making them into three.js textures.
function loadTexture( path ) {
var texture = new THREE.Texture( texture_placeholder );
var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
var image = new Image();
image.onload = function () {
texture.needsUpdate = true;
material.map.image = this;
render();
};
image.src = path;
return material;
}
// input control events
function onDocumentMouseDown( event ) {
event.preventDefault();
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
function onDocumentMouseMove( event ) {
if ( isUserInteracting ) {
lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
render();
}
}
function onDocumentMouseUp( event ) {
isUserInteracting = false;
render();
}
function onDocumentMouseWheel( event ) {
camera.fov -= event.wheelDeltaY * 0.05;
camera.updateProjectionMatrix();
render();
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
onPointerDownPointerX = event.touches[ 0 ].pageX;
onPointerDownPointerY = event.touches[ 0 ].pageY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
lon = ( onPointerDownPointerX - event.touches[0].pageX ) * 0.1 + onPointerDownLon;
lat = ( event.touches[0].pageY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
render();
}
}
// animation loop
function animate() {
for(var i = 0; i<particles.length; i++)
{
var particle = particles[i];
particle.velocity.multiplyScalar(particle.drag);
particle.velocity.addSelf(particle.gravity);
particle.position.addSelf(particle.velocity);
with(particle.position)
{
if(y<-150) y+=300;
if(x>150) x-=-300;
else if(x<-150) x+=300;
if(z>150) z-=150;
else if(z<-150) z+=300;
}
}
requestAnimationFrame( animate );
render();
stats.update();
}
// threejs rendering function
function render() {
lat = Math.max( - 85, Math.min( 85, lat ) );
phi = ( 90 - lat ) * Math.PI / 180;
theta = lon * Math.PI / 180;
target.x = 500 * Math.sin( phi ) * Math.cos( theta );
target.y = 500 * Math.cos( phi );
target.z = 500 * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( target );
renderer.render( scene, camera );
}
function randomNumber(min, max){
return Math.random() * (max- min) + min;
}
</script>
<button onclick="shake()">SHAKE!</button>
</body>
</html>