A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { Renderer, deviceGeneration, type RenderTargetHandle } from './renderer';
import { getResourceManager } from '../wasm/resourceManager';
import { warn } from '../util/logger';
let warnedNoTarget = false;
export interface RenderTextureOptions {
width: number;
height: number;
depth?: boolean;
filter?: 'nearest' | 'linear';
}
export interface RenderTextureHandle {
_handle: RenderTargetHandle;
/** Raw device texture id — for Draw.texture / low-level GL paths only. */
textureId: number;
/**
* Resource-table handle for the color texture — what `Sprite.texture` /
* `UIVisual.texture` consume (components resolve through the texture table;
* a raw device id would fall back to the white texture). 0 when no resource
* manager is live (headless renderer-only hosts).
*/
texture: number;
width: number;
height: number;
_depth: boolean;
_filter: 'nearest' | 'linear';
/** The device generation its pixels were drawn on — see {@link RenderTexture.contentLost}. */
_generation: number;
}
/**
* Handle → the size of the target behind it. A `Camera.renderTarget` is a bare
* handle (a component holds no object), and what decides that camera's
* resolution is the target's own size — so the one place that knows it says so.
*/
const sizes = new Map<RenderTargetHandle, { width: number; height: number }>();
/** The size of a live render target, or null once it has been released. */
export function renderTargetSize(handle: RenderTargetHandle): { width: number; height: number } | null {
return sizes.get(handle) ?? null;
}
export const RenderTexture = {
create(options: RenderTextureOptions): RenderTextureHandle {
const depth = options.depth ?? true;
const linear = options.filter === 'linear';
const flags = (depth ? 1 : 0) | (linear ? 2 : 0);
const handle = Renderer.createRenderTarget(options.width, options.height, flags);
if (handle === 0 && !warnedNoTarget) {
warnedNoTarget = true;
warn('render', `RenderTexture.create(${options.width}x${options.height}) made no render target: the renderer is not up yet, or this engine core has none`);
}
const textureId = Renderer.getTargetTexture(handle);
// The target keeps its texture; the resource table only names it, so
// components can reference it by handle.
const texture = textureId !== 0
? getResourceManager()?.wrapDeviceTexture?.(textureId, options.width, options.height) ?? 0
: 0;
sizes.set(handle, { width: options.width, height: options.height });
return {
_handle: handle,
textureId,
texture,
width: options.width,
height: options.height,
_depth: depth,
_filter: linear ? 'linear' : 'nearest',
_generation: deviceGeneration(),
};
},
release(rt: RenderTextureHandle): void {
if (rt.texture !== 0) getResourceManager()?.releaseTexture(rt.texture);
sizes.delete(rt._handle);
Renderer.releaseRenderTarget(rt._handle);
},
resize(rt: RenderTextureHandle, width: number, height: number): RenderTextureHandle {
RenderTexture.release(rt);
return RenderTexture.create({ width, height, depth: rt._depth, filter: rt._filter });
},
begin(rt: RenderTextureHandle, viewProjection: Float32Array): void {
Renderer.begin(viewProjection, rt._handle);
},
end(): void {
Renderer.end();
},
getDepthTexture(rt: RenderTextureHandle): number {
return Renderer.getTargetDepthTexture(rt._handle);
},
/**
* Whether the device has been rebuilt since this target was drawn: the storage
* is there, zero-filled, and only whoever drew the pixels can put them back.
* A target a camera renders every frame never needs asking; one drawn once
* (a minimap, a baked mask) does. Pair it with `onDeviceRestored`.
*/
contentLost(rt: RenderTextureHandle): boolean {
return rt._generation !== deviceGeneration();
},
/** Says the target has been drawn again, which is what clears {@link contentLost}. */
contentDrawn(rt: RenderTextureHandle): void {
rt._generation = deviceGeneration();
},
};