webvr_dragging.html 8.1 KB
Newer Older
M
r90  
Mr.doob 已提交
1
<!DOCTYPE html>
M
r81  
Mr.doob 已提交
2 3
<html lang="en">
	<head>
M
r94  
Mr.doob 已提交
4
		<title>three.js webvr - dragging</title>
M
r81  
Mr.doob 已提交
5 6
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
7 8 9 10 11 12
		<!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-08-30 -->
		<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-08-30" content="Ag80lPFLAvRyqP2W5I5XBzACxrTAQTWa3cXebXzq+WzW66nlQa6lvejGg1gdAMrzYbY6jUWp8g08kEnzb6svVgcAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzNTYzNjYxOX0=">
		<!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-08-28 -->
		<meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-08-28" content="Ag7nJS0Q6nBKfmRY1XLKHslnz73amLhaf8RoFpYz36MpMq0oa30AETLXer74BIwa3t8uDXlR0n4W9f/o674Rqw4AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUzNTQxNDQwMH0=">
		<!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-08-28 -->
		<meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-08-28" content="AsflqqNG2L/Eepy8xSwCYwWH5U7w3nN7Ak137jGxMeBFz9lqQVcMBqMTcMw6ZkxThB7ZM2Cn7hgPqX++ZlgC9wMAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTM1NDE0NDAwfQ==">
M
r81  
Mr.doob 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
		<style>
			body {
				font-family: Monospace;
				background-color: #101010;
				color: #fff;
				margin: 0px;
				overflow: hidden;
			}
			a {
				color: #f00;
			}
		</style>
	</head>
	<body>

		<script src="../build/three.js"></script>
M
r82  
Mr.doob 已提交
29
		<script src="js/vr/WebVR.js"></script>
M
r81  
Mr.doob 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

		<script>

			var container;
			var camera, scene, renderer;
			var controller1, controller2;

			var raycaster, intersected = [];
			var tempMatrix = new THREE.Matrix4();

			var group;

			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';
M
r94  
Mr.doob 已提交
55
				info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - dragging';
M
r81  
Mr.doob 已提交
56 57 58 59 60 61 62
				container.appendChild( info );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x808080 );

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );

M
r91  
Mr.doob 已提交
63
				var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
M
r81  
Mr.doob 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
				var material = new THREE.MeshStandardMaterial( {
					color: 0xeeeeee,
					roughness: 1.0,
					metalness: 0.0
				} );
				var floor = new THREE.Mesh( geometry, material );
				floor.rotation.x = - Math.PI / 2;
				floor.receiveShadow = true;
				scene.add( floor );

				scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );

				var light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 0, 6, 0 );
				light.castShadow = true;
				light.shadow.camera.top = 2;
				light.shadow.camera.bottom = -2;
				light.shadow.camera.right = 2;
				light.shadow.camera.left = -2;
				light.shadow.mapSize.set( 4096, 4096 );
				scene.add( light );

				group = new THREE.Group();
				scene.add( group );

				var geometries = [
M
r91  
Mr.doob 已提交
90 91 92 93 94
					new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ),
					new THREE.ConeBufferGeometry( 0.2, 0.2, 64 ),
					new THREE.CylinderBufferGeometry( 0.2, 0.2, 0.2, 64 ),
					new THREE.IcosahedronBufferGeometry( 0.2, 3 ),
					new THREE.TorusBufferGeometry( 0.2, 0.04, 64, 32 )
M
r81  
Mr.doob 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
				];

				for ( var i = 0; i < 50; i ++ ) {

					var geometry = geometries[ Math.floor( Math.random() * geometries.length ) ];
					var material = new THREE.MeshStandardMaterial( {
						color: Math.random() * 0xffffff,
						roughness: 0.7,
						metalness: 0.0
					} );

					var object = new THREE.Mesh( geometry, material );

					object.position.x = Math.random() * 4 - 2;
					object.position.y = Math.random() * 2;
					object.position.z = Math.random() * 4 - 2;

					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.setScalar( Math.random() + 0.5 );

					object.castShadow = true;
					object.receiveShadow = true;

					group.add( object );

				}

				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.gammaInput = true;
				renderer.gammaOutput = true;
