浏览代码

Added handtracking example

Fernando Serrano 5 年之前
父节点
当前提交
303f42e84a
共有 1 个文件被更改,包括 423 次插入0 次删除
  1. 423 0
      examples/webxr_vr_handtracking.html

+ 423 - 0
examples/webxr_vr_handtracking.html

@@ -0,0 +1,423 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js vr - dragging</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
+		<link type="text/css" rel="stylesheet" href="main.css">
+	</head>
+	<body>
+
+		<div id="info">
+			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - hand and controller support
+		</div>
+
+		<script type="module">
+
+			import * as THREE from '../build/three.module.js';
+			import { OrbitControls } from './jsm/controls/OrbitControls.js';
+			import { VRButton } from './jsm/webxr/VRButton.js';
+			import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
+			import { XRHandModelFactory } from './jsm/webxr/WebXRHandController.js';
+
+			var container;
+			var camera, scene, renderer;
+			var hand1, hand2;
+			var controller1, controller2;
+			var controllerGrip1, controllerGrip2;
+
+			var raycaster, intersected = [];
+			var tempMatrix = new THREE.Matrix4();
+
+			var controls, group;
+
+			var grabbing = false;
+			var scaling = {
+				active: false,
+				initialDistance: 0,
+				object: null,
+				initialScale: 1
+			};
+
+			var spheres = [];
+
+			init();
+			animate();
+
+			function init() {
+
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0x808080 );
+
+				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
+				camera.position.set( 0, 1.6, 3 );
+
+				controls = new OrbitControls( camera, container );
+				controls.target.set( 0, 1.6, 0 );
+				controls.update();
+
+				var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
+				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 = [
+					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 )
+				];
+
+				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.outputEncoding = THREE.sRGBEncoding;
+				renderer.shadowMap.enabled = true;
+				renderer.xr.enabled = true;
+
+				container.appendChild( renderer.domElement );
+
+				document.body.appendChild( VRButton.createButton( renderer ) );
+
+				// controllers
+
+				controller1 = renderer.xr.getController( 0 );
+				controller1.addEventListener( 'selectstart', onSelectStart );
+				controller1.addEventListener( 'selectend', onSelectEnd );
+				scene.add( controller1 );
+
+				controller2 = renderer.xr.getController( 1 );
+				controller2.addEventListener( 'selectstart', onSelectStart );
+				controller2.addEventListener( 'selectend', onSelectEnd );
+				scene.add( controller2 );
+
+				var controllerModelFactory = new XRControllerModelFactory();
+				var handModelFactory = new XRHandModelFactory();
+
+				// Hand 1
+				controllerGrip1 = renderer.xr.getControllerGrip( 0 );
+				controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
+				scene.add( controllerGrip1 );
+
+				hand1 = renderer.xr.getHand( 0 );
+				hand1.addEventListener( 'pinchstart', onPinchStartLeft );
+				hand1.addEventListener( 'pinchend', () => {
+
+					scaling.active = false;
+
+				} );
+				hand1.add( handModelFactory.createHandModel( hand1 ) );
+
+				scene.add( hand1 );
+
+				// Hand 2
+				controllerGrip2 = renderer.xr.getControllerGrip( 1 );
+				controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
+				scene.add( controllerGrip2 );
+
+				hand2 = renderer.xr.getHand( 1 );
+				hand2.addEventListener( 'pinchstart', onPinchStartRight );
+				hand2.addEventListener( 'pinchend', onPinchEndRight );
+				hand2.add( handModelFactory.createHandModel( hand2 ) );
+				scene.add( hand2 );
+
+				//
+
+				var geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
+
+				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();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			const SphereRadius = 0.05;
+			function onPinchStartLeft( event ) {
+
+				var controller = event.target;
+
+				if ( grabbing ) {
+
+					const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
+					const sphere = collideObject( indexTip );
+
+					if ( sphere ) {
+
+						const sphere2 = hand2.userData.selected;
+						console.log( "sphere1", sphere, "sphere2", sphere2 );
+						if ( sphere === sphere2 ) {
+
+							scaling.active = true;
+							scaling.object = sphere;
+							scaling.initialScale = sphere.scale.x;
+							scaling.initialDistance = indexTip.position.distanceTo( hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position );
+							return;
+
+						}
+
+					}
+
+				}
+
+				var geometry = new THREE.BoxBufferGeometry( SphereRadius, SphereRadius, SphereRadius );
+				var material = new THREE.MeshStandardMaterial( {
+					color: Math.random() * 0xffffff,
+					roughness: 1.0,
+					metalness: 0.0
+				} );
+				var spawn = new THREE.Mesh( geometry, material );
+				spawn.geometry.computeBoundingSphere();
+
+				const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
+				spawn.position.copy( indexTip.position );
+				spawn.quaternion.copy( indexTip.quaternion );
+
+				spheres.push( spawn );
+
+				scene.add( spawn );
+			}
+
+			function collideObject( indexTip ) {
+
+				for ( var i = 0; i < spheres.length; i ++ ) {
+
+					const sphere = spheres[ i ];
+					const distance = indexTip.getWorldPosition().distanceTo( sphere.getWorldPosition() );
+
+					if ( distance < sphere.geometry.boundingSphere.radius * sphere.scale.x ) {
+
+						return sphere;
+
+					}
+
+				}
+
+				return null;
+
+			}
+
+			function onPinchStartRight( event ) {
+
+				var controller = event.target;
+				const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
+				const object = collideObject( indexTip );
+				if ( object ) {
+
+					grabbing = true;
+					indexTip.attach( object );
+					controller.userData.selected = object;
+					console.log( "Selected", object );
+
+				}
+
+			}
+
+			function onPinchEndRight( event ) {
+
+				var controller = event.target;
+
+				if ( controller.userData.selected !== undefined ) {
+
+					var object = controller.userData.selected;
+					object.material.emissive.b = 0;
+					scene.attach( object );
+
+					controller.userData.selected = undefined;
+					grabbing = false;
+
+				}
+
+				scaling.active = false;
+
+			}
+
+			function onSelectStart( event ) {
+
+				var controller = event.target;
+
+				var intersections = getIntersections( controller );
+
+				if ( intersections.length > 0 ) {
+
+					var intersection = intersections[ 0 ];
+
+					var object = intersection.object;
+					object.material.emissive.b = 1;
+					controller.attach( object );
+
+					controller.userData.selected = object;
+
+				}
+
+			}
+
+			function onSelectEnd( event ) {
+
+				var controller = event.target;
+
+				if ( controller.userData.selected !== undefined ) {
+
+					var object = controller.userData.selected;
+					object.material.emissive.b = 0;
+					group.attach( 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() {
+
+				renderer.setAnimationLoop( render );
+
+			}
+
+			function render() {
+
+				cleanIntersected();
+
+				if ( scaling.active ) {
+
+					const indexTip1Pos = hand1.joints[ XRHand.INDEX_PHALANX_TIP ].position;
+					const indexTip2Pos = hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position;
+					const distance = indexTip1Pos.distanceTo( indexTip2Pos );
+					const newScale = scaling.initialScale + distance / scaling.initialDistance - 1;
+					scaling.object.scale.setScalar( newScale );
+				}
+
+				intersectObjects( controller1 );
+				intersectObjects( controller2 );
+
+				renderer.render( scene, camera );
+
+			}
+
+		</script>
+	</body>
+</html>