Examples : Bender
Description
Demonstrates Bending BufferGeometries in Threejs.
Bender GitHub Repository : https://github.com/Sean-Bradley/Bender
This example demonstrates,
- Bending a THREE.BoxGeometry
- Bending a TextGeometry
- Replacing a meshes geometry at runtime
- Replacing the text of a TextGeometry at runtime
- Using the FontLoader
Code
./src/client/utils/bender.ts
//MIT License
//Copyright (c) 2020-2021 Sean Bradley
//https://github.com/Sean-Bradley/Bender/blob/main/LICENSE
export default class Bender {
public bend(geometry: THREE.BufferGeometry, axis: string, angle: number) {
let theta = 0
if (angle !== 0) {
const v = (geometry.attributes.position as THREE.BufferAttribute)
.array as number[]
for (let i = 0; i < v.length; i += 3) {
let x = v[i]
let y = v[i + 1]
let z = v[i + 2]
switch (axis) {
case 'x':
theta = z * angle
break
case 'y':
theta = x * angle
break
default:
//z
theta = x * angle
break
}
let sinTheta = Math.sin(theta)
let cosTheta = Math.cos(theta)
switch (axis) {
case 'x':
v[i] = x
v[i + 1] = (y - 1.0 / angle) * cosTheta + 1.0 / angle
v[i + 2] = -(y - 1.0 / angle) * sinTheta
break
case 'y':
v[i] = -(z - 1.0 / angle) * sinTheta
v[i + 1] = y
v[i + 2] = (z - 1.0 / angle) * cosTheta + 1.0 / angle
break
default:
//z
v[i] = -(y - 1.0 / angle) * sinTheta
v[i + 1] = (y - 1.0 / angle) * cosTheta + 1.0 / angle
v[i + 2] = z
break
}
}
geometry.attributes.position.needsUpdate = true
}
}
}
./src/client/client.ts
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|