GLTFLoader.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 (experimental)</li>
  31. </ul>
  32. <h2>Example</h2>
  33. <code>
  34. // Instantiate a loader
  35. var loader = new THREE.GLTFLoader();
  36. // Optional: Provide a DRACOLoader instance to decode compressed mesh data
  37. THREE.DRACOLoader.setDecoderPath( '/examples/js/libs/draco' );
  38. loader.setDRACOLoader( new THREE.DRACOLoader() );
  39. // Load a glTF resource
  40. loader.load(
  41. // resource URL
  42. 'models/gltf/duck/duck.gltf',
  43. // called when the resource is loaded
  44. function ( gltf ) {
  45. scene.add( gltf.scene );
  46. gltf.animations; // Array&lt;THREE.AnimationClip&gt;
  47. gltf.scene; // THREE.Scene
  48. gltf.scenes; // Array&lt;THREE.Scene&gt;
  49. gltf.cameras; // Array&lt;THREE.Camera&gt;
  50. gltf.asset; // Object
  51. },
  52. // called while loading is progressing
  53. function ( xhr ) {
  54. console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  55. },
  56. // called when loading has errors
  57. function ( error ) {
  58. console.log( 'An error happened' );
  59. }
  60. );
  61. </code>
  62. [example:webgl_loader_gltf]
  63. <h2>Browser compatibility</h2>
  64. <p>GLTFLoader relies on ES6 [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promises],
  65. which are not supported in IE11. To use the loader in IE11, you must
  66. [link:https://github.com/stefanpenner/es6-promise include a polyfill]
  67. providing a Promise replacement.</p>
  68. <h2>Textures</h2>
  69. <p>Textures containing color information (.map, .emissiveMap, and .specularMap) always use sRGB colorspace in
  70. glTF, while vertex colors and material properties (.color, .emissive, .specular) use linear colorspace. In a
  71. typical rendering workflow, textures are converted to linear colorspace by the renderer, lighting calculations
  72. are made, then final output is converted back to sRGB and displayed on screen. Unless you need post-processing
  73. in linear colorspace, always configure [page:WebGLRenderer] as follows when using glTF:</p>
  74. <code>
  75. renderer.gammaOutput = true;
  76. renderer.gammaFactor = 2.2;
  77. </code>
  78. <p>GLTFLoader will automatically configure textures referenced from a .gltf or .glb file correctly, with the
  79. assumption that the renderer is set up as shown above. When loading textures externally (e.g., using
  80. [page:TextureLoader]) and applying them to a glTF model, colorspace and orientation must be given:</p>
  81. <code>
  82. // If texture is used for color information, set colorspace.
  83. texture.encoding = THREE.sRGBEncoding;
  84. // UVs use the convention that (0, 0) corresponds to the upper left corner of a texture.
  85. texture.flipY = false;
  86. </code>
  87. <h2>Custom extensions</h2>
  88. <p>
  89. Metadata from unknown extensions is preserved as “.userData.gltfExtensions” on Object3D, Scene, and Material instances,
  90. or attached to the response “gltf” object. Example:
  91. </p>
  92. <code>
  93. loader.load('foo.gltf', function ( gltf ) {
  94. var scene = gltf.scene;
  95. var mesh = scene.children[ 3 ];
  96. var fooExtension = mesh.userData.gltfExtensions.EXT_foo;
  97. gltf.parser.getDependency( 'bufferView', fooExtension.bufferView )
  98. .then( function ( fooBuffer ) { ... } );
  99. } );
  100. </code>
  101. <br>
  102. <hr>
  103. <h2>Constructor</h2>
  104. <h3>[name]( [param:LoadingManager manager] )</h3>
  105. <p>
  106. [page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
  107. </p>
  108. <p>
  109. Creates a new [name].
  110. </p>
  111. <h2>Properties</h2>
  112. <h2>Methods</h2>
  113. <h3>[method:null load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  114. <p>
  115. [page:String url] — A string containing the path/URL of the <em>.gltf</em> or <em>.glb</em> file.<br />
  116. [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 />
  117. [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 />
  118. [page:Function onError] — (optional) A function to be called if an error occurs during loading. The function receives error as an argument.<br />
  119. </p>
  120. <p>
  121. Begin loading from url and call the callback function with the parsed response content.
  122. </p>
  123. <h3>[method:null setPath]( [param:String path] )</h3>
  124. <p>
  125. [page:String path] — Base path for loading additional resources e.g. textures and .bin data.
  126. </p>
  127. <p>
  128. Set the base path for additional resources.
  129. </p>
  130. <h3>[method:null setCrossOrigin]( [param:String value] )</h3>
  131. <p>
  132. [page:String value] — The crossOrigin string to implement CORS for loading the url from a different domain that allows CORS.
  133. </p>
  134. <h3>[method:null setDRACOLoader]( [param:DRACOLoader dracoLoader] )</h3>
  135. <p>
  136. [page:DRACOLoader dracoLoader] — Instance of THREE.DRACOLoader, to be used for decoding assets compressed with the KHR_draco_mesh_compression extension.
  137. </p>
  138. <p>
  139. 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.
  140. </p>
  141. <h3>[method:null parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )</h3>
  142. <p>
  143. [page:ArrayBuffer data] — glTF asset to parse, as an ArrayBuffer or <em>JSON</em> string.<br />
  144. [page:String path] — The base path from which to find subsequent glTF resources such as textures and .bin data files.<br />
  145. [page:Function onLoad] — A function to be called when parse completes.<br />
  146. [page:Function onError] — (optional) A function to be called if an error occurs during parsing. The function receives error as an argument.<br />
  147. </p>
  148. <p>
  149. 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].
  150. </p>
  151. <h2>Source</h2>
  152. [link:https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/GLTFLoader.js examples/js/loaders/GLTFLoader.js]
  153. </body>
  154. </html>