webxr_vr_handinput_pointerclick.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { VRButton } from 'three/addons/webxr/VRButton.js';
  24. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  25. import { OculusHandModel } from 'three/addons/webxr/OculusHandModel.js';
  26. import { OculusHandPointerModel } from 'three/addons/webxr/OculusHandPointerModel.js';
  27. import { createText } from 'three/addons/webxr/Text2D.js';
  28. import { World, System, Component, TagComponent, Types } from 'three/addons/libs/ecsy.module.js';
  29. class Object3D extends Component { }
  30. Object3D.schema = {
  31. object: { type: Types.Ref }
  32. };
  33. class Button extends Component { }
  34. Button.schema = {
  35. // button states: [none, hovered, pressed]
  36. currState: { type: Types.String, default: 'none' },
  37. prevState: { type: Types.String, default: 'none' },
  38. action: { type: Types.Ref, default: () => { } }
  39. };
  40. class ButtonSystem extends System {
  41. execute( /* delta, time */ ) {
  42. this.queries.buttons.results.forEach( entity => {
  43. const button = entity.getMutableComponent( Button );
  44. const buttonMesh = entity.getComponent( Object3D ).object;
  45. if ( button.currState == 'none' ) {
  46. buttonMesh.scale.set( 1, 1, 1 );
  47. } else {
  48. buttonMesh.scale.set( 1.1, 1.1, 1.1 );
  49. }
  50. if ( button.currState == 'pressed' && button.prevState != 'pressed' ) {
  51. button.action();
  52. }
  53. // preserve prevState, clear currState
  54. // HandRaySystem will update currState
  55. button.prevState = button.currState;
  56. button.currState = 'none';
  57. } );
  58. }
  59. }
  60. ButtonSystem.queries = {
  61. buttons: {
  62. components: [ Button ]
  63. }
  64. };
  65. class Intersectable extends TagComponent { }
  66. class HandRaySystem extends System {
  67. init( attributes ) {
  68. this.handPointers = attributes.handPointers;
  69. }
  70. execute( /* delta, time */ ) {
  71. this.handPointers.forEach( hp => {
  72. let distance = null;
  73. let intersectingEntity = null;
  74. this.queries.intersectable.results.forEach( entity => {
  75. const object = entity.getComponent( Object3D ).object;
  76. const intersections = hp.intersectObject( object, false );
  77. if ( intersections && intersections.length > 0 ) {
  78. if ( distance == null || intersections[ 0 ].distance < distance ) {
  79. distance = intersections[ 0 ].distance;
  80. intersectingEntity = entity;
  81. }
  82. }
  83. } );
  84. if ( distance ) {
  85. hp.setCursor( distance );
  86. if ( intersectingEntity.hasComponent( Button ) ) {
  87. const button = intersectingEntity.getMutableComponent( Button );
  88. if ( hp.isPinched() ) {
  89. button.currState = 'pressed';
  90. } else if ( button.currState != 'pressed' ) {
  91. button.currState = 'hovered';
  92. }
  93. }
  94. } else {
  95. hp.setCursor( 1.5 );
  96. }
  97. } );
  98. }
  99. }
  100. HandRaySystem.queries = {
  101. intersectable: {
  102. components: [ Intersectable ]
  103. }
  104. };
  105. class Rotating extends TagComponent { }
  106. class RotatingSystem extends System {
  107. execute( delta/*, time*/ ) {
  108. this.queries.rotatingObjects.results.forEach( entity => {
  109. const object = entity.getComponent( Object3D ).object;
  110. object.rotation.x += 0.4 * delta;
  111. object.rotation.y += 0.4 * delta;
  112. } );
  113. }
  114. }
  115. RotatingSystem.queries = {
  116. rotatingObjects: {
  117. components: [ Rotating ]
  118. }
  119. };
  120. class HandsInstructionText extends TagComponent { }
  121. class InstructionSystem extends System {
  122. init( attributes ) {
  123. this.controllers = attributes.controllers;
  124. }
  125. execute( /* delta, time */ ) {
  126. let visible = false;
  127. this.controllers.forEach( controller => {
  128. if ( controller.visible ) {
  129. visible = true;
  130. }
  131. } );
  132. this.queries.instructionTexts.results.forEach( entity => {
  133. const object = entity.getComponent( Object3D ).object;
  134. object.visible = visible;
  135. } );
  136. }
  137. }
  138. InstructionSystem.queries = {
  139. instructionTexts: {
  140. components: [ HandsInstructionText ]
  141. }
  142. };
  143. class OffsetFromCamera extends Component { }
  144. OffsetFromCamera.schema = {
  145. x: { type: Types.Number, default: 0 },
  146. y: { type: Types.Number, default: 0 },
  147. z: { type: Types.Number, default: 0 },
  148. };
  149. class NeedCalibration extends TagComponent { }
  150. class CalibrationSystem extends System {
  151. init( attributes ) {
  152. this.camera = attributes.camera;
  153. this.renderer = attributes.renderer;
  154. }
  155. execute( /* delta, time */ ) {
  156. this.queries.needCalibration.results.forEach( entity => {
  157. if ( this.renderer.xr.getSession() ) {
  158. const offset = entity.getComponent( OffsetFromCamera );
  159. const object = entity.getComponent( Object3D ).object;
  160. const xrCamera = this.renderer.xr.getCamera();
  161. object.position.x = xrCamera.position.x + offset.x;
  162. object.position.y = xrCamera.position.y + offset.y;
  163. object.position.z = xrCamera.position.z + offset.z;
  164. entity.removeComponent( NeedCalibration );
  165. }
  166. } );
  167. }
  168. }
  169. CalibrationSystem.queries = {
  170. needCalibration: {
  171. components: [ NeedCalibration ]
  172. }
  173. };
  174. const world = new World();
  175. const clock = new THREE.Clock();
  176. let camera, scene, renderer;
  177. init();
  178. function makeButtonMesh( x, y, z, color ) {
  179. const geometry = new THREE.BoxGeometry( x, y, z );
  180. const material = new THREE.MeshPhongMaterial( { color: color } );
  181. const buttonMesh = new THREE.Mesh( geometry, material );
  182. buttonMesh.castShadow = true;
  183. buttonMesh.receiveShadow = true;
  184. return buttonMesh;
  185. }
  186. function init() {
  187. const container = document.createElement( 'div' );
  188. document.body.appendChild( container );
  189. scene = new THREE.Scene();
  190. scene.background = new THREE.Color( 0x444444 );
  191. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  192. camera.position.set( 0, 1.2, 0.3 );
  193. scene.add( new THREE.HemisphereLight( 0xcccccc, 0x999999, 3 ) );
  194. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  195. light.position.set( 0, 6, 0 );
  196. light.castShadow = true;
  197. light.shadow.camera.top = 2;
  198. light.shadow.camera.bottom = - 2;
  199. light.shadow.camera.right = 2;
  200. light.shadow.camera.left = - 2;
  201. light.shadow.mapSize.set( 4096, 4096 );
  202. scene.add( light );
  203. renderer = new THREE.WebGLRenderer( { antialias: true } );
  204. renderer.setPixelRatio( window.devicePixelRatio );
  205. renderer.setSize( window.innerWidth, window.innerHeight );
  206. renderer.setAnimationLoop( animate );
  207. renderer.shadowMap.enabled = true;
  208. renderer.xr.enabled = true;
  209. renderer.xr.cameraAutoUpdate = false;
  210. container.appendChild( renderer.domElement );
  211. const sessionInit = {
  212. requiredFeatures: [ 'hand-tracking' ]
  213. };
  214. document.body.appendChild( VRButton.createButton( renderer, sessionInit ) );
  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. const delta = clock.getDelta();
  351. const elapsedTime = clock.elapsedTime;
  352. renderer.xr.updateCamera( camera );
  353. world.execute( delta, elapsedTime );
  354. renderer.render( scene, camera );
  355. }
  356. </script>
  357. </body>
  358. </html>
粤ICP备19079148号