webvr_cubes.html 6.9 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
M
Mr.doob 已提交
4
		<title>three.js webvr - cubes</title>
5
		<meta charset="utf-8">
M
Mr.doob 已提交
6
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
M
Mugen87 已提交
7 8
		<!-- Origin Trial Token, feature = WebVR (For Chrome M59+), origin = https://threejs.org, expires = 2017-07-28 -->
		<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M59+)" data-expires="2017-07-28" content="Ave6CPNUgSwHb3vCbyd55P/R7pfkwNniUJsYfSoUqI+l1X1BIOt6HfriVP0g2hmaG7Pp3qaUXuXdZeqGBmoMKg8AAABNeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMSIsImV4cGlyeSI6MTUwMTI2NzQwNX0=">
9 10 11
		<style>
			body {
				font-family: Monospace;
M
Mr.doob 已提交
12 13
				background-color: #101010;
				color: #fff;
14 15 16
				margin: 0px;
				overflow: hidden;
			}
M
Mr.doob 已提交
17 18
			a {
				color: #f00;
19 20 21 22 23
			}
		</style>
	</head>
	<body>

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

M
Mr.doob 已提交
26
		<script src="js/vr/WebVR.js"></script>
27 28 29

		<script>

M
Mugen87 已提交
30
			WEBVR.checkAvailability().catch( function( message ) {
M
Mr.doob 已提交
31

M
Mugen87 已提交
32
				document.body.appendChild( WEBVR.getMessageContainer( message ) );
M
Mr.doob 已提交
33

M
Mugen87 已提交
34
			} );
M
Mr.doob 已提交
35 36 37

			//

38 39
			var clock = new THREE.Clock();

M
Mr.doob 已提交
40
			var container;
41
			var camera, scene, raycaster, renderer;
42

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

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

			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';
62
				info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes';
63 64 65 66
				container.appendChild( info );

				scene = new THREE.Scene();

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

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

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

M
Mr.doob 已提交
87
				scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
M
Mr.doob 已提交
88 89

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

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

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

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

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

					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 已提交
111
					object.userData.velocity = new THREE.Vector3();
M
Mr.doob 已提交
112 113 114
					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 已提交
115

M
Mr.doob 已提交
116
					room.add( object );
117 118 119 120 121

				}

				raycaster = new THREE.Raycaster();

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

M
Mr.doob 已提交
128
				renderer.vr.enabled = true;
129

130
				WEBVR.getVRDisplay( function ( display ) {
M
Mr.doob 已提交
131

M
Mr.doob 已提交
132 133
					renderer.vr.setDevice( display );

134
					document.body.appendChild( WEBVR.getButton( display, renderer.domElement ) );
M
Mr.doob 已提交
135

136
				} );
137

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

143
				//
M
Mr.doob 已提交
144

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

147
			}
M
Mr.doob 已提交
148

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

151
				isMouseDown = true;
152

153 154 155 156 157
			}

			function onMouseUp() {

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

			}

			function onWindowResize() {

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

M
Mr.doob 已提交
166
				renderer.setSize( window.innerWidth, window.innerHeight );
167 168 169 170 171 172 173

			}

			//

			function animate() {

M
Mr.doob 已提交
174
				renderer.animate( render );
175 176 177 178 179

			}

			function render() {

180 181
				var delta = clock.getDelta() * 60;

182 183 184 185 186 187 188
				if ( isMouseDown === true ) {

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

					cube.position.set( 0, 0, - 0.75 );
					cube.position.applyQuaternion( camera.quaternion );
189 190 191
					cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
					cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
					cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
192 193 194 195 196
					cube.userData.velocity.applyQuaternion( camera.quaternion );
					room.add( cube );

				}

197
				// find intersections
198

199
				raycaster.setFromCamera( { x: 0, y: 0 }, camera );
200

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

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

				}

223
				// Keep cubes inside room
B
Brian Peiris 已提交
224

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

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

229
					cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
M
Mr.doob 已提交
230

M
Mr.doob 已提交
231 232
					cube.position.add( cube.userData.velocity );

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

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

					}

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

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

					}

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

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

					}
M
Mr.doob 已提交
253

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

				}

M
Mr.doob 已提交
260
				renderer.render( scene, camera );
261 262 263 264 265 266

			}

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