|
| 1 | +import { |
| 2 | + afterNextRender, |
| 3 | + ChangeDetectionStrategy, |
| 4 | + Component, |
| 5 | + computed, |
| 6 | + CUSTOM_ELEMENTS_SCHEMA, |
| 7 | + ElementRef, |
| 8 | + inject, |
| 9 | + input, |
| 10 | + untracked, |
| 11 | +} from '@angular/core'; |
| 12 | +import { injectStore, NgtArgs, NgtSelection, omit, pick, resolveRef } from 'angular-three'; |
| 13 | +import { injectAutoEffect } from 'ngxtension/auto-effect'; |
| 14 | +import { mergeInputs } from 'ngxtension/inject-inputs'; |
| 15 | +import { OutlineEffect } from 'postprocessing'; |
| 16 | +import { Object3D } from 'three'; |
| 17 | +import { NgtpEffectComposer } from '../effect-composer'; |
| 18 | + |
| 19 | +export type NgtpOutlineOptions = ConstructorParameters<typeof OutlineEffect>[2] & { |
| 20 | + selection?: Array<Object3D | ElementRef<Object3D>>; |
| 21 | + selectionLayer: number; |
| 22 | +}; |
| 23 | + |
| 24 | +const defaultOptions: NgtpOutlineOptions = { |
| 25 | + selectionLayer: 10, |
| 26 | +}; |
| 27 | + |
| 28 | +@Component({ |
| 29 | + selector: 'ngtp-outline', |
| 30 | + standalone: true, |
| 31 | + template: ` |
| 32 | + <ngt-primitive *args="[effect()]" /> |
| 33 | + `, |
| 34 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 35 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 36 | + imports: [NgtArgs], |
| 37 | +}) |
| 38 | +export class NgtpOutline { |
| 39 | + options = input(defaultOptions, { transform: mergeInputs(defaultOptions) }); |
| 40 | + |
| 41 | + private ngtSelection = inject(NgtSelection, { optional: true }); |
| 42 | + private effectComposer = inject(NgtpEffectComposer); |
| 43 | + private store = injectStore(); |
| 44 | + private invalidate = this.store.select('invalidate'); |
| 45 | + |
| 46 | + private selection = pick(this.options, 'selection'); |
| 47 | + private selectionLayer = pick(this.options, 'selectionLayer'); |
| 48 | + |
| 49 | + private blendFunction = pick(this.options, 'blendFunction'); |
| 50 | + private patternTexture = pick(this.options, 'patternTexture'); |
| 51 | + private edgeStrength = pick(this.options, 'edgeStrength'); |
| 52 | + private pulseSpeed = pick(this.options, 'pulseSpeed'); |
| 53 | + private visibleEdgeColor = pick(this.options, 'visibleEdgeColor'); |
| 54 | + private hiddenEdgeColor = pick(this.options, 'hiddenEdgeColor'); |
| 55 | + private width = pick(this.options, 'width'); |
| 56 | + private height = pick(this.options, 'height'); |
| 57 | + private kernelSize = pick(this.options, 'kernelSize'); |
| 58 | + private blur = pick(this.options, 'blur'); |
| 59 | + private xRay = pick(this.options, 'xRay'); |
| 60 | + private restOptions = omit(this.options, [ |
| 61 | + 'blendFunction', |
| 62 | + 'patternTexture', |
| 63 | + 'edgeStrength', |
| 64 | + 'pulseSpeed', |
| 65 | + 'visibleEdgeColor', |
| 66 | + 'hiddenEdgeColor', |
| 67 | + 'width', |
| 68 | + 'height', |
| 69 | + 'kernelSize', |
| 70 | + 'blur', |
| 71 | + 'xRay', |
| 72 | + ]); |
| 73 | + |
| 74 | + effect = computed(() => { |
| 75 | + const [ |
| 76 | + scene, |
| 77 | + camera, |
| 78 | + blendFunction, |
| 79 | + patternTexture, |
| 80 | + edgeStrength, |
| 81 | + pulseSpeed, |
| 82 | + visibleEdgeColor, |
| 83 | + hiddenEdgeColor, |
| 84 | + width, |
| 85 | + height, |
| 86 | + kernelSize, |
| 87 | + blur, |
| 88 | + xRay, |
| 89 | + restOptions, |
| 90 | + ] = [ |
| 91 | + this.effectComposer.scene(), |
| 92 | + this.effectComposer.camera(), |
| 93 | + this.blendFunction(), |
| 94 | + this.patternTexture(), |
| 95 | + this.edgeStrength(), |
| 96 | + this.pulseSpeed(), |
| 97 | + this.visibleEdgeColor(), |
| 98 | + this.hiddenEdgeColor(), |
| 99 | + this.width(), |
| 100 | + this.height(), |
| 101 | + this.kernelSize(), |
| 102 | + this.blur(), |
| 103 | + this.xRay(), |
| 104 | + untracked(this.restOptions), |
| 105 | + ]; |
| 106 | + |
| 107 | + return new OutlineEffect(scene, camera, { |
| 108 | + blendFunction, |
| 109 | + patternTexture, |
| 110 | + edgeStrength, |
| 111 | + pulseSpeed, |
| 112 | + visibleEdgeColor, |
| 113 | + hiddenEdgeColor, |
| 114 | + width, |
| 115 | + height, |
| 116 | + kernelSize, |
| 117 | + blur, |
| 118 | + xRay, |
| 119 | + ...restOptions, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + constructor() { |
| 124 | + const autoEffect = injectAutoEffect(); |
| 125 | + |
| 126 | + afterNextRender(() => { |
| 127 | + autoEffect(() => { |
| 128 | + const effect = this.effect(); |
| 129 | + return () => effect.dispose(); |
| 130 | + }); |
| 131 | + |
| 132 | + autoEffect(() => { |
| 133 | + const [effect, invalidate, selectionLayer] = [this.effect(), this.invalidate(), this.selectionLayer()]; |
| 134 | + effect.selectionLayer = selectionLayer; |
| 135 | + invalidate(); |
| 136 | + }); |
| 137 | + |
| 138 | + autoEffect(() => { |
| 139 | + // NOTE: we run this effect if declarative NgtSelection is not enabled |
| 140 | + if (!this.ngtSelection) { |
| 141 | + // NOTE: if NgtSelection is not used and selection is not provided, we throw |
| 142 | + if (this.selection() === undefined) { |
| 143 | + throw new Error('[NGT PostProcessing]: ngtp-outline requires selection input or use NgtSelection'); |
| 144 | + } |
| 145 | + |
| 146 | + return this.handleSelectionChangeEffect(this.selection, this.effect, this.invalidate); |
| 147 | + } |
| 148 | + |
| 149 | + // NOTE: we run this effect if declarative NgtSelection is enabled |
| 150 | + const selectionEnabled = this.ngtSelection.enabled(); |
| 151 | + if (!selectionEnabled) return; |
| 152 | + return this.handleSelectionChangeEffect(this.ngtSelection.collection, this.effect, this.invalidate); |
| 153 | + }); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + private handleSelectionChangeEffect( |
| 158 | + collection: () => Array<Object3D | ElementRef<Object3D>> | undefined, |
| 159 | + _effect: () => OutlineEffect, |
| 160 | + _invalidate: () => () => void, |
| 161 | + ) { |
| 162 | + const selection = collection(); |
| 163 | + if (!selection || selection.length === 0) return; |
| 164 | + |
| 165 | + const [effect, invalidate] = [_effect(), _invalidate()]; |
| 166 | + |
| 167 | + const objects: Object3D[] = []; |
| 168 | + for (const el of selection) { |
| 169 | + const obj = resolveRef(el); |
| 170 | + if (!obj) continue; |
| 171 | + objects.push(obj); |
| 172 | + } |
| 173 | + |
| 174 | + if (objects.length === 0) return; |
| 175 | + |
| 176 | + effect.selection.set(objects); |
| 177 | + invalidate(); |
| 178 | + |
| 179 | + return () => { |
| 180 | + effect.selection.clear(); |
| 181 | + invalidate(); |
| 182 | + }; |
| 183 | + } |
| 184 | +} |
0 commit comments