webvr_rollercoaster.html 6.0 KB
Newer Older
M
r75  
Mr.doob 已提交
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
M
r76  
Mr.doob 已提交
4
		<title>three.js webvr - roller coaster</title>
M
r75  
Mr.doob 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
		<style>
			body {
				margin: 0px;
				color: #fff;
				font-family: Monospace;
				background-color: #444;
				overflow: hidden;
			}
			a {
				color: #00f;
			}
		</style>
	</head>
	<body>

M
r76  
Mr.doob 已提交
22
		<script src="../build/three.js"></script>
M
r75  
Mr.doob 已提交
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

		<script src="js/RollerCoaster.js"></script>

		<script src="js/WebVR.js"></script>
		<script src="js/effects/VREffect.js"></script>
		<script src="js/controls/VRControls.js"></script>

		<script>

			if ( WEBVR.isLatestAvailable() === false ) {

				document.body.appendChild( WEBVR.getMessage() );

			}

			//

			var renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setClearColor( 0xf0f0ff );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			var scene = new THREE.Scene();

			var light = new THREE.HemisphereLight( 0xfff0f0, 0x606066 );
			light.position.set( 1, 1, 1 );
			scene.add( light );

			var train = new THREE.Object3D();
			scene.add( train );

			var camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
			camera.rotation.y = Math.PI;
			train.add( camera );

			// environment

			var geometry = new THREE.PlaneGeometry( 5000, 5000, 15, 15 );
			geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
			var material = new THREE.MeshLambertMaterial( { color: 0x407000, shading: THREE.FlatShading } );

			for ( var i = 0; i < geometry.vertices.length; i ++ ) {

				var vertex = geometry.vertices[ i ];

				vertex.x += Math.random() * 100 - 50;
				vertex.z += Math.random() * 100 - 50;

				var distance = ( vertex.distanceTo( scene.position ) / 5 ) - 250;

				vertex.y = Math.random() * Math.max( 0, distance );

			}

			geometry.computeFaceNormals();

			var mesh = new THREE.Mesh( geometry, material );
			scene.add( mesh );

			var geometry = new TreesGeometry( mesh );
			var material = new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } );
			var mesh = new THREE.Mesh( geometry, material );
			scene.add( mesh );

			var geometry = new SkyGeometry();
			var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
			var mesh = new THREE.Mesh( geometry, material );
			scene.add( mesh );

			//

			var PI2 = Math.PI * 2;

			var curve = ( function () {

				var vector = new THREE.Vector3();
				var vector2 = new THREE.Vector3();

				return {

					getPointAt: function ( t ) {

M
r79  
Mr.doob 已提交
106
						t *= Math.PI;
M
r75  
Mr.doob 已提交
107

M
r79  
Mr.doob 已提交
108 109 110
						var x = Math.sin(t * 4) * Math.cos(t * 6) * 1000;
						var y = Math.cos(t * 8) * 80 + Math.cos(t * 20 * Math.sin(t)) * 40 + 200;
						var z = Math.sin(t * 5) * Math.sin(t * 3) * 1000;
M
r75  
Mr.doob 已提交
111

M
r79  
Mr.doob 已提交
112
						return vector.set( x, y, z );
M
r75  
Mr.doob 已提交
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

					},

					getTangentAt: function ( t ) {

						var delta = 0.0001;
						var t1 = Math.max( 0, t - delta );
						var t2 = Math.min( 1, t + delta );

						return vector2.copy( this.getPointAt ( t2 ) ).sub( this.getPointAt( t1 ) ).normalize();

					}

				};

			} )();

			var geometry = new RollerCoasterGeometry( curve, 1500 );
			var material = new THREE.MeshStandardMaterial( {
				roughness: 0.1,
				metalness: 0,
				vertexColors: THREE.VertexColors
			} );
			var mesh = new THREE.Mesh( geometry, material );
			scene.add( mesh );

			var geometry = new RollerCoasterLiftersGeometry( curve, 100 );
			var material = new THREE.MeshStandardMaterial( {
				roughness: 0.1,
				metalness: 0
			} );
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.y = 1;
			scene.add( mesh );

			var geometry = new RollerCoasterShadowGeometry( curve, 500 );
			var material = new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.1, depthWrite: false, transparent: true } );
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.y = 1;
			scene.add( mesh );

			var funfairs = [];

			//

			var geometry = new THREE.CylinderGeometry( 100, 100, 50, 15 );
			var material = new THREE.MeshLambertMaterial( { color: 0xff8080, shading: THREE.FlatShading } );
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.set( - 800, 100, - 700 );
			mesh.rotation.x = Math.PI / 2;
			scene.add( mesh );

			funfairs.push( mesh );

			var geometry = new THREE.CylinderGeometry( 50, 60, 40, 10 );
			var material = new THREE.MeshLambertMaterial( { color: 0x8080ff, shading: THREE.FlatShading } );
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.set( 500, 20, 300 );
			scene.add( mesh );

			funfairs.push( mesh );

			//

			var controls = new THREE.VRControls( camera );
			var effect = new THREE.VREffect( renderer );

			if ( WEBVR.isAvailable() === true ) {

				document.body.appendChild( WEBVR.getButton( effect ) );

			}

			//

			window.addEventListener( 'resize', function () {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}, false );

			//

			var position = new THREE.Vector3();
			var tangent = new THREE.Vector3();

			var lookAt = new THREE.Vector3();

			var velocity = 0;
			var progress = 0;

M
r79  
Mr.doob 已提交
207 208
			var clock = new THREE.Clock();

M
r75  
Mr.doob 已提交
209 210
			function animate( time ) {

M
r79  
Mr.doob 已提交
211 212 213
				effect.requestAnimationFrame( animate );

				var delta = clock.getDelta() * 60;
M
r75  
Mr.doob 已提交
214 215 216 217 218 219 220 221 222

				for ( var i = 0; i < funfairs.length; i ++ ) {

					funfairs[ i ].rotation.y = time * 0.0002;

				}

				//

M
r79  
Mr.doob 已提交
223
				progress += velocity * delta;
M
r75  
Mr.doob 已提交
224 225 226 227 228 229 230 231 232
				progress = progress % 1;

				position.copy( curve.getPointAt( progress ) );
				position.y += 3;

				train.position.copy( position );

				tangent.copy( curve.getTangentAt( progress ) );

M
r79  
Mr.doob 已提交
233
				velocity -= tangent.y * 0.0000015 * delta;
M
r75  
Mr.doob 已提交
234 235
				velocity = Math.max( velocity, 0.00004 );

M
r76  
Mr.doob 已提交
236
				train.lookAt( lookAt.copy( position ).sub( tangent ) );
M
r75  
Mr.doob 已提交
237 238 239 240 241 242 243 244 245

				//

				controls.update();

				effect.render( scene, camera );

			};

M
r79  
Mr.doob 已提交
246
			effect.requestAnimationFrame( animate );
M
r75  
Mr.doob 已提交
247 248 249 250 251

		</script>

	</body>
</html>