XRControllerModelFactory.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import {
  2. Mesh,
  3. MeshBasicMaterial,
  4. Object3D,
  5. SphereGeometry,
  6. } from 'three';
  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. /**
  16. * Represents a XR controller model.
  17. *
  18. * @augments Object3D
  19. */
  20. class XRControllerModel extends Object3D {
  21. /**
  22. * Constructs a new XR controller model.
  23. */
  24. constructor() {
  25. super();
  26. /**
  27. * The motion controller.
  28. *
  29. * @type {?MotionController}
  30. * @default null
  31. */
  32. this.motionController = null;
  33. /**
  34. * The controller's environment map.
  35. *
  36. * @type {?Texture}
  37. * @default null
  38. */
  39. this.envMap = null;
  40. }
  41. /**
  42. * Sets an environment map that is applied to the controller model.
  43. *
  44. * @param {?Texture} envMap - The environment map to apply.
  45. * @return {XRControllerModel} A reference to this instance.
  46. */
  47. setEnvironmentMap( envMap ) {
  48. if ( this.envMap == envMap ) {
  49. return this;
  50. }
  51. this.envMap = envMap;
  52. this.traverse( ( child ) => {
  53. if ( child.isMesh ) {
  54. child.material.envMap = this.envMap;
  55. child.material.needsUpdate = true;
  56. }
  57. } );
  58. return this;
  59. }
  60. /**
  61. * Overwritten with a custom implementation. Polls data from the XRInputSource and updates the
  62. * model's components to match the real world data.
  63. *
  64. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  65. * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
  66. */
  67. updateMatrixWorld( force ) {
  68. super.updateMatrixWorld( force );
  69. if ( ! this.motionController ) return;
  70. // Cause the MotionController to poll the Gamepad for data
  71. this.motionController.updateFromGamepad();
  72. // Update the 3D model to reflect the button, thumbstick, and touchpad state
  73. Object.values( this.motionController.components ).forEach( ( component ) => {
  74. // Update node data based on the visual responses' current states
  75. Object.values( component.visualResponses ).forEach( ( visualResponse ) => {
  76. const { valueNode, minNode, maxNode, value, valueNodeProperty } = visualResponse;
  77. // Skip if the visual response node is not found. No error is needed,
  78. // because it will have been reported at load time.
  79. if ( ! valueNode ) return;
  80. // Calculate the new properties based on the weight supplied
  81. if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.VISIBILITY ) {
  82. valueNode.visible = value;
  83. } else if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.TRANSFORM ) {
  84. valueNode.quaternion.slerpQuaternions(
  85. minNode.quaternion,
  86. maxNode.quaternion,
  87. value
  88. );
  89. valueNode.position.lerpVectors(
  90. minNode.position,
  91. maxNode.position,
  92. value
  93. );
  94. }
  95. } );
  96. } );
  97. }
  98. }
  99. /**
  100. * Walks the model's tree to find the nodes needed to animate the components and
  101. * saves them to the motionController components for use in the frame loop. When
  102. * touchpads are found, attaches a touch dot to them.
  103. *
  104. * @private
  105. * @param {MotionController} motionController
  106. * @param {Object3D} scene
  107. */
  108. function findNodes( motionController, scene ) {
  109. // Loop through the components and find the nodes needed for each components' visual responses
  110. Object.values( motionController.components ).forEach( ( component ) => {
  111. const { type, touchPointNodeName, visualResponses } = component;
  112. if ( type === MotionControllerConstants.ComponentType.TOUCHPAD ) {
  113. component.touchPointNode = scene.getObjectByName( touchPointNodeName );
  114. if ( component.touchPointNode ) {
  115. // Attach a touch dot to the touchpad.
  116. const sphereGeometry = new SphereGeometry( 0.001 );
  117. const material = new MeshBasicMaterial( { color: 0x0000FF } );
  118. const sphere = new Mesh( sphereGeometry, material );
  119. component.touchPointNode.add( sphere );
  120. } else {
  121. console.warn( `Could not find touch dot, ${component.touchPointNodeName}, in touchpad component ${component.id}` );
  122. }
  123. }
  124. // Loop through all the visual responses to be applied to this component
  125. Object.values( visualResponses ).forEach( ( visualResponse ) => {
  126. const { valueNodeName, minNodeName, maxNodeName, valueNodeProperty } = visualResponse;
  127. // If animating a transform, find the two nodes to be interpolated between.
  128. if ( valueNodeProperty === MotionControllerConstants.VisualResponseProperty.TRANSFORM ) {
  129. visualResponse.minNode = scene.getObjectByName( minNodeName );
  130. visualResponse.maxNode = scene.getObjectByName( maxNodeName );
  131. // If the extents cannot be found, skip this animation
  132. if ( ! visualResponse.minNode ) {
  133. console.warn( `Could not find ${minNodeName} in the model` );
  134. return;
  135. }
  136. if ( ! visualResponse.maxNode ) {
  137. console.warn( `Could not find ${maxNodeName} in the model` );
  138. return;
  139. }
  140. }
  141. // If the target node cannot be found, skip this animation
  142. visualResponse.valueNode = scene.getObjectByName( valueNodeName );
  143. if ( ! visualResponse.valueNode ) {
  144. console.warn( `Could not find ${valueNodeName} in the model` );
  145. }
  146. } );
  147. } );
  148. }
  149. function addAssetSceneToControllerModel( controllerModel, scene ) {
  150. // Find the nodes needed for animation and cache them on the motionController.
  151. findNodes( controllerModel.motionController, scene );
  152. // Apply any environment map that the mesh already has set.
  153. if ( controllerModel.envMap ) {
  154. scene.traverse( ( child ) => {
  155. if ( child.isMesh ) {
  156. child.material.envMap = controllerModel.envMap;
  157. child.material.needsUpdate = true;
  158. }
  159. } );
  160. }
  161. // Add the glTF scene to the controllerModel.
  162. controllerModel.add( scene );
  163. }
  164. /**
  165. * Allows to create controller models for WebXR controllers that can be added as a visual
  166. * representation to your scene. `XRControllerModelFactory` will automatically fetch controller
  167. * models that match what the user is holding as closely as possible. The models should be
  168. * attached to the object returned from getControllerGrip in order to match the orientation of
  169. * the held device.
  170. *
  171. * This module depends on the [motion-controllers]{@link https://github.com/immersive-web/webxr-input-profiles/blob/main/packages/motion-controllers/README.md}
  172. * third-part library.
  173. *
  174. * ```js
  175. * const controllerModelFactory = new XRControllerModelFactory();
  176. *
  177. * const controllerGrip = renderer.xr.getControllerGrip( 0 );
  178. * controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  179. * scene.add( controllerGrip );
  180. * ```
  181. *
  182. * @three_import import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  183. */
  184. class XRControllerModelFactory {
  185. /**
  186. * Constructs a new XR controller model factory.
  187. *
  188. * @param {?GLTFLoader} [gltfLoader=null] - A glTF loader that is used to load controller models.
  189. * @param {?Function} [onLoad=null] - A callback that is executed when a controller model has been loaded.
  190. */
  191. constructor( gltfLoader = null, onLoad = null ) {
  192. /**
  193. * A glTF loader that is used to load controller models.
  194. *
  195. * @type {?GLTFLoader}
  196. * @default null
  197. */
  198. this.gltfLoader = gltfLoader;
  199. /**
  200. * The path to the model repository.
  201. *
  202. * @type {string}
  203. */
  204. this.path = DEFAULT_PROFILES_PATH;
  205. this._assetCache = {};
  206. /**
  207. * A callback that is executed when a controller model has been loaded.
  208. *
  209. * @type {?Function}
  210. * @default null
  211. */
  212. this.onLoad = onLoad;
  213. // If a GLTFLoader wasn't supplied to the constructor create a new one.
  214. if ( ! this.gltfLoader ) {
  215. this.gltfLoader = new GLTFLoader();
  216. }
  217. }
  218. /**
  219. * Sets the path to the model repository.
  220. *
  221. * @param {string} path - The path to set.
  222. * @return {XRControllerModelFactory} A reference to this instance.
  223. */
  224. setPath( path ) {
  225. this.path = path;
  226. return this;
  227. }
  228. /**
  229. * Creates a controller model for the given WebXR controller.
  230. *
  231. * @param {Group} controller - The controller.
  232. * @return {XRControllerModel} The XR controller model.
  233. */
  234. createControllerModel( controller ) {
  235. const controllerModel = new XRControllerModel();
  236. let scene = null;
  237. controller.addEventListener( 'connected', ( event ) => {
  238. const xrInputSource = event.data;
  239. if ( xrInputSource.targetRayMode !== 'tracked-pointer' || ! xrInputSource.gamepad || xrInputSource.hand ) return;
  240. fetchProfile( xrInputSource, this.path, DEFAULT_PROFILE ).then( ( { profile, assetPath } ) => {
  241. controllerModel.motionController = new MotionController(
  242. xrInputSource,
  243. profile,
  244. assetPath
  245. );
  246. const cachedAsset = this._assetCache[ controllerModel.motionController.assetUrl ];
  247. if ( cachedAsset ) {
  248. scene = cachedAsset.scene.clone();
  249. addAssetSceneToControllerModel( controllerModel, scene );
  250. if ( this.onLoad ) this.onLoad( scene );
  251. } else {
  252. if ( ! this.gltfLoader ) {
  253. throw new Error( 'GLTFLoader not set.' );
  254. }
  255. this.gltfLoader.setPath( '' );
  256. this.gltfLoader.load( controllerModel.motionController.assetUrl, ( asset ) => {
  257. this._assetCache[ controllerModel.motionController.assetUrl ] = asset;
  258. scene = asset.scene.clone();
  259. addAssetSceneToControllerModel( controllerModel, scene );
  260. if ( this.onLoad ) this.onLoad( scene );
  261. },
  262. null,
  263. () => {
  264. throw new Error( `Asset ${controllerModel.motionController.assetUrl} missing or malformed.` );
  265. } );
  266. }
  267. } ).catch( ( err ) => {
  268. console.warn( err );
  269. } );
  270. } );
  271. controller.addEventListener( 'disconnected', () => {
  272. controllerModel.motionController = null;
  273. controllerModel.remove( scene );
  274. scene = null;
  275. } );
  276. return controllerModel;
  277. }
  278. }
  279. export { XRControllerModelFactory };