webxr_vr_handinput_pointerclick.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput - point and click</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 click<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 Intersectable extends TagComponent { }
  67. class HandRaySystem extends System {
  68. init( attributes ) {
  69. this.handPointers = attributes.handPointers;
  70. }
  71. execute( /* delta, time */ ) {
  72. this.handPointers.forEach( hp => {
  73. let distance = null;
  74. let intersectingEntity = null;
  75. this.queries.intersectable.results.forEach( entity => {
  76. const object = entity.getComponent( Object3D ).object;
  77. const intersections = hp.intersectObject( object, false );
  78. if ( intersections && intersections.length > 0 ) {
  79. if ( distance == null || intersections[ 0 ].distance < distance ) {
  80. distance = intersections[ 0 ].distance;
  81. intersectingEntity = entity;
  82. }
  83. }
  84. } );
  85. if ( distance ) {
  86. hp.setCursor( distance );
  87. if ( intersectingEntity.hasComponent( Button ) ) {
  88. const button = intersectingEntity.getMutableComponent( Button );
  89. if ( hp.isPinched() ) {
  90. button.currState = 'pressed';
  91. } else if ( button.currState != 'pressed' ) {
  92. button.currState = 'hovered';
  93. }
  94. }
  95. } else {
  96. hp.setCursor( 1.5 );
  97. }
  98. } );
  99. }
  100. }
  101. HandRaySystem.queries = {
  102. intersectable: {
  103. components: [ Intersectable ]
  104. }
  105. };
  106. class Rotating extends TagComponent { }
  107. class RotatingSystem extends System {
  108. execute( delta/*, time*/ ) {
  109. this.queries.rotatingObjects.results.forEach( entity => {
  110. const object = entity.getComponent( Object3D ).object;
  111. object.rotation.x += 0.4 * delta;
  112. object.rotation.y += 0.4 * delta;
  113. } );
  114. }
  115. }
  116. RotatingSystem.queries = {
  117. rotatingObjects: {
  118. components: [ Rotating ]
  119. }
  120. };
  121. class HandsInstructionText extends TagComponent { }
  122. class InstructionSystem extends System {
  123. init( attributes ) {
  124. this.controllers = attributes.controllers;
  125. }
  126. execute( /* delta, time */ ) {
  127. let visible = false;
  128. this.controllers.forEach( controller => {
  129. if ( controller.visible ) {
  130. visible = true;
  131. }
  132. } );
  133. this.queries.instructionTexts.results.forEach( entity => {
  134. const object = entity.getComponent( Object3D ).object;
  135. object.visible = visible;
  136. } );
  137. }
  138. }
  139. InstructionSystem.queries = {
  140. instructionTexts: {
  141. components: [ HandsInstructionText ]
  142. }
  143. };
  144. class OffsetFromCamera extends Component { }
  145. OffsetFromCamera.schema = {
  146. x: { type: Types.Number, default: 0 },
  147. y: { type: Types.Number, default: 0 },
  148. z: { type: Types.Number, default: 0 },
  149. };
  150. class NeedCalibration extends TagComponent { }
  151. class CalibrationSystem extends System {
  152. init( attributes ) {
  153. this.camera = attributes.camera;
  154. this.renderer = attributes.renderer;
  155. }
  156. execute( /* delta, time */ ) {
  157. this.queries.needCalibration.results.forEach( entity => {
  158. if ( this.renderer.xr.getSession() ) {
  159. const offset = entity.getComponent( OffsetFromCamera );
  160. const object = entity.getComponent( Object3D ).object;
  161. const xrCamera = this.renderer.xr.getCamera();
  162. object.position.x = xrCamera.position.x + offset.x;
  163. object.position.y = xrCamera.position.y + offset.y;
  164. object.position.z = xrCamera.position.z + offset.z;
  165. entity.removeComponent( NeedCalibration );
  166. }
  167. } );
  168. }
  169. }
  170. CalibrationSystem.queries = {
  171. needCalibration: {
  172. components: [ NeedCalibration ]
  173. }
  174. };
  175. const world = new World();
  176. const clock = new THREE.Clock();
  177. let camera, scene, renderer;
  178. init();
  179. function makeButtonMesh( x, y, z, color ) {
  180. const geometry = new THREE.BoxGeometry( x, y, z );
  181. const material = new THREE.MeshPhongMaterial( { color: color } );
  182. const buttonMesh = new THREE.Mesh( geometry, material );
  183. buttonMesh.castShadow = true;
  184. buttonMesh.receiveShadow = true;
  185. return buttonMesh;
  186. }
  187. function init() {
  188. const container = document.createElement( 'div' );
  189. document.body.appendChild( container );
  190. scene = new THREE.Scene();
  191. scene.background = new THREE.Color( 0x444444 );
  192. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  193. camera.position.set( 0, 1.2, 0.3 );
  194. scene.add( new THREE.HemisphereLight( 0xcccccc, 0x999999, 3 ) );
  195. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  196. light.position.set( 0, 6, 0 );
  197. light.castShadow = true;
  198. light.shadow.camera.top = 2;
  199. light.shadow.camera.bottom = - 2;
  200. light.shadow.camera.right = 2;
  201. light.shadow.camera.left = - 2;
  202. light.shadow.mapSize.set( 4096, 4096 );
  203. scene.add( light );
  204. renderer = new THREE.WebGLRenderer( { antialias: true } );
  205. renderer.setPixelRatio( window.devicePixelRatio );
  206. renderer.setSize( window.innerWidth, window.innerHeight );
  207. renderer.setAnimationLoop( animate );
  208. renderer.shadowMap.enabled = true;
  209. renderer.xr.enabled = true;
  210. renderer.xr.cameraAutoUpdate = false;
  211. container.appendChild( renderer.domElement );
  212. const sessionInit = {
  213. requiredFeatures: [ 'hand-tracking' ]
  214. };
  215. document.body.appendChild( VRButton.createButton( renderer, sessionInit ) );
  216. // controllers
  217. const controller1 = renderer.xr.getController( 0 );
  218. scene.add( controller1 );
  219. const controller2 = renderer.xr.getController( 1 );
  220. scene.add( controller2 );
  221. const controllerModelFactory = new XRControllerModelFactory();
  222. // Hand 1
  223. const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  224. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  225. scene.add( controllerGrip1 );
  226. const hand1 = renderer.xr.getHand( 0 );
  227. hand1.add( new OculusHandModel( hand1 ) );
  228. const handPointer1 = new OculusHandPointerModel( hand1, controller1 );
  229. hand1.add( handPointer1 );
  230. scene.add( hand1 );
  231. // Hand 2
  232. const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  233. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  234. scene.add( controllerGrip2 );
  235. const hand2 = renderer.xr.getHand( 1 );
  236. hand2.add( new OculusHandModel( hand2 ) );
  237. const handPointer2 = new OculusHandPointerModel( hand2, controller2 );
  238. hand2.add( handPointer2 );
  239. scene.add( hand2 );
  240. // setup objects in scene and entities
  241. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  242. const floorMaterial = new THREE.MeshPhongMaterial( { color: 0x222222 } );
  243. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  244. floor.rotation.x = - Math.PI / 2;
  245. floor.receiveShadow = true;
  246. scene.add( floor );
  247. const menuGeometry = new THREE.PlaneGeometry( 0.24, 0.5 );
  248. const menuMaterial = new THREE.MeshPhongMaterial( {
  249. opacity: 0,
  250. transparent: true,
  251. } );
  252. const menuMesh = new THREE.Mesh( menuGeometry, menuMaterial );
  253. menuMesh.position.set( 0.4, 1, - 1 );
  254. menuMesh.rotation.y = - Math.PI / 12;
  255. scene.add( menuMesh );
  256. const orangeButton = makeButtonMesh( 0.2, 0.1, 0.01, 0xffd3b5 );
  257. orangeButton.position.set( 0, 0.18, 0 );
  258. menuMesh.add( orangeButton );
  259. const pinkButton = makeButtonMesh( 0.2, 0.1, 0.01, 0xe84a5f );
  260. pinkButton.position.set( 0, 0.06, 0 );
  261. menuMesh.add( pinkButton );
  262. const resetButton = makeButtonMesh( 0.2, 0.1, 0.01, 0x355c7d );
  263. const resetButtonText = createText( 'reset', 0.06 );
  264. resetButton.add( resetButtonText );
  265. resetButtonText.position.set( 0, 0, 0.0051 );
  266. resetButton.position.set( 0, - 0.06, 0 );
  267. menuMesh.add( resetButton );
  268. const exitButton = makeButtonMesh( 0.2, 0.1, 0.01, 0xff0000 );
  269. const exitButtonText = createText( 'exit', 0.06 );
  270. exitButton.add( exitButtonText );
  271. exitButtonText.position.set( 0, 0, 0.0051 );
  272. exitButton.position.set( 0, - 0.18, 0 );
  273. menuMesh.add( exitButton );
  274. const tkGeometry = new THREE.TorusKnotGeometry( 0.5, 0.2, 200, 32 );
  275. const tkMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  276. tkMaterial.metalness = 0.8;
  277. const torusKnot = new THREE.Mesh( tkGeometry, tkMaterial );
  278. torusKnot.position.set( 0, 1, - 5 );
  279. scene.add( torusKnot );
  280. const instructionText = createText( 'This is a WebXR Hands demo, please explore with hands.', 0.04 );
  281. instructionText.position.set( 0, 1.6, - 0.6 );
  282. scene.add( instructionText );
  283. const exitText = createText( 'Exiting session...', 0.04 );
  284. exitText.position.set( 0, 1.5, - 0.6 );
  285. exitText.visible = false;
  286. scene.add( exitText );
  287. world
  288. .registerComponent( Object3D )
  289. .registerComponent( Button )
  290. .registerComponent( Intersectable )
  291. .registerComponent( Rotating )
  292. .registerComponent( HandsInstructionText )
  293. .registerComponent( OffsetFromCamera )
  294. .registerComponent( NeedCalibration );
  295. world
  296. .registerSystem( RotatingSystem )
  297. .registerSystem( InstructionSystem, { controllers: [ controllerGrip1, controllerGrip2 ] } )
  298. .registerSystem( CalibrationSystem, { renderer: renderer, camera: camera } )
  299. .registerSystem( ButtonSystem )
  300. .registerSystem( HandRaySystem, { handPointers: [ handPointer1, handPointer2 ] } );
  301. const menuEntity = world.createEntity();
  302. menuEntity.addComponent( Intersectable );
  303. menuEntity.addComponent( OffsetFromCamera, { x: 0.4, y: 0, z: - 1 } );
  304. menuEntity.addComponent( NeedCalibration );
  305. menuEntity.addComponent( Object3D, { object: menuMesh } );
  306. const obEntity = world.createEntity();
  307. obEntity.addComponent( Intersectable );
  308. obEntity.addComponent( Object3D, { object: orangeButton } );
  309. const obAction = function () {
  310. torusKnot.material.color.setHex( 0xffd3b5 );
  311. };
  312. obEntity.addComponent( Button, { action: obAction } );
  313. const pbEntity = world.createEntity();
  314. pbEntity.addComponent( Intersectable );
  315. pbEntity.addComponent( Object3D, { object: pinkButton } );
  316. const pbAction = function () {
  317. torusKnot.material.color.setHex( 0xe84a5f );
  318. };
  319. pbEntity.addComponent( Button, { action: pbAction } );
  320. const rbEntity = world.createEntity();
  321. rbEntity.addComponent( Intersectable );
  322. rbEntity.addComponent( Object3D, { object: resetButton } );
  323. const rbAction = function () {
  324. torusKnot.material.color.setHex( 0xffffff );
  325. };
  326. rbEntity.addComponent( Button, { action: rbAction } );
  327. const ebEntity = world.createEntity();
  328. ebEntity.addComponent( Intersectable );
  329. ebEntity.addComponent( Object3D, { object: exitButton } );
  330. const ebAction = function () {
  331. exitText.visible = true;
  332. setTimeout( function () {
  333. exitText.visible = false; renderer.xr.getSession().end();
  334. }, 2000 );
  335. };
  336. ebEntity.addComponent( Button, { action: ebAction } );
  337. const tkEntity = world.createEntity();
  338. tkEntity.addComponent( Rotating );
  339. tkEntity.addComponent( Object3D, { object: torusKnot } );
  340. const itEntity = world.createEntity();
  341. itEntity.addComponent( HandsInstructionText );
  342. itEntity.addComponent( Object3D, { object: instructionText } );
  343. window.addEventListener( 'resize', onWindowResize );
  344. }
  345. function onWindowResize() {
  346. camera.aspect = window.innerWidth / window.innerHeight;
  347. camera.updateProjectionMatrix();
  348. renderer.setSize( window.innerWidth, window.innerHeight );
  349. }
  350. function animate() {
  351. const delta = clock.getDelta();
  352. const elapsedTime = clock.elapsedTime;
  353. renderer.xr.updateCamera( camera );
  354. world.execute( delta, elapsedTime );
  355. renderer.render( scene, camera );
  356. }
  357. </script>
  358. </body>
  359. </html>