Import-via-modules.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <h1>[name]</h1><br />
  12. <p>
  13. While importing three.js via script tags is a great way to get up and running fast, it has a few drawbacks for longer lived projects, for example:
  14. <ul>
  15. <li>You have to manually fetch and include a copy of the library as part of your project's source code</li>
  16. <li>Updating the library's version is a manual process</li>
  17. <li>When checking in a new version of the library your version control diffs are cluttered by the many lines of the build file</li>
  18. </ul>
  19. </p>
  20. <p>Using a dependency manager like npm avoids these caveats by allowing you to simply download and import your desired version of the library onto your machine.</p>
  21. <h2>Installation via npm</h2>
  22. <p>Three.js is published as an npm module, see: [link:https://www.npmjs.com/package/three npm]. This means all you need to do to include three.js into your project is run "npm install three"</p>
  23. <h2>Importing the module</h2>
  24. <p>Assuming that you're bundling your files with a tool such as [link:https://webpack.github.io/ Webpack] or [link:https://github.com/substack/node-browserify Browserify], which allow you to "require('modules')" in the browser by bundling up all of your dependencies.</p>
  25. <p>
  26. You should now be able to import the module into your source files and continue to use it as per normal.
  27. </p>
  28. <code>
  29. var THREE = require('three');
  30. var scene = new THREE.Scene();
  31. ...
  32. </code>
  33. <p>
  34. You're also able to leverage [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import ES6 import syntax]:
  35. </p>
  36. <code>
  37. import * as THREE from 'three';
  38. const scene = new THREE.Scene();
  39. ...
  40. </code>
  41. <p>
  42. or if you wish to import only select parts of three.js library, for example Scene:
  43. </p>
  44. <code>
  45. import { Scene } from 'three';
  46. const scene = new Scene();
  47. ...
  48. </code>
  49. <h2>Importable Examples</h2>
  50. <p>
  51. The core of three.js is focused on the most important components of a 3D engine. Many other components like loaders or controls are part of the
  52. examples directory. three.js ensures that these files are kept in sync with the core but users have to import them separately if they are required
  53. for their project. However, most of these files are not modules which makes their usage in certain cases inconvenient. In order to address this issue,
  54. we are working to provide all the examples as modules in the [link:https://github.com/mrdoob/three.js/tree/master/examples/jsm examples/jsm] directory.
  55. If you install three.js via npm, you can import them like so:
  56. </p>
  57. <code>
  58. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  59. </code>
  60. <p>
  61. The following examples files are already available as modules:
  62. <ul>
  63. <li>controls
  64. <ul>
  65. <li>DeviceOrientationControls</li>
  66. <li>DragControls</li>
  67. <li>EditorControls</li>
  68. <li>FirstPersonControls</li>
  69. <li>FlyControls</li>
  70. <li>MapControls</li>
  71. <li>OrbitControls</li>
  72. <li>OrthographicTrackballControls</li>
  73. <li>PointerLockControls</li>
  74. <li>TrackballControls</li>
  75. <li>TransformControls</li>
  76. </ul>
  77. </li>
  78. <li>curves
  79. <ul>
  80. <li>NURBSCurve</li>
  81. <li>NURBSSurface</li>
  82. <li>NURBSUtils</li>
  83. </ul>
  84. </li>
  85. <li>exporters
  86. <ul>
  87. <li>ColladaExporter</li>
  88. <li>GLTFExporter</li>
  89. <li>MMDExporter</li>
  90. <li>OBJExporter</li>
  91. <li>PLYExporter</li>
  92. <li>STLExporter</li>
  93. <li>TypedGeometryExporter</li>
  94. </ul>
  95. </li>
  96. <li>loaders
  97. <ul>
  98. <li>BVHLoader</li>
  99. <li>ColladaLoader</li>
  100. <li>DDSLoader</li>
  101. <li>FBXLoader</li>
  102. <li>GLTFLoader</li>
  103. <li>MTLLoader</li>
  104. <li>OBJLoader</li>
  105. <li>PCDLoader</li>
  106. <li>PDBLoader</li>
  107. <li>PLYLoader</li>
  108. <li>STLLoader</li>
  109. <li>SVGLoader</li>
  110. <li>TGALoader</li>
  111. <li>VRMLLoader</li>
  112. </ul>
  113. </li>
  114. <li>pmrem
  115. <ul>
  116. <li>PMREMCubeUVPacker</li>
  117. <li>PMREMGenerator</li>
  118. </ul>
  119. </li>
  120. <li>renderers
  121. <ul>
  122. <li>CSS2DRenderer</li>
  123. <li>CSS3DRenderer</li>
  124. <li>Projector</li>
  125. <li>SoftwareRenderer</li>
  126. <li>SVGRenderer</li>
  127. </ul>
  128. </li>
  129. <li>utils
  130. <ul>
  131. <li>BufferGeometryUtils</li>
  132. <li>GeometryUtils</li>
  133. <li>MathUtils</li>
  134. <li>SceneUtils</li>
  135. <li>ShadowMapViewer</li>
  136. <li>SkeletonUtils</li>
  137. <li>TypedArrayUtils</li>
  138. <li>UVsDebug</li>
  139. </ul>
  140. </li>
  141. </ul>
  142. </p>
  143. <p>
  144. Note: When using code from the examples directory, it's important that all files match the version of
  145. your three.js main file. For example, it's not acceptable to use *GLTFLoader* and *OrbitControls* from R96 together
  146. with three.js R103. You can easily keep your files in sync by using the modules from the JSM directory. If the file
  147. is not available as a module, you can still use third-party npm packages or convert the file to a module by yourself.
  148. In both cases, ensure the code is compatible with your three.js main file.
  149. </p>
  150. </body>
  151. </html>