0
0

XRControllerModelFactory.js 6.9 KB

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