Skip to content

Commit

Permalink
adding prepass to render target #1
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigFeldspar committed Nov 29, 2020
1 parent 6876f6b commit 1590aa1
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 75 deletions.
34 changes: 34 additions & 0 deletions src/Materials/Textures/prePassRenderTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Scene } from "../../scene";
import { Constants } from "../../Engines/constants";
import { PostProcess } from "../../PostProcesses/postProcess";
import { ImageProcessingPostProcess } from "../../PostProcesses/imageProcessingPostProcess";
import { PrePassEffectConfiguration } from "../../Rendering/prePassEffectConfiguration";

/**
* A multi render target designed to render the prepass.
Expand Down Expand Up @@ -37,6 +38,11 @@ export class PrePassRenderTarget extends MultiRenderTarget {
*/
public imageProcessingPostProcess: ImageProcessingPostProcess;

/**
* Configuration for prepass effects
*/
public _effectConfigurations: PrePassEffectConfiguration[] = [];

private _prePassRenderer: PrePassRenderer;

/**
Expand Down Expand Up @@ -138,6 +144,25 @@ export class PrePassRenderTarget extends MultiRenderTarget {
this._reinitializeAttachments();
}

/**
* Adds an effect configuration to the prepass render target.
* If an effect has already been added, it won't add it twice and will return the configuration
* already present.
* @param cfg the effect configuration
* @return the effect configuration now used by the prepass
*/
public _addEffectConfiguration(cfg: PrePassEffectConfiguration) : PrePassEffectConfiguration {
// Do not add twice
for (let i = 0; i < this._effectConfigurations.length; i++) {
if (this._effectConfigurations[i].name === cfg.name) {
return this._effectConfigurations[i];
}
}

this._effectConfigurations.push(cfg);
return cfg;
}

/**
* Prepares this rt to rebuild attachments according to the current texture layout
*/
Expand Down Expand Up @@ -166,7 +191,9 @@ export class PrePassRenderTarget extends MultiRenderTarget {
this.useGeometryBufferFallback = true;
}

const applyByPostProcess = this._scene.imageProcessingConfiguration?.applyByPostProcess;
this.imageProcessingPostProcess = new ImageProcessingPostProcess("prePassComposition", 1, null, undefined, this._engine);
this.imageProcessingPostProcess.imageProcessingConfiguration.applyByPostProcess = applyByPostProcess;
}

public _updateGeometryBufferLayout() {
Expand Down Expand Up @@ -255,6 +282,13 @@ export class PrePassRenderTarget extends MultiRenderTarget {
*/
public dispose() {
super.dispose();

for (let i = 0; i < this._effectConfigurations.length; i++) {
if (this._effectConfigurations[i].dispose) {
this._effectConfigurations[i].dispose!();
}
}

if (this.imageProcessingPostProcess) {
this.imageProcessingPostProcess.dispose();
}
Expand Down
29 changes: 23 additions & 6 deletions src/Materials/Textures/renderTargetTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export class RenderTargetTexture extends Texture {
* Define if sprites should be rendered in your texture.
*/
public renderSprites = false;
/**
* @hidden
* Defines if prepass should be rendered in your texture.
* This is automatically set by the prepass renderer
*/
public _renderPrePass = false;
/**
* Define the camera used to render the texture.
*/
Expand All @@ -133,6 +139,13 @@ export class RenderTargetTexture extends Texture {
public ignoreCameraViewport: boolean = false;

private _postProcessManager: Nullable<PostProcessManager>;

/**
* Post-processes for this render target
*/
public get postProcesses() {
return this._postProcesses;
}
private _postProcesses: PostProcess[];
private _resizeObserver: Nullable<Observer<Engine>>;

Expand Down Expand Up @@ -849,11 +862,13 @@ export class RenderTargetTexture extends Texture {
}

// Bind
if (this._postProcessManager) {
this._postProcessManager._prepareFrame(this._texture, this._postProcesses);
}
else if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
this._bindFrameBuffer(faceIndex, layer);
if (!this._renderPrePass) {
if (this._postProcessManager) {
this._postProcessManager._prepareFrame(this._texture, this._postProcesses);
}
else if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
this._bindFrameBuffer(faceIndex, layer);
}
}

if (this.is2DArray) {
Expand Down Expand Up @@ -889,7 +904,9 @@ export class RenderTargetTexture extends Texture {
if (this.onClearObservable.hasObservers()) {
this.onClearObservable.notifyObservers(engine);
} else {
engine.clear(this.clearColor || scene.clearColor, true, true, true);
if (!this._renderPrePass) {
engine.clear(this.clearColor || scene.clearColor, true, true, true);
}
}

if (!this._doNotChangeAspectRatio) {
Expand Down
Loading

0 comments on commit 1590aa1

Please sign in to comment.