webvr_cubes.html 6.5 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

M
Mr.doob 已提交
42
			var room;
43
			var isMouseDown = false;
M
Mr.doob 已提交
44

45 46
			var INTERSECTED;
			var crosshair;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

			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 已提交
66
				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
M
Mr.doob 已提交
67 68
				scene.add( camera );

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

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

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

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

92
				var geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
93

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

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

M
Mr.doob 已提交
98 99 100
					object.position.x = Math.random() * 4 - 2;
					object.position.y = Math.random() * 4 - 2;
					object.position.z = Math.random() * 4 - 2;
101 102 103 104 105 106 107 108 109

					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 已提交
110
					object.userData.velocity = new THREE.Vector3();
M
Mr.doob 已提交
111 112 113
					object.userData.velocity.x = Math.random() * 0.01 - 0.005;
					object.userData.velocity.y = Math.random() * 0.01 - 0.005;
					object.userData.velocity.z = Math.random() * 0.01 - 0.005;
M
Mr.doob 已提交
114

M
Mr.doob 已提交
115
					room.add( 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
				}

137 138 139 140
				document.addEventListener( 'mousedown', onMouseDown, false );
				document.addEventListener( 'mouseup', onMouseUp, false );
				document.addEventListener( 'touchstart', onMouseDown, false );
				document.addEventListener( 'touchend', onMouseUp, false );
M
Mr.doob 已提交
141

142
				//
M
Mr.doob 已提交
143

144
				window.addEventListener( 'resize', onWindowResize, false );
M
Mr.doob 已提交
145

146
			}
M
Mr.doob 已提交
147

148
			function onMouseDown() {
M
Mr.doob 已提交
149

150
				isMouseDown = true;
151

152 153 154 155 156
			}

			function onMouseUp() {

				isMouseDown = false;
157 158 159 160 161 162 163 164

			}

			function onWindowResize() {

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

M
Mr.doob 已提交
165
				effect.setSize( window.innerWidth, window.innerHeight );
166 167 168 169 170 171 172 173 174 175 176 177 178 179

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
				if ( isMouseDown === true ) {

					var cube = room.children[ 0 ];
					room.remove( cube );

					cube.position.set( 0, 0, - 0.75 );
					cube.position.applyQuaternion( camera.quaternion );
					cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
					cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
					cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 );
					cube.userData.velocity.applyQuaternion( camera.quaternion );
					room.add( cube );

				}

195
				// find intersections
196

197
				raycaster.setFromCamera( { x: 0, y: 0 }, camera );
198

M
Mr.doob 已提交
199
				var intersects = raycaster.intersectObjects( room.children );
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

				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 已提交
217
					INTERSECTED = undefined;
218 219 220

				}

221
				// Keep cubes inside room
B
Brian Peiris 已提交
222

M
Mr.doob 已提交
223
				for ( var i = 0; i < room.children.length; i ++ ) {
M
Mr.doob 已提交
224

M
Mr.doob 已提交
225
					var cube = room.children[ i ];
M
Mr.doob 已提交
226

M
Mr.doob 已提交
227 228
					cube.userData.velocity.multiplyScalar( 0.999 );

M
Mr.doob 已提交
229 230
					cube.position.add( cube.userData.velocity );

M
Mr.doob 已提交
231
					if ( cube.position.x < - 3 || cube.position.x > 3 ) {
M
Mr.doob 已提交
232

M
Mr.doob 已提交
233
						cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
234 235 236 237
						cube.userData.velocity.x = - cube.userData.velocity.x;

					}

M
Mr.doob 已提交
238
					if ( cube.position.y < - 3 || cube.position.y > 3 ) {
239

M
Mr.doob 已提交
240
						cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
241 242 243 244
						cube.userData.velocity.y = - cube.userData.velocity.y;

					}

M
Mr.doob 已提交
245
					if ( cube.position.z < - 3 || cube.position.z > 3 ) {
246

M
Mr.doob 已提交
247
						cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
248 249 250
						cube.userData.velocity.z = - cube.userData.velocity.z;

					}
M
Mr.doob 已提交
251

252 253 254
					cube.rotation.x += cube.userData.velocity.x * 2;
					cube.rotation.y += cube.userData.velocity.y * 2;
					cube.rotation.z += cube.userData.velocity.z * 2;
M
Mr.doob 已提交
255 256 257

				}

258
				controls.update();
M
Mr.doob 已提交
259
				effect.render( scene, camera );
260 261 262 263 264 265

			}

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