Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created April 17, 2025 10:09
Show Gist options
  • Save ayamflow/3b23c3abc5d45f2719c8c826bb0ab0f1 to your computer and use it in GitHub Desktop.
Save ayamflow/3b23c3abc5d45f2719c8c826bb0ab0f1 to your computer and use it in GitHub Desktop.
blit threejs framebuffer / renderTarget
function blit(renderer: WebGLRenderer, source: WebGLRenderTarget, dest: WebGLRenderTarget) {
const gl = renderer.getContext() as WebGL2RenderingContext;
const prop = renderer.properties;
const readBuffer = (prop.get(source) as any).__webglFramebuffer;
const readTexture = (prop.get(source.texture) as any).__webglTexture;
const writeBuffer = (prop.get(dest) as any).__webglFramebuffer;
const writeTexture = (prop.get(dest.texture) as any).__webglTexture;
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, readBuffer);
gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, readTexture, 0);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, writeBuffer);
gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, writeTexture, 0);
gl.blitFramebuffer(
0,
0,
source.width,
source.height,
0,
0,
source.width,
source.height,
gl.COLOR_BUFFER_BIT,
gl.NEAREST,
);
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment