Skip to content

Commit c5d6e3d

Browse files
committed
fix(soba): use injectBeforeRender with priority signal
1 parent 16307f3 commit c5d6e3d

File tree

1 file changed

+47
-55
lines changed

1 file changed

+47
-55
lines changed

libs/soba/materials/src/lib/mesh-portal-material.ts

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
Directive,
99
effect,
1010
ElementRef,
11-
inject,
12-
Injector,
1311
input,
1412
signal,
1513
TemplateRef,
@@ -50,7 +48,6 @@ export class ManagePortalScene {
5048
worldUnits = input.required<boolean>();
5149

5250
constructor() {
53-
const injector = inject(Injector);
5451
const renderTextureStore = injectStore();
5552

5653
const buffer1 = injectFBO();
@@ -100,59 +97,54 @@ export class ManagePortalScene {
10097
setEvents({ enabled: events });
10198
});
10299

103-
effect((onCleanup) => {
104-
const priority = this.priority();
105-
106-
// we start the before render in effect because we need the priority input to be resolved
107-
const sub = injectBeforeRender(
108-
({ gl, camera }) => {
109-
const material = this.material();
110-
111-
const instanceState = getInstanceState(material);
112-
if (!instanceState) return;
113-
114-
const parent = instanceState.parent();
115-
if (!parent) return;
116-
117-
const materialBlend = 'blend' in material && typeof material.blend === 'number' ? material.blend : 0;
118-
const [worldUnits, rootScene, scene, [quad, blend]] = [
119-
this.worldUnits(),
120-
this.rootScene(),
121-
renderTextureStore.snapshot.scene,
122-
fullScreenQuad(),
123-
];
124-
// Move portal contents along with the parent if worldUnits is true
125-
if (!worldUnits) {
126-
// If the portal renders exclusively the original scene needs to be updated
127-
if (priority && materialBlend === 1) parent['updateWorldMatrix'](true, false);
128-
scene.matrixWorld.copy(parent['matrixWorld']);
129-
} else {
130-
scene.matrixWorld.identity();
100+
injectBeforeRender(
101+
({ gl, camera }) => {
102+
const material = this.material();
103+
104+
const instanceState = getInstanceState(material);
105+
if (!instanceState) return;
106+
107+
const parent = instanceState.parent();
108+
if (!parent) return;
109+
110+
const priority = this.priority();
111+
const materialBlend = 'blend' in material && typeof material.blend === 'number' ? material.blend : 0;
112+
const [worldUnits, rootScene, scene, [quad, blend]] = [
113+
this.worldUnits(),
114+
this.rootScene(),
115+
renderTextureStore.snapshot.scene,
116+
fullScreenQuad(),
117+
];
118+
// Move portal contents along with the parent if worldUnits is true
119+
if (!worldUnits) {
120+
// If the portal renders exclusively the original scene needs to be updated
121+
if (priority && materialBlend === 1) parent['updateWorldMatrix'](true, false);
122+
scene.matrixWorld.copy(parent['matrixWorld']);
123+
} else {
124+
scene.matrixWorld.identity();
125+
}
126+
127+
// This bit is only necessary if the portal is blended, now it has a render-priority
128+
// and will take over the render loop
129+
if (priority) {
130+
if (materialBlend > 0 && materialBlend < 1) {
131+
// If blend is ongoing (> 0 and < 1) then we need to render both the root scene
132+
// and the portal scene, both will then be mixed in the quad from above
133+
blend.value = materialBlend;
134+
gl.setRenderTarget(buffer1());
135+
gl.render(scene, camera);
136+
gl.setRenderTarget(buffer2());
137+
gl.render(rootScene, camera);
138+
gl.setRenderTarget(null);
139+
quad.render(gl);
140+
} else if (materialBlend === 1) {
141+
// However if blend is 1 we only need to render the portal scene
142+
gl.render(scene, camera);
131143
}
132-
133-
// This bit is only necessary if the portal is blended, now it has a render-priority
134-
// and will take over the render loop
135-
if (priority) {
136-
if (materialBlend > 0 && materialBlend < 1) {
137-
// If blend is ongoing (> 0 and < 1) then we need to render both the root scene
138-
// and the portal scene, both will then be mixed in the quad from above
139-
blend.value = materialBlend;
140-
gl.setRenderTarget(buffer1());
141-
gl.render(scene, camera);
142-
gl.setRenderTarget(buffer2());
143-
gl.render(rootScene, camera);
144-
gl.setRenderTarget(null);
145-
quad.render(gl);
146-
} else if (materialBlend === 1) {
147-
// However if blend is 1 we only need to render the portal scene
148-
gl.render(scene, camera);
149-
}
150-
}
151-
},
152-
{ injector, priority },
153-
);
154-
onCleanup(() => sub());
155-
});
144+
}
145+
},
146+
{ priority: this.priority },
147+
);
156148
}
157149
}
158150

0 commit comments

Comments
 (0)