2
0

webxr_vr_handinput_pointerdrag.html 15 KB

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