ColladaLoader.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. FileLoader,
  3. Loader,
  4. LoaderUtils,
  5. Scene,
  6. TextureLoader
  7. } from 'three';
  8. import { TGALoader } from '../loaders/TGALoader.js';
  9. import { ColladaParser } from './collada/ColladaParser.js';
  10. import { ColladaComposer } from './collada/ColladaComposer.js';
  11. /**
  12. * A loader for the Collada format.
  13. *
  14. * The Collada format is very complex so this loader only supports a subset of what
  15. * is defined in the [official specification](https://www.khronos.org/files/collada_spec_1_5.pdf).
  16. *
  17. * Assets with a Z-UP coordinate system are transformed it into Y-UP by a simple rotation.
  18. * The vertex data are not converted.
  19. *
  20. * ```js
  21. * const loader = new ColladaLoader();
  22. *
  23. * const result = await loader.loadAsync( './models/collada/elf/elf.dae' );
  24. * scene.add( result.scene );
  25. * ```
  26. *
  27. * @augments Loader
  28. * @three_import import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
  29. */
  30. class ColladaLoader extends Loader {
  31. /**
  32. * Starts loading from the given URL and passes the loaded Collada asset
  33. * to the `onLoad()` callback.
  34. *
  35. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  36. * @param {function({scene:Group,animations:Array<AnimationClip>,kinematics:Object})} onLoad - Executed when the loading process has been finished.
  37. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  38. * @param {onErrorCallback} onError - Executed when errors occur.
  39. */
  40. load( url, onLoad, onProgress, onError ) {
  41. const scope = this;
  42. const path = ( scope.path === '' ) ? LoaderUtils.extractUrlBase( url ) : scope.path;
  43. const loader = new FileLoader( scope.manager );
  44. loader.setPath( scope.path );
  45. loader.setRequestHeader( scope.requestHeader );
  46. loader.setWithCredentials( scope.withCredentials );
  47. loader.load( url, function ( text ) {
  48. try {
  49. onLoad( scope.parse( text, path ) );
  50. } catch ( e ) {
  51. if ( onError ) {
  52. onError( e );
  53. } else {
  54. console.error( e );
  55. }
  56. scope.manager.itemError( url );
  57. }
  58. }, onProgress, onError );
  59. }
  60. /**
  61. * Parses the given Collada data and returns a result object holding the parsed scene,
  62. * an array of animation clips and kinematics.
  63. *
  64. * @param {string} text - The raw Collada data as a string.
  65. * @param {string} [path] - The asset path.
  66. * @return {?{scene:Group,animations:Array<AnimationClip>,kinematics:Object}} An object representing the parsed asset.
  67. */
  68. parse( text, path ) {
  69. if ( text.length === 0 ) {
  70. return { scene: new Scene() };
  71. }
  72. // Parse XML to library data
  73. const parser = new ColladaParser();
  74. const parseResult = parser.parse( text );
  75. if ( parseResult === null ) {
  76. return null;
  77. }
  78. const { library, asset, collada } = parseResult;
  79. // Setup texture loaders
  80. const textureLoader = new TextureLoader( this.manager );
  81. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  82. let tgaLoader;
  83. if ( TGALoader ) {
  84. tgaLoader = new TGALoader( this.manager );
  85. tgaLoader.setPath( this.resourcePath || path );
  86. }
  87. // Compose Three.js objects from library data
  88. const composer = new ColladaComposer( library, collada, textureLoader, tgaLoader );
  89. const { scene, animations, kinematics } = composer.compose();
  90. scene.animations = animations;
  91. // Handle coordinate system conversion
  92. if ( asset.upAxis === 'Z_UP' ) {
  93. console.warn( 'THREE.ColladaLoader: You are loading an asset with a Z-UP coordinate system. The loader just rotates the asset to transform it into Y-UP. The vertex data are not converted, see #24289.' );
  94. scene.rotation.set( - Math.PI / 2, 0, 0 );
  95. }
  96. // Apply unit scale
  97. scene.scale.multiplyScalar( asset.unit );
  98. return {
  99. get animations() {
  100. console.warn( 'THREE.ColladaLoader: Please access animations over scene.animations now.' );
  101. return animations;
  102. },
  103. kinematics: kinematics,
  104. library: library,
  105. scene: scene
  106. };
  107. }
  108. }
  109. export { ColladaLoader };
粤ICP备19079148号