threepipe
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

fat-lines.md 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ---
  2. next:
  3. text: 'GLTF Fat/Mesh Lines'
  4. link: './gltf-mesh-lines'
  5. prev: false
  6. aside: false
  7. ---
  8. # Fat Lines: Spiral Example
  9. <iframe src="https://threepipe.org/examples/fat-line-spiral/" style="width:100%;height:600px;border:none;"></iframe>
  10. This example shows how to use ThreePipe's fat lines (MeshLine) to draw a thick, colorful spiral. The spiral's shape and color are generated in code, and you can adjust its parameters live using the UI.
  11. **How it works:**
  12. - The spiral is made by calculating 3D positions in a loop, using radius, height, and loops for shape.
  13. - Each point gets a color, creating a gradient from blue to red.
  14. - `MeshLine` (an extension of three.js Line2) is used to make the line thick and support per-vertex colors.
  15. - The UI lets you change the spiral's radius, height, and loops, and see updates instantly.
  16. **Key code:**
  17. ```typescript
  18. function makeSpiral(radius = 1, height = 2, loops = 10) {
  19. const positions = [], colors = []
  20. for (let i = 0; i <= 1000; i++) {
  21. const t = i / 1000, angle = t * Math.PI * 2 * loops
  22. positions.push(radius * Math.cos(angle), radius * Math.sin(angle), t * height - height/2)
  23. colors.push(t, 0, 1-t)
  24. }
  25. return {positions, colors}
  26. }
  27. const line = new MeshLine(new LineGeometry2(), new LineMaterial2())
  28. line.material.vertexColors = true
  29. line.material.linewidth = 5
  30. function updateSpiral() {
  31. const {positions, colors} = makeSpiral(spiral.radius, spiral.height, spiral.loops)
  32. line.geometry.setPositions(positions)
  33. line.geometry.setColors(colors)
  34. line.material.setDirty()
  35. }
  36. ```
  37. The line and its controls are added to the UI with Tweakpane:
  38. ```typescript
  39. ui.appendChild({
  40. type: 'folder',
  41. label: 'Spiral',
  42. children: generateUiConfig(spiral),
  43. onChange: updateSpiral,
  44. })
  45. ui.appendChild(line.uiConfig)
  46. ```
  47. See the [full code here](https://github.com/repalash/threepipe/blob/master/examples/fat-line-spiral/script.ts), live example on [threepipe.org/examples](https://threepipe.org/examples/fat-line-spiral/).
  48. ::: warning
  49. Fat lines (MeshLine) cannot be exported with glTF. If you want to create lines that can be exported as glTF lines, use three.js `Line` directly (for now).
  50. :::
  51. ## Features of `MeshLine`
  52. - **Adjustable thickness:** Unlike standard three.js lines, fat lines can be any width, not just 1 pixel.
  53. - **Per-vertex color:** Supports gradients and color effects along the line.
  54. - **UI integration:** Easily expose line and spiral parameters for live editing.
  55. - **Performance:** Efficiently renders thousands of segments as a single mesh.
  56. ## How Fat Lines Work Under the Hood
  57. ThreePipe's `MeshLine` is built on top of the three.js [Line2](https://threejs.org/docs/#examples/en/lines/Line2) addon. Instead of drawing a single-pixel line, it creates a mesh strip along the path, allowing for thick, anti-aliased lines. The geometry stores positions and colors, and the material handles width, color, and other effects. This approach works in all modern browsers and supports advanced features like dashes and transparency.
  58. ## When to Use `MeshLine`
  59. Use fat lines when you need:
  60. - Outlines, paths, or polylines with variable thickness
  61. - Technical illustrations or stylized effects
  62. - Color gradients or dashes along a line
  63. - Interactive geometry that updates in real time
  64. - Using as a viewer (not exporting as glTF)
  65. For simple, single-pixel lines, the default three.js `Line` is still fastest, but for anything more advanced, `MeshLine` is the way to go.
  66. ## Using with glTF files
  67. When importing glTF files with embedded line geometries and materials, `GLTFLoader2.UseMeshLines` feature can be used to automatically convert those lines to `MeshLine` instances. This allows you to take advantage of the advanced line rendering capabilities without needing to modify the original GLTF files.
  68. Checkout the article on [GLTF Mesh Lines](https://threepipe.org/notes/gltf-mesh-lines) for more details on how to use this feature.