|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, Input, computed } from '@angular/core'; |
| 2 | +import { NgtArgs, NgtPortal, NgtPortalContent, injectBeforeRender, injectNgtRef, signalStore } from 'angular-three'; |
| 3 | +import { NgtsPerspectiveCamera } from 'angular-three-soba/cameras'; |
| 4 | +import { NgtsFBOParams, injectNgtsFBO } from 'angular-three-soba/misc'; |
| 5 | +import * as THREE from 'three'; |
| 6 | +import { Mesh } from 'three'; |
| 7 | +import { color, makeDecorators, makeStoryFunction, makeStoryObject } from '../setup-canvas'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'fbo-spinning-thing', |
| 11 | + standalone: true, |
| 12 | + template: ` |
| 13 | + <ngt-mesh (beforeRender)="onBeforeRender($event.object)"> |
| 14 | + <ngt-torus-knot-geometry *args="[1, 0.4, 100, 64]" /> |
| 15 | + <ngt-mesh-normal-material /> |
| 16 | + </ngt-mesh> |
| 17 | + `, |
| 18 | + imports: [NgtArgs], |
| 19 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 20 | +}) |
| 21 | +class SpinningThing { |
| 22 | + onBeforeRender(thing: Mesh) { |
| 23 | + thing.rotation.x = thing.rotation.y = thing.rotation.z += 0.01; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +@Component({ |
| 28 | + standalone: true, |
| 29 | + template: ` |
| 30 | + <ngts-perspective-camera [position]="[0, 0, 3]" [cameraRef]="cameraRef" /> |
| 31 | +
|
| 32 | + <ngt-portal [container]="scene()" [autoRender]="false"> |
| 33 | + <fbo-spinning-thing *ngtPortalContent /> |
| 34 | + </ngt-portal> |
| 35 | +
|
| 36 | + <ngt-mesh> |
| 37 | + <ngt-box-geometry *args="[3, 3, 3]" /> |
| 38 | + <ngt-mesh-standard-material [map]="target.nativeElement?.texture" /> |
| 39 | + </ngt-mesh> |
| 40 | + `, |
| 41 | + imports: [SpinningThing, NgtsPerspectiveCamera, NgtPortal, NgtPortalContent, NgtArgs], |
| 42 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 43 | +}) |
| 44 | +class DefaultFBOStory { |
| 45 | + cameraRef = injectNgtRef<THREE.PerspectiveCamera>(); |
| 46 | + |
| 47 | + private inputs = signalStore<{ color: THREE.ColorRepresentation; fboParams: Partial<NgtsFBOParams['settings']> }>({ |
| 48 | + color: 'orange' as THREE.ColorRepresentation, |
| 49 | + }); |
| 50 | + @Input() set color(color: THREE.ColorRepresentation) { |
| 51 | + this.inputs.set({ color }); |
| 52 | + } |
| 53 | + |
| 54 | + @Input() set fboParams(fboParams: Partial<NgtsFBOParams['settings']>) { |
| 55 | + this.inputs.set({ fboParams }); |
| 56 | + } |
| 57 | + |
| 58 | + private _color = this.inputs.select('color'); |
| 59 | + |
| 60 | + scene = computed(() => { |
| 61 | + const scene = new THREE.Scene(); |
| 62 | + scene.background = new THREE.Color(this._color()); |
| 63 | + return scene; |
| 64 | + }); |
| 65 | + |
| 66 | + target = injectNgtsFBO(() => ({ width: this.inputs.select('fboParams')() })); |
| 67 | + |
| 68 | + constructor() { |
| 69 | + injectBeforeRender((state) => { |
| 70 | + const [camera, target, scene] = [this.cameraRef.nativeElement, this.target.nativeElement, this.scene()]; |
| 71 | + if (!target) return; |
| 72 | + camera.position.z = 5 + Math.sin(state.clock.getElapsedTime() * 1.5) * 2; |
| 73 | + state.gl.setRenderTarget(target); |
| 74 | + state.gl.render(scene, camera); |
| 75 | + state.gl.setRenderTarget(null); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export default { |
| 81 | + title: 'Misc/injectNgtsFBO', |
| 82 | + decorators: makeDecorators(), |
| 83 | +}; |
| 84 | + |
| 85 | +export const Default = makeStoryFunction(DefaultFBOStory); |
| 86 | +export const WithSettings = makeStoryObject(DefaultFBOStory, { |
| 87 | + argsOptions: { |
| 88 | + color: color('blue'), |
| 89 | + fboParams: { |
| 90 | + multisample: true, |
| 91 | + samples: 8, |
| 92 | + stencilBuffer: false, |
| 93 | + format: THREE.RGBAFormat, |
| 94 | + }, |
| 95 | + }, |
| 96 | +}); |
0 commit comments