|
| 1 | +import { Component, inject, Input, OnInit } from '@angular/core'; |
| 2 | +import { RxActionFactory } from '@rx-angular/state/actions'; |
| 3 | +import { extend, getLocalState, injectNgtRef, NgtRxStore, NgtStore, startWithUndefined } from 'angular-three'; |
| 4 | +import { DepthDownsamplingPass, EffectComposer, EffectPass, NormalPass, RenderPass } from 'postprocessing'; |
| 5 | +import { combineLatest, map } from 'rxjs'; |
| 6 | +import * as THREE from 'three'; |
| 7 | +import { Group } from 'three'; |
| 8 | +import { isWebGL2Available } from 'three-stdlib'; |
| 9 | + |
| 10 | +extend({ Group }); |
| 11 | + |
| 12 | +@Component({ |
| 13 | + selector: 'ngtp-effect-composer', |
| 14 | + standalone: true, |
| 15 | + template: ` |
| 16 | + <ngt-group [ref]="composerRef"> |
| 17 | + <ng-content /> |
| 18 | + </ngt-group> |
| 19 | + `, |
| 20 | + providers: [RxActionFactory], |
| 21 | +}) |
| 22 | +export class NgtpEffectComposer extends NgtRxStore implements OnInit { |
| 23 | + @Input() composerRef = injectNgtRef<Group>(); |
| 24 | + |
| 25 | + @Input() set enabled(enabled: boolean) { |
| 26 | + this.set({ enabled }); |
| 27 | + } |
| 28 | + |
| 29 | + @Input() set depthBuffer(depthBuffer: boolean) { |
| 30 | + this.set({ depthBuffer }); |
| 31 | + } |
| 32 | + |
| 33 | + @Input() set disableNormalPass(disableNormalPass: boolean) { |
| 34 | + this.set({ disableNormalPass }); |
| 35 | + } |
| 36 | + |
| 37 | + @Input() set stencilBuffer(stencilBuffer: boolean) { |
| 38 | + this.set({ stencilBuffer }); |
| 39 | + } |
| 40 | + |
| 41 | + @Input() set autoClear(autoClear: boolean) { |
| 42 | + this.set({ autoClear }); |
| 43 | + } |
| 44 | + |
| 45 | + @Input() set resolutionScale(resolutionScale: number) { |
| 46 | + this.set({ resolutionScale }); |
| 47 | + } |
| 48 | + |
| 49 | + @Input() set multisampling(multisampling: number) { |
| 50 | + this.set({ multisampling }); |
| 51 | + } |
| 52 | + |
| 53 | + @Input() set frameBufferType(frameBufferType: THREE.TextureDataType) { |
| 54 | + this.set({ frameBufferType }); |
| 55 | + } |
| 56 | + |
| 57 | + @Input() set renderPriority(renderPriority: number) { |
| 58 | + this.set({ renderPriority }); |
| 59 | + } |
| 60 | + |
| 61 | + @Input() set camera(camera: THREE.Camera) { |
| 62 | + this.set({ camera }); |
| 63 | + } |
| 64 | + |
| 65 | + @Input() set scene(scene: THREE.Scene) { |
| 66 | + this.set({ scene }); |
| 67 | + } |
| 68 | + |
| 69 | + override initialize(): void { |
| 70 | + super.initialize(); |
| 71 | + this.set({ |
| 72 | + enabled: true, |
| 73 | + renderPriority: 1, |
| 74 | + autoClear: true, |
| 75 | + multisampling: 0, |
| 76 | + frameBufferType: THREE.HalfFloatType, |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + private readonly store = inject(NgtStore); |
| 81 | + private readonly actions = inject(RxActionFactory<{ setBeforeRender: void }>).create(); |
| 82 | + |
| 83 | + ngOnInit() { |
| 84 | + this.connect( |
| 85 | + 'activeScene', |
| 86 | + combineLatest([this.store.select('scene'), this.select('scene').pipe(startWithUndefined())]).pipe( |
| 87 | + map(([defaultScene, scene]) => scene || defaultScene) |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + this.connect( |
| 92 | + 'activeCamera', |
| 93 | + combineLatest([this.store.select('camera'), this.select('camera').pipe(startWithUndefined())]).pipe( |
| 94 | + map(([defaultCamera, camera]) => camera || defaultCamera) |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + this.connect( |
| 99 | + 'entities', |
| 100 | + combineLatest([ |
| 101 | + this.store.select('gl'), |
| 102 | + this.select('activeScene'), |
| 103 | + this.select('activeCamera'), |
| 104 | + this.select('multisampling'), |
| 105 | + this.select('frameBufferType'), |
| 106 | + this.select('depthBuffer').pipe(startWithUndefined()), |
| 107 | + this.select('stencilBuffer').pipe(startWithUndefined()), |
| 108 | + this.select('disableNormalPass').pipe(startWithUndefined()), |
| 109 | + this.select('resolutionScale').pipe(startWithUndefined()), |
| 110 | + ]).pipe( |
| 111 | + map( |
| 112 | + ([ |
| 113 | + gl, |
| 114 | + scene, |
| 115 | + camera, |
| 116 | + multisampling, |
| 117 | + frameBufferType, |
| 118 | + depthBuffer, |
| 119 | + stencilBuffer, |
| 120 | + disableNormalPass, |
| 121 | + resolutionScale, |
| 122 | + ]) => { |
| 123 | + const webGL2Available = isWebGL2Available(); |
| 124 | + // Initialize composer |
| 125 | + const effectComposer = new EffectComposer(gl, { |
| 126 | + depthBuffer, |
| 127 | + stencilBuffer, |
| 128 | + multisampling: multisampling > 0 && webGL2Available ? multisampling : 0, |
| 129 | + frameBufferType, |
| 130 | + }); |
| 131 | + |
| 132 | + // Add render pass |
| 133 | + effectComposer.addPass(new RenderPass(scene, camera)); |
| 134 | + |
| 135 | + // Create normal pass |
| 136 | + let downSamplingPass = null; |
| 137 | + let normalPass = null; |
| 138 | + if (!disableNormalPass) { |
| 139 | + normalPass = new NormalPass(scene, camera); |
| 140 | + normalPass.enabled = false; |
| 141 | + effectComposer.addPass(normalPass); |
| 142 | + if (resolutionScale !== undefined && webGL2Available) { |
| 143 | + downSamplingPass = new DepthDownsamplingPass({ |
| 144 | + normalBuffer: normalPass.texture, |
| 145 | + resolutionScale, |
| 146 | + }); |
| 147 | + downSamplingPass.enabled = false; |
| 148 | + effectComposer.addPass(downSamplingPass); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return [effectComposer, normalPass, downSamplingPass]; |
| 153 | + } |
| 154 | + ) |
| 155 | + ) |
| 156 | + ); |
| 157 | + |
| 158 | + this.setComposerSize(); |
| 159 | + this.setEffectPassed(); |
| 160 | + this.setBeforeRender(); |
| 161 | + } |
| 162 | + |
| 163 | + private setComposerSize() { |
| 164 | + this.hold( |
| 165 | + combineLatest([this.select('entities'), this.store.select('size')]), |
| 166 | + ([[composer], size]) => void (composer as EffectComposer).setSize(size.width, size.height) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + private setEffectPassed() { |
| 171 | + this.effect( |
| 172 | + combineLatest([ |
| 173 | + this.select('entities'), |
| 174 | + this.select('activeCamera'), |
| 175 | + this.composerRef.children$('nonObjects'), |
| 176 | + ]), |
| 177 | + ([[composer, normalPass, downSamplingPass], camera, effects]) => { |
| 178 | + let effectPass: EffectPass; |
| 179 | + if ( |
| 180 | + this.composerRef.nativeElement && |
| 181 | + Object.keys(getLocalState(this.composerRef.nativeElement)).length && |
| 182 | + composer |
| 183 | + ) { |
| 184 | + effectPass = new EffectPass(camera, ...effects); |
| 185 | + effectPass.renderToScreen = true; |
| 186 | + if (normalPass) normalPass.enabled = true; |
| 187 | + if (downSamplingPass) downSamplingPass.enabled = true; |
| 188 | + } |
| 189 | + |
| 190 | + return () => { |
| 191 | + if (effectPass) composer?.removePass(effectPass); |
| 192 | + if (normalPass) normalPass.enabled = false; |
| 193 | + if (downSamplingPass) downSamplingPass.enabled = false; |
| 194 | + }; |
| 195 | + } |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + private setBeforeRender() { |
| 200 | + const renderPriority = this.get('renderPriority'); |
| 201 | + const enabled = this.get('enabled'); |
| 202 | + this.effect(this.actions.setBeforeRender$, () => |
| 203 | + this.store.get('internal').subscribe( |
| 204 | + ({ delta }) => { |
| 205 | + const [composer] = this.get('entities') || []; |
| 206 | + const enabled = this.get('enabled'); |
| 207 | + const autoClear = this.get('autoClear'); |
| 208 | + const gl = this.store.get('gl'); |
| 209 | + if (composer && enabled) { |
| 210 | + gl.autoClear = autoClear; |
| 211 | + composer.render(delta); |
| 212 | + } |
| 213 | + }, |
| 214 | + enabled ? renderPriority : 0 |
| 215 | + ) |
| 216 | + ); |
| 217 | + this.actions.setBeforeRender(); |
| 218 | + } |
| 219 | +} |
0 commit comments