loading-3d-models.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Loading 3D Models</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Loading 3D Models">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Loading 3D Models</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>
  29. 3D models are available in hundreds of file formats, each with different
  30. purposes, assorted features, and varying complexity. Although
  31. <a href="https://github.com/mrdoob/three.js/tree/dev/examples/jsm/loaders" target="_blank" rel="noopener">
  32. three.js provides many loaders</a>, choosing the right format and
  33. workflow will save time and frustration later on. Some formats are
  34. difficult to work with, inefficient for realtime experiences, or simply not
  35. fully supported at this time.
  36. </p>
  37. <p>
  38. This guide provides a workflow recommended for most users, and suggestions
  39. for what to try if things don't go as expected.
  40. </p>
  41. <h2>Before we start</h2>
  42. <p>
  43. If you're new to running a local server, begin with
  44. <a href="installation.html">Installation</a>
  45. first. Many common errors viewing 3D models can be avoided by hosting files
  46. correctly.
  47. </p>
  48. <h2>Recommended workflow</h2>
  49. <p>
  50. Where possible, we recommend using glTF (GL Transmission Format). Both
  51. <small>.GLB</small> and <small>.GLTF</small> versions of the format are
  52. well supported. Because glTF is focused on runtime asset delivery, it is
  53. compact to transmit and fast to load. Features include meshes, materials,
  54. textures, skins, skeletons, morph targets, animations, lights, and
  55. cameras.
  56. </p>
  57. <p>
  58. Public-domain glTF files are available on sites like
  59. <a href="https://sketchfab.com/models?features=downloadable&sort_by=-likeCount&type=models" target="_blank" rel="noopener">
  60. Sketchfab</a>, or various tools include glTF export:
  61. </p>
  62. <ul>
  63. <li><a href="https://www.blender.org/" target="_blank" rel="noopener">Blender</a> by the Blender Foundation</li>
  64. <li><a href="https://www.allegorithmic.com/products/substance-painter" target="_blank" rel="noopener">Substance Painter</a> by Allegorithmic</li>
  65. <li><a href="https://www.foundry.com/products/modo" target="_blank" rel="noopener">Modo</a> by Foundry</li>
  66. <li><a href="https://www.marmoset.co/toolbag/" target="_blank" rel="noopener">Toolbag</a> by Marmoset</li>
  67. <li><a href="https://www.sidefx.com/products/houdini/" target="_blank" rel="noopener">Houdini</a> by SideFX</li>
  68. <li><a href="https://labs.maxon.net/?p=3360" target="_blank" rel="noopener">Cinema 4D</a> by MAXON</li>
  69. <li><a href="https://github.com/KhronosGroup/COLLADA2GLTF" target="_blank" rel="noopener">COLLADA2GLTF</a> by the Khronos Group</li>
  70. <li><a href="https://github.com/facebookincubator/FBX2glTF" target="_blank" rel="noopener">FBX2GLTF</a> by Facebook</li>
  71. <li><a href="https://github.com/AnalyticalGraphicsInc/obj2gltf" target="_blank" rel="noopener">OBJ2GLTF</a> by Analytical Graphics Inc</li>
  72. <li>&hellip;and <a href="http://github.khronos.org/glTF-Project-Explorer/" target="_blank" rel="noopener">many more</a></li>
  73. </ul>
  74. <p>
  75. If your preferred tools do not support glTF, consider requesting glTF
  76. export from the authors, or posting on
  77. <a href="https://github.com/KhronosGroup/glTF/issues/1051" target="_blank" rel="noopener">the glTF roadmap thread</a>.
  78. </p>
  79. <p>
  80. When glTF is not an option, popular formats such as FBX, OBJ, or COLLADA
  81. are also available and regularly maintained.
  82. </p>
  83. <h2>Loading</h2>
  84. <p>
  85. Only a few loaders (e.g. `ObjectLoader`) are included by default with
  86. three.js — others should be added to your app individually.
  87. </p>
  88. <pre class="prettyprint notranslate lang-js" translate="no">
  89. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  90. </pre>
  91. <p>
  92. Once you've imported a loader, you're ready to add a model to your scene. Syntax varies among
  93. different loaders — when using another format, check the examples and documentation for that
  94. loader. For glTF, usage with global scripts would be:
  95. </p>
  96. <pre class="prettyprint notranslate lang-js" translate="no">
  97. const loader = new GLTFLoader();
  98. loader.load( 'path/to/model.glb', function ( gltf ) {
  99. scene.add( gltf.scene );
  100. }, undefined, function ( error ) {
  101. console.error( error );
  102. } );
  103. </pre>
  104. <h2>Troubleshooting</h2>
  105. <p>
  106. You've spent hours modeling an artisanal masterpiece, you load it into
  107. the webpage, and — oh no! 😭 It's distorted, miscolored, or missing entirely.
  108. Start with these troubleshooting steps:
  109. </p>
  110. <ol>
  111. <li>
  112. Check the JavaScript console for errors, and make sure you've used an
  113. `onError` callback when calling `.load()` to log the result.
  114. </li>
  115. <li>
  116. View the model in another application. For glTF, drag-and-drop viewers
  117. are available for
  118. <a href="https://gltf-viewer.donmccurdy.com/" target="_blank" rel="noopener">three.js</a> and
  119. <a href="https://sandbox.babylonjs.com/" target="_blank" rel="noopener">babylon.js</a>. If the model
  120. appears correctly in one or more applications,
  121. <a href="https://github.com/mrdoob/three.js/issues/new" target="_blank" rel="noopener">file a bug against three.js</a>.
  122. If the model cannot be shown in any application, we strongly encourage
  123. filing a bug with the application used to create the model.
  124. </li>
  125. <li>
  126. Try scaling the model up or down by a factor of 1000. Many models are
  127. scaled differently, and large models may not appear if the camera is
  128. inside the model.
  129. </li>
  130. <li>
  131. Try to add and position a light source. The model may be hidden in the dark.
  132. </li>
  133. <li>
  134. Look for failed texture requests in the network tab, like
  135. `"C:\\Path\To\Model\texture.jpg"`. Use paths relative to your
  136. model instead, such as `images/texture.jpg` — this may require
  137. editing the model file in a text editor.
  138. </li>
  139. </ol>
  140. <h2>Asking for help</h2>
  141. <p>
  142. If you've gone through the troubleshooting process above and your model
  143. still isn't working, the right approach to asking for help will get you to
  144. a solution faster. Post a question on the
  145. <a href="https://discourse.threejs.org/" target="_blank" rel="noopener">three.js forum</a> and, whenever possible,
  146. include your model (or a simpler model with the same problem) in any formats
  147. you have available. Include enough information for someone else to reproduce
  148. the issue quickly — ideally, a live demo.
  149. </p>
  150. </div>
  151. </div>
  152. </div>
  153. <script src="../resources/prettify.js"></script>
  154. <script src="../resources/lesson.js"></script>
  155. </body></html>
粤ICP备19079148号