Skip to content

Commit 285baba

Browse files
committed
Added the new snapshotFramebuffer method
1 parent d4b1a04 commit 285baba

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

src/renderer/webgl/WebGLRenderer.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ var WebGLRenderer = new Class({
211211
getPixel: false,
212212
callback: null,
213213
type: 'image/png',
214-
encoder: 0.92
214+
encoder: 0.92,
215+
isFramebuffer: false,
216+
bufferWidth: 0,
217+
bufferHeight: 0
215218
};
216219

217220
// Internal Renderer State (Textures, Framebuffers, Pipelines, Buffers, etc)
@@ -2199,6 +2202,61 @@ var WebGLRenderer = new Class({
21992202
return this;
22002203
},
22012204

2205+
/**
2206+
* Takes a snapshot of the given area of the given frame buffer.
2207+
*
2208+
* Unlike the other snapshot methods, this one is processed immediately and doesn't wait for the next render.
2209+
*
2210+
* Snapshots work by using the WebGL `readPixels` feature to grab every pixel from the frame buffer into an ArrayBufferView.
2211+
* It then parses this, copying the contents to a temporary Canvas and finally creating an Image object from it,
2212+
* which is the image returned to the callback provided. All in all, this is a computationally expensive and blocking process,
2213+
* which gets more expensive the larger the canvas size gets, so please be careful how you employ this in your game.
2214+
*
2215+
* @method Phaser.Renderer.WebGL.WebGLRenderer#snapshotFramebuffer
2216+
* @since 3.19.0
2217+
*
2218+
* @param {WebGLFramebuffer} framebuffer - The framebuffer to grab from.
2219+
* @param {integer} bufferWidth - The width of the framebuffer.
2220+
* @param {integer} bufferHeight - The height of the framebuffer.
2221+
* @param {Phaser.Types.Renderer.Snapshot.SnapshotCallback} callback - The Function to invoke after the snapshot image is created.
2222+
* @param {integer} [x=0] - The x coordinate to grab from.
2223+
* @param {integer} [y=0] - The y coordinate to grab from.
2224+
* @param {integer} [width=bufferWidth] - The width of the area to grab.
2225+
* @param {integer} [height=bufferHeight] - The height of the area to grab.
2226+
* @param {string} [type='image/png'] - The format of the image to create, usually `image/png` or `image/jpeg`.
2227+
* @param {number} [encoderOptions=0.92] - The image quality, between 0 and 1. Used for image formats with lossy compression, such as `image/jpeg`.
2228+
*
2229+
* @return {this} This WebGL Renderer.
2230+
*/
2231+
snapshotFramebuffer: function (framebuffer, bufferWidth, bufferHeight, callback, x, y, width, height, type, encoderOptions)
2232+
{
2233+
if (x === undefined) { x = 0; }
2234+
if (y === undefined) { y = 0; }
2235+
if (width === undefined) { width = bufferWidth; }
2236+
if (height === undefined) { height = bufferHeight; }
2237+
2238+
var currentFramebuffer = this.currentFramebuffer;
2239+
2240+
this.snapshotArea(x, y, width, height, callback, type, encoderOptions);
2241+
2242+
var state = this.snapshotState;
2243+
2244+
state.isFramebuffer = true;
2245+
state.bufferWidth = bufferWidth;
2246+
state.bufferHeight = bufferHeight;
2247+
2248+
this.setFramebuffer(framebuffer);
2249+
2250+
WebGLSnapshot(this.canvas, state);
2251+
2252+
this.setFramebuffer(currentFramebuffer);
2253+
2254+
state.callback = null;
2255+
state.isFramebuffer = false;
2256+
2257+
return this;
2258+
},
2259+
22022260
/**
22032261
* Creates a WebGL Texture based on the given canvas element.
22042262
*

0 commit comments

Comments
 (0)