|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, ViewChild, computed, effect } from '@angular/core'; |
| 2 | +import { NgtArgs, extend, injectBeforeRender, injectNgtLoader, injectNgtRef } from 'angular-three'; |
| 3 | +import { NgtsCurveModifier } from 'angular-three-soba/modifiers'; |
| 4 | +import * as THREE from 'three'; |
| 5 | +import { FontLoader, TextGeometry } from 'three-stdlib'; |
| 6 | +import { makeCanvasOptions, makeDecorators, makeStoryFunction } from '../setup-canvas'; |
| 7 | + |
| 8 | +extend({ TextGeometry }); |
| 9 | + |
| 10 | +@Component({ |
| 11 | + standalone: true, |
| 12 | + template: ` |
| 13 | + <ngts-curve-modifier #curveModifier [curve]="curve"> |
| 14 | + <ngt-mesh> |
| 15 | + <ngt-text-geometry *args="args()" [ref]="geometryRef" /> |
| 16 | + <ngt-mesh-normal-material /> |
| 17 | + </ngt-mesh> |
| 18 | + </ngts-curve-modifier> |
| 19 | + <ngt-primitive *args="[line]" /> |
| 20 | + `, |
| 21 | + imports: [NgtsCurveModifier, NgtArgs], |
| 22 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 23 | +}) |
| 24 | +class DefaultCurveModifierStory { |
| 25 | + private font = injectNgtLoader( |
| 26 | + () => FontLoader, |
| 27 | + () => 'soba/fonts/helvetiker_regular.typeface.json', |
| 28 | + ); |
| 29 | + args = computed(() => { |
| 30 | + const font = this.font(); |
| 31 | + if (!font) return []; |
| 32 | + return [ |
| 33 | + 'hello angular-three-soba', |
| 34 | + { |
| 35 | + font, |
| 36 | + size: 2, |
| 37 | + height: 0.05, |
| 38 | + curveSegments: 12, |
| 39 | + bevelEnabled: true, |
| 40 | + bevelThickness: 0.02, |
| 41 | + bevelSize: 0.01, |
| 42 | + bevelOffset: 0, |
| 43 | + bevelSegments: 5, |
| 44 | + }, |
| 45 | + ]; |
| 46 | + }); |
| 47 | + private handlePosition = [ |
| 48 | + [10, 0, -10], |
| 49 | + [10, 0, 10], |
| 50 | + [-10, 0, 10], |
| 51 | + [-10, 0, -10], |
| 52 | + ].map((hand) => new THREE.Vector3(...hand)); |
| 53 | + |
| 54 | + curve = new THREE.CatmullRomCurve3(this.handlePosition, true, 'centripetal'); |
| 55 | + line = new THREE.LineLoop( |
| 56 | + new THREE.BufferGeometry().setFromPoints(this.curve.getPoints(50)), |
| 57 | + new THREE.LineBasicMaterial({ color: 0x00ff00 }), |
| 58 | + ); |
| 59 | + |
| 60 | + geometryRef = injectNgtRef<TextGeometry>(); |
| 61 | + @ViewChild('curveModifier', { static: true }) curveModifier!: NgtsCurveModifier; |
| 62 | + |
| 63 | + constructor() { |
| 64 | + effect(() => { |
| 65 | + const geom = this.geometryRef.nativeElement; |
| 66 | + if (!geom) return; |
| 67 | + geom.rotateX(Math.PI); |
| 68 | + }); |
| 69 | + |
| 70 | + injectBeforeRender(() => { |
| 71 | + this.curveModifier?.moveAlongCurve(0.001); |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export default { |
| 77 | + title: 'Modifiers/CurveModifier', |
| 78 | + decorators: makeDecorators(), |
| 79 | +}; |
| 80 | + |
| 81 | +const canvasOptions = makeCanvasOptions({ |
| 82 | + camera: { position: [0, 10, 20] }, |
| 83 | + useLegacyLights: true, |
| 84 | +}); |
| 85 | + |
| 86 | +export const Default = makeStoryFunction(DefaultCurveModifierStory, canvasOptions); |
0 commit comments