webxr_vr_handinput_pressbutton.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webxr hands - 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 "https://ecsy.io/build/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. let 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. var button = entity.getMutableComponent(Button);
  64. var 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. var button = entity.getMutableComponent(Button);
  102. let object = entity.getComponent(Object3D).object;
  103. let pressingDistances = [];
  104. this.hands.forEach(hand => {
  105. if (hand && hand.intersectBoxObject(object)) {
  106. let 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. let 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. var 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. var 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. let offset = entity.getComponent(OffsetFromCamera);
  191. let object = entity.getComponent(Object3D).object;
  192. let 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. let world = new World();
  207. var 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. return buttonMesh;
  216. }
  217. function init() {
  218. let container = document.createElement('div');
  219. document.body.appendChild(container);
  220. scene = new THREE.Scene();
  221. scene.background = new THREE.Color(0x444444);
  222. camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10);
  223. camera.position.set(0, 1.2, 0.3);
  224. scene.add(new THREE.HemisphereLight(0x808080, 0x606060));
  225. const light = new THREE.DirectionalLight(0xffffff);
  226. light.position.set(0, 6, 0);
  227. light.castShadow = true;
  228. light.shadow.camera.top = 2;
  229. light.shadow.camera.bottom = - 2;
  230. light.shadow.camera.right = 2;
  231. light.shadow.camera.left = - 2;
  232. light.shadow.mapSize.set(4096, 4096);
  233. scene.add(light);
  234. renderer = new THREE.WebGLRenderer({ antialias: true });
  235. renderer.setPixelRatio(window.devicePixelRatio);
  236. renderer.setSize(window.innerWidth, window.innerHeight);
  237. renderer.outputEncoding = THREE.sRGBEncoding;
  238. renderer.shadowMap.enabled = true;
  239. renderer.xr.enabled = true;
  240. container.appendChild(renderer.domElement);
  241. document.body.appendChild(VRButton.createButton(renderer));
  242. // controllers
  243. let controller1 = renderer.xr.getController(0);
  244. scene.add(controller1);
  245. let controller2 = renderer.xr.getController(1);
  246. scene.add(controller2);
  247. const controllerModelFactory = new XRControllerModelFactory();
  248. // Hand 1
  249. let controllerGrip1 = renderer.xr.getControllerGrip(0);
  250. controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1));
  251. scene.add(controllerGrip1);
  252. let hand1 = renderer.xr.getHand(0);
  253. let handModel1 = new OculusHandModel(hand1)
  254. hand1.add(handModel1);
  255. scene.add(hand1);
  256. // Hand 2
  257. let controllerGrip2 = renderer.xr.getControllerGrip(1);
  258. controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2));
  259. scene.add(controllerGrip2);
  260. let hand2 = renderer.xr.getHand(1);
  261. let handModel2 = new OculusHandModel(hand2);
  262. hand2.add(handModel2);
  263. scene.add(hand2);
  264. // buttons
  265. const floorGeometry = new THREE.PlaneGeometry(4, 4);
  266. const floorMaterial = new THREE.MeshPhongMaterial({ color: 0x222222 });
  267. const floor = new THREE.Mesh(floorGeometry, floorMaterial);
  268. floor.rotation.x = - Math.PI / 2;
  269. scene.add(floor);
  270. const consoleGeometry = new THREE.BoxGeometry(0.5, 0.12, 0.15);
  271. const consoleMaterial = new THREE.MeshPhongMaterial({ color: 0x595959 });
  272. let consoleMesh = new THREE.Mesh(consoleGeometry, consoleMaterial);
  273. consoleMesh.position.set(0, 1, -0.3);
  274. scene.add(consoleMesh);
  275. let orangeButton = makeButtonMesh(0.08, 0.1, 0.08, 0xffd3b5);
  276. orangeButton.position.set(-0.15, 0.04, 0);
  277. consoleMesh.add(orangeButton);
  278. let pinkButton = makeButtonMesh(0.08, 0.1, 0.08, 0xe84a5f);
  279. pinkButton.position.set(-0.05, 0.04, 0);
  280. consoleMesh.add(pinkButton);
  281. let resetButton = makeButtonMesh(0.08, 0.1, 0.08, 0x355c7d);
  282. let resetButtonText = createText("reset", 0.03);
  283. resetButton.add(resetButtonText);
  284. resetButtonText.rotation.x = - Math.PI / 2;
  285. resetButtonText.position.set(0, 0.051, 0);
  286. resetButton.position.set(0.05, 0.04, 0);
  287. consoleMesh.add(resetButton);
  288. let exitButton = makeButtonMesh(0.08, 0.1, 0.08, 0xff0000);
  289. let exitButtonText = createText("exit", 0.03);
  290. exitButton.add(exitButtonText);
  291. exitButtonText.rotation.x = - Math.PI / 2;
  292. exitButtonText.position.set(0, 0.051, 0);
  293. exitButton.position.set(0.15, 0.04, 0);
  294. consoleMesh.add(exitButton);
  295. let tkGeometry = new THREE.TorusKnotGeometry(0.5, 0.2, 200, 32);
  296. let tkMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
  297. tkMaterial.metalness = 0.8;
  298. let torusKnot = new THREE.Mesh(tkGeometry, tkMaterial);
  299. torusKnot.position.set(0, 1, -5);
  300. scene.add(torusKnot);
  301. let instructionText = createText("This is a WebXR Hands demo, please explore with hands.", 0.04);
  302. instructionText.position.set(0, 1.6, -0.6);
  303. scene.add(instructionText);
  304. let exitText = createText("Exiting session...", 0.04);
  305. exitText.position.set(0, 1.5, -0.6);
  306. exitText.visible = false;
  307. scene.add(exitText);
  308. world
  309. .registerComponent(Object3D)
  310. .registerComponent(Button)
  311. .registerComponent(Pressable)
  312. .registerComponent(Rotating)
  313. .registerComponent(HandsInstructionText)
  314. .registerComponent(OffsetFromCamera)
  315. .registerComponent(NeedCalibration);
  316. world
  317. .registerSystem(RotatingSystem)
  318. .registerSystem(InstructionSystem, { controllers: [controllerGrip1, controllerGrip2] })
  319. .registerSystem(CalibrationSystem, { renderer: renderer, camera: camera })
  320. .registerSystem(ButtonSystem, { renderer: renderer, camera: camera })
  321. .registerSystem(FingerInputSystem, { hands: [handModel1, handModel2] });
  322. var csEntity = world.createEntity();
  323. csEntity.addComponent(OffsetFromCamera, { x: 0, y: -0.4, z: -0.3 });
  324. csEntity.addComponent(NeedCalibration);
  325. csEntity.addComponent(Object3D, { object: consoleMesh });
  326. var obEntity = world.createEntity();
  327. obEntity.addComponent(Pressable);
  328. obEntity.addComponent(Object3D, { object: orangeButton });
  329. let obAction = function () { torusKnot.material.color.setHex(0xffd3b5); };
  330. obEntity.addComponent(Button, { action: obAction, surfaceY: 0.05, fullPressDistance: 0.02 });
  331. var pbEntity = world.createEntity();
  332. pbEntity.addComponent(Pressable);
  333. pbEntity.addComponent(Object3D, { object: pinkButton });
  334. let pbAction = function () { torusKnot.material.color.setHex(0xe84a5f); };
  335. pbEntity.addComponent(Button, { action: pbAction, surfaceY: 0.05, fullPressDistance: 0.02 });
  336. var rbEntity = world.createEntity();
  337. rbEntity.addComponent(Pressable);
  338. rbEntity.addComponent(Object3D, { object: resetButton });
  339. let rbAction = function () { torusKnot.material.color.setHex(0xffffff); };
  340. rbEntity.addComponent(Button, { action: rbAction, surfaceY: 0.05, fullPressDistance: 0.02 });
  341. var ebEntity = world.createEntity();
  342. ebEntity.addComponent(Pressable);
  343. ebEntity.addComponent(Object3D, { object: exitButton });
  344. let ebAction = function () {
  345. exitText.visible = true;
  346. setTimeout(function () { exitText.visible = false; renderer.xr.getSession().end(); }, 2000);
  347. };
  348. ebEntity.addComponent(Button, { action: ebAction, surfaceY: 0.05, recoverySpeed: 0.2, fullPressDistance: 0.03 });
  349. var tkEntity = world.createEntity();
  350. tkEntity.addComponent(Rotating);
  351. tkEntity.addComponent(Object3D, { object: torusKnot });
  352. var itEntity = world.createEntity();
  353. itEntity.addComponent(HandsInstructionText);
  354. itEntity.addComponent(Object3D, { object: instructionText });
  355. window.addEventListener('resize', onWindowResize);
  356. }
  357. function onWindowResize() {
  358. camera.aspect = window.innerWidth / window.innerHeight;
  359. camera.updateProjectionMatrix();
  360. renderer.setSize(window.innerWidth, window.innerHeight);
  361. }
  362. function animate() {
  363. renderer.setAnimationLoop(render);
  364. }
  365. function render() {
  366. var delta = clock.getDelta();
  367. var elapsedTime = clock.elapsedTime;
  368. world.execute(delta, elapsedTime);
  369. renderer.render(scene, camera);
  370. }
  371. </script>
  372. </body>
  373. </html>