webxr_vr_handinput_pointerclick.html 14 KB

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