|
| 1 | +import { |
| 2 | + CUSTOM_ELEMENTS_SCHEMA, |
| 3 | + Component, |
| 4 | + Input, |
| 5 | + computed, |
| 6 | + effect, |
| 7 | + runInInjectionContext, |
| 8 | + type Injector, |
| 9 | +} from '@angular/core'; |
| 10 | +import { |
| 11 | + NgtPortal, |
| 12 | + NgtPortalContent, |
| 13 | + NgtRef, |
| 14 | + assertInjectionContext, |
| 15 | + extend, |
| 16 | + injectBeforeRender, |
| 17 | + injectNgtRef, |
| 18 | + injectNgtStore, |
| 19 | + is, |
| 20 | + signalStore, |
| 21 | +} from 'angular-three'; |
| 22 | +import { MeshLineGeometry, MeshLineMaterial } from 'meshline'; |
| 23 | +import * as THREE from 'three'; |
| 24 | +import { Group, Mesh } from 'three'; |
| 25 | + |
| 26 | +const shiftLeft = (collection: Float32Array, steps = 1): Float32Array => { |
| 27 | + collection.set(collection.subarray(steps)); |
| 28 | + collection.fill(-Infinity, -steps); |
| 29 | + return collection; |
| 30 | +}; |
| 31 | + |
| 32 | +export type NgtsTrailSettings = { |
| 33 | + width: number; |
| 34 | + length: number; |
| 35 | + decay: number; |
| 36 | + /** |
| 37 | + * Wether to use the target's world or local positions |
| 38 | + */ |
| 39 | + local: boolean; |
| 40 | + // Min distance between previous and current points |
| 41 | + stride: number; |
| 42 | + // Number of frames to wait before next calculation |
| 43 | + interval: number; |
| 44 | +}; |
| 45 | + |
| 46 | +const defaultSettings: NgtsTrailSettings = { |
| 47 | + width: 0.2, |
| 48 | + length: 1, |
| 49 | + decay: 1, |
| 50 | + local: false, |
| 51 | + stride: 0, |
| 52 | + interval: 1, |
| 53 | +}; |
| 54 | + |
| 55 | +export function injectNgtsTrail( |
| 56 | + targetFactory: () => NgtRef<THREE.Object3D> | null, |
| 57 | + settingsFactory: () => Partial<NgtsTrailSettings>, |
| 58 | + { injector }: { injector?: Injector } = {}, |
| 59 | +) { |
| 60 | + injector = assertInjectionContext(injectNgtsTrail, injector); |
| 61 | + return runInInjectionContext(injector, () => { |
| 62 | + const points = injectNgtRef<Float32Array>(); |
| 63 | + let frameCount = 0; |
| 64 | + |
| 65 | + const prevPosition = new THREE.Vector3(); |
| 66 | + const worldPosition = new THREE.Vector3(); |
| 67 | + |
| 68 | + const _target = computed(() => { |
| 69 | + const _target = targetFactory(); |
| 70 | + if (is.ref(_target)) return _target.nativeElement; |
| 71 | + return _target; |
| 72 | + }); |
| 73 | + |
| 74 | + const _settings = computed(() => ({ ...defaultSettings, ...settingsFactory() })); |
| 75 | + const _length = computed(() => _settings().length); |
| 76 | + |
| 77 | + effect(() => { |
| 78 | + const [target, length] = [_target(), _length()]; |
| 79 | + if (target) { |
| 80 | + points.nativeElement = Float32Array.from({ length: length * 10 * 3 }, (_, i) => |
| 81 | + target.position.getComponent(i % 3), |
| 82 | + ); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + injectBeforeRender(() => { |
| 87 | + const [target, _points, { local, decay, stride, interval }] = [ |
| 88 | + _target(), |
| 89 | + points.nativeElement, |
| 90 | + _settings(), |
| 91 | + ]; |
| 92 | + if (!target) return; |
| 93 | + if (!_points) return; |
| 94 | + |
| 95 | + if (frameCount === 0) { |
| 96 | + let newPosition: THREE.Vector3; |
| 97 | + if (local) { |
| 98 | + newPosition = target.position; |
| 99 | + } else { |
| 100 | + target.getWorldPosition(worldPosition); |
| 101 | + newPosition = worldPosition; |
| 102 | + } |
| 103 | + |
| 104 | + const steps = 1 * decay; |
| 105 | + for (let i = 0; i < steps; i++) { |
| 106 | + if (newPosition.distanceTo(prevPosition) < stride) continue; |
| 107 | + |
| 108 | + shiftLeft(_points, 3); |
| 109 | + _points.set(newPosition.toArray(), _points.length - 3); |
| 110 | + } |
| 111 | + prevPosition.copy(newPosition); |
| 112 | + } |
| 113 | + |
| 114 | + frameCount++; |
| 115 | + frameCount = frameCount % interval; |
| 116 | + }); |
| 117 | + |
| 118 | + return points; |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +export type NgtsTrailState = { |
| 123 | + color: THREE.ColorRepresentation; |
| 124 | + attenuation: (width: number) => number; |
| 125 | + settings: NgtsTrailSettings; |
| 126 | + target?: NgtRef<THREE.Object3D>; |
| 127 | +}; |
| 128 | + |
| 129 | +declare global { |
| 130 | + interface HTMLElementTagNameMap { |
| 131 | + 'ngts-trail': NgtsTrailState; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +extend({ Group, Mesh }); |
| 136 | + |
| 137 | +@Component({ |
| 138 | + selector: 'ngts-trail', |
| 139 | + standalone: true, |
| 140 | + template: ` |
| 141 | + <ngt-group> |
| 142 | + <ngt-portal [container]="scene()" [autoRender]="false"> |
| 143 | + <ng-template ngtPortalContent> |
| 144 | + <ngt-mesh [ref]="trailRef" [geometry]="geometry" [material]="material()" /> |
| 145 | + </ng-template> |
| 146 | + </ngt-portal> |
| 147 | + <ngt-group [ref]="groupRef"> |
| 148 | + <ng-content /> |
| 149 | + </ngt-group> |
| 150 | + </ngt-group> |
| 151 | + `, |
| 152 | + imports: [NgtPortal, NgtPortalContent], |
| 153 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 154 | +}) |
| 155 | +export class NgtsTrail { |
| 156 | + private inputs = signalStore<NgtsTrailState>({ settings: defaultSettings, color: 'hotpink' }); |
| 157 | + |
| 158 | + @Input() trailRef = injectNgtRef<Mesh>(); |
| 159 | + |
| 160 | + @Input({ alias: 'color' }) set _color(color: THREE.ColorRepresentation) { |
| 161 | + this.inputs.set({ color }); |
| 162 | + } |
| 163 | + |
| 164 | + @Input({ alias: 'attenuation' }) set _attenuation(attenuation: (width: number) => number) { |
| 165 | + this.inputs.set({ attenuation }); |
| 166 | + } |
| 167 | + |
| 168 | + @Input({ alias: 'target' }) set _target(target: NgtRef<THREE.Object3D>) { |
| 169 | + this.inputs.set({ target }); |
| 170 | + } |
| 171 | + |
| 172 | + @Input({ alias: 'settings' }) set _settings(settings: Partial<NgtsTrailSettings>) { |
| 173 | + this.inputs.set({ settings: { ...defaultSettings, ...settings } }); |
| 174 | + } |
| 175 | + |
| 176 | + groupRef = injectNgtRef<Group>(); |
| 177 | + |
| 178 | + private children = this.groupRef.children('both'); |
| 179 | + |
| 180 | + private store = injectNgtStore(); |
| 181 | + private size = this.store.select('size'); |
| 182 | + |
| 183 | + private target = this.inputs.select('target'); |
| 184 | + private settings = this.inputs.select('settings'); |
| 185 | + private width = computed(() => this.settings().width); |
| 186 | + private color = this.inputs.select('color'); |
| 187 | + private attenuation = this.inputs.select('attenuation'); |
| 188 | + |
| 189 | + private anchor = computed(() => { |
| 190 | + const target = this.target(); |
| 191 | + if (target) return target; |
| 192 | + const group = this.groupRef.nativeElement; |
| 193 | + if (group) { |
| 194 | + return group.children.find((child) => child instanceof THREE.Object3D) || null; |
| 195 | + } |
| 196 | + return null; |
| 197 | + }); |
| 198 | + |
| 199 | + private points = injectNgtsTrail(this.anchor, this.settings); |
| 200 | + |
| 201 | + scene = this.store.select('scene'); |
| 202 | + geometry = new MeshLineGeometry(); |
| 203 | + material = computed(() => { |
| 204 | + const [width, color, size] = [this.width(), this.color(), this.size()]; |
| 205 | + |
| 206 | + const m = new MeshLineMaterial({ |
| 207 | + lineWidth: 0.1 * width, |
| 208 | + color, |
| 209 | + sizeAttenuation: 1, |
| 210 | + resolution: new THREE.Vector2(size.width, size.height), |
| 211 | + }); |
| 212 | + |
| 213 | + // TODO: understand this first |
| 214 | + // Get and apply first <meshLineMaterial /> from children |
| 215 | + // let matOverride: React.ReactElement | undefined; |
| 216 | + // if (children) { |
| 217 | + // if (Array.isArray(children)) { |
| 218 | + // matOverride = children.find((child: React.ReactNode) => { |
| 219 | + // const c = child as React.ReactElement; |
| 220 | + // return typeof c.type === 'string' && c.type === 'meshLineMaterial'; |
| 221 | + // }) as React.ReactElement | undefined; |
| 222 | + // } else { |
| 223 | + // const c = children as React.ReactElement; |
| 224 | + // if (typeof c.type === 'string' && c.type === 'meshLineMaterial') { |
| 225 | + // matOverride = c; |
| 226 | + // } |
| 227 | + // } |
| 228 | + // } |
| 229 | + // if (typeof matOverride?.props === 'object') { |
| 230 | + // m.setValues(matOverride.props); |
| 231 | + // } |
| 232 | + |
| 233 | + return m; |
| 234 | + }); |
| 235 | + |
| 236 | + constructor() { |
| 237 | + this.setMaterialSize(); |
| 238 | + this.beforeRender(); |
| 239 | + } |
| 240 | + |
| 241 | + private setMaterialSize() { |
| 242 | + effect(() => { |
| 243 | + const [material, size] = [this.material(), this.size()]; |
| 244 | + material.uniforms['resolution'].value.set(size.width, size.height); |
| 245 | + }); |
| 246 | + } |
| 247 | + |
| 248 | + private beforeRender() { |
| 249 | + injectBeforeRender(() => { |
| 250 | + const [points, attenuation] = [this.points.nativeElement, this.attenuation()]; |
| 251 | + if (!points) return; |
| 252 | + this.geometry.setPoints(points, attenuation); |
| 253 | + }); |
| 254 | + } |
| 255 | +} |
0 commit comments