-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useCompositeEffectPass hook to have more control over multi…
… pass effects
- Loading branch information
Showing
13 changed files
with
180 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createRenderTarget } from "../core/renderTarget"; | ||
import { useLifeCycleCallback } from "../internal/useLifeCycleCallback"; | ||
import type { | ||
CompositeEffectPass, | ||
EffectPass, | ||
RenderCallback, | ||
RenderTarget, | ||
UpdatedCallback, | ||
} from "../types"; | ||
|
||
export function useCompositeEffectPass<P extends Record<string, EffectPass<any>>>( | ||
passes: P, | ||
): CompositeEffectPass<P> { | ||
const effectPasses = Object.values(passes); | ||
const outputPass = effectPasses.at(-1)!; | ||
|
||
const [beforeRenderCallbacks, onBeforeRender] = useLifeCycleCallback<RenderCallback<any>>(); | ||
const [afterRenderCallbacks, onAfterRender] = useLifeCycleCallback<RenderCallback<any>>(); | ||
const [onUpdatedCallbacks, onUpdated] = useLifeCycleCallback<UpdatedCallback<any>>(); | ||
|
||
function render() { | ||
for (const callback of beforeRenderCallbacks) callback({ uniforms: {} }); | ||
for (const pass of effectPasses) pass.render(); | ||
for (const callback of afterRenderCallbacks) callback({ uniforms: {} }); | ||
} | ||
|
||
function initialize(gl: WebGL2RenderingContext) { | ||
for (const [index, pass] of effectPasses.entries()) { | ||
pass.initialize(gl); | ||
|
||
if (index < effectPasses.length - 1) { | ||
pass.setTarget(createRenderTarget(gl)); | ||
} | ||
|
||
pass.onUpdated((newUniforms, oldUniforms) => { | ||
for (const callback of onUpdatedCallbacks) { | ||
callback(newUniforms, oldUniforms); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function setSize(size: { width: number; height: number }) { | ||
for (const pass of effectPasses) { | ||
pass.setSize(size); | ||
} | ||
} | ||
|
||
function setTarget(target: RenderTarget | null) { | ||
outputPass.setTarget(target); | ||
} | ||
|
||
return { | ||
target: outputPass.target, | ||
passes, | ||
onBeforeRender, | ||
onAfterRender, | ||
onUpdated, | ||
initialize, | ||
render, | ||
setSize, | ||
setTarget, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { EffectUniforms, RenderPass } from "../types"; | ||
import type { EffectPass, EffectUniforms } from "../types"; | ||
import type { QuadPassOptions } from "./useQuadRenderPass"; | ||
import { useQuadRenderPass } from "./useQuadRenderPass"; | ||
|
||
export function useEffectPass<U extends EffectUniforms>( | ||
options: QuadPassOptions<U>, | ||
): RenderPass<U> { | ||
): EffectPass<U> { | ||
return useQuadRenderPass(undefined, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.