|
@@ -1,7 +1,7 @@
|
|
|
<!DOCTYPE html>
|
|
|
<html lang="en">
|
|
|
<head>
|
|
|
- <title>three.js vr - hand and controller - simple</title>
|
|
|
+ <title>three.js vr - handinput - cubes</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">
|
|
@@ -9,7 +9,8 @@
|
|
|
<body>
|
|
|
|
|
|
<div id="info">
|
|
|
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - hand and controller support
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - handinput - cubes<br/>
|
|
|
+ (Oculus Browser with #webxr-hands flag enabled)
|
|
|
</div>
|
|
|
|
|
|
<script type="module">
|
|
@@ -28,6 +29,16 @@
|
|
|
|
|
|
var controls;
|
|
|
|
|
|
+ var grabbing = false;
|
|
|
+ var scaling = {
|
|
|
+ active: false,
|
|
|
+ initialDistance: 0,
|
|
|
+ object: null,
|
|
|
+ initialScale: 1
|
|
|
+ };
|
|
|
+
|
|
|
+ var spheres = [];
|
|
|
+
|
|
|
init();
|
|
|
animate();
|
|
|
|
|
@@ -37,7 +48,7 @@
|
|
|
document.body.appendChild( container );
|
|
|
|
|
|
scene = new THREE.Scene();
|
|
|
- scene.background = new THREE.Color( 0x808080 );
|
|
|
+ scene.background = new THREE.Color( 0x444444 );
|
|
|
|
|
|
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
|
|
|
camera.position.set( 0, 1.6, 3 );
|
|
@@ -47,11 +58,7 @@
|
|
|
controls.update();
|
|
|
|
|
|
var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
|
|
|
- var material = new THREE.MeshStandardMaterial( {
|
|
|
- color: 0xeeeeee,
|
|
|
- roughness: 1.0,
|
|
|
- metalness: 0.0
|
|
|
- } );
|
|
|
+ var material = new THREE.MeshStandardMaterial( { color: 0x222222 } );
|
|
|
var floor = new THREE.Mesh( geometry, material );
|
|
|
floor.rotation.x = - Math.PI / 2;
|
|
|
floor.receiveShadow = true;
|
|
@@ -99,6 +106,12 @@
|
|
|
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 );
|
|
@@ -109,6 +122,8 @@
|
|
|
scene.add( controllerGrip2 );
|
|
|
|
|
|
hand2 = renderer.xr.getHand( 1 );
|
|
|
+ hand2.addEventListener( 'pinchstart', onPinchStartRight );
|
|
|
+ hand2.addEventListener( 'pinchend', onPinchEndRight );
|
|
|
hand2.add( handModelFactory.createHandModel( hand2 ) );
|
|
|
scene.add( hand2 );
|
|
|
|
|
@@ -137,6 +152,108 @@
|
|
|
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 animate() {
|
|
@@ -147,6 +264,16 @@
|
|
|
|
|
|
function render() {
|
|
|
|
|
|
+ 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 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
renderer.render( scene, camera );
|
|
|
|
|
|
}
|