webvr_cubes.html 5.7 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 43
			var room;

44 45
			var INTERSECTED;
			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, 10 );
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
				room = new THREE.Mesh(
					new THREE.BoxGeometry( 6, 6, 6, 10, 10, 10 ),
M
Mr.doob 已提交
81 82
					new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
				);
M
Mr.doob 已提交
83
				scene.add( room );
M
Mr.doob 已提交
84

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 );

M
Mr.doob 已提交
91
				var geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
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 } ) );

M
Mr.doob 已提交
97 98 99
					object.position.x = Math.random() * 4 - 2;
					object.position.y = Math.random() * 4 - 2;
					object.position.z = Math.random() * 4 - 2;
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.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 已提交
113

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

				}

				raycaster = new THREE.Raycaster();

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

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

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

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

134 135
				}

M
Mr.doob 已提交
136 137 138 139 140
				renderer.domElement.addEventListener( 'click', function ( event ) {

					if ( INTERSECTED ) {

						var object = INTERSECTED;
M
Mr.doob 已提交
141
						object.userData.velocity.subVectors( object.position, camera.position ).normalize().multiplyScalar( 0.1 );
M
Mr.doob 已提交
142 143 144 145 146

					}

				} );

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

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

			}

			function onWindowResize() {

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

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

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

173
				// find intersections
174

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

M
Mr.doob 已提交
177
				var intersects = raycaster.intersectObjects( room.children );
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

				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 已提交
195
					INTERSECTED = undefined;
196 197 198

				}

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

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

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

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

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

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

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

					}

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

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

					}

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

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

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

					cube.rotation.x += 0.01;

				}

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

			}

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