webxr_vr_handinput_pointerdrag.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ve - handinput - point and drag</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - handinput - point and drag<br />
  12. (Oculus Browser 15.1+)
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { VRButton } from 'three/addons/webxr/VRButton.js';
  25. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  26. import { OculusHandModel } from 'three/addons/webxr/OculusHandModel.js';
  27. import { OculusHandPointerModel } from 'three/addons/webxr/OculusHandPointerModel.js';
  28. import { createText } from 'three/addons/webxr/Text2D.js';
  29. import { World, System, Component, TagComponent, Types } from 'three/addons/libs/ecsy.module.js';
  30. class Object3D extends Component { }
  31. Object3D.schema = {
  32. object: { type: Types.Ref }
  33. };
  34. class Button extends Component { }
  35. Button.schema = {
  36. // button states: [none, hovered, pressed]
  37. currState: { type: Types.String, default: 'none' },
  38. prevState: { type: Types.String, default: 'none' },
  39. action: { type: Types.Ref, default: () => { } }
  40. };
  41. class ButtonSystem extends System {
  42. execute( /*delta, time*/ ) {
  43. this.queries.buttons.results.forEach( entity => {
  44. const button = entity.getMutableComponent( Button );
  45. const buttonMesh = entity.getComponent( Object3D ).object;
  46. if ( button.currState == 'none' ) {
  47. buttonMesh.scale.set( 1, 1, 1 );
  48. } else {
  49. buttonMesh.scale.set( 1.1, 1.1, 1.1 );
  50. }
  51. if ( button.currState == 'pressed' && button.prevState != 'pressed' ) {
  52. button.action();
  53. }
  54. // preserve prevState, clear currState
  55. // HandRaySystem will update currState
  56. button.prevState = button.currState;
  57. button.currState = 'none';
  58. } );
  59. }
  60. }
  61. ButtonSystem.queries = {
  62. buttons: {
  63. components: [ Button ]
  64. }
  65. };
  66. class Draggable extends Component { }
  67. Draggable.schema = {
  68. // draggable states: [detached, hovered, to-be-attached, attached, to-be-detached]
  69. state: { type: Types.String, default: 'none' },
  70. originalParent: { type: Types.Ref, default: null },
  71. attachedPointer: { type: Types.Ref, default: null }
  72. };
  73. class DraggableSystem extends System {
  74. execute( /*delta, time*/ ) {
  75. this.queries.draggable.results.forEach( entity => {
  76. const draggable = entity.getMutableComponent( Draggable );
  77. const object = entity.getComponent( Object3D ).object;
  78. if ( draggable.originalParent == null ) {
  79. draggable.originalParent = object.parent;
  80. }
  81. switch ( draggable.state ) {
  82. case 'to-be-attached':
  83. draggable.attachedPointer.children[ 0 ].attach( object );
  84. draggable.state = 'attached';
  85. break;
  86. case 'to-be-detached':
  87. draggable.originalParent.attach( object );
  88. draggable.state = 'detached';
  89. break;
  90. default:
  91. object.scale.set( 1, 1, 1 );
  92. }
  93. } );
  94. }
  95. }
  96. DraggableSystem.queries = {
  97. draggable: {
  98. components: [ Draggable ]
  99. }
  100. };
  101. class Intersectable extends TagComponent { }
  102. class HandRaySystem extends System {
  103. init( attributes ) {
  104. this.handPointers = attributes.handPointers;
  105. }
  106. execute( /*delta, time*/ ) {
  107. this.handPointers.forEach( hp => {
  108. let distance = null;
  109. let intersectingEntity = null;
  110. this.queries.intersectable.results.forEach( entity => {
  111. const object = entity.getComponent( Object3D ).object;
  112. const intersections = hp.intersectObject( object, false );
  113. if ( intersections && intersections.length > 0 ) {
  114. if ( distance == null || intersections[ 0 ].distance < distance ) {
  115. distance = intersections[ 0 ].distance;
  116. intersectingEntity = entity;
  117. }
  118. }
  119. } );
  120. if ( distance ) {
  121. hp.setCursor( distance );
  122. if ( intersectingEntity.hasComponent( Button ) ) {
  123. const button = intersectingEntity.getMutableComponent( Button );
  124. if ( hp.isPinched() ) {
  125. button.currState = 'pressed';
  126. } else if ( button.currState != 'pressed' ) {
  127. button.currState = 'hovered';
  128. }
  129. }
  130. if ( intersectingEntity.hasComponent( Draggable ) ) {
  131. const draggable = intersectingEntity.getMutableComponent( Draggable );
  132. const object = intersectingEntity.getComponent( Object3D ).object;
  133. object.scale.set( 1.1, 1.1, 1.1 );
  134. if ( hp.isPinched() ) {
  135. if ( ! hp.isAttached() && draggable.state != 'attached' ) {
  136. draggable.state = 'to-be-attached';
  137. draggable.attachedPointer = hp;
  138. hp.setAttached( true );
  139. }
  140. } else {
  141. if ( hp.isAttached() && draggable.state == 'attached' ) {
  142. console.log( 'hello' );
  143. draggable.state = 'to-be-detached';
  144. draggable.attachedPointer = null;
  145. hp.setAttached( false );
  146. }
  147. }
  148. }
  149. } else {
  150. hp.setCursor( 1.5 );
  151. }
  152. } );
  153. }
  154. }
  155. HandRaySystem.queries = {
  156. intersectable: {
  157. components: [ Intersectable ]
  158. }
  159. };
  160. class HandsInstructionText extends TagComponent { }
  161. class InstructionSystem extends System {
  162. init( attributes ) {
  163. this.controllers = attributes.controllers;
  164. }
  165. execute( /*delta, time*/ ) {
  166. let visible = false;
  167. this.controllers.forEach( controller => {
  168. if ( controller.visible ) {
  169. visible = true;
  170. }
  171. } );
  172. this.queries.instructionTexts.results.forEach( entity => {
  173. const object = entity.getComponent( Object3D ).object;
  174. object.visible = visible;
  175. } );
  176. }
  177. }
  178. InstructionSystem.queries = {
  179. instructionTexts: {
  180. components: [ HandsInstructionText ]
  181. }
  182. };
  183. class OffsetFromCamera extends Component { }
  184. OffsetFromCamera.schema = {
  185. x: { type: Types.Number, default: 0 },
  186. y: { type: Types.Number, default: 0 },
  187. z: { type: Types.Number, default: 0 },
  188. };
  189. class NeedCalibration extends TagComponent { }
  190. class CalibrationSystem extends System {
  191. init( attributes ) {
  192. this.camera = attributes.camera;
  193. this.renderer = attributes.renderer;
  194. }
  195. execute( /*delta, time*/ ) {
  196. this.queries.needCalibration.results.forEach( entity => {
  197. if ( this.renderer.xr.getSession() ) {
  198. const offset = entity.getComponent( OffsetFromCamera );
  199. const object = entity.getComponent( Object3D ).object;
  200. const xrCamera = this.renderer.xr.getCamera();
  201. object.position.x = xrCamera.position.x + offset.x;
  202. object.position.y = xrCamera.position.y + offset.y;
  203. object.position.z = xrCamera.position.z + offset.z;
  204. entity.removeComponent( NeedCalibration );
  205. }
  206. } );
  207. }
  208. }
  209. CalibrationSystem.queries = {
  210. needCalibration: {
  211. components: [ NeedCalibration ]
  212. }
  213. };
  214. class Randomizable extends TagComponent { }
  215. class RandomizerSystem extends System {
  216. init( /*attributes*/ ) {
  217. this.needRandomizing = true;
  218. }
  219. execute( /*delta, time*/ ) {
  220. if ( ! this.needRandomizing ) {
  221. return;
  222. }
  223. this.queries.randomizable.results.forEach( entity => {
  224. const object = entity.getComponent( Object3D ).object;
  225. object.material.color.setHex( Math.random() * 0xffffff );
  226. object.position.x = Math.random() * 2 - 1;
  227. object.position.y = Math.random() * 2;
  228. object.position.z = Math.random() * 2 - 1;
  229. object.rotation.x = Math.random() * 2 * Math.PI;
  230. object.rotation.y = Math.random() * 2 * Math.PI;
  231. object.rotation.z = Math.random() * 2 * Math.PI;
  232. object.scale.x = Math.random() + 0.5;
  233. object.scale.y = Math.random() + 0.5;
  234. object.scale.z = Math.random() + 0.5;
  235. this.needRandomizing = false;
  236. } );
  237. }
  238. }
  239. RandomizerSystem.queries = {
  240. randomizable: {
  241. components: [ Randomizable ]
  242. }
  243. };
  244. const world = new World();
  245. const clock = new THREE.Clock();
  246. let camera, scene, renderer;
  247. init();
  248. function makeButtonMesh( x, y, z, color ) {
  249. const geometry = new THREE.BoxGeometry( x, y, z );
  250. const material = new THREE.MeshPhongMaterial( { color: color } );
  251. const buttonMesh = new THREE.Mesh( geometry, material );
  252. return buttonMesh;
  253. }
  254. function init() {
  255. const container = document.createElement( 'div' );
  256. document.body.appendChild( container );
  257. scene = new THREE.Scene();
  258. scene.background = new THREE.Color( 0x444444 );
  259. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  260. camera.position.set( 0, 1.2, 0.3 );
  261. scene.add( new THREE.HemisphereLight( 0xcccccc, 0x999999, 3 ) );
  262. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  263. light.position.set( 0, 6, 0 );
  264. light.castShadow = true;
  265. light.shadow.camera.top = 2;
  266. light.shadow.camera.bottom = - 2;
  267. light.shadow.camera.right = 2;
  268. light.shadow.camera.left = - 2;
  269. light.shadow.mapSize.set( 4096, 4096 );
  270. scene.add( light );
  271. renderer = new THREE.WebGLRenderer( { antialias: true } );
  272. renderer.setPixelRatio( window.devicePixelRatio );
  273. renderer.setSize( window.innerWidth, window.innerHeight );
  274. renderer.setAnimationLoop( animate );
  275. renderer.shadowMap.enabled = true;
  276. renderer.xr.enabled = true;
  277. renderer.xr.cameraAutoUpdate = false;
  278. container.appendChild( renderer.domElement );
  279. const sessionInit = {
  280. requiredFeatures: [ 'hand-tracking' ]
  281. };
  282. document.body.appendChild( VRButton.createButton( renderer, sessionInit ) );
  283. // controllers
  284. const controller1 = renderer.xr.getController( 0 );
  285. scene.add( controller1 );
  286. const controller2 = renderer.xr.getController( 1 );
  287. scene.add( controller2 );
  288. const controllerModelFactory = new XRControllerModelFactory();
  289. // Hand 1
  290. const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  291. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  292. scene.add( controllerGrip1 );
  293. const hand1 = renderer.xr.getHand( 0 );
  294. hand1.add( new OculusHandModel( hand1 ) );
  295. const handPointer1 = new OculusHandPointerModel( hand1, controller1 );
  296. hand1.add( handPointer1 );
  297. scene.add( hand1 );
  298. // Hand 2
  299. const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  300. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  301. scene.add( controllerGrip2 );
  302. const hand2 = renderer.xr.getHand( 1 );
  303. hand2.add( new OculusHandModel( hand2 ) );
  304. const handPointer2 = new OculusHandPointerModel( hand2, controller2 );
  305. hand2.add( handPointer2 );
  306. scene.add( hand2 );
  307. // setup objects in scene and entities
  308. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  309. const floorMaterial = new THREE.MeshPhongMaterial( { color: 0x222222 } );
  310. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  311. floor.rotation.x = - Math.PI / 2;
  312. floor.receiveShadow = true;
  313. scene.add( floor );
  314. const menuGeometry = new THREE.PlaneGeometry( 0.24, 0.5 );
  315. const menuMaterial = new THREE.MeshPhongMaterial( {
  316. opacity: 0,
  317. transparent: true,
  318. } );
  319. const menuMesh = new THREE.Mesh( menuGeometry, menuMaterial );
  320. menuMesh.position.set( 0.4, 1, - 1 );
  321. menuMesh.rotation.y = - Math.PI / 12;
  322. scene.add( menuMesh );
  323. const resetButton = makeButtonMesh( 0.2, 0.1, 0.01, 0x355c7d );
  324. const resetButtonText = createText( 'reset', 0.06 );
  325. resetButton.add( resetButtonText );
  326. resetButtonText.position.set( 0, 0, 0.0051 );
  327. resetButton.position.set( 0, - 0.06, 0 );
  328. menuMesh.add( resetButton );
  329. const exitButton = makeButtonMesh( 0.2, 0.1, 0.01, 0xff0000 );
  330. const exitButtonText = createText( 'exit', 0.06 );
  331. exitButton.add( exitButtonText );
  332. exitButtonText.position.set( 0, 0, 0.0051 );
  333. exitButton.position.set( 0, - 0.18, 0 );
  334. menuMesh.add( exitButton );
  335. const instructionText = createText( 'This is a WebXR Hands demo, please explore with hands.', 0.04 );
  336. instructionText.position.set( 0, 1.6, - 0.6 );
  337. scene.add( instructionText );
  338. const exitText = createText( 'Exiting session...', 0.04 );
  339. exitText.position.set( 0, 1.5, - 0.6 );
  340. exitText.visible = false;
  341. scene.add( exitText );
  342. world
  343. .registerComponent( Object3D )
  344. .registerComponent( Button )
  345. .registerComponent( Intersectable )
  346. .registerComponent( HandsInstructionText )
  347. .registerComponent( OffsetFromCamera )
  348. .registerComponent( NeedCalibration )
  349. .registerComponent( Randomizable )
  350. .registerComponent( Draggable );
  351. world
  352. .registerSystem( RandomizerSystem )
  353. .registerSystem( InstructionSystem, { controllers: [ controllerGrip1, controllerGrip2 ] } )
  354. .registerSystem( CalibrationSystem, { renderer: renderer, camera: camera } )
  355. .registerSystem( ButtonSystem )
  356. .registerSystem( DraggableSystem )
  357. .registerSystem( HandRaySystem, { handPointers: [ handPointer1, handPointer2 ] } );
  358. for ( let i = 0; i < 20; i ++ ) {
  359. const object = new THREE.Mesh( new THREE.BoxGeometry( 0.15, 0.15, 0.15 ), new THREE.MeshLambertMaterial( { color: 0xffffff } ) );
  360. scene.add( object );
  361. const entity = world.createEntity();
  362. entity.addComponent( Intersectable );
  363. entity.addComponent( Randomizable );
  364. entity.addComponent( Object3D, { object: object } );
  365. entity.addComponent( Draggable );
  366. }
  367. const menuEntity = world.createEntity();
  368. menuEntity.addComponent( Intersectable );
  369. menuEntity.addComponent( OffsetFromCamera, { x: 0.4, y: 0, z: - 1 } );
  370. menuEntity.addComponent( NeedCalibration );
  371. menuEntity.addComponent( Object3D, { object: menuMesh } );
  372. const rbEntity = world.createEntity();
  373. rbEntity.addComponent( Intersectable );
  374. rbEntity.addComponent( Object3D, { object: resetButton } );
  375. const rbAction = function () {
  376. world.getSystem( RandomizerSystem ).needRandomizing = true;
  377. };
  378. rbEntity.addComponent( Button, { action: rbAction } );
  379. const ebEntity = world.createEntity();
  380. ebEntity.addComponent( Intersectable );
  381. ebEntity.addComponent( Object3D, { object: exitButton } );
  382. const ebAction = function () {
  383. exitText.visible = true;
  384. setTimeout( function () {
  385. exitText.visible = false; renderer.xr.getSession().end();
  386. }, 2000 );
  387. };
  388. ebEntity.addComponent( Button, { action: ebAction } );
  389. const itEntity = world.createEntity();
  390. itEntity.addComponent( HandsInstructionText );
  391. itEntity.addComponent( Object3D, { object: instructionText } );
  392. window.addEventListener( 'resize', onWindowResize );
  393. }
  394. function onWindowResize() {
  395. camera.aspect = window.innerWidth / window.innerHeight;
  396. camera.updateProjectionMatrix();
  397. renderer.setSize( window.innerWidth, window.innerHeight );
  398. }
  399. function animate() {
  400. const delta = clock.getDelta();
  401. const elapsedTime = clock.elapsedTime;
  402. renderer.xr.updateCamera( camera );
  403. world.execute( delta, elapsedTime );
  404. renderer.render( scene, camera );
  405. }
  406. </script>
  407. </body>
  408. </html>