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

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

		<script>

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

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

			}

			//

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

43
			var cubes = [];
44
			var INTERSECTED;
45
			var radius = 100, theta = 0;
46
			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, 1000 );
M
Mr.doob 已提交
67 68
				scene.add( camera );

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

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

				scene.add( new THREE.AmbientLight( 0x202040 ) );

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

M
Mr.doob 已提交
92
				var geometry = new THREE.BoxGeometry( 3, 3, 3 );
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() * 100 - 50;
					object.position.y = Math.random() * 100 - 50;
					object.position.z = Math.random() * 100 - 50;
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 111 112 113 114
					object.userData.velocity = new THREE.Vector3();
					object.userData.velocity.x = ( Math.random() - 0.5 ) * 0.1;
					object.userData.velocity.y = ( Math.random() - 0.5 ) * 0.1;
					object.userData.velocity.z = ( Math.random() - 0.5 ) * 0.1;

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

				}

				raycaster = new THREE.Raycaster();

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

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

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

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

136 137 138 139
				}

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
140
				stats.domElement.style.top = '0';
141 142 143 144 145 146 147 148 149 150 151 152 153
				container.appendChild( stats.domElement );

				//

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

			}

			function onWindowResize() {

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

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

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

171
				// find intersections
172

173
				raycaster.setFromCamera( { x: 0, y: 0 }, camera );
174

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

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

					INTERSECTED = null;

				}

M
Mr.doob 已提交
197
				controls.update();
B
Brian Peiris 已提交
198

M
Mr.doob 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
				for ( var i = 0; i < cubes.length; i ++ ) {

					var cube = cubes[ i ];

					cube.position.add( cube.userData.velocity );

					if ( cube.position.x < 100 ) cube.position.x += 200;
					if ( cube.position.y < 100 ) cube.position.y += 200;
					if ( cube.position.z < 100 ) cube.position.z += 200;

					if ( cube.position.x > 100 ) cube.position.x -= 200;
					if ( cube.position.y > 100 ) cube.position.y -= 200;
					if ( cube.position.z > 100 ) cube.position.z -= 200;

					cube.rotation.x += 0.01;

				}

M
Mr.doob 已提交
217
				effect.render( scene, camera );
218 219 220 221 222 223

			}

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