|
@@ -0,0 +1,216 @@
|
|
|
+<!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 - haptics
|
|
|
+ </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';
|
|
|
+
|
|
|
+ var container;
|
|
|
+ var camera, scene, renderer;
|
|
|
+ var controller1, controller2;
|
|
|
+ var controllerGrip1, controllerGrip2;
|
|
|
+ var box = new THREE.Box3();
|
|
|
+
|
|
|
+ var controllers = [];
|
|
|
+ var controls, group;
|
|
|
+
|
|
|
+ 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();
|
|
|
+ group.position.z = - 0.5;
|
|
|
+ scene.add( group );
|
|
|
+
|
|
|
+ for ( var i = 0; i < 10; i ++ ) {
|
|
|
+
|
|
|
+ const w = 0.1;
|
|
|
+ const h = 0.1;
|
|
|
+ const minH = 1;
|
|
|
+ var geometry = new THREE.BoxBufferGeometry( w, h * i + minH, w );
|
|
|
+ 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 = ( i - 5 ) * ( w + 0.05 );
|
|
|
+ object.castShadow = true;
|
|
|
+ object.receiveShadow = true;
|
|
|
+ object.userData = {
|
|
|
+ index: i + 1
|
|
|
+ };
|
|
|
+
|
|
|
+ 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 );
|
|
|
+ scene.add( controller1 );
|
|
|
+
|
|
|
+ controller2 = renderer.xr.getController( 1 );
|
|
|
+ scene.add( controller2 );
|
|
|
+
|
|
|
+ var controllerModelFactory = new XRControllerModelFactory();
|
|
|
+
|
|
|
+ controllerGrip1 = renderer.xr.getControllerGrip( 0 );
|
|
|
+ controllerGrip1.addEventListener( "connected", controllerConnected );
|
|
|
+ controllerGrip1.addEventListener( "disconnected", controllerDisconnected );
|
|
|
+ controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
|
|
|
+ scene.add( controllerGrip1 );
|
|
|
+
|
|
|
+ controllerGrip2 = renderer.xr.getControllerGrip( 1 );
|
|
|
+ controllerGrip2.addEventListener( "connected", controllerConnected );
|
|
|
+ controllerGrip2.addEventListener( "disconnected", controllerDisconnected );
|
|
|
+ controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
|
|
|
+ scene.add( controllerGrip2 );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize, false );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function controllerConnected( evt ) {
|
|
|
+
|
|
|
+ controllers.push( {
|
|
|
+ gamepad: evt.data.gamepad,
|
|
|
+ grip: evt.target
|
|
|
+ } );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function controllerDisconnected( evt ) {
|
|
|
+
|
|
|
+ const index = controllers.findIndex( o => o.controller === evt.target );
|
|
|
+ if ( index !== - 1 ) {
|
|
|
+
|
|
|
+ controllers.splice( index, 1 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ renderer.setAnimationLoop( render );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function render() {
|
|
|
+
|
|
|
+ for ( var g = 0; g < controllers.length; g ++ ) {
|
|
|
+
|
|
|
+ const { grip, gamepad } = controllers[ g ];
|
|
|
+ var sphere = {
|
|
|
+ radius: 0.03,
|
|
|
+ center: grip.position
|
|
|
+ };
|
|
|
+
|
|
|
+ if ( "hapticActuators" in gamepad && gamepad.hapticActuators != null && gamepad.hapticActuators.length > 0 ) {
|
|
|
+
|
|
|
+ for ( var i = 0; i < group.children.length; i ++ ) {
|
|
|
+
|
|
|
+ const child = group.children[ i ];
|
|
|
+ box.setFromObject( child );
|
|
|
+ if ( box.intersectsSphere( sphere ) ) {
|
|
|
+
|
|
|
+ child.material.emissive.b = 1;
|
|
|
+ gamepad.hapticActuators[ 0 ].pulse( child.userData.index / group.children.length, 100 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ child.material.emissive.b = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ renderer.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|