threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started.md 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ---
  2. prev:
  3. text: 'What is Threepipe?'
  4. link: './introduction'
  5. next:
  6. text: 'Editors in Threepipe'
  7. link: './editors'
  8. ---
  9. # Getting Started
  10. Getting started with Threepipe is easy. You can use it in your HTML/JS, React, Vue.js, Svelte, or any other web framework. The best way to use it is as an ES module in your project. Simply install the package `threepipe` from npm, or include it in html from any CDN like [esm.sh](https://esm.sh/threepipe) or [jsdelivr](https://cdn.jsdelivr.net/npm/threepipe@latest).
  11. [![NPM Package](https://img.shields.io/npm/v/threepipe.svg)](https://www.npmjs.com/package/threepipe)
  12. ## What you need
  13. - A 3D Model (GLTF, GLB, OBJ, FBX, etc). Optionally, the model, scene, and plugins can be configured, compressed and exported using the [Threepipe Editor](https://editor.threepipe.org) or [Tweakpane Editor](https://threepipe.org/examples/tweakpane-editor/). A sample is used in examples below.
  14. - A modern browser that supports [WebGL2](https://caniuse.com/webgl2) and [WebAssembly](https://caniuse.com/webassembly) (for some plugins).
  15. - Node.js (for local development). Node 18+ is recommended.
  16. - An existing project or willingness to start a new one
  17. - Basic knowledge of JavaScript/TypeScript
  18. ## Quickstart
  19. ### Codepen
  20. You can quickly prototype in JavaScript on Codepen. Here is a starter pen with the basic setup: [Threepipe Starter Codepen](https://codepen.io/repalash/pen/GRbEONZ?editors=0010)
  21. Simply fork the pen and start coding.
  22. Each example on [Threepipe Examples](https://threepipe.org/examples) also has a Codepen Button <input type="image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-1/cp-arrow-right.svg" width="35" height="35" style="margin-bottom: -0.6rem; cursor: unset;"> to open the example directly in Codepen.
  23. ### Local Setup
  24. To get started with a new project using Threepipe locally, you need to have Node.js installed on your machine.
  25. A new project can be quickly created using the `npm create` command. Open your terminal and run the following command:
  26. ```bash
  27. npm create threepipe@latest
  28. ```
  29. and follow the prompts to pick a project name, select a template/framework and pick between JavaScript or TypeScript. Supported templates -
  30. - `vanilla`
  31. - `vanilla-ts`
  32. This will create a ready-to-use project with all the necessary dependencies and configurations.
  33. Now, code from any of the examples on the [Threepipe Examples](https://threepipe.org/examples) page can be copied and pasted into the project to get started.
  34. ## Install in existing projects
  35. ### HTML/JS
  36. ```html
  37. <canvas id="three-canvas" style="width: 800px; height: 600px;"></canvas>
  38. <script type="module">
  39. import {ThreeViewer, DepthBufferPlugin} from 'https://threepipe.org/dist/index.mjs'
  40. // or
  41. // import {ThreeViewer, DepthBufferPlugin} from 'https://cdn.jsdelivr.net/npm/threepipe@latest/dist/index.mjs'
  42. // import {ThreeViewer, DepthBufferPlugin} from 'https://esm.sh/threepipe'
  43. // import {ThreeViewer, DepthBufferPlugin} from 'threepipe' // using npm or importmaps
  44. const viewer = new ThreeViewer({canvas: document.getElementById('three-canvas')})
  45. // Add some plugins
  46. viewer.addPluginSync(new DepthBufferPlugin())
  47. // Load an environment map
  48. const envPromise = viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  49. const modelPromise = viewer.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  50. autoCenter: true,
  51. autoScale: true,
  52. })
  53. Promise.all([envPromise, modelPromise]).then(([env, model]) => {
  54. console.log('Loaded', model, env, viewer)
  55. })
  56. </script>
  57. ```
  58. Check it in action: [html-js-sample](https://threepipe.org/examples/#html-js-sample/)
  59. Check out the details about [ThreeViewer API](#viewer-api) and more [plugins](#threepipe-plugins).
  60. ### NPM
  61. #### Installation
  62. ```bash
  63. npm install threepipe
  64. ```
  65. #### Loading a 3D Model
  66. First, create a canvas element in your HTML page:
  67. ```html
  68. <canvas id="three-canvas" style="width: 800px; height: 600px;"></canvas>
  69. ```
  70. Then, import the viewer and create a new instance:
  71. ```typescript
  72. import {ThreeViewer, IObject3D} from 'threepipe'
  73. // Create a viewer
  74. const viewer = new ThreeViewer({canvas: document.getElementById('three-canvas') as HTMLCanvasElement})
  75. // Load an environment map
  76. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  77. // Load a model
  78. const result = await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  79. autoCenter: true,
  80. autoScale: true,
  81. })
  82. ```
  83. That's it! You should now see a 3D model on your page.
  84. The 3D model can be opened in the [editor](https://threepipe.org/examples/tweakpane-editor/) to view and edit the scene settings, objects, materials, lights, cameras, post-processing, etc. and exported as a GLB file. All settings are automatically serialized and saved in the GLB file, which can be loaded into the viewer. Any plugins used in the editor can be added to the viewer to add the same functionality. The plugin data is automatically loaded(if the plugin is added) when the model is added to the scene.
  85. The viewer initializes with a Scene, Camera, Camera controls(Orbit Controls), several importers, exporters and a default rendering pipeline. Additional functionality can be added with plugins.
  86. Check out the GLTF Load example to see it in action or to check the JS equivalent code: [Example: gltf-load](https://threepipe.org/examples/#gltf-load/)
  87. Check out the [Plugins](#plugin-system) section below to learn how to add additional functionality to the viewer.
  88. ### React
  89. A sample [react](https://react.dev) component in tsx to render a model with an environment map.
  90. ```tsx
  91. import React from 'react'
  92. function ThreeViewerComponent({src, env}: {src: string, env: string}) {
  93. const canvasRef = React.useRef(null)
  94. React.useEffect(() => {
  95. const viewer = new ThreeViewer({canvas: canvasRef.current})
  96. const envPromise = viewer.setEnvironmentMap(env)
  97. const modelPromise = viewer.load(src)
  98. Promise.all([envPromise, modelPromise]).then(([env, model]) => {
  99. console.log('Loaded', model, env, viewer)
  100. })
  101. return () => {
  102. viewer.dispose()
  103. }
  104. }, [])
  105. return (
  106. <canvas id="three-canvas" style={{width: 800, height: 600}} ref={canvasRef} />
  107. )
  108. }
  109. ```
  110. Check it in action: [react-tsx-sample](https://threepipe.org/examples/#react-tsx-sample/)
  111. Other examples in js: [react-js-sample](https://threepipe.org/examples/#react-js-sample/) and jsx: [react-jsx-sample](https://threepipe.org/examples/#react-jsx-sample/)
  112. ### Vue.js
  113. A sample [vue.js](https://vuejs.org/) component in js to render a model with an environment map.
  114. ```js
  115. const ThreeViewerComponent = {
  116. setup() {
  117. const canvasRef = ref(null);
  118. onMounted(() => {
  119. const viewer = new ThreeViewer({ canvas: canvasRef.value });
  120. const envPromise = viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr');
  121. const modelPromise = viewer.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf');
  122. Promise.all([envPromise, modelPromise]).then(([env, model]) => {
  123. console.log('Loaded', model, env, viewer)
  124. })
  125. onBeforeUnmount(() => {
  126. viewer.dispose();
  127. });
  128. });
  129. return { canvasRef };
  130. },
  131. };
  132. ```
  133. Check it in action: [vue-html-sample](https://threepipe.org/examples/#vue-html-sample/)
  134. Another example with Vue SFC(Single file component): [Example: vue-sfc-sample](https://threepipe.org/examples/#vue-sfc-sample/)
  135. ### Svelte
  136. A sample [svelte](https://svelte.dev/) component in js to render a model with an environment map.
  137. ```html
  138. <script>
  139. import {onDestroy, onMount} from 'svelte';
  140. import {ThreeViewer} from 'threepipe';
  141. let canvasRef;
  142. let viewer;
  143. onMount(() => {
  144. viewer = new ThreeViewer({canvas: canvasRef});
  145. const envPromise = viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr');
  146. const modelPromise = viewer.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf');
  147. Promise.all([envPromise, modelPromise]).then(([env, model]) => {
  148. console.log('Loaded', model, env, viewer)
  149. })
  150. });
  151. onDestroy(() => viewer.dispose())
  152. </script>
  153. <canvas bind:this={canvasRef} id="three-canvas" style="width: 800px; height: 600px"></canvas>
  154. ```
  155. Check it in action: [svelte-sample](https://threepipe.org/examples/#svelte-sample/)