2
0

webxr_vr_handinput_pointerclick.html 13 KB

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