### Install MeshLine Source: https://github.com/pmndrs/meshline/blob/master/README.md Install the MeshLine library using npm. This is the first step to integrate MeshLine into your Three.js project. ```bash npm install meshline ``` -------------------------------- ### Setting MeshLine Geometry Points (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Explains how to define the points for a MeshLineGeometry. It accepts Float32Array, THREE.BufferGeometry, or an array of points in various formats. An example shows creating a circular path using trigonometric functions. ```jsx const geometry = new MeshLineGeometry() const points = [] for (let j = 0; j < Math.PI; j += (2 * Math.PI) / 100) points.push(Math.cos(j), Math.sin(j), 0) geometry.setPoints(points) ``` -------------------------------- ### React Three Fiber Integration with Meshline (JSX) Source: https://context7.com/pmndrs/meshline/llms.txt This example demonstrates integrating Meshline with React Three Fiber for declarative 3D line creation in a React application. It shows how to create animated lines with dynamic dash offsets and tapering lines using callbacks. Dependencies include '@react-three/fiber', 'meshline', and 'three'. ```jsx import { Canvas, extend, useFrame } from '@react-three/fiber' import { MeshLineGeometry, MeshLineMaterial, raycast } from 'meshline' import { useRef, useMemo } from 'react' import * as THREE from 'three' extend({ MeshLineGeometry, MeshLineMaterial }) function AnimatedLine() { const meshRef = useRef() // Generate curve points const points = useMemo(() => { const curve = new THREE.CatmullRomCurve3([ new THREE.Vector3(-3, 0, 0), new THREE.Vector3(-1, 2, 0), new THREE.Vector3(1, -2, 0), new THREE.Vector3(3, 0, 0) ]) return curve.getPoints(100).flatMap(p => [p.x, p.y, p.z]) }, []) // Animate dash offset useFrame((state, delta) => { if (meshRef.current) { meshRef.current.material.dashOffset -= delta * 0.5 } }) return ( console.log('Line clicked', e.point)} onPointerOver={(e) => (document.body.style.cursor = 'pointer')} onPointerOut={(e) => (document.body.style.cursor = 'default')} > ) } function TaperingLine() { const points = useMemo(() => { const pts = [] for (let i = 0; i < 50; i++) { pts.push(i * 0.1 - 2.5, Math.sin(i * 0.3), 0) } return pts }, []) return ( Math.sin(p * Math.PI)} /> ) } function App() { return ( ) } export default App ``` -------------------------------- ### TypeScript Setup for React Three Fiber MeshLine Source: https://context7.com/pmndrs/meshline/llms.txt Adds type declarations for MeshLineGeometry and MeshLineMaterial to React Three Fiber, enabling their use with TypeScript. This allows for type-safe usage of MeshLine components within TSX files. ```typescript import { Object3DNode, MaterialNode } from '@react-three/fiber' import { MeshLineGeometry, MeshLineMaterial } from 'meshline' declare module '@react-three/fiber' { interface ThreeElements { meshLineGeometry: Object3DNode meshLineMaterial: MaterialNode } } // Now TypeScript recognizes and // Usage in TSX files: function TypedLine() { return ( ) } ``` -------------------------------- ### Basic MeshLine Usage (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Demonstrates the fundamental steps to use MeshLine in a JavaScript Three.js project. It involves creating geometry, material, and a mesh, then adding it to the scene. The `raycast` function from MeshLine can be optionally assigned to the mesh for raycasting. ```jsx import * as THREE from 'three' import { MeshLineGeometry, MeshLineMaterial, raycast } from 'meshline' const geometry = new MeshLineGeometry() geometry.setPoints([...]) const material = new MeshLineMaterial({ ... }) const mesh = new THREE.Mesh(geometry, material) mesh.raycast = raycast scene.add(mesh) ``` -------------------------------- ### Variable Width for MeshLine Points (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Illustrates how to set variable widths for points along a MeshLine. The `.setPoints` method accepts an optional callback function that determines the width at each point, allowing for dynamic line thickness. ```jsx // p is a decimal percentage of the number of points // ie. point 200 of 250 points, p = 0.8 geometry.setPoints(points, (p) => 2) // makes width 2 * lineWidth geometry.setPoints(points, (p) => 1 - p) // makes width taper geometry.setPoints(points, (p) => 2 + Math.sin(50 * p)) // makes width sinusoidal ``` -------------------------------- ### MeshLineMaterial Options (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Details the configurable properties of MeshLineMaterial for controlling line appearance, including textures, colors, opacity, dashing, and resolution. Specific attributes like `map`, `useMap`, `color`, `dashArray`, and `resolution` are explained. ```jsx const material = new MeshLineMaterial(options) ``` -------------------------------- ### Responsive Resolution Handling for MeshLine Material Source: https://context7.com/pmndrs/meshline/llms.txt Updates the resolution of a MeshLineMaterial on window resize to ensure lines render correctly across different screen sizes. This involves updating the material's resolution vector and potentially the camera and renderer. ```javascript import * as THREE from 'three' import { MeshLineMaterial } from 'meshline' const material = new MeshLineMaterial({ color: 0xff0000, lineWidth: 0.1, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight) }) // Handle window resize window.addEventListener('resize', () => { const width = window.innerWidth const height = window.innerHeight // Update camera camera.aspect = width / height camera.updateProjectionMatrix() // Update renderer renderer.setSize(width, height) // Update material resolution (REQUIRED for correct line rendering) material.resolution.set(width, height) }) ``` -------------------------------- ### Create MeshLineMaterial with Advanced Options Source: https://context7.com/pmndrs/meshline/llms.txt Provides material options for rendering lines with textures, gradients, and dashed patterns. Key properties include `color`, `lineWidth`, `resolution`, `sizeAttenuation`, `map`, `alphaMap`, `dashArray`, `dashOffset`, and `gradient`. ```javascript import * as THREE from 'three' import { MeshLineMaterial } from 'meshline' // Basic solid color line const material = new MeshLineMaterial({ color: new THREE.Color(0xff0080), lineWidth: 0.1, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight), sizeAttenuation: 1, // 1 = world units, 0 = screen pixels transparent: true, opacity: 0.8 }) // Textured line with alpha map const texture = new THREE.TextureLoader().load('texture.png') const alphaTexture = new THREE.TextureLoader().load('alpha.png') const texturedMaterial = new MeshLineMaterial({ map: texture, useMap: 1, alphaMap: alphaTexture, useAlphaMap: 1, repeat: new THREE.Vector2(4, 1), lineWidth: 0.2, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight), transparent: true, depthWrite: false }) // Dashed line with animation const dashedMaterial = new MeshLineMaterial({ color: 0xffffff, lineWidth: 0.15, dashArray: 0.1, // Length of dash + gap dashRatio: 0.5, // 0.5 = equal dash and gap dashOffset: 0, // Animate this to move dashes resolution: new THREE.Vector2(window.innerWidth, window.innerHeight) }) // Gradient line const gradientMaterial = new MeshLineMaterial({ useGradient: 1, gradient: [new THREE.Color(0xff0000), new THREE.Color(0x0000ff)], lineWidth: 0.1, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight) }) ``` -------------------------------- ### Declarative MeshLine Usage (React-three-fiber) Source: https://github.com/pmndrs/meshline/blob/master/README.md Demonstrates how to use MeshLine components declaratively within a React-three-fiber application. It involves extending the custom elements and using JSX components for `meshLineGeometry` and `meshLineMaterial`. ```jsx import { Canvas, extend } from '@react-three/fiber' import { MeshLineGeometry, MeshLineMaterial, raycast } from 'meshline' extend({ MeshLineGeometry, MeshLineMaterial }) function App() { return ( ) } ``` -------------------------------- ### Forming MeshLine Mesh (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Shows how to combine the MeshLineGeometry and MeshLineMaterial to create a Three.js Mesh object, which can then be added to the scene. This is a standard Three.js pattern for creating visible objects. ```jsx const mesh = new THREE.Mesh(geometry, material) scene.add(mesh) ``` -------------------------------- ### Create MeshLineGeometry from Points Data Source: https://context7.com/pmndrs/meshline/llms.txt Generates geometry for rendering thick lines using an array of points. Supports plain arrays, Vector3 arrays, and BufferGeometries. The `setPoints` method takes the point data and optionally a callback for variable width. ```javascript import * as THREE from 'three' import { MeshLineGeometry, MeshLineMaterial } from 'meshline' // Create a spiral line const geometry = new MeshLineGeometry() const points = [] for (let j = 0; j < Math.PI * 2; j += 0.1) { points.push(Math.cos(j) * 2, Math.sin(j) * 2, j * 0.5) } gemetry.setPoints(points) // Alternative: from Vector3 array const vectorPoints = [ new THREE.Vector3(0, 0, 0), new THREE.Vector3(1, 1, 0), new THREE.Vector3(2, 0, 0) ] geometry.setPoints(vectorPoints) // Alternative: from BufferGeometry const curve = new THREE.CatmullRomCurve3([ new THREE.Vector3(-2, 0, 0), new THREE.Vector3(0, 2, 0), new THREE.Vector3(2, 0, 0) ]) const curveGeometry = new THREE.BufferGeometry().setFromPoints(curve.getPoints(50)) geometry.setPoints(curveGeometry) ``` -------------------------------- ### Create Line Mesh with Raycast Support (JavaScript) Source: https://context7.com/pmndrs/meshline/llms.txt This snippet demonstrates how to create a 3D line mesh using MeshLineGeometry and MeshLineMaterial in Three.js. It includes enabling raycasting on the mesh to detect clicks and log intersection points. Dependencies include 'three' and 'meshline'. ```javascript import * as THREE from 'three' import { MeshLineGeometry, MeshLineMaterial, raycast } from 'meshline' const scene = new THREE.Scene() const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) const renderer = new THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) // Create line mesh const geometry = new MeshLineGeometry() const points = new THREE.CatmullRomCurve3([ new THREE.Vector3(-5, 0, 0), new THREE.Vector3(0, 3, 0), new THREE.Vector3(5, 0, 0) ]).getPoints(100) gemometry.setPoints(points) const material = new MeshLineMaterial({ color: 0x00ffff, lineWidth: 0.2, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight) }) const mesh = new THREE.Mesh(geometry, material) mesh.raycast = raycast // Enable raycasting scene.add(mesh) // Setup raycaster for mouse interaction const raycaster = new THREE.Raycaster() raycaster.params.Line = { threshold: 0.1 } const mouse = new THREE.Vector2() window.addEventListener('click', (event) => { mouse.x = (event.clientX / window.innerWidth) * 2 - 1 mouse.y = -(event.clientY / window.innerHeight) * 2 + 1 raycaster.setFromCamera(mouse, camera) const intersects = raycaster.intersectObject(mesh) if (intersects.length > 0) { console.log('Line clicked at:', intersects[0].point) } }) camera.position.z = 10 renderer.render(scene, camera) ``` -------------------------------- ### Set Variable Line Width with Callback Source: https://context7.com/pmndrs/meshline/llms.txt Defines width variation along the line by passing a callback function to `setGeometry.setPoints`. The callback receives a progress value (0 to 1) and returns the desired width multiplier. ```javascript import { MeshLineGeometry } from 'meshline' const geometry = new MeshLineGeometry() const points = [] for (let i = 0; i < 100; i++) { points.push(i * 0.1, Math.sin(i * 0.2), 0) } // Tapering line (wide to narrow) geometry.setPoints(points, (p) => 1 - p) // Sinusoidal width variation geometry.setPoints(points, (p) => 1 + Math.sin(p * Math.PI * 4) * 0.5) // Bell curve width geometry.setPoints(points, (p) => Math.exp(-Math.pow((p - 0.5) * 4, 2))) // Constant 2x width geometry.setPoints(points, (p) => 2) ``` -------------------------------- ### Enabling Raycasting for MeshLine (JavaScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Explains how to add raycasting capabilities to a MeshLine object. This is achieved by overwriting the mesh's `raycast` method with the `raycast` function provided by the MeshLine library. ```jsx import { raycast } from 'meshline' mesh.raycast = raycast ``` -------------------------------- ### Animate Line Position and Material Properties (JavaScript) Source: https://context7.com/pmndrs/meshline/llms.txt This code snippet shows how to animate a line's position and material properties over time using Three.js and Meshline. It updates the line's trail with new positions and animates the dash offset for a 'marching ants' effect. Requires 'three' and 'meshline'. ```javascript import * as THREE from 'three' import { MeshLineGeometry, MeshLineMaterial } from 'meshline' const geometry = new MeshLineGeometry() const points = [] const numPoints = 100 // Initialize trail points for (let i = 0; i < numPoints; i++) { points.push(0, 0, 0) } gemometry.setPoints(points) const material = new MeshLineMaterial({ color: 0xff6600, lineWidth: 0.1, resolution: new THREE.Vector2(window.innerWidth, window.innerHeight), dashArray: 0.2, dashRatio: 0.7 }) const mesh = new THREE.Mesh(geometry, material) // Animation loop const clock = new THREE.Clock() function animate() { requestAnimationFrame(animate) const time = clock.getElapsedTime() // Advance the line trail with new position const newPosition = new THREE.Vector3( Math.cos(time) * 3, Math.sin(time * 2) * 2, Math.sin(time) * 3 ) geometry.advance(newPosition) // Animate dash offset for marching ants effect material.dashOffset -= 0.01 renderer.render(scene, camera) } animate() ``` -------------------------------- ### Variable Line Widths in React-three-fiber Source: https://github.com/pmndrs/meshline/blob/master/README.md Shows how to set variable line widths per point in React-three-fiber using the `widthCallback` prop on the `meshLineGeometry` component. This allows for dynamic and responsive line thickness based on the point's position or other factors. ```jsx p * Math.random()} /> ``` -------------------------------- ### Augment @react-three/fiber Types with MeshLine Declarations (TypeScript) Source: https://github.com/pmndrs/meshline/blob/master/README.md Extends the React Three Fiber `ThreeElements` interface to include custom nodes for `meshLineGeometry` and `meshLineMaterial`. This allows for type-safe usage of these components within a TypeScript project using React Three Fiber. Ensure these declarations are added to your project's entry point for the types to be recognized. ```tsx import { Object3DNode, MaterialNode } from '@react-three/fiber' declare module '@react-three/fiber' { interface ThreeElements { meshLineGeometry: Object3DNode meshLineMaterial: MaterialNode } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.