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

Allow Layers in RTT #5469

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- Added opacity texture support to `GridMaterial` ([Deltakosh](https://github.com/deltakosh))
- Added support for deserializing morph target animations in animation groups
- AssetContainer dispose method ([TrevorDev](https://github.com/TrevorDev))
- `Layer` are now supported in `RenderTargetTexture` ([Sebavan](https://github.com/Sebavan))

### glTF Loader

Expand Down
16 changes: 15 additions & 1 deletion src/Layer/babylon.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ module BABYLON {
*/
public layerMask: number = 0x0FFFFFFF;

/**
* Define the list of render target the layer is visible into.
*/
public renderTargetTextures: RenderTargetTexture[] = [];

/**
* Define if the layer is only used in renderTarget or if it also
* renders in the main frame buffer of the canvas.
*/
public renderOnlyInRenderTargetTextures = false;

private _scene: Scene;
private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
private _indexBuffer: Nullable<WebGLBuffer>;
Expand Down Expand Up @@ -198,7 +209,7 @@ module BABYLON {

// Check
if (!currentEffect.isReady() || !this.texture || !this.texture.isReady()) {
return;
return;
}

var engine = this._scene.getEngine();
Expand Down Expand Up @@ -256,6 +267,9 @@ module BABYLON {
this.texture = null;
}

// Clean RTT list
this.renderTargetTextures = [];

// Remove from scene
var index = this._scene.layers.indexOf(this);
this._scene.layers.splice(index, 1);
Expand Down
49 changes: 40 additions & 9 deletions src/Layer/babylon.layerSceneComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ module BABYLON {
* Registers the component in a given scene
*/
public register(): void {
this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawBackground);
this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawForeground);
this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawCameraBackground);
this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawCameraForeground);

this.scene._beforeRenderTargetDrawStage.registerStep(SceneComponentConstants.STEP_BEFORERENDERTARGETDRAW_LAYER, this, this._drawRenderTargetBackground);
this.scene._afterRenderTargetDrawStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERTARGETDRAW_LAYER, this, this._drawRenderTargetForeground);
}

/**
Expand All @@ -64,27 +67,55 @@ module BABYLON {
}
}

private _draw(camera: Camera, isBackground: boolean): void {
private _draw(predicate: (layer: Layer) => boolean): void {
let layers = this.scene.layers;

if (layers.length) {
this._engine.setDepthBuffer(false);
const cameraLayerMask = camera.layerMask;
for (let layer of layers) {
if (layer.isBackground === isBackground && ((layer.layerMask & cameraLayerMask) !== 0)) {
if (predicate(layer)) {
layer.render();
}
}
this._engine.setDepthBuffer(true);
}
}

private _drawBackground(camera: Camera): void {
this._draw(camera, true);
private _drawCameraPredicate(layer: Layer, isBackground: boolean, cameraLayerMask: number): boolean {
return !layer.renderOnlyInRenderTargetTextures &&
layer.isBackground === isBackground &&
((layer.layerMask & cameraLayerMask) !== 0);
}

private _drawCameraBackground(camera: Camera): void {
this._draw((layer: Layer) => {
return this._drawCameraPredicate(layer, true, camera.layerMask);
});
}

private _drawCameraForeground(camera: Camera): void {
this._draw((layer: Layer) => {
return this._drawCameraPredicate(layer, false, camera.layerMask);
});
}

private _drawRenderTargetPredicate(layer: Layer, isBackground: boolean, cameraLayerMask: number, renderTargetTexture: RenderTargetTexture): boolean {
return (layer.renderTargetTextures.length > 0) &&
layer.isBackground === isBackground &&
(layer.renderTargetTextures.indexOf(renderTargetTexture) > -1) &&
((layer.layerMask & cameraLayerMask) !== 0);
}

private _drawRenderTargetBackground(renderTarget: RenderTargetTexture): void {
this._draw((layer: Layer) => {
return this._drawRenderTargetPredicate(layer, true, this.scene.activeCamera!.layerMask, renderTarget);
});
}

private _drawForeground(camera: Camera): void {
this._draw(camera, false);
private _drawRenderTargetForeground(renderTarget: RenderTargetTexture): void {
this._draw((layer: Layer) => {
return this._drawRenderTargetPredicate(layer, false, this.scene.activeCamera!.layerMask, renderTarget);
});
}
}
}
10 changes: 10 additions & 0 deletions src/Materials/Textures/babylon.renderTargetTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,19 @@ module BABYLON {
scene.updateTransformMatrix(true);
}

// Before Camera Draw
for (let step of scene._beforeRenderTargetDrawStage) {
step.action(this);
}

// Render
this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);

// After Camera Draw
for (let step of scene._afterRenderTargetDrawStage) {
step.action(this);
}

if (this._postProcessManager) {
this._postProcessManager._finalizeFrame(false, this._texture, faceIndex, this._postProcesses, this.ignoreCameraViewport);
}
Expand Down
12 changes: 12 additions & 0 deletions src/babylon.scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,11 @@ module BABYLON {
* Defines the actions happening just before the active camera is drawing.
*/
public _beforeCameraDrawStage = Stage.Create<CameraStageAction>();
/**
* @hidden
* Defines the actions happening just before a render target is drawing.
*/
public _beforeRenderTargetDrawStage = Stage.Create<RenderTargetStageAction>();
/**
* @hidden
* Defines the actions happening just before a rendering group is drawing.
Expand All @@ -1195,6 +1200,11 @@ module BABYLON {
* Defines the actions happening just after the active camera has been drawn.
*/
public _afterCameraDrawStage = Stage.Create<CameraStageAction>();
/**
* @hidden
* Defines the actions happening just after a render target has been drawn.
*/
public _afterRenderTargetDrawStage = Stage.Create<RenderTargetStageAction>();
/**
* @hidden
* Defines the actions happening just after rendering all cameras and computing intersections.
Expand Down Expand Up @@ -4782,11 +4792,13 @@ module BABYLON {
this._activeMeshStage.clear();
this._cameraDrawRenderTargetStage.clear();
this._beforeCameraDrawStage.clear();
this._beforeRenderTargetDrawStage.clear();
this._beforeRenderingGroupDrawStage.clear();
this._beforeRenderingMeshStage.clear();
this._afterRenderingMeshStage.clear();
this._afterRenderingGroupDrawStage.clear();
this._afterCameraDrawStage.clear();
this._afterRenderTargetDrawStage.clear();
this._afterRenderStage.clear();
this._beforeCameraUpdateStage.clear();
this._beforeClearStage.clear();
Expand Down
9 changes: 9 additions & 0 deletions src/babylon.sceneComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module BABYLON {
public static readonly STEP_BEFORECAMERADRAW_EFFECTLAYER = 0;
public static readonly STEP_BEFORECAMERADRAW_LAYER = 1;

public static readonly STEP_BEFORERENDERTARGETDRAW_LAYER = 0;

public static readonly STEP_BEFORERENDERINGMESH_OUTLINE = 0;

public static readonly STEP_AFTERRENDERINGMESH_OUTLINE = 0;
Expand All @@ -47,6 +49,8 @@ module BABYLON {

public static readonly STEP_BEFORECLEAR_PROCEDURALTEXTURE = 0;

public static readonly STEP_AFTERRENDERTARGETDRAW_LAYER = 0;

public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER = 0;
public static readonly STEP_AFTERCAMERADRAW_LENSFLARESYSTEM = 1;
public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW = 2;
Expand Down Expand Up @@ -145,6 +149,11 @@ module BABYLON {
*/
export type CameraStageAction = (camera: Camera) => void;

/**
* Strong typing of a Render Target related stage step action
*/
export type RenderTargetStageAction = (renderTarget: RenderTargetTexture) => void;

/**
* Strong typing of a RenderingGroup related stage step action
*/
Expand Down