webxr_vr_handinput_pointerdrag.html 15 KB

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