|
| 1 | +// @ts-expect-error - n8ao is not typed |
| 2 | +import { N8AOPostPass } from 'n8ao'; |
| 3 | + |
| 4 | +import { |
| 5 | + ChangeDetectionStrategy, |
| 6 | + Component, |
| 7 | + computed, |
| 8 | + CUSTOM_ELEMENTS_SCHEMA, |
| 9 | + effect, |
| 10 | + inject, |
| 11 | + input, |
| 12 | +} from '@angular/core'; |
| 13 | +import { applyProps, NgtArgs, pick } from 'angular-three'; |
| 14 | +import { NgtpEffectComposer } from 'angular-three-postprocessing'; |
| 15 | +import { mergeInputs } from 'ngxtension/inject-inputs'; |
| 16 | +import { ColorRepresentation } from 'three'; |
| 17 | + |
| 18 | +export interface NgtpN8AOOptions { |
| 19 | + aoRadius: number; |
| 20 | + aoTones: number; |
| 21 | + distanceFalloff: number; |
| 22 | + intensity: number; |
| 23 | + biasOffset: number; |
| 24 | + biasMultiplier: number; |
| 25 | + aoSamples: number; |
| 26 | + denoiseSamples: number; |
| 27 | + denoiseRadius: number; |
| 28 | + color: ColorRepresentation; |
| 29 | + halfRes: boolean; |
| 30 | + depthAwareUpsampling: boolean; |
| 31 | + screenSpaceRadius: boolean; |
| 32 | + renderMode: 0 | 1 | 2 | 3 | 4; |
| 33 | + denoiseIterations: number; |
| 34 | + transparencyAware: boolean; |
| 35 | + gammaCorrection: boolean; |
| 36 | + logarithmicDepthBuffer: boolean; |
| 37 | + colorMultiply: boolean; |
| 38 | + accumulate: boolean; |
| 39 | + quality?: 'performance' | 'low' | 'medium' | 'high' | 'ultra'; |
| 40 | +} |
| 41 | + |
| 42 | +const defaultOptions: NgtpN8AOOptions = { |
| 43 | + aoSamples: 16, |
| 44 | + aoRadius: 5.0, |
| 45 | + aoTones: 0.0, |
| 46 | + denoiseSamples: 8, |
| 47 | + denoiseRadius: 12, |
| 48 | + distanceFalloff: 1.0, |
| 49 | + intensity: 5, |
| 50 | + denoiseIterations: 2.0, |
| 51 | + renderMode: 0, |
| 52 | + biasOffset: 0.0, |
| 53 | + biasMultiplier: 0.0, |
| 54 | + color: 'black', |
| 55 | + gammaCorrection: true, |
| 56 | + logarithmicDepthBuffer: false, |
| 57 | + screenSpaceRadius: false, |
| 58 | + halfRes: false, |
| 59 | + depthAwareUpsampling: true, |
| 60 | + colorMultiply: true, |
| 61 | + transparencyAware: false, |
| 62 | + accumulate: false, |
| 63 | +}; |
| 64 | + |
| 65 | +@Component({ |
| 66 | + selector: 'ngtp-n8ao', |
| 67 | + standalone: true, |
| 68 | + template: ` |
| 69 | + <ngt-primitive *args="[effect()]" /> |
| 70 | + `, |
| 71 | + imports: [NgtArgs], |
| 72 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 73 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 74 | +}) |
| 75 | +export class NgtpN8AO { |
| 76 | + options = input(defaultOptions, { transform: mergeInputs(defaultOptions) }); |
| 77 | + |
| 78 | + private quality = pick(this.options, 'quality'); |
| 79 | + |
| 80 | + private effectComposer = inject(NgtpEffectComposer); |
| 81 | + |
| 82 | + effect = computed(() => { |
| 83 | + const [scene, camera] = [this.effectComposer.scene(), this.effectComposer.camera()]; |
| 84 | + return new N8AOPostPass(scene, camera); |
| 85 | + }); |
| 86 | + |
| 87 | + constructor() { |
| 88 | + effect(() => { |
| 89 | + const n8aoEffect = this.effect(); |
| 90 | + if (!n8aoEffect) return; |
| 91 | + |
| 92 | + const { quality: _, ...configurations } = this.options(); |
| 93 | + applyProps(n8aoEffect.configuration, configurations); |
| 94 | + }); |
| 95 | + |
| 96 | + effect(() => { |
| 97 | + this.setQualityEffect(); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + private setQualityEffect() { |
| 102 | + const effect = this.effect(); |
| 103 | + if (!effect) return; |
| 104 | + |
| 105 | + const quality = this.quality(); |
| 106 | + if (!quality) return; |
| 107 | + |
| 108 | + const titleCaseQuality = quality.charAt(0).toUpperCase() + quality.slice(1); |
| 109 | + effect.setQuality(titleCaseQuality); |
| 110 | + } |
| 111 | +} |
0 commit comments