threepipe
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

vanilla-threejs.md 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ---
  2. prev:
  3. text: 'ShaderToy Shaders in Three.js'
  4. link: './shadertoy-player'
  5. next:
  6. text: 'Saving three.js properties in glTF'
  7. link: './gltf-three-extras-ext'
  8. aside: false
  9. ---
  10. # Using Vanilla Three.js code in Threepipe
  11. Threepipe is built on top of three.js, so most vanilla three.js code should work without any issues.
  12. However, since threepipe uses [a fork of three.js](https://github.com/repalash/three.js-modded), it is bundled within the package instead of being a separate dependency.
  13. This means you can use threepipe as a drop-in replacement for three.js in many cases.
  14. The three.js fork is [updated till r158](https://github.com/repalash/three.js-modded/releases), and is regularly updated with new features and bug fixes.
  15. ## Importing three.js objects
  16. Classes, types, constants, and functions from three.js can be imported from `threepipe` in the same way as you would import from `three`.
  17. With npm modules -
  18. ```typescript
  19. import { Scene, PerspectiveCamera, WebGLRenderer, Mesh, BoxGeometry, MeshBasicMaterial } from 'threepipe';
  20. ```
  21. In many cases, threepipe provides an extended class as an alternative to the original three.js class.
  22. These extended classes provide additional features and methods that are not available in the original three.js classes and provide better experience with typescript and autocomplete. It's not necessary to use these extended classes, and standard ones would work fine, but they are recommended for better experience.
  23. Some examples are -
  24. - `MeshPhysicalMaterial`, `MeshStandardMaterial` -> `PhysicalMaterial`
  25. - `MeshBasicMaterial` -> `UnlitMaterial`
  26. - `Mesh2` -> `Mesh`
  27. - `BufferGeometry2` -> `BufferGeometry`
  28. Check more [here](./../guide/viewer-api#other-classes-and-interfaces)
  29. Read the [Viewer API guide](./../guide/viewer-api) for more details on classes provided by threepipe.
  30. ## Using `THREE.` style
  31. You can use the `THREE.` style code in Threepipe, but you need to import the necessary classes from `threepipe` instead of `three`.
  32. With npm modules -
  33. ```typescript
  34. import * as THREE from 'threepipe';
  35. ```
  36. With UMD modules -
  37. ```html
  38. <script>window.THREE = window.threepipe</script>
  39. ```
  40. With import map -
  41. ```html
  42. <script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
  43. <script type="importmap">
  44. {
  45. "imports": {
  46. "three": "https://unpkg.com/threepipe/dist/index.mjs",
  47. "threepipe": "https://unpkg.com/threepipe/dist/index.mjs"
  48. }
  49. }
  50. </script>
  51. ```
  52. ## Using packages that depend on `three`
  53. If you are using packages that depend on `three`, override in package.json to use `threepipe` as the `three` dependency.
  54. ```json
  55. {
  56. "dependencies": {
  57. "three": "./node_modules/threepipe/",
  58. "threepipe": "^0.0.51"
  59. },
  60. "overrides": {
  61. "three": "$three"
  62. }
  63. }
  64. ```
  65. If using `vite`, add a plugin to replace `three` import to `threepipe` in your `vite.config.js`(or rollup)
  66. ```javascript
  67. import {defineConfig} from 'vite'
  68. import replace from '@rollup/plugin-replace';
  69. export default defineConfig({
  70. plugins: [
  71. replace({
  72. 'from \'three\'': 'from \'threepipe\'',
  73. 'from \'three/examples/jsm/loaders/GLTFLoader.js\'': 'from \'threepipe\'',
  74. 'from \'three/examples/jsm/postprocessing/Pass.js\'': 'from \'threepipe\'',
  75. 'from \'three/examples/jsm/utils/BufferGeometryUtils.js\'': 'from \'threepipe\'',
  76. // add more that are being used
  77. // 'from \'three/examples/jsm/.*\'': 'from \'threepipe\'', // Note - regex doesnt work...
  78. delimiters: ['', ''],
  79. preventAssignment: true,
  80. }),
  81. replace({ // this is added to throw an error, in that case, add it to above
  82. 'from \'three/': 'from \'unknown/',
  83. delimiters: ['', ''],
  84. preventAssignment: true,
  85. }),
  86. ]
  87. })
  88. ```
  89. [//]: # (todo add sample with vite for above using some three.js library that has it as dependency/peerDependency)