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

W
WestLangley 已提交
22
		<script src="../build/three.js"></script>
M
Mr.doob 已提交
23 24

		<script src="js/WebVR.js"></script>
25 26
		<script src="js/effects/VREffect.js"></script>
		<script src="js/controls/VRControls.js"></script>
27 28 29

		<script>

M
Mr.doob 已提交
30 31 32 33 34 35 36 37
			if ( WEBVR.isLatestAvailable() === false ) {

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

			}

			//

M
Mr.doob 已提交
38
			var container;
39
			var camera, scene, raycaster, renderer;
M
Mr.doob 已提交
40
			var effect, controls;
41

42
			var cubes = [];
43
			var INTERSECTED;
44
			var radius = 100, theta = 0;
45
			var crosshair;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				var info = document.createElement( 'div' );
				info.style.position = 'absolute';
				info.style.top = '10px';
				info.style.width = '100%';
				info.style.textAlign = 'center';
				info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
				container.appendChild( info );

				scene = new THREE.Scene();

M
Mr.doob 已提交
65
				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 );
M
Mr.doob 已提交
66 67
				scene.add( camera );

68
				crosshair = new THREE.Mesh(
69
					new THREE.RingGeometry( 0.02, 0.04, 32 ),
B
Brian Peiris 已提交
70
					new THREE.MeshBasicMaterial( {
M
Mr.doob 已提交
71 72 73
						color: 0xffffff,
						opacity: 0.5,
						transparent: true
74 75
					} )
				);
76
				crosshair.position.z = - 2;
M
Mr.doob 已提交
77
				camera.add( crosshair );
78

M
Mr.doob 已提交
79 80 81 82 83 84
				var mesh = new THREE.Mesh(
					new THREE.BoxGeometry( 200, 200, 200, 10, 10, 10 ),
					new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
				);
				scene.add( mesh );

M
Mr.doob 已提交
85
				scene.add( new THREE.HemisphereLight( 0x404020, 0x202040, 0.5 ) );
M
Mr.doob 已提交
86 87

				var light = new THREE.DirectionalLight( 0xffffff );
88 89 90
				light.position.set( 1, 1, 1 ).normalize();
				scene.add( light );

91
				var geometry = new THREE.BoxGeometry( 5, 5, 5 );
92

M
Mr.doob 已提交
93
				for ( var i = 0; i < 200; i ++ ) {
94 95 96

					var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );

97 98 99
					object.position.x = Math.random() * 200 - 100;
					object.position.y = Math.random() * 200 - 100;
					object.position.z = Math.random() * 200 - 100;
100 101 102 103 104 105 106 107 108

					object.rotation.x = Math.random() * 2 * Math.PI;
					object.rotation.y = Math.random() * 2 * Math.PI;
					object.rotation.z = Math.random() * 2 * Math.PI;

					object.scale.x = Math.random() + 0.5;
					object.scale.y = Math.random() + 0.5;
					object.scale.z = Math.random() + 0.5;

M
Mr.doob 已提交
109
					object.userData.velocity = new THREE.Vector3();
M
Mr.doob 已提交
110 111 112
					object.userData.velocity.x = Math.random() - 0.5;
					object.userData.velocity.y = Math.random() - 0.5;
					object.userData.velocity.z = Math.random() - 0.5;
M
Mr.doob 已提交
113

114
					scene.add( object );
115
					cubes.push( object );
116 117 118 119 120

				}

				raycaster = new THREE.Raycaster();

121
				renderer = new THREE.WebGLRenderer( { antialias: true } );
M
Mr.doob 已提交
122
				renderer.setClearColor( 0x101010 );
123
				renderer.setPixelRatio( window.devicePixelRatio );
M
Mr.doob 已提交
124 125 126
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.sortObjects = false;
				container.appendChild( renderer.domElement );
127

M
Mr.doob 已提交
128 129
				controls = new THREE.VRControls( camera );
				effect = new THREE.VREffect( renderer );
130

M
Mr.doob 已提交
131
				if ( WEBVR.isAvailable() === true ) {
132

M
Mr.doob 已提交
133
					document.body.appendChild( WEBVR.getButton( effect ) );
M
Mr.doob 已提交
134

135 136
				}

M
Mr.doob 已提交
137 138 139 140 141 142 143 144 145 146 147
				renderer.domElement.addEventListener( 'click', function ( event ) {

					if ( INTERSECTED ) {

						var object = INTERSECTED;
						object.userData.velocity.subVectors( object.position, camera.position ).normalize();

					}

				} );

148 149 150 151 152 153 154 155 156 157 158
				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

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

M
Mr.doob 已提交
159
				effect.setSize( window.innerWidth, window.innerHeight );
160 161 162 163 164 165 166 167 168 169 170 171 172 173

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

174
				// find intersections
175

176
				raycaster.setFromCamera( { x: 0, y: 0 }, camera );
177

178
				var intersects = raycaster.intersectObjects( cubes );
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

				if ( intersects.length > 0 ) {

					if ( INTERSECTED != intersects[ 0 ].object ) {

						if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

						INTERSECTED = intersects[ 0 ].object;
						INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
						INTERSECTED.material.emissive.setHex( 0xff0000 );

					}

				} else {

					if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

M
Mr.doob 已提交
196
					INTERSECTED = undefined;
197 198 199

				}

M
Mr.doob 已提交
200
				controls.update();
B
Brian Peiris 已提交
201

M
Mr.doob 已提交
202 203 204 205
				for ( var i = 0; i < cubes.length; i ++ ) {

					var cube = cubes[ i ];

M
Mr.doob 已提交
206 207
					cube.userData.velocity.multiplyScalar( 0.999 );

M
Mr.doob 已提交
208 209
					cube.position.add( cube.userData.velocity );

210
					if ( cube.position.x < - 100 || cube.position.x > 100 ) {
M
Mr.doob 已提交
211

M
Mr.doob 已提交
212
						cube.position.x = THREE.Math.clamp( cube.position.x, - 100, 100 );
213 214 215 216 217 218
						cube.userData.velocity.x = - cube.userData.velocity.x;

					}

					if ( cube.position.y < - 100 || cube.position.y > 100 ) {

M
Mr.doob 已提交
219
						cube.position.y = THREE.Math.clamp( cube.position.y, - 100, 100 );
220 221 222 223 224 225
						cube.userData.velocity.y = - cube.userData.velocity.y;

					}

					if ( cube.position.z < - 100 || cube.position.z > 100 ) {

M
Mr.doob 已提交
226
						cube.position.z = THREE.Math.clamp( cube.position.z, - 100, 100 );
227 228 229
						cube.userData.velocity.z = - cube.userData.velocity.z;

					}
M
Mr.doob 已提交
230 231 232 233 234

					cube.rotation.x += 0.01;

				}

M
Mr.doob 已提交
235
				effect.render( scene, camera );
236 237 238 239 240 241

			}

		</script>
	</body>
</html>