Skip to content

Commit 15a65ff

Browse files
committed
fbo stories
1 parent e5c67ea commit 15a65ff

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

libs/core/src/lib/portal.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ export class NgtPortal implements OnInit {
176176
const { events, size, ...restInputsState } = inputsState.state || {};
177177

178178
const containerState = inputsState.container;
179-
const container = is.ref(containerState) ? containerState.nativeElement : containerState;
179+
let container = is.ref(containerState) ? containerState.nativeElement : containerState;
180+
181+
if (!is.instance(container)) {
182+
container = prepare(container);
183+
}
180184

181185
const localState = getLocalState(container);
182186
if (!localState.store) {

libs/soba/cameras/src/camera/camera.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ export abstract class NgtsCamera<TCamera extends NgtCamera> {
4141

4242
@Input() cameraRef = injectNgtRef<TCamera>();
4343

44+
private cameraResolution = this.inputs.select('resolution');
45+
4446
protected store = injectNgtStore();
4547
protected fboRef = injectNgtsFBO(() => ({ width: this.cameraResolution() }));
4648

47-
private cameraResolution = this.inputs.select('resolution');
48-
4949
constructor() {
5050
this.setDefaultCamera();
5151
this.updateProjectionMatrix();

libs/soba/src/misc/fbo.stories.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)