webxr_vr_handinput_pressbutton.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput - press button</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 - press button<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 { createText } from './jsm/webxr/Text2D.js';
  20. import { World, System, Component, TagComponent, Types } from './jsm/libs/ecsy.module.js';
  21. class Object3D extends Component { }
  22. Object3D.schema = {
  23. object: { type: Types.Ref }
  24. };
  25. class Button extends Component { }
  26. Button.schema = {
  27. // button states: [resting, pressed, fully_pressed, recovering]
  28. currState: { type: Types.String, default: 'resting' },
  29. prevState: { type: Types.String, default: 'resting' },
  30. pressSound: { type: Types.Ref, default: null },
  31. releaseSound: { type: Types.Ref, default: null },
  32. restingY: { type: Types.Number, default: null },
  33. surfaceY: { type: Types.Number, default: null },
  34. recoverySpeed: { type: Types.Number, default: 0.4 },
  35. fullPressDistance: { type: Types.Number, default: null },
  36. action: { type: Types.Ref, default: () => { } }
  37. };
  38. class ButtonSystem extends System {
  39. init( attributes ) {
  40. this.renderer = attributes.renderer;
  41. this.soundAdded = false;
  42. }
  43. execute( /*delta, time*/ ) {
  44. let buttonPressSound, buttonReleaseSound;
  45. if ( this.renderer.xr.getSession() && ! this.soundAdded ) {
  46. const xrCamera = this.renderer.xr.getCamera( camera );
  47. const listener = new THREE.AudioListener();
  48. xrCamera.add( listener );
  49. // create a global audio source
  50. buttonPressSound = new THREE.Audio( listener );
  51. buttonReleaseSound = new THREE.Audio( listener );
  52. // load a sound and set it as the Audio object's buffer
  53. const audioLoader = new THREE.AudioLoader();
  54. audioLoader.load( 'sounds/button-press.ogg', function ( buffer ) {
  55. buttonPressSound.setBuffer( buffer );
  56. } );
  57. audioLoader.load( 'sounds/button-release.ogg', function ( buffer ) {
  58. buttonReleaseSound.setBuffer( buffer );
  59. } );
  60. this.soundAdded = true;
  61. }
  62. this.queries.buttons.results.forEach( entity => {
  63. const button = entity.getMutableComponent( Button );
  64. const buttonMesh = entity.getComponent( Object3D ).object;
  65. // populate restingY
  66. if ( button.restingY == null ) {
  67. button.restingY = buttonMesh.position.y;
  68. }
  69. if ( buttonPressSound ) {
  70. button.pressSound = buttonPressSound;
  71. }
  72. if ( buttonReleaseSound ) {
  73. button.releaseSound = buttonReleaseSound;
  74. }
  75. if ( button.currState == 'fully_pressed' && button.prevState != 'fully_pressed' ) {
  76. button.pressSound?.play();
  77. button.action();
  78. }
  79. if ( button.currState == 'recovering' && button.prevState != 'recovering' ) {
  80. button.releaseSound?.play();
  81. }
  82. // preserve prevState, clear currState
  83. // FingerInputSystem will update currState
  84. button.prevState = button.currState;
  85. button.currState = 'resting';
  86. } );
  87. }
  88. }
  89. ButtonSystem.queries = {
  90. buttons: {
  91. components: [ Button ]
  92. }
  93. };
  94. class Pressable extends TagComponent { }
  95. class FingerInputSystem extends System {
  96. init( attributes ) {
  97. this.hands = attributes.hands;
  98. }
  99. execute( delta/*, time*/ ) {
  100. this.queries.pressable.results.forEach( entity => {
  101. const button = entity.getMutableComponent( Button );
  102. const object = entity.getComponent( Object3D ).object;
  103. const pressingDistances = [];
  104. this.hands.forEach( hand => {
  105. if ( hand && hand.intersectBoxObject( object ) ) {
  106. const pressingPosition = hand.getPointerPosition();
  107. pressingDistances.push( button.surfaceY - object.worldToLocal( pressingPosition ).y );
  108. }
  109. } );
  110. if ( pressingDistances.length == 0 ) { // not pressed this frame
  111. if ( object.position.y < button.restingY ) {
  112. object.position.y += button.recoverySpeed * delta;
  113. button.currState = 'recovering';
  114. } else {
  115. object.position.y = button.restingY;
  116. button.currState = 'resting';
  117. }
  118. } else {
  119. button.currState = 'pressed';
  120. const pressingDistance = Math.max( pressingDistances );
  121. if ( pressingDistance > 0 ) {
  122. object.position.y -= pressingDistance;
  123. }
  124. if ( object.position.y <= button.restingY - button.fullPressDistance ) {
  125. button.currState = 'fully_pressed';
  126. object.position.y = button.restingY - button.fullPressDistance;
  127. }
  128. }
  129. } );
  130. }
  131. }
  132. FingerInputSystem.queries = {
  133. pressable: {
  134. components: [ Pressable ]
  135. }
  136. };
  137. class Rotating extends TagComponent { }
  138. class RotatingSystem extends System {
  139. execute( delta/*, time*/ ) {
  140. this.queries.rotatingObjects.results.forEach( entity => {
  141. const object = entity.getComponent( Object3D ).object;
  142. object.rotation.x += 0.4 * delta;
  143. object.rotation.y += 0.4 * delta;
  144. } );
  145. }
  146. }
  147. RotatingSystem.queries = {
  148. rotatingObjects: {
  149. components: [ Rotating ]
  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. const world = new World();
  207. const clock = new THREE.Clock();
  208. let camera, scene, renderer;
  209. init();
  210. animate();
  211. function makeButtonMesh( x, y, z, color ) {
  212. const geometry = new THREE.BoxGeometry( x, y, z );
  213. const material = new THREE.MeshPhongMaterial( { color: color } );
  214. const buttonMesh = new THREE.Mesh( geometry, material );
  215. buttonMesh.castShadow = true;
  216. buttonMesh.receiveShadow = true;
  217. return buttonMesh;
  218. }
  219. function init() {
  220. const container = document.createElement( 'div' );
  221. document.body.appendChild( container );
  222. scene = new THREE.Scene();
  223. scene.background = new THREE.Color( 0x444444 );
  224. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  225. camera.position.set( 0, 1.2, 0.3 );
  226. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  227. const light = new THREE.DirectionalLight( 0xffffff );
  228. light.position.set( 0, 6, 0 );
  229. light.castShadow = true;
  230. light.shadow.camera.top = 2;
  231. light.shadow.camera.bottom = - 2;
  232. light.shadow.camera.right = 2;
  233. light.shadow.camera.left = - 2;
  234. light.shadow.mapSize.set( 4096, 4096 );
  235. scene.add( light );
  236. renderer = new THREE.WebGLRenderer( { antialias: true } );
  237. renderer.setPixelRatio( window.devicePixelRatio );
  238. renderer.setSize( window.innerWidth, window.innerHeight );
  239. renderer.outputEncoding = THREE.sRGBEncoding;
  240. renderer.shadowMap.enabled = true;
  241. renderer.xr.enabled = true;
  242. container.appendChild( renderer.domElement );
  243. document.body.appendChild( VRButton.createButton( renderer ) );
  244. // controllers
  245. const controller1 = renderer.xr.getController( 0 );
  246. scene.add( controller1 );
  247. const controller2 = renderer.xr.getController( 1 );
  248. scene.add( controller2 );
  249. const controllerModelFactory = new XRControllerModelFactory();
  250. // Hand 1
  251. const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  252. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  253. scene.add( controllerGrip1 );
  254. const hand1 = renderer.xr.getHand( 0 );
  255. const handModel1 = new OculusHandModel( hand1 );
  256. hand1.add( handModel1 );
  257. scene.add( hand1 );
  258. // Hand 2
  259. const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  260. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  261. scene.add( controllerGrip2 );
  262. const hand2 = renderer.xr.getHand( 1 );
  263. const handModel2 = new OculusHandModel( hand2 );
  264. hand2.add( handModel2 );
  265. scene.add( hand2 );
  266. // buttons
  267. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  268. const floorMaterial = new THREE.MeshPhongMaterial( { color: 0x222222 } );
  269. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  270. floor.rotation.x = - Math.PI / 2;
  271. floor.receiveShadow = true;
  272. scene.add( floor );
  273. const consoleGeometry = new THREE.BoxGeometry( 0.5, 0.12, 0.15 );
  274. const consoleMaterial = new THREE.MeshPhongMaterial( { color: 0x595959 } );
  275. const consoleMesh = new THREE.Mesh( consoleGeometry, consoleMaterial );
  276. consoleMesh.position.set( 0, 1, - 0.3 );
  277. consoleMesh.castShadow = true;
  278. consoleMesh.receiveShadow = true;
  279. scene.add( consoleMesh );
  280. const orangeButton = makeButtonMesh( 0.08, 0.1, 0.08, 0xffd3b5 );
  281. orangeButton.position.set( - 0.15, 0.04, 0 );
  282. consoleMesh.add( orangeButton );
  283. const pinkButton = makeButtonMesh( 0.08, 0.1, 0.08, 0xe84a5f );
  284. pinkButton.position.set( - 0.05, 0.04, 0 );
  285. consoleMesh.add( pinkButton );
  286. const resetButton = makeButtonMesh( 0.08, 0.1, 0.08, 0x355c7d );
  287. const resetButtonText = createText( 'reset', 0.03 );
  288. resetButton.add( resetButtonText );
  289. resetButtonText.rotation.x = - Math.PI / 2;
  290. resetButtonText.position.set( 0, 0.051, 0 );
  291. resetButton.position.set( 0.05, 0.04, 0 );
  292. consoleMesh.add( resetButton );
  293. const exitButton = makeButtonMesh( 0.08, 0.1, 0.08, 0xff0000 );
  294. const exitButtonText = createText( 'exit', 0.03 );
  295. exitButton.add( exitButtonText );
  296. exitButtonText.rotation.x = - Math.PI / 2;
  297. exitButtonText.position.set( 0, 0.051, 0 );
  298. exitButton.position.set( 0.15, 0.04, 0 );
  299. consoleMesh.add( exitButton );
  300. const tkGeometry = new THREE.TorusKnotGeometry( 0.5, 0.2, 200, 32 );
  301. const tkMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  302. tkMaterial.metalness = 0.8;
  303. const torusKnot = new THREE.Mesh( tkGeometry, tkMaterial );
  304. torusKnot.position.set( 0, 1, - 5 );
  305. scene.add( torusKnot );
  306. const instructionText = createText( 'This is a WebXR Hands demo, please explore with hands.', 0.04 );
  307. instructionText.position.set( 0, 1.6, - 0.6 );
  308. scene.add( instructionText );
  309. const exitText = createText( 'Exiting session...', 0.04 );
  310. exitText.position.set( 0, 1.5, - 0.6 );
  311. exitText.visible = false;
  312. scene.add( exitText );
  313. world
  314. .registerComponent( Object3D )
  315. .registerComponent( Button )
  316. .registerComponent( Pressable )
  317. .registerComponent( Rotating )
  318. .registerComponent( HandsInstructionText )
  319. .registerComponent( OffsetFromCamera )
  320. .registerComponent( NeedCalibration );
  321. world
  322. .registerSystem( RotatingSystem )
  323. .registerSystem( InstructionSystem, { controllers: [ controllerGrip1, controllerGrip2 ] } )
  324. .registerSystem( CalibrationSystem, { renderer: renderer, camera: camera } )
  325. .registerSystem( ButtonSystem, { renderer: renderer, camera: camera } )
  326. .registerSystem( FingerInputSystem, { hands: [ handModel1, handModel2 ] } );
  327. const csEntity = world.createEntity();
  328. csEntity.addComponent( OffsetFromCamera, { x: 0, y: - 0.4, z: - 0.3 } );
  329. csEntity.addComponent( NeedCalibration );
  330. csEntity.addComponent( Object3D, { object: consoleMesh } );
  331. const obEntity = world.createEntity();
  332. obEntity.addComponent( Pressable );
  333. obEntity.addComponent( Object3D, { object: orangeButton } );
  334. const obAction = function () {
  335. torusKnot.material.color.setHex( 0xffd3b5 );
  336. };
  337. obEntity.addComponent( Button, { action: obAction, surfaceY: 0.05, fullPressDistance: 0.02 } );
  338. const pbEntity = world.createEntity();
  339. pbEntity.addComponent( Pressable );
  340. pbEntity.addComponent( Object3D, { object: pinkButton } );
  341. const pbAction = function () {
  342. torusKnot.material.color.setHex( 0xe84a5f );
  343. };
  344. pbEntity.addComponent( Button, { action: pbAction, surfaceY: 0.05, fullPressDistance: 0.02 } );
  345. const rbEntity = world.createEntity();
  346. rbEntity.addComponent( Pressable );
  347. rbEntity.addComponent( Object3D, { object: resetButton } );
  348. const rbAction = function () {
  349. torusKnot.material.color.setHex( 0xffffff );
  350. };
  351. rbEntity.addComponent( Button, { action: rbAction, surfaceY: 0.05, fullPressDistance: 0.02 } );
  352. const ebEntity = world.createEntity();
  353. ebEntity.addComponent( Pressable );
  354. ebEntity.addComponent( Object3D, { object: exitButton } );
  355. const ebAction = function () {
  356. exitText.visible = true;
  357. setTimeout( function () {
  358. exitText.visible = false; renderer.xr.getSession().end();
  359. }, 2000 );
  360. };
  361. ebEntity.addComponent( Button, { action: ebAction, surfaceY: 0.05, recoverySpeed: 0.2, fullPressDistance: 0.03 } );
  362. const tkEntity = world.createEntity();
  363. tkEntity.addComponent( Rotating );
  364. tkEntity.addComponent( Object3D, { object: torusKnot } );
  365. const itEntity = world.createEntity();
  366. itEntity.addComponent( HandsInstructionText );
  367. itEntity.addComponent( Object3D, { object: instructionText } );
  368. window.addEventListener( 'resize', onWindowResize );
  369. }
  370. function onWindowResize() {
  371. camera.aspect = window.innerWidth / window.innerHeight;
  372. camera.updateProjectionMatrix();
  373. renderer.setSize( window.innerWidth, window.innerHeight );
  374. }
  375. function animate() {
  376. renderer.setAnimationLoop( render );
  377. }
  378. function render() {
  379. const delta = clock.getDelta();
  380. const elapsedTime = clock.elapsedTime;
  381. world.execute( delta, elapsedTime );
  382. renderer.render( scene, camera );
  383. }
  384. </script>
  385. </body>
  386. </html>