GLTFLoader.html 8.7 KB

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