GLTFLoader.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <em>glTF 2.0</em> 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. <h2>Extensions</h2>
  21. <p>
  22. GLTFLoader supports the following
  23. [link:https://github.com/KhronosGroup/glTF/tree/master/extensions/ glTF 2.0 extensions]:
  24. </p>
  25. <ul>
  26. <li>KHR_draco_mesh_compression</li>
  27. <li>KHR_materials_clearcoat</li>
  28. <li>KHR_materials_pbrSpecularGlossiness</li>
  29. <li>KHR_materials_unlit</li>
  30. <li>KHR_mesh_quantization</li>
  31. <li>KHR_lights_punctual<sup>1</sup></li>
  32. <li>KHR_texture_basisu</li>
  33. <li>KHR_texture_transform<sup>2</sup></li>
  34. <li>EXT_texture_webp</li>
  35. <li>EXT_meshopt_compression</li>
  36. </ul>
  37. <p>
  38. The following glTF 2.0 extension is supported by an external user plugin
  39. </p>
  40. <ul>
  41. <li>[link:https://github.com/takahirox/three-gltf-extensions KHR_materials_variants]<sup>3</sup></li>
  42. <li>[link:https://github.com/takahirox/three-gltf-extensions MSFT_texture_dds]</li>
  43. </ul>
  44. <p><i>
  45. <sup>1</sup>Requires [link:https://threejs.org/docs/#api/en/renderers/WebGLRenderer.physicallyCorrectLights physicallyCorrectLights] to be enabled.
  46. </i></p>
  47. <p><i>
  48. <sup>2</sup>UV transforms are supported, with several key limitations. Transforms applied to
  49. a texture using the first UV slot (all textures except aoMap and lightMap) must share the same
  50. transform, or no transform at all. The aoMap and lightMap textures cannot be transformed. No
  51. more than one transform may be used per material. Each use of a texture with a unique
  52. transform will result in an additional GPU texture upload. See
  53. #[link:https://github.com/mrdoob/three.js/pull/13831 13831] and
  54. #[link:https://github.com/mrdoob/three.js/issues/12788 12788].
  55. </i></p>
  56. <p><i>
  57. <sup>3</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].
  58. </i></p>
  59. <h2>Code Example</h2>
  60. <code>
  61. // Instantiate a loader
  62. const loader = new GLTFLoader();
  63. // Optional: Provide a DRACOLoader instance to decode compressed mesh data
  64. const dracoLoader = new DRACOLoader();
  65. dracoLoader.setDecoderPath( '/examples/js/libs/draco/' );
  66. loader.setDRACOLoader( dracoLoader );
  67. // Load a glTF resource
  68. loader.load(
  69. // resource URL
  70. 'models/gltf/duck/duck.gltf',
  71. // called when the resource is loaded
  72. function ( gltf ) {
  73. scene.add( gltf.scene );
  74. gltf.animations; // Array&lt;THREE.AnimationClip&gt;
  75. gltf.scene; // THREE.Group
  76. gltf.scenes; // Array&lt;THREE.Group&gt;
  77. gltf.cameras; // Array&lt;THREE.Camera&gt;
  78. gltf.asset; // Object
  79. },
  80. // called while loading is progressing
  81. function ( xhr ) {
  82. console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  83. },
  84. // called when loading has errors
  85. function ( error ) {
  86. console.log( 'An error happened' );
  87. }
  88. );
  89. </code>
  90. <h2>Examples</h2>
  91. <p>
  92. [example:webgl_loader_gltf]
  93. </p>
  94. <h2>Browser compatibility</h2>
  95. <p>GLTFLoader relies on ES6 [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promises],
  96. which are not supported in IE11. To use the loader in IE11, you must
  97. [link:https://github.com/stefanpenner/es6-promise include a polyfill]
  98. providing a Promise replacement.</p>
  99. <h2>Textures</h2>
  100. <p>Textures containing color information (.map, .emissiveMap, and .specularMap) always use sRGB colorspace in
  101. glTF, while vertex colors and material properties (.color, .emissive, .specular) use linear colorspace. In a
  102. typical rendering workflow, textures are converted to linear colorspace by the renderer, lighting calculations
  103. are made, then final output is converted back to sRGB and displayed on screen. Unless you need post-processing
  104. in linear colorspace, always configure [page:WebGLRenderer] as follows when using glTF:</p>
  105. <code>
  106. renderer.outputEncoding = THREE.sRGBEncoding;
  107. </code>
  108. <p>GLTFLoader will automatically configure textures referenced from a .gltf or .glb file correctly, with the
  109. assumption that the renderer is set up as shown above. When loading textures externally (e.g., using
  110. [page:TextureLoader]) and applying them to a glTF model, colorspace and orientation must be given:</p>
  111. <code>
  112. // If texture is used for color information, set colorspace.
  113. texture.encoding = THREE.sRGBEncoding;
  114. // UVs use the convention that (0, 0) corresponds to the upper left corner of a texture.
  115. texture.flipY = false;
  116. </code>
  117. <h2>Custom extensions</h2>
  118. <p>
  119. Metadata from unknown extensions is preserved as “.userData.gltfExtensions” on Object3D, Scene, and Material instances,
  120. or attached to the response “gltf” object. Example:
  121. </p>
  122. <code>
  123. loader.load('foo.gltf', function ( gltf ) {
  124. const scene = gltf.scene;
  125. const mesh = scene.children[ 3 ];
  126. const fooExtension = mesh.userData.gltfExtensions.EXT_foo;
  127. gltf.parser.getDependency( 'bufferView', fooExtension.bufferView )
  128. .then( function ( fooBuffer ) { ... } );
  129. } );
  130. </code>
  131. <br>
  132. <hr>
  133. <h2>Constructor</h2>
  134. <h3>[name]( [param:LoadingManager manager] )</h3>
  135. <p>
  136. [page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
  137. </p>
  138. <p>
  139. Creates a new [name].
  140. </p>
  141. <h2>Properties</h2>
  142. <p>See the base [page:Loader] class for common properties.</p>
  143. <h2>Methods</h2>
  144. <p>See the base [page:Loader] class for common methods.</p>
  145. <h3>[method:null load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  146. <p>
  147. [page:String url] — A string containing the path/URL of the <em>.gltf</em> or <em>.glb</em> file.<br />
  148. [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 />
  149. [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.<br />
  150. [page:Function onError] — (optional) A function to be called if an error occurs during loading. The function receives error as an argument.<br />
  151. </p>
  152. <p>
  153. Begin loading from url and call the callback function with the parsed response content.
  154. </p>
  155. <h3>[method:null setDRACOLoader]( [param:DRACOLoader dracoLoader] )</h3>
  156. <p>
  157. [page:DRACOLoader dracoLoader] — Instance of THREE.DRACOLoader, to be used for decoding assets compressed with the KHR_draco_mesh_compression extension.
  158. </p>
  159. <p>
  160. Refer to this [link:https://github.com/mrdoob/three.js/tree/dev/examples/js/libs/draco#readme readme] for the details of Draco and its decoder.
  161. </p>
  162. <h3>[method:null setKTX2Loader]( [param:KTX2Loader ktx2Loader] )</h3>
  163. <p>
  164. [page:KTX2Loader ktx2Loader] — Instance of THREE.KTX2Loader, to be used for loading KTX2 compressed textures.
  165. </p>
  166. <h3>[method:null parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )</h3>
  167. <p>
  168. [page:ArrayBuffer data] — glTF asset to parse, as an ArrayBuffer or <em>JSON</em> string.<br />
  169. [page:String path] — The base path from which to find subsequent glTF resources such as textures and .bin data files.<br />
  170. [page:Function onLoad] — A function to be called when parse completes.<br />
  171. [page:Function onError] — (optional) A function to be called if an error occurs during parsing. The function receives error as an argument.<br />
  172. </p>
  173. <p>
  174. Parse a glTF-based ArrayBuffer or <em>JSON</em> String 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].
  175. </p>
  176. <h2>Source</h2>
  177. <p>
  178. [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/GLTFLoader.js examples/jsm/loaders/GLTFLoader.js]
  179. </p>
  180. </body>
  181. </html>