source: other-projects/nz-flag-design/trunk/render-3d/flag.html@ 29616

Last change on this file since 29616 was 29616, checked in by davidb, 9 years ago

Extra code added so initial flag is one based on JSESSIONID, if it exists

File size: 12.8 KB
Line 
1<!DOCTYPE html>
2<html lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
6 <title>three.js webgl - 3D Flag hhh</title>
7 <meta charset="utf-8">
8 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
9 <head>
10 <link rel="stylesheet" type="text/css" href="CSS/button.css">
11 <link href='http://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
12 <style>
13 body {
14 font-family: Monospace;
15 background-color: #000;
16 color: #000;
17 margin: 0px;
18 overflow: hidden;
19 position: fixed;
20 }
21
22 #menuContainer{
23 position: fixed;
24 right: -18vw;
25 float: right;
26 width: 25vw;
27 transition: right 0.5s;
28 background-image: url("./images/slider.png");
29 background-size: 25vw 150vh;
30 background-position: center;
31 }
32
33 #weatherMenu {
34 text-align: center;
35 padding: 0px;
36 width: 20vw;
37 z-index: 10;
38 height: 100vh;
39 position: relative;
40 float: right;
41 }
42
43 #arrow{
44 float: left;
45 background-image: url("./images/arrow.png");
46 background-position: center;
47 background-size: cover;
48 background-repeat: no-repeat;
49 margin: auto;
50 right: 1vw;
51 width: 5vw;
52 height: 5vw;
53 position: relative;
54 border-radius: 100%;
55 top: 45vh;
56 z-index: 9;
57 transition: transform 0.5s;
58 transform: rotate(360deg);
59 }
60
61 </style>
62 <style type="text/css"></style></head>
63
64 <body>
65 <div id="menuContainer">
66 <div id="weatherMenu" style="color : white">
67 <!--Toggle:
68 <a onclick="wind = !wind;">Wind</a> |-->
69 <a class="myButton" style="background-image:url('./images/sunButton.jpg')" onclick="fine = !fine;"></a>
70 <a class="myButton" style="background-image:url('./images/rainButton.jpg')" onclick="raining = !raining;"></a>
71 <a class="myButton" style="background-image:url('./images/snowButton.jpg')" onclick="snowing = !snowing;"></a>
72 </div>
73 <div id="arrow" onclick="openclose()">
74 </div>
75 </div>
76
77
78 <script type="text/javascript">
79 var open = false;
80 function openclose(){
81 if (open){
82 document.getElementById("menuContainer").style.right = "-20vw";
83 document.getElementById("arrow").style.transform = "rotate(360deg)";
84 }else{document.getElementById("menuContainer").style.right = "0vw";
85 document.getElementById("arrow").style.transform = "rotate(180deg)";
86 }
87 open = !open;
88
89 }
90 </script>
91
92 <script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
93 <script type="text/javascript" src="../js/jquery.cookie.js"></script>
94
95 <script type="text/javascript" src="./Flag_files/Three.js"></script>
96 <script type="text/javascript" src="./Flag_files/Detector.js"></script>
97 <script type="text/javascript" src="./Flag_files/Stats.js"></script>
98 <script type="text/javascript" src="./Flag_files/Flag.js"></script>
99 <script type="text/javascript" src="./Flag_files/OrbitControls.js"></script>
100 <script type="text/javascript" src="./weather/weather.js"></script>
101 <script type="text/javascript" src="./weather/rain.js"></script>
102 <script type="text/javascript" src="./weather/snow.js"></script>
103
104 <script type="text/javascript" src="./Flag_files/KeyboardState.js"></script>
105
106 <script type="x-shader/x-fragment" id="fragmentShaderDepth">
107
108 uniform sampler2D texture;
109 varying vec2 vUV;
110
111 vec4 pack_depth( const in float depth ) {
112
113 const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );
114 const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );
115 vec4 res = fract( depth * bit_shift );
116 res -= res.xxyz * bit_mask;
117 return res;
118
119 }
120
121 void main() {
122
123 vec4 pixel = texture2D( texture, vUV );
124
125 if ( pixel.a < 0.5 ) discard;
126
127 gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );
128
129 }
130 </script>
131
132 <script type="x-shader/x-vertex" id="vertexShaderDepth">
133
134 varying vec2 vUV;
135
136 void main() {
137
138 vUV = 0.75 * uv;
139
140 vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
141
142 gl_Position = projectionMatrix * mvPosition;
143
144 }
145
146 </script>
147
148 <script type="text/javascript">
149 var keyboard = new KeyboardState();
150 var clothTextures = [];
151
152 var initial = true;
153
154 var pins = [];
155 for (var j=0;j<=cloth.h;j++){
156 pins.push(cloth.index(0, j));
157 }
158 /* Light variables */
159 var light;
160
161 /* Weather variables */
162 var weatherSystem;
163 var raining = false;
164 var snowing = false;
165 var snowSystem;
166 var rainSystem;
167
168 /* Mouse variables */
169 var mouseX;
170 var mouseY;
171 var clickPos = '';
172 var mouseDown = false;
173
174 if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
175
176 var container, stats;
177 var camera, scene, renderer;
178
179 var cameraRot = 90;
180 var cameraElevation = 0;
181 var controls;
182
183 var clothGeometry;
184
185 var object, tmpFlag, arrow;
186
187 var rotate = false;
188
189 // array for holding existing flags
190
191 var flags = ["./images/flag.png", "./images/flag2.png", "./images/canada.png"];
192 var flagSelector = 0;
193 var materials;
194 var vertexShader;
195 var fragmentShader;
196 var uniforms;
197
198 init();
199 animate();
200
201 function init() {
202
203 container = document.createElement( 'div' );
204 document.body.appendChild( container );
205
206
207
208 // scene
209
210 scene = new THREE.Scene();
211
212 scene.fog = new THREE.Fog( 0x404040, 500, 10000 );
213 scene.fog.color.setHSL( 0.1, 0.1, 0.8 );
214
215 // camera
216
217 camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
218 camera.position.y = 50;
219 camera.position.z = 1500;
220 scene.add( camera );
221 controls = new THREE.OrbitControls( camera );
222 controls.addEventListener( 'change', render );
223
224 // lights
225
226 scene.add( new THREE.AmbientLight( 0x404040 ) );
227
228 light = new THREE.DirectionalLight( 0xffffff, 1 );
229 light.color.setHSL( 0.6, 0.125, 1 );
230 light.position.set( 50, 200, 100 );
231 light.position.multiplyScalar( 1.3 );
232
233 light.castShadow = true;
234 light.shadowCameraVisible = false;
235
236 light.shadowMapWidth = 2048;
237 light.shadowMapHeight = 2048;
238
239 var d = 200;
240
241 light.shadowCameraLeft = -d;
242 light.shadowCameraRight = d;
243 light.shadowCameraTop = d;
244 light.shadowCameraBottom = -d;
245
246 light.shadowCameraFar = 1000;
247 light.shadowDarkness = 0.5;
248
249 scene.add( light );
250
251 /*light = new THREE.DirectionalLight( 0xffffff, 1.25 );
252 light.color.setHSL( 0.3, 0.95, 1 );
253 light.position.set( 0, -1, 0 );*/
254
255 //scene.add( light );
256
257 /* Weather init */
258
259 rainSystem = new rain();
260 snowSystem = new snow();
261 weatherSystem = new weatherSystem(scene);
262
263 /* ------------------ */
264
265 var session_id = $.cookie('JSESSIONID');
266 if (session_id) {
267 flags[0] = "flag-" + session_id + ".png";
268 }
269
270 // load flag textures
271 var i;
272
273 for(i = 0; i < flags.length; i++){
274 clothTextures[i] = THREE.ImageUtils.loadTexture( flags[i] );
275 //clothTextures[i].needsUpdate = true;
276 clothTextures[i].wrapS = clothTextures[i].wrapT = THREE.ClampToEdgeWrapping;
277 clothTextures[i].anisotropy = 16;
278 }
279 createFlag();
280
281 // weather
282
283 // arrow
284
285 arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 0 ), 50, 0xff0000 );
286 arrow.position.set( -200, -50, -200 );
287 //scene.add( arrow );
288
289
290
291 // ground
292
293 var initColor = new THREE.Color( 0x00ff00 );
294 initColor.setHSL( 0.3, 0.85, 0.8 );
295 var initTexture = THREE.ImageUtils.generateDataTexture( 1, 1, initColor );
296
297 var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: initTexture, perPixel: true } );
298
299 var groundTexture = THREE.ImageUtils.loadTexture( "./images/grass.jpg", undefined, function() { groundMaterial.map = groundTexture } );
300 groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
301 groundTexture.repeat.set( 32, 32 );
302 groundTexture.anisotropy = 16;
303
304 var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
305 mesh.position.y = -250;
306 mesh.rotation.x = - Math.PI / 2;
307 mesh.receiveShadow = true;
308 scene.add( mesh );
309
310 // poles
311
312 var poleGeo = new THREE.BoxGeometry( 5, 750, 5 );
313 var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100, perPixel: true } );
314
315 var mesh = new THREE.Mesh( poleGeo, poleMat );
316 mesh.position.y = -25; //-250
317 mesh.position.x = 0;
318 mesh.receiveShadow = true;
319 mesh.castShadow = false;
320 scene.add( mesh );
321
322
323 var gg = new THREE.BoxGeometry( 10, 10, 10 );
324 var mesh = new THREE.Mesh( gg, poleMat );
325 mesh.position.y = -250;
326 mesh.position.x = 0; //125
327 mesh.receiveShadow = true;
328 mesh.castShadow = false;
329 scene.add( mesh );
330
331
332 //
333 renderer = new THREE.WebGLRenderer( { antialias: true } );
334 renderer.setSize( window.innerWidth, window.innerHeight );
335 renderer.setClearColor( scene.fog.color );
336
337 container.appendChild( renderer.domElement );
338
339 renderer.gammaInput = true;
340 renderer.gammaOutput = true;
341 renderer.physicallyBasedShading = true;
342
343 renderer.shadowMapEnabled = true;
344
345 //
346
347 stats = new Stats();
348 stats.domElement.style.position = 'absolute';
349 stats.domElement.style.top = '0px';
350 container.appendChild( stats.domElement );
351
352 stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
353 stats.domElement.children[ 0 ].style.background = "transparent";
354 stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
355
356 //
357
358 window.addEventListener( 'resize', onWindowResize, false );
359
360 }
361
362 //
363 var flag_toggle = 0;
364
365 function createFlag(flagFilename) {
366 if (flagFilename) {
367 flags[0] = "./images/" + flagFilename;
368 }
369
370 if(initial){
371
372 // create materials
373 materials = [
374 new THREE.MeshLambertMaterial( {specular: 0x030303, fog: false, emissive: 0x111111, shininess: 10, map: clothTextures[0], side: THREE.DoubleSide } ),
375 ];
376
377 // cloth geometry
378
379 clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h, true );
380 clothGeometry.dynamic = true;
381 clothGeometry.computeFaceNormals();
382
383 uniforms = { texture: { type: "t", value: 0, texture: clothTextures[0] } };
384 vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
385 fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
386
387 // cloth mesh
388 object = new THREE.Mesh( clothGeometry, materials[ 0 ] );
389 object.position.set( 0, 150, 0 );
390 object.castShadow = false;
391 object.receiveShadow = false;
392 object.material.transparent = true;
393 object.material.opacity = 0.85;
394 scene.add( object );
395 initial = false;
396 object.customDepthMaterial = new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: vertexShader, fragmentShader: fragmentShader } );
397
398 }else{
399 clothTextures[flagSelector] = THREE.ImageUtils.loadTexture( flags[flagSelector]+'?'+Date.now() );
400 object.material.map = clothTextures[flagSelector];
401 object.material.needsUpdate = true;
402 }
403
404 }
405
406
407 //
408
409 function onWindowResize() {
410
411 camera.aspect = window.innerWidth / window.innerHeight;
412 camera.updateProjectionMatrix();
413
414 renderer.setSize( window.innerWidth, window.innerHeight );
415
416 }
417
418 //
419
420 function animate() {
421
422 requestAnimationFrame( animate );
423
424 var time = Date.now();
425 windStrength = Math.cos( time / 7000 ) * 100 + 200;
426 windForce.set( 1000,500,Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
427 arrow.setLength( windStrength );
428 arrow.setDirection( windForce );
429 controls.update();
430 simulate(time);
431 render();
432 stats.update();
433
434 }
435
436 function render() {
437
438
439 var timer = Date.now() * 0.0002;
440
441 keyboard.update();
442 if( keyboard.down("A")) {
443 flagSelector--;
444 if(flagSelector < 0) flagSelector = flags.length-1;
445 createFlag();
446
447 }else if(keyboard.down("D")) {
448 flagSelector++;
449 if(flagSelector >= flags.length) flagSelector = 0;
450 createFlag();
451 }
452
453 var p = cloth.particles;
454 for ( var i = 0, il = p.length; i < il; i ++ ) {
455
456 clothGeometry.vertices[ i ].copy( p[ i ].position );
457
458 }
459
460 clothGeometry.computeFaceNormals();
461 clothGeometry.computeVertexNormals();
462
463 clothGeometry.normalsNeedUpdate = true;
464 clothGeometry.verticesNeedUpdate = true;
465
466
467
468 if(raining && weatherSystem.type != rainSystem){
469 snowing = false;
470 weatherSystem.swap(rainSystem);
471 }else if(snowing && weatherSystem.type != snowSystem){
472 raining = false;
473 weatherSystem.swap(snowSystem);
474 }
475
476 if(weatherSystem.isSet() === true){
477 weatherSystem.update();
478 }
479
480 camera.lookAt( new THREE.Vector3(scene.position.x+150, scene.position.y+150, scene.position.z) );
481
482 renderer.render( scene, camera );
483
484 }
485
486 </script><div><canvas width="1366" height="600"></canvas>
487</body></html>
Note: See TracBrowser for help on using the repository browser.