Skip to content

Render Texture

A RenderTexture lets you render a scene (or part of it) to an off-screen texture instead of the screen. This is useful for minimaps, mirrors, portals, post-processing inputs, and any effect that requires capturing rendered output.

Creating a RenderTexture

import { RenderTexture, type RenderTextureHandle } from 'esengine';
const rt: RenderTextureHandle = RenderTexture.create({
width: 512,
height: 512,
depth: true,
filter: 'linear',
});

RenderTextureOptions

FieldTypeDefaultDescription
widthnumberTexture width in pixels
heightnumberTexture height in pixels
depthbooleanfalseAttach a depth buffer
filter'nearest' | 'linear''nearest'Texture filtering mode

Rendering to Texture

Wrap your draw calls between begin() and end(). Everything rendered in between goes to the texture instead of the screen:

import { RenderTexture, Draw } from 'esengine';
RenderTexture.begin(rt, viewProjectionMatrix);
Draw.rect(0, 0, 100, 100, { r: 1, g: 0, b: 0, a: 1 });
RenderTexture.end();

The viewProjection parameter is a Float32Array containing the 4x4 view-projection matrix that defines the camera for this off-screen render pass.

Using the Result

After rendering, rt.textureId holds the GPU texture ID. Use it anywhere a texture is expected:

import { Draw, Sprite } from 'esengine';
Draw.texture(rt.textureId, 0, 0, rt.width, rt.height);

You can also assign it to a Sprite’s texture for entity-based rendering.

Depth Texture

If the RenderTexture was created with depth: true, retrieve the depth texture for effects like shadow mapping:

const depthTextureId = RenderTexture.getDepthTexture(rt);

Lifecycle

Resizing

When the target resolution changes (e.g. window resize), resize the RenderTexture:

const resized = RenderTexture.resize(rt, newWidth, newHeight);

resize() returns a new RenderTextureHandle. Use the returned handle going forward.

Releasing

Release GPU resources when the RenderTexture is no longer needed:

RenderTexture.release(rt);

API Reference

MethodReturnsDescription
RenderTexture.create(options)RenderTextureHandleCreate a new render texture
RenderTexture.begin(rt, viewProjection)voidBegin rendering to texture
RenderTexture.end()voidEnd rendering to texture
RenderTexture.getDepthTexture(rt)numberGet depth texture ID
RenderTexture.resize(rt, width, height)RenderTextureHandleResize render texture
RenderTexture.release(rt)voidRelease GPU resources

RenderTextureHandle

FieldTypeDescription
textureIdnumberGPU texture ID for the color attachment
widthnumberCurrent width in pixels
heightnumberCurrent height in pixels

Next Steps