XRControllerModelLoader.js 6.6 KB

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