webxr_vr_handinput_pointerclick.html 13 KB

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