|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, Input, computed, effect, inject } from '@angular/core'; |
| 2 | +import { NgtArgs, NgtSignalStore, NgtStore, injectNgtRef, requestAnimationInInjectionContext } from 'angular-three'; |
| 3 | +import { BlendFunction, LUT3DEffect } from 'postprocessing'; |
| 4 | + |
| 5 | +export interface NgtpLUTState { |
| 6 | + lut: THREE.Texture; |
| 7 | + blendFunction?: BlendFunction; |
| 8 | + tetrahedralInterpolation?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'ngtp-lut', |
| 13 | + standalone: true, |
| 14 | + template: ` <ngt-primitive *args="[effect()]" [ref]="effectRef" /> `, |
| 15 | + imports: [NgtArgs], |
| 16 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 17 | +}) |
| 18 | +export class NgtpLUT extends NgtSignalStore<NgtpLUTState> { |
| 19 | + @Input() effectRef = injectNgtRef<LUT3DEffect>(); |
| 20 | + |
| 21 | + @Input({ required: true }) set lut(lut: THREE.Texture) { |
| 22 | + this.set({ lut }); |
| 23 | + } |
| 24 | + |
| 25 | + @Input() set blendFunction(blendFunction: BlendFunction) { |
| 26 | + this.set({ blendFunction }); |
| 27 | + } |
| 28 | + |
| 29 | + @Input() set tetrahedralInterpolation(tetrahedralInterpolation: boolean) { |
| 30 | + this.set({ tetrahedralInterpolation }); |
| 31 | + } |
| 32 | + |
| 33 | + readonly #lut = this.select('lut'); |
| 34 | + readonly #tetrahedralInterpolation = this.select('tetrahedralInterpolation'); |
| 35 | + readonly #blendFunction = this.select('blendFunction'); |
| 36 | + |
| 37 | + readonly #store = inject(NgtStore); |
| 38 | + readonly #invalidate = this.#store.select('invalidate'); |
| 39 | + |
| 40 | + readonly effect = computed( |
| 41 | + () => |
| 42 | + new LUT3DEffect(this.#lut(), { |
| 43 | + blendFunction: this.#blendFunction(), |
| 44 | + tetrahedralInterpolation: this.#tetrahedralInterpolation(), |
| 45 | + }) |
| 46 | + ); |
| 47 | + |
| 48 | + constructor() { |
| 49 | + super(); |
| 50 | + requestAnimationInInjectionContext(() => { |
| 51 | + this.#setProps(); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + #setProps() { |
| 56 | + const trigger = computed(() => ({ |
| 57 | + effect: this.effect(), |
| 58 | + invalidate: this.#invalidate(), |
| 59 | + lut: this.#lut(), |
| 60 | + tetrahedralInterpolation: this.#tetrahedralInterpolation(), |
| 61 | + })); |
| 62 | + effect(() => { |
| 63 | + const { effect, lut, invalidate, tetrahedralInterpolation } = trigger(); |
| 64 | + if (!effect) return; |
| 65 | + if (tetrahedralInterpolation) effect.tetrahedralInterpolation = tetrahedralInterpolation; |
| 66 | + if (lut) effect.lut = lut; |
| 67 | + invalidate(); |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
0 commit comments