-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree-helpers.ts
More file actions
52 lines (44 loc) · 1.78 KB
/
Copy paththree-helpers.ts
File metadata and controls
52 lines (44 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as THREE from "three"
// Helper function to create a line geometry between two points
export function createLine(pointA: THREE.Vector3, pointB: THREE.Vector3, color = "#ffffff", lineWidth = 1) {
const points = [pointA, pointB]
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({ color, linewidth: lineWidth })
return new THREE.Line(geometry, material)
}
// Helper function to create a smooth curve between points
export function createCurve(points: THREE.Vector3[], segments = 50, color = "#ffffff", lineWidth = 1) {
const curve = new THREE.CatmullRomCurve3(points)
const curvePoints = curve.getPoints(segments)
const geometry = new THREE.BufferGeometry().setFromPoints(curvePoints)
const material = new THREE.LineBasicMaterial({ color, linewidth: lineWidth })
return new THREE.Line(geometry, material)
}
// Create a gradient material for meshes
export function createGradientMaterial(colorA: string, colorB: string, direction: "x" | "y" | "z" = "y") {
// Create canvas for gradient texture
const canvas = document.createElement("canvas")
canvas.width = 256
canvas.height = 256
const context = canvas.getContext("2d")
// Create gradient
const gradient = context.createLinearGradient(
direction === "x" ? 0 : 0,
direction === "y" ? 0 : 0,
direction === "x" ? canvas.width : 0,
direction === "y" ? canvas.height : 0,
)
gradient.addColorStop(0, colorA)
gradient.addColorStop(1, colorB)
// Fill with gradient
context.fillStyle = gradient
context.fillRect(0, 0, canvas.width, canvas.height)
// Create texture
const texture = new THREE.CanvasTexture(canvas)
// Create material
return new THREE.MeshStandardMaterial({
map: texture,
metalness: 0.5,
roughness: 0.2,
})
}