M
r88  
Mr.doob 已提交
132 133
				renderer.shadowMap.enabled = true;
				renderer.vr.enabled = true;
M
r81  
Mr.doob 已提交
134 135
				container.appendChild( renderer.domElement );

M
r88  
Mr.doob 已提交
136
				document.body.appendChild( WEBVR.createButton( renderer ) );
M
r81  
Mr.doob 已提交
137 138 139

				// controllers

M
r94  
Mr.doob 已提交
140 141 142
				controller1 = renderer.vr.getController( 0 );
				controller1.addEventListener( 'selectstart', onSelectStart );
				controller1.addEventListener( 'selectend', onSelectEnd );
M
r91  
Mr.doob 已提交
143
				scene.add( controller1 );
M
r81  
Mr.doob 已提交
144

M
r94  
Mr.doob 已提交
145 146 147
				controller2 = renderer.vr.getController( 0 );
				controller1.addEventListener( 'selectstart', onSelectStart );
				controller1.addEventListener( 'selectend', onSelectEnd );
M
r91  
Mr.doob 已提交
148
				scene.add( controller2 );
M
r81  
Mr.doob 已提交
149 150 151

				//

M
r91  
Mr.doob 已提交
152
				var geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
M
r81  
Mr.doob 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

				var line = new THREE.Line( geometry );
				line.name = 'line';
				line.scale.z = 5;

				controller1.add( line.clone() );
				controller2.add( line.clone() );

				raycaster = new THREE.Raycaster();

				//

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

			}

			function onWindowResize() {

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

M
r86  
Mr.doob 已提交
174
				renderer.setSize( window.innerWidth, window.innerHeight );
M
r81  
Mr.doob 已提交
175 176 177

			}

M
r94  
Mr.doob 已提交
178
			function onSelectStart( event ) {
M
r81  
Mr.doob 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

				var controller = event.target;

				var intersections = getIntersections( controller );

				if ( intersections.length > 0 ) {

					var intersection = intersections[ 0 ];

					tempMatrix.getInverse( controller.matrixWorld );

					var object = intersection.object;
					object.matrix.premultiply( tempMatrix );
					object.matrix.decompose( object.position, object.quaternion, object.scale );
					object.material.emissive.b = 1;
					controller.add( object );

					controller.userData.selected = object;

				}

			}

M
r94  
Mr.doob 已提交
202
			function onSelectEnd( event ) {
M
r81  
Mr.doob 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

				var controller = event.target;

				if ( controller.userData.selected !== undefined ) {

					var object = controller.userData.selected;
					object.matrix.premultiply( controller.matrixWorld );
					object.matrix.decompose( object.position, object.quaternion, object.scale );
					object.material.emissive.b = 0;
					group.add( object );

					controller.userData.selected = undefined;

				}


			}

			function getIntersections( controller ) {

				tempMatrix.identity().extractRotation( controller.matrixWorld );

				raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
				raycaster.ray.direction.set( 0, 0, -1 ).applyMatrix4( tempMatrix );

				return raycaster.intersectObjects( group.children );

			}

			function intersectObjects( controller ) {

				// Do not highlight when already selected

				if ( controller.userData.selected !== undefined ) return;

				var line = controller.getObjectByName( 'line' );
				var intersections = getIntersections( controller );

				if ( intersections.length > 0 ) {

					var intersection = intersections[ 0 ];

					var object = intersection.object;
					object.material.emissive.r = 1;
					intersected.push( object );

					line.scale.z = intersection.distance;

				} else {

					line.scale.z = 5;

				}

			}

			function cleanIntersected() {

				while ( intersected.length ) {

					var object = intersected.pop();
					object.material.emissive.r = 0;

				}

			}

			//

			function animate() {

M
r93  
Mr.doob 已提交
274
				renderer.setAnimationLoop( render );
M
r81  
Mr.doob 已提交
275 276 277 278 279 280 281 282 283 284

			}

			function render() {

				cleanIntersected();

				intersectObjects( controller1 );
				intersectObjects( controller2 );

M
r86  
Mr.doob 已提交
285
				renderer.render( scene, camera );
M
r81  
Mr.doob 已提交
286 287 288 289 290 291

			}

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