GLTFLoader.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. [page:Loader] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc"> A loader for `glTF 2.0` resources. <br /><br />
  13. [link:https://www.khronos.org/gltf glTF] (GL Transmission Format) is an
  14. [link:https://github.com/KhronosGroup/glTF/tree/master/specification/2.0 open format specification]
  15. for efficient delivery and loading of 3D content. Assets may be provided either in JSON (.gltf)
  16. or binary (.glb) format. External files store textures (.jpg, .png) and additional binary
  17. data (.bin). A glTF asset may deliver one or more scenes, including meshes, materials,
  18. textures, skins, skeletons, morph targets, animations, lights, and/or cameras.
  19. </p>
  20. <p>
  21. [name] uses [page:ImageBitmapLoader] whenever possible. Be advised that image bitmaps are not automatically GC-collected when they are no longer referenced,
  22. and they require special handling during the disposal process. More information in the [link:https://threejs.org/docs/#manual/en/introduction/How-to-dispose-of-objects How to dispose of objects] guide.
  23. </p>
  24. <h2>Import</h2>
  25. <p>
  26. [name] is an add-on, and must be imported explicitly.
  27. See [link:#manual/introduction/Installation Installation / Addons].
  28. </p>
  29. <code>
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. </code>
  32. <h2>Extensions</h2>
  33. <p>
  34. GLTFLoader supports the following
  35. [link:https://github.com/KhronosGroup/glTF/tree/master/extensions/ glTF 2.0 extensions]:
  36. </p>
  37. <ul>
  38. <li>KHR_draco_mesh_compression</li>
  39. <li>KHR_materials_clearcoat</li>
  40. <li>KHR_materials_ior</li>
  41. <li>KHR_materials_specular</li>
  42. <li>KHR_materials_transmission</li>
  43. <li>KHR_materials_iridescence</li>
  44. <li>KHR_materials_unlit</li>
  45. <li>KHR_materials_volume</li>
  46. <li>KHR_mesh_quantization</li>
  47. <li>KHR_lights_punctual</li>
  48. <li>KHR_texture_basisu</li>
  49. <li>KHR_texture_transform</li>
  50. <li>EXT_texture_webp</li>
  51. <li>EXT_meshopt_compression</li>
  52. <li>EXT_mesh_gpu_instancing</li>
  53. </ul>
  54. <p>
  55. The following glTF 2.0 extension is supported by an external user plugin
  56. </p>
  57. <ul>
  58. <li>[link:https://github.com/takahirox/three-gltf-extensions KHR_materials_variants]<sup>1</sup></li>
  59. <li>[link:https://github.com/takahirox/three-gltf-extensions MSFT_texture_dds]</li>
  60. </ul>
  61. <p><i>
  62. <sup>1</sup>You can also manually process the extension after loading in your application. See [link:https://threejs.org/examples/#webgl_loader_gltf_variants Three.js glTF materials variants example].
  63. </i></p>
  64. <h2>Code Example</h2>
  65. <code>
  66. // Instantiate a loader
  67. const loader = new GLTFLoader();
  68. // Optional: Provide a DRACOLoader instance to decode compressed mesh data
  69. const dracoLoader = new DRACOLoader();
  70. dracoLoader.setDecoderPath( '/examples/jsm/libs/draco/' );
  71. loader.setDRACOLoader( dracoLoader );
  72. // Load a glTF resource
  73. loader.load(
  74. // resource URL
  75. 'models/gltf/duck/duck.gltf',
  76. // called when the resource is loaded
  77. function ( gltf ) {
  78. scene.add( gltf.scene );
  79. gltf.animations; // Array&lt;THREE.AnimationClip&gt;
  80. gltf.scene; // THREE.Group
  81. gltf.scenes; // Array&lt;THREE.Group&gt;
  82. gltf.cameras; // Array&lt;THREE.Camera&gt;
  83. gltf.asset; // Object
  84. },
  85. // called while loading is progressing
  86. function ( xhr ) {
  87. console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  88. },
  89. // called when loading has errors
  90. function ( error ) {
  91. console.log( 'An error happened' );
  92. }
  93. );
  94. </code>
  95. <h2>Examples</h2>
  96. <p>
  97. [example:webgl_loader_gltf]
  98. </p>
  99. <h2>Textures</h2>
  100. <p>When loading textures externally (e.g., using [page:TextureLoader]) and applying them to a glTF model,
  101. textures must be configured. Textures referenced from the glTF model are configured automatically by
  102. GLTFLoader.</p>
  103. <code>
  104. // If texture is used for color information (.map, .emissiveMap, .specularMap, ...), set color space
  105. texture.colorSpace = THREE.SRGBColorSpace;
  106. // UVs use the convention that (0, 0) corresponds to the upper left corner of a texture.
  107. texture.flipY = false;
  108. </code>
  109. <h2>Custom extensions</h2>
  110. <p>
  111. Metadata from unknown extensions is preserved as “.userData.gltfExtensions” on Object3D, Scene, and Material instances,
  112. or attached to the response “gltf” object. Example:
  113. </p>
  114. <code>
  115. loader.load('foo.gltf', function ( gltf ) {
  116. const scene = gltf.scene;
  117. const mesh = scene.children[ 3 ];
  118. const fooExtension = mesh.userData.gltfExtensions.EXT_foo;
  119. gltf.parser.getDependency( 'bufferView', fooExtension.bufferView )
  120. .then( function ( fooBuffer ) { ... } );
  121. } );
  122. </code>
  123. <br>
  124. <hr>
  125. <h2>Constructor</h2>
  126. <h3>[name]( [param:LoadingManager manager] )</h3>
  127. <p>
  128. [page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
  129. </p>
  130. <p>
  131. Creates a new [name].
  132. </p>
  133. <h2>Properties</h2>
  134. <p>See the base [page:Loader] class for common properties.</p>
  135. <h2>Methods</h2>
  136. <p>See the base [page:Loader] class for common methods.</p>
  137. <h3>[method:undefined load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  138. <p>
  139. [page:String url] — A string containing the path/URL of the `.gltf` or `.glb` file.<br />
  140. [page:Function onLoad] — A function to be called after the loading is successfully completed. The function receives the loaded JSON response returned from [page:Function parse].<br />
  141. [page:Function onProgress] — (optional) A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, that contains .[page:Integer total] and .[page:Integer loaded] bytes. If the server does not set the Content-Length header; .[page:Integer total] will be 0.<br />
  142. [page:Function onError] — (optional) A function to be called if an error occurs during loading. The function receives error as an argument.<br />
  143. </p>
  144. <p>
  145. Begin loading from url and call the callback function with the parsed response content.
  146. </p>
  147. <h3>[method:this setDRACOLoader]( [param:DRACOLoader dracoLoader] )</h3>
  148. <p>
  149. [page:DRACOLoader dracoLoader] — Instance of THREE.DRACOLoader, to be used for decoding assets compressed with the KHR_draco_mesh_compression extension.
  150. </p>
  151. <p>
  152. Refer to this [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm/libs/draco#readme readme] for the details of Draco and its decoder.
  153. </p>
  154. <h3>[method:this setKTX2Loader]( [param:KTX2Loader ktx2Loader] )</h3>
  155. <p>
  156. [page:KTX2Loader ktx2Loader] — Instance of THREE.KTX2Loader, to be used for loading KTX2 compressed textures.
  157. </p>
  158. <h3>[method:undefined parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )</h3>
  159. <p>
  160. [page:ArrayBuffer data] — glTF asset to parse, as an `ArrayBuffer`, `JSON` string or object.<br />
  161. [page:String path] — The base path from which to find subsequent glTF resources such as textures and .bin data files.<br />
  162. [page:Function onLoad] — A function to be called when parse completes.<br />
  163. [page:Function onError] — (optional) A function to be called if an error occurs during parsing. The function receives error as an argument.<br />
  164. </p>
  165. <p>
  166. Parse a glTF-based `ArrayBuffer`, `JSON` string or object and fire [page:Function onLoad] callback when complete. The argument to [page:Function onLoad] will be an [page:Object] that contains loaded parts: .[page:Group scene], .[page:Array scenes], .[page:Array cameras], .[page:Array animations], and .[page:Object asset].
  167. </p>
  168. <h2>Source</h2>
  169. <p>
  170. [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/GLTFLoader.js examples/jsm/loaders/GLTFLoader.js]
  171. </p>
  172. </body>
  173. </html>