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

Last change on this file since 29693 was 29693, checked in by bmt11, 9 years ago

Added slider for wind control but the black box is a bug I have no figured out for some time now
Camera control is disabled whilst using the slider also :)

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