GLTFLoader.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="zh">
  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>GLTF加载器([name])</h1>
  12. <p class="desc">用于载入<em>glTF 2.0</em>资源的加载器。<br /><br />
  13. [link:https://www.khronos.org/gltf glTF](gl传输格式)是一种开放格式的规范
  14. ([link:https://github.com/KhronosGroup/glTF/tree/master/specification/2.0 open format specification]),
  15. 用于更高效地传输、加载3D内容。该类文件以JSON(.gltf)格式或二进制(.glb)格式提供,
  16. 外部文件存储贴图(.jpg、.png)和额外的二进制数据(.bin)。一个glTF组件可传输一个或多个场景,
  17. 包括网格、材质、贴图、蒙皮、骨架、变形目标、动画、灯光以及摄像机。
  18. </p>
  19. <p>
  20. [name] uses [page:ImageBitmapLoader] whenever possible. Be advised that image bitmaps are not automatically GC-collected when they are no longer referenced,
  21. 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.
  22. </p>
  23. <h2>进口</h2>
  24. <p>
  25. [name] 是一个附加组件,必须显式导入。
  26. See [link:#manual/introduction/Installation Installation / Addons].
  27. </p>
  28. <code>
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. </code>
  31. <h2>扩展</h2>
  32. <p>
  33. GLTFLoader支持下列glTF 2.0扩展([link:https://github.com/KhronosGroup/glTF/tree/master/extensions/ glTF 2.0 extensions]):
  34. </p>
  35. <ul>
  36. <li>KHR_draco_mesh_compression</li>
  37. <li>KHR_materials_clearcoat</li>
  38. <li>KHR_materials_ior</li>
  39. <li>KHR_materials_specular</li>
  40. <li>KHR_materials_transmission</li>
  41. <li>KHR_materials_iridescence</li>
  42. <li>KHR_materials_unlit</li>
  43. <li>KHR_materials_volume</li>
  44. <li>KHR_mesh_quantization</li>
  45. <li>KHR_lights_punctual<sup>1</sup></li>
  46. <li>KHR_texture_basisu</li>
  47. <li>KHR_texture_transform<sup>2</sup></li>
  48. <li>EXT_texture_webp</li>
  49. <li>EXT_meshopt_compression</li>
  50. <li>EXT_mesh_gpu_instancing</li>
  51. </ul>
  52. <p>
  53. The following glTF 2.0 extension is supported by an external user plugin
  54. </p>
  55. <ul>
  56. <li>[link:https://github.com/takahirox/three-gltf-extensions KHR_materials_variants]<sup>3</sup></li>
  57. <li>[link:https://github.com/takahirox/three-gltf-extensions MSFT_texture_dds]</li>
  58. </ul>
  59. <p><i>
  60. <sup>1</sup>Requires [link:https://threejs.org/docs/#api/en/renderers/WebGLRenderer.useLegacyLights useLegacyLights] to be disabled.
  61. </i></p>
  62. <p><i>
  63. <sup>2</sup>支持UV变换,但存在一些重要的限制。
  64. Transforms applied to
  65. a texture using the first UV slot (all textures except aoMap and lightMap) must share the same
  66. transform, or no transform at all.
  67. aoMap 和 lightMap 纹理不能被变换。每个材质最多只能使用一次变换。
  68. 请参阅#[link:https://github.com/mrdoob/three.js/pull/13831 13831] 和
  69. #[link:https://github.com/mrdoob/three.js/issues/12788 12788]。
  70. </i></p>
  71. <p><i>
  72. <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].
  73. </i></p>
  74. <h2>代码示例</h2>
  75. <code>
  76. // Instantiate a loader
  77. const loader = new GLTFLoader();
  78. // Optional: Provide a DRACOLoader instance to decode compressed mesh data
  79. const dracoLoader = new DRACOLoader();
  80. dracoLoader.setDecoderPath( '/examples/jsm/libs/draco/' );
  81. loader.setDRACOLoader( dracoLoader );
  82. // Load a glTF resource
  83. loader.load(
  84. // resource URL
  85. 'models/gltf/duck/duck.gltf',
  86. // called when the resource is loaded
  87. function ( gltf ) {
  88. scene.add( gltf.scene );
  89. gltf.animations; // Array&lt;THREE.AnimationClip&gt;
  90. gltf.scene; // THREE.Group
  91. gltf.scenes; // Array&lt;THREE.Group&gt;
  92. gltf.cameras; // Array&lt;THREE.Camera&gt;
  93. gltf.asset; // Object
  94. },
  95. // called while loading is progressing
  96. function ( xhr ) {
  97. console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  98. },
  99. // called when loading has errors
  100. function ( error ) {
  101. console.log( 'An error happened' );
  102. }
  103. );
  104. </code>
  105. <h2>例子</h2>
  106. <p>
  107. [example:webgl_loader_gltf]
  108. </p>
  109. <h2>纹理</h2>
  110. <p>纹理中包含的颜色信息(.map, .emissiveMap, 和 .specularMap)在glTF中总是使用sRGB颜色空间,而顶点颜色和材质属性(.color, .emissive, .specular)
  111. 则使用线性颜色空间。在典型的渲染工作流程中,纹理会被渲染器转换为线性颜色空间,进行光照计算,然后最终输出会被转换回 sRGB
  112. 颜色空间并显示在屏幕上。除非你需要使用线性颜色空间进行后期处理,否则请在使用glTF的时候将[page:WebGLRenderer]进行如下配置:</p>
  113. <code>
  114. renderer.outputColorSpace = THREE.SRGBColorSpace;
  115. </code>
  116. <p>假设渲染器的配置如上所示,则GLTFLoader将可以正确地自动配置从.gltf或.glb文件中引用的纹理。
  117. 当从外部加载纹理(例如,使用[page:TextureLoader])并将纹理应用到glTF模型,则必须给定对应的颜色空间与朝向:</p>
  118. <code>
  119. // If texture is used for color information, set colorspace.
  120. texture.colorSpace = THREE.SRGBColorSpace;
  121. // UVs use the convention that (0, 0) corresponds to the upper left corner of a texture.
  122. texture.flipY = false;
  123. </code>
  124. <h2>自定义扩展</h2>
  125. <p>
  126. 来自未知扩展的元数据会被保存到Object3D、Group和Material实例中上的“.userData.gltfExtensions”,
  127. 或被附加到 response “gltf”对象。示例:
  128. </p>
  129. <code>
  130. loader.load('foo.gltf', function ( gltf ) {
  131. const scene = gltf.scene;
  132. const mesh = scene.children[ 3 ];
  133. const fooExtension = mesh.userData.gltfExtensions.EXT_foo;
  134. gltf.parser.getDependency( 'bufferView', fooExtension.bufferView )
  135. .then( function ( fooBuffer ) { ... } );
  136. } );
  137. </code>
  138. <br>
  139. <hr>
  140. <h2>构造函数</h2>
  141. <h3>[name]( [param:LoadingManager manager] )</h3>
  142. <p>
  143. [page:LoadingManager manager] — 该加载器将要使用的 [page:LoadingManager loadingManager] 。默认为 [page:LoadingManager THREE.DefaultLoadingManager]。
  144. </p>
  145. <p>
  146. 创建一个新的[name]。
  147. </p>
  148. <h2>属性</h2>
  149. <p>共有属性请参见其基类[page:Loader]。</p>
  150. <h2>方法</h2>
  151. <p>共有方法请参见其基类[page:Loader]。</p>
  152. <h3>[method:undefined load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  153. <p>
  154. [page:String url] — 包含有<em>.gltf</em>/<em>.glb</em>文件路径/URL的字符串。<br />
  155. [page:Function onLoad] — 加载成功完成后将会被调用的函数。该函数接收[page:Function parse]所返回的已加载的JSON响应。<br />
  156. [page:Function onProgress] — (可选)加载正在进行过程中会被调用的函数。其参数将会是XMLHttpRequest实例,包含有总字节数.[page:Integer total]与已加载的字节数.[page:Integer loaded]。<br />
  157. [page:Function onError] — (可选)若在加载过程发生错误,将被调用的函数。该函数接收error来作为参数。<br />
  158. </p>
  159. <p>
  160. 开始从url加载,并使用解析过的响应内容调用回调函数。
  161. </p>
  162. <h3>[method:this setDRACOLoader]( [param:DRACOLoader dracoLoader] )</h3>
  163. <p>
  164. [page:DRACOLoader dracoLoader] — THREE.DRACOLoader的实例,用于解码使用KHR_draco_mesh_compression扩展压缩过的文件。
  165. </p>
  166. <p>
  167. 请参阅[link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm/libs/draco#readme readme]来了解Draco及其解码器的详细信息。
  168. </p>
  169. <h3>[method:undefined parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )</h3>
  170. <p>
  171. [page:ArrayBuffer data] — 需要解析的glTF文件,值为一个ArrayBuffer或<em>JSON</em>字符串。<br />
  172. [page:String path] — 用于找到后续glTF资源(如纹理和.bin数据文件)的基础路径。<br />
  173. [page:Function onLoad] — 解析成功完成后将会被调用的函数。<br />
  174. [page:Function onError] — (可选)若在解析过程发生错误,将被调用的函数。该函数接收error来作为参数。<br />
  175. </p>
  176. <p>
  177. 解析基于glTF的ArrayBuffer或<em>JSON</em>字符串,并在完成后触发[page:Function onLoad]回调。[page:Function onLoad]的参数将是一个包含有已加载部分的[page:Object]:.[page:Group scene]、 .[page:Array scenes]、 .[page:Array cameras]、 .[page:Array animations] 和 .[page:Object asset]。
  178. </p>
  179. <h2>源代码</h2>
  180. <p>
  181. [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/GLTFLoader.js examples/jsm/loaders/GLTFLoader.js]
  182. </p>
  183. </body>
  184. </html>