XRControllerModelFactory.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import {
  2. Mesh,
  3. MeshBasicMaterial,
  4. Object3D,
  5. Quaternion,
  6. SphereGeometry,
  7. } from '../../../build/three.module.js';
  8. import { GLTFLoader } from '../loaders/GLTFLoader.js';
  9. import {
  10. Constants as MotionControllerConstants,
  11. fetchProfile,
  12. MotionController
  13. } from '../libs/motion-controllers.module.js';
  14. const DEFAULT_PROFILES_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles';
  15. const DEFAULT_PROFILE = 'generic-trigger';
  16. function XRControllerModel( ) {
  17. Object3D.call( this );
  18. this.motionController = null;
  19. this.envMap = null;
  20. }
  21. XRControllerModel.prototype = Object.assign( Object.create( Object3D.prototype ), {
  22. constructor: XRControllerModel,
  23. setEnvironmentMap: function ( envMap ) {
  24. if ( this.envMap == envMap ) {
  25. return this;
  26. }
  27. this.envMap = envMap;
  28. this.traverse( ( child ) => {
  29. if ( child.isMesh ) {
  30. child.material.envMap = this.envMap;
  31. child.material.needsUpdate = true;
  32. }
  33. } );
  34. return this;
  35. },
  36. /**
  37. * Polls data from the XRInputSource and updates the model's components to match
  38. * the real world data
  39. */
  40. updateMatrixWorld: function ( force ) {
  41. Object3D.prototype.updateMatrixWorld.call( this, force );
  42. if ( ! this.motionController ) return;
  43. // Cause the MotionController to poll the Gamepad for data
  44. this.motionController.updateFromGamepad();
  45. // Update the 3D model to reflect the button, thumbstick, and touchpad state
  46. Object.values( this.motionController.components ).forEach( ( component ) => {
  47. // Update node data based on the visual responses' current states
  48. Object.values( component.visualResponses ).forEach( ( visualResponse ) => {
  49. const { valueNode, minNode, maxNode, value, valueNodeProperty } = visualResponse;
  50. // Skip if the visual response node is not found. No error is needed,
  51. // because it will have been reported at load time.
  52. if ( ! valueNode ) return;
  53. // Calculate the new properties based on the weight supplied
  54. if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.VISIBILITY ) {
  55. valueNode.visible = value;
  56. } else if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.TRANSFORM ) {
  57. Quaternion.slerp(
  58. minNode.quaternion,
  59. maxNode.quaternion,
  60. valueNode.quaternion,
  61. value
  62. );
  63. valueNode.position.lerpVectors(
  64. minNode.position,
  65. maxNode.position,
  66. value
  67. );
  68. }
  69. } );
  70. } );
  71. }
  72. } );
  73. /**
  74. * Walks the model's tree to find the nodes needed to animate the components and
  75. * saves them to the motionContoller components for use in the frame loop. When
  76. * touchpads are found, attaches a touch dot to them.
  77. */
  78. function findNodes( motionController, scene ) {
  79. // Loop through the components and find the nodes needed for each components' visual responses
  80. Object.values( motionController.components ).forEach( ( component ) => {
  81. const { type, touchPointNodeName, visualResponses } = component;
  82. if ( type === MotionControllerConstants.ComponentType.TOUCHPAD ) {
  83. component.touchPointNode = scene.getObjectByName( touchPointNodeName );
  84. if ( component.touchPointNode ) {
  85. // Attach a touch dot to the touchpad.
  86. const sphereGeometry = new SphereGeometry( 0.001 );
  87. const material = new MeshBasicMaterial( { color: 0x0000FF } );
  88. const sphere = new Mesh( sphereGeometry, material );
  89. component.touchPointNode.add( sphere );
  90. } else {
  91. console.warn( `Could not find touch dot, ${component.touchPointNodeName}, in touchpad component ${component.id}` );
  92. }
  93. }
  94. // Loop through all the visual responses to be applied to this component
  95. Object.values( visualResponses ).forEach( ( visualResponse ) => {
  96. const { valueNodeName, minNodeName, maxNodeName, valueNodeProperty } = visualResponse;
  97. // If animating a transform, find the two nodes to be interpolated between.
  98. if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.TRANSFORM ) {
  99. visualResponse.minNode = scene.getObjectByName( minNodeName );
  100. visualResponse.maxNode = scene.getObjectByName( maxNodeName );
  101. // If the extents cannot be found, skip this animation
  102. if ( ! visualResponse.minNode ) {
  103. console.warn( `Could not find ${minNodeName} in the model` );
  104. return;
  105. }
  106. if ( ! visualResponse.maxNode ) {
  107. console.warn( `Could not find ${maxNodeName} in the model` );
  108. return;
  109. }
  110. }
  111. // If the target node cannot be found, skip this animation
  112. visualResponse.valueNode = scene.getObjectByName( valueNodeName );
  113. if ( ! visualResponse.valueNode ) {
  114. console.warn( `Could not find ${valueNodeName} in the model` );
  115. }
  116. } );
  117. } );
  118. }
  119. function addAssetSceneToControllerModel( controllerModel, scene ) {
  120. // Find the nodes needed for animation and cache them on the motionController.
  121. findNodes( controllerModel.motionController, scene );
  122. // Apply any environment map that the mesh already has set.
  123. if ( controllerModel.envMap ) {
  124. scene.traverse( ( child ) => {
  125. if ( child.isMesh ) {
  126. child.material.envMap = controllerModel.envMap;
  127. child.material.needsUpdate = true;
  128. }
  129. } );
  130. }
  131. // Add the glTF scene to the controllerModel.
  132. controllerModel.add( scene );
  133. }
  134. var XRControllerModelFactory = ( function () {
  135. function XRControllerModelFactory( gltfLoader = null ) {
  136. this.gltfLoader = gltfLoader;
  137. this.path = DEFAULT_PROFILES_PATH;
  138. this._assetCache = {};
  139. // If a GLTFLoader wasn't supplied to the constructor create a new one.
  140. if ( ! this.gltfLoader ) {
  141. this.gltfLoader = new GLTFLoader();
  142. }
  143. }
  144. XRControllerModelFactory.prototype = {
  145. constructor: XRControllerModelFactory,
  146. createControllerModel: function ( controller ) {
  147. const controllerModel = new XRControllerModel();
  148. let scene = null;
  149. controller.addEventListener( 'connected', ( event ) => {
  150. const xrInputSource = event.data;
  151. if ( xrInputSource.targetRayMode !== 'tracked-pointer' || ! xrInputSource.gamepad ) return;
  152. fetchProfile( xrInputSource, this.path, DEFAULT_PROFILE ).then( ( { profile, assetPath } ) => {
  153. controllerModel.motionController = new MotionController(
  154. xrInputSource,
  155. profile,
  156. assetPath
  157. );
  158. const cachedAsset = this._assetCache[ controllerModel.motionController.assetUrl ];
  159. if ( cachedAsset ) {
  160. scene = cachedAsset.scene.clone();
  161. addAssetSceneToControllerModel( controllerModel, scene );
  162. } else {
  163. if ( ! this.gltfLoader ) {
  164. throw new Error( 'GLTFLoader not set.' );
  165. }
  166. this.gltfLoader.setPath( '' );
  167. this.gltfLoader.load( controllerModel.motionController.assetUrl, ( asset ) => {
  168. this._assetCache[ controllerModel.motionController.assetUrl ] = asset;
  169. scene = asset.scene.clone();
  170. addAssetSceneToControllerModel( controllerModel, scene );
  171. },
  172. null,
  173. () => {
  174. throw new Error( `Asset ${controllerModel.motionController.assetUrl} missing or malformed.` );
  175. } );
  176. }
  177. } ).catch( ( err ) => {
  178. console.warn( err );
  179. } );
  180. } );
  181. controller.addEventListener( 'disconnected', () => {
  182. controllerModel.motionController = null;
  183. controllerModel.remove( scene );
  184. scene = null;
  185. } );
  186. return controllerModel;
  187. }
  188. };
  189. return XRControllerModelFactory;
  190. } )();
  191. export { XRControllerModelFactory };