Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostProcess: Add hooks to alter shader code used by post processes #13256

Merged
merged 9 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Register a shader code processing per pp name
  • Loading branch information
Popov72 committed Nov 16, 2022
commit 3513c2bb248d64769783af2db423623b1e6a52e9
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,7 @@ export class DefaultRenderingPipeline extends PostProcessRenderPipeline implemen
* @param cameras The array of cameras that the rendering pipeline will be attached to (default: scene.cameras)
* @param automaticBuild If false, you will have to manually call prepare() to update the pipeline (default: true)
*/
constructor(
name = "",
hdr = true,
scene: Scene = EngineStore.LastCreatedScene!,
cameras?: Camera[],
automaticBuild = true,
) {
constructor(name = "", hdr = true, scene: Scene = EngineStore.LastCreatedScene!, cameras?: Camera[], automaticBuild = true) {
super(scene.getEngine(), name);
this._cameras = cameras || scene.cameras;
this._cameras = this._cameras.slice();
Expand Down
35 changes: 26 additions & 9 deletions packages/dev/core/src/PostProcesses/postProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,26 @@ export class PostProcess {
/** @internal */
public _parentContainer: Nullable<AbstractScene> = null;

private static _CustomShaderCodeProcessing: { [postProcessName: string]: PostProcessCustomShaderCodeProcessing } = {};

/**
* Gets or sets the custom callbacks used to alther the post process shader code
* Registers a shader code processing with a post process name.
* @param postProcessName name of the post process. Use null for the fallback shader code processing. This is the shader code processing that will be used in case no specific shader code processing has been associated to a post process name
* @param customShaderCodeProcessing shader code processing to associate to the post process name
* @returns
*/
public static CustomShaderCodeProcessing?: PostProcessCustomShaderCodeProcessing;
public static RegisterShaderCodeProcessing(postProcessName: Nullable<string>, customShaderCodeProcessing?: PostProcessCustomShaderCodeProcessing) {
if (!customShaderCodeProcessing) {
delete PostProcess._CustomShaderCodeProcessing[postProcessName ?? ""];
return;
}

PostProcess._CustomShaderCodeProcessing[postProcessName ?? ""] = customShaderCodeProcessing;
}

private static _GetShaderCodeProcessing(postProcessName: string) {
return PostProcess._CustomShaderCodeProcessing[postProcessName] ?? PostProcess._CustomShaderCodeProcessing[""];
}

/**
* Gets or sets the unique id of the post process
Expand Down Expand Up @@ -526,14 +542,15 @@ export class PostProcess {
vertexUrl?: string,
fragmentUrl?: string
) {
if (PostProcess.CustomShaderCodeProcessing?.defineCustomBindings) {
const customShaderCodeProcessing = PostProcess._GetShaderCodeProcessing(this.name);
if (customShaderCodeProcessing?.defineCustomBindings) {
const newUniforms = uniforms?.slice() ?? [];
newUniforms.push(...this._parameters);

const newSamplers = samplers?.slice() ?? [];
newSamplers.push(...this._samplers);

defines = PostProcess.CustomShaderCodeProcessing.defineCustomBindings(this.name, defines, newUniforms, newSamplers);
defines = customShaderCodeProcessing.defineCustomBindings(this.name, defines, newUniforms, newSamplers);
uniforms = newUniforms;
samplers = newSamplers;
}
Expand All @@ -550,11 +567,11 @@ export class PostProcess {
onCompiled: onCompiled ?? null,
onError: onError ?? null,
indexParameters: indexParameters || this._indexParameters,
processCodeAfterIncludes: PostProcess.CustomShaderCodeProcessing?.processCodeAfterIncludes
? (shaderType: string, code: string) => PostProcess.CustomShaderCodeProcessing!.processCodeAfterIncludes!(this.name, shaderType, code)
processCodeAfterIncludes: customShaderCodeProcessing?.processCodeAfterIncludes
? (shaderType: string, code: string) => customShaderCodeProcessing!.processCodeAfterIncludes!(this.name, shaderType, code)
: null,
processFinalCode: PostProcess.CustomShaderCodeProcessing?.processFinalCode
? (shaderType: string, code: string) => PostProcess.CustomShaderCodeProcessing!.processFinalCode!(this.name, shaderType, code)
processFinalCode: customShaderCodeProcessing?.processFinalCode
? (shaderType: string, code: string) => customShaderCodeProcessing!.processFinalCode!(this.name, shaderType, code)
: null,
},
this._engine
Expand Down Expand Up @@ -836,7 +853,7 @@ export class PostProcess {
this._drawWrapper.effect.setVector2("scale", this._scaleRatio);
this.onApplyObservable.notifyObservers(this._drawWrapper.effect);

PostProcess.CustomShaderCodeProcessing?.bindCustomBindings?.(this.name, this._drawWrapper.effect);
PostProcess._GetShaderCodeProcessing(this.name)?.bindCustomBindings?.(this.name, this._drawWrapper.effect);

return this._drawWrapper.effect;
}
Expand Down