Skip to content

Commit 5ec26fa

Browse files
committed
Cameras now emit prerender and postrender events if they are set to render to textures.
1 parent e92a019 commit 5ec26fa

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Doing this gives you the ability to modify the texture before this happens, allo
5656
* `Camera.glTexture` is a WebGL Texture that the Camera will render to if running under the WebGL Renderer and rendering to a texture.
5757
* `Camera.framebuffer` is a WebGL Frame Buffer that the Camera will render to if running under the WebGL Renderer and rendering to a texture.
5858
* `Camera.pipeline` is the Pipeline that the Camera will render with if running under the WebGL Renderer and rendering to a texture with a pipeline set.
59+
* If you set a Camera to render to a texture then it will emit 2 events during the render loop. First, it will emit the event `prerender`. This happens right before any Game Object's are drawn to the Camera texture. Then, it will emit the event `postrender`. This happens after all Game Object's have been drawn, but right before the Camera texture is rendered to the main game canvas. It's the final point at which you can manipulate the texture before it appears in-game.
5960

6061
## New Features
6162

src/cameras/2d/Camera.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,17 @@ var Camera = new Class({
291291
* on the texture.
292292
*
293293
* If running under Canvas the Camera will render to its `canvas` property.
294+
*
294295
* If running under WebGL the Camera will create a frame buffer, which is stored in its `framebuffer` and `glTexture` properties.
295296
*
297+
* If you set a camera to render to a texture then it will emit 2 events during the render loop:
298+
*
299+
* First, it will emit the event `prerender`. This happens right before any Game Object's are drawn to the Camera texture.
300+
*
301+
* Then, it will emit the event `postrender`. This happens after all Game Object's have been drawn, but right before the
302+
* Camera texture is rendered to the main game canvas. It's the final point at which you can manipulate the texture before
303+
* it appears in-game.
304+
*
296305
* You should not enable this unless you plan on actually using the texture it creates
297306
* somehow, otherwise you're just doubling the work required to render your game.
298307
*

src/renderer/canvas/CanvasRenderer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ var CanvasRenderer = new Class({
401401
var cw = camera._cw;
402402
var ch = camera._ch;
403403

404-
var ctx = scene.sys.context;
404+
var ctx = (camera.renderToTexture) ? camera.context : scene.sys.context;
405+
405406
var scissor = (cx !== 0 || cy !== 0 || cw !== ctx.canvas.width || ch !== ctx.canvas.height);
406407

407408
this.currentContext = ctx;
@@ -428,6 +429,11 @@ var CanvasRenderer = new Class({
428429
ctx.clip();
429430
}
430431

432+
if (camera.renderToTexture)
433+
{
434+
camera.emit('prerender', camera);
435+
}
436+
431437
camera.matrix.copyToContext(ctx);
432438

433439
for (var i = 0; i < childCount; i++)
@@ -466,6 +472,13 @@ var CanvasRenderer = new Class({
466472
{
467473
ctx.restore();
468474
}
475+
476+
if (camera.renderToTexture)
477+
{
478+
camera.emit('postrender', camera);
479+
480+
scene.sys.context.drawImage(camera.canvas, cx, cy);
481+
}
469482
},
470483

471484
/**

src/renderer/webgl/WebGLRenderer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,8 @@ var WebGLRenderer = new Class({
15691569
color.alphaGL
15701570
);
15711571
}
1572+
1573+
camera.emit('prerender', camera);
15721574
}
15731575
else if (color.alphaGL > 0)
15741576
{
@@ -1608,11 +1610,12 @@ var WebGLRenderer = new Class({
16081610

16091611
if (camera.renderToTexture)
16101612
{
1611-
// this.flush();
16121613
TextureTintPipeline.flush();
16131614

16141615
this.setFramebuffer(null);
16151616

1617+
camera.emit('postrender', camera);
1618+
16161619
TextureTintPipeline.projOrtho(0, TextureTintPipeline.width, TextureTintPipeline.height, 0, -1000.0, 1000.0);
16171620

16181621
var getTint = Utils.getTintAppendFloatAlpha;
@@ -1641,8 +1644,6 @@ var WebGLRenderer = new Class({
16411644
null
16421645
);
16431646

1644-
// this.setPipeline(TextureTintPipeline);
1645-
16461647
// Force clear the current texture so that items next in the batch (like Graphics) don't try and use it
16471648
this.setBlankTexture(true);
16481649
}

0 commit comments

Comments
 (0)