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
fix(sdk): RenderTexture exposes a component-usable texture handle
esengine171dd63c4a91
diff --git a/docs/astro/src/content/docs/guides/drawing.mdx b/docs/astro/src/content/docs/guides/drawing.mdxindex 901e5e667..9ab611885 100644--- a/docs/astro/src/content/docs/guides/drawing.mdx+++ b/docs/astro/src/content/docs/guides/drawing.mdx@@ -164,7 +164,7 @@ layout, so they pair directly with fragment-only `.esshader` materials.For minimaps, portraits, mirrors — and as the backing of[cache-as-bitmap](/docs/guides/sprites/) — `RenderTexture` allocates an offscreenrender target. Wrap draw commands in `begin`/`end` to render into it; the-resulting `textureId` is an ordinary texture handle usable anywhere (a+resulting `texture` is an ordinary texture handle usable anywhere (a`Sprite.texture`, `Draw.texture`, a material texture param):```typescript@@ -176,7 +176,7 @@ RenderTexture.begin(rt, viewProjection); // an ortho view-projection you supplDraw.circle({ x: 0, y: 0 }, 80, { r: 1, g: 0.5, b: 0, a: 1 });RenderTexture.end();-sprite.texture = rt.textureId;+sprite.texture = rt.texture;```### `RenderTextureOptions`@@ -191,10 +191,10 @@ sprite.texture = rt.textureId;| Method | Does ||---|---|-| `create(options)` | Allocates the target; returns a handle with `textureId`, `width`, `height`. |+| `create(options)` | Allocates the target; returns a handle with `texture` (resource-table handle for components), `textureId` (raw device id for low-level paths), `width`, `height`. || `begin(rt, viewProjection)` | Redirects subsequent draws into the target (you supply the matrix). || `end()` | Ends offscreen rendering, back to the screen. |-| `resize(rt, width, height)` | Releases and re-creates — returns a **new** handle (the `textureId` changes). |+| `resize(rt, width, height)` | Releases and re-creates — returns a **new** handle (the `texture` handle changes). || `getDepthTexture(rt)` | The depth attachment as a sampleable texture handle. || `release(rt)` | Frees the target and its textures. |diff --git a/docs/astro/src/content/docs/zh-cn/guides/drawing.mdx b/docs/astro/src/content/docs/zh-cn/guides/drawing.mdxindex a40974d19..c3c468125 100644--- a/docs/astro/src/content/docs/zh-cn/guides/drawing.mdx+++ b/docs/astro/src/content/docs/zh-cn/guides/drawing.mdx@@ -153,7 +153,7 @@ registerDrawCallback('custom-mesh', () => {小地图、头像、镜面——以及[位图缓存](/docs/zh-cn/guides/sprites/)的底层——`RenderTexture` 分配一个离屏渲染目标。把绘制命令包在 `begin`/`end` 之间即渲染-进去;得到的 `textureId` 是普通纹理句柄,哪里都能用(`Sprite.texture`、+进去;得到的 `texture` 是普通纹理句柄,哪里都能用(`Sprite.texture`、`Draw.texture`、材质纹理参数):```typescript@@ -165,7 +165,7 @@ RenderTexture.begin(rt, viewProjection); // 你提供的正交 view-projectionDraw.circle({ x: 0, y: 0 }, 80, { r: 1, g: 0.5, b: 0, a: 1 });RenderTexture.end();-sprite.texture = rt.textureId;+sprite.texture = rt.texture;```### `RenderTextureOptions`@@ -180,10 +180,10 @@ sprite.texture = rt.textureId;| 方法 | 作用 ||---|---|-| `create(options)` | 分配目标;返回带 `textureId`、`width`、`height` 的句柄。 |+| `create(options)` | 分配目标;返回带 `texture`(组件用的资源表句柄)、`textureId`(底层设备 id)、`width`、`height` 的句柄。 || `begin(rt, viewProjection)` | 把后续绘制重定向进目标(矩阵由你提供)。 || `end()` | 结束离屏渲染,回到屏幕。 |-| `resize(rt, width, height)` | 释放并重建——返回**新**句柄(`textureId` 会变)。 |+| `resize(rt, width, height)` | 释放并重建——返回**新**句柄(`texture` 句柄会变)。 || `getDepthTexture(rt)` | 以可采样纹理句柄取深度附件。 || `release(rt)` | 释放目标及其纹理。 |diff --git a/examples/render-texture/README.md b/examples/render-texture/README.mdindex fc5e6c4f1..96a547506 100644--- a/examples/render-texture/README.md+++ b/examples/render-texture/README.md@@ -7,7 +7,7 @@ Colored shapes drift along orbits across a 3200×2000 world while the cameraonly ever shows the middle 1280×720 — most shapes are offscreen at any moment.A quad pinned to the top-right corner shows the whole world live: every framea system paints a schematic of the world into a `RenderTexture`, and the-quad's `Sprite` samples the target's `textureId`.+quad's `Sprite` samples the target's `texture`.| Key | Action ||---|---|@@ -26,7 +26,7 @@ quad's `Sprite` samples the target's `textureId`.So a "minimap" here is *painted*, not re-rendered: the system queries theshapes and draws one dot per shape with `Draw.rect`. (For a top-down minimap-that is also what most real games do.) The resulting `textureId` is an+that is also what most real games do.) The resulting `texture` is anordinary texture handle usable anywhere — a `Sprite.texture`, `Draw.texture`,a material texture parameter.@@ -91,7 +91,7 @@ const rt = RenderTexture.create({ width: 384, height: 240, depth: false, filter:| Field | Description ||---|---|-| `textureId` | The color attachment as an ordinary texture handle — assign it to `Sprite.texture`, pass it to `Draw.texture`, bind it as a material param. |+| `texture` | The color attachment as an ordinary texture handle — assign it to `Sprite.texture`, pass it to `Draw.texture`, bind it as a material param. || `width` / `height` | The target's size in pixels. |### Methods@@ -100,7 +100,7 @@ const rt = RenderTexture.create({ width: 384, height: 240, depth: false, filter:|---|---|| `begin(rt, viewProjection)` | Redirects subsequent rendering into the target under the matrix you supply. **Does not clear** the target. || `end()` | Ends the offscreen pass; rendering goes back to the screen. |-| `resize(rt, width, height)` | Releases the old target and creates a fresh one, preserving `depth`/`filter`. **Returns a new handle — the old handle (and its `textureId`) is dead.** Re-point every consumer at the new `textureId`; this example syncs the quad's `Sprite.texture` to `rt.textureId` every frame, so pressing **R** just works. |+| `resize(rt, width, height)` | Releases the old target and creates a fresh one, preserving `depth`/`filter`. **Returns a new handle — the old handle (and its `texture`) is dead.** Re-point every consumer at the new `texture`; this example syncs the quad's `Sprite.texture` to `rt.texture` every frame, so pressing **R** just works. || `getDepthTexture(rt)` | The depth attachment as a sampleable texture handle (targets created with `depth: true` only). || `release(rt)` | Frees the target and its textures. |@@ -108,7 +108,7 @@ const rt = RenderTexture.create({ width: 384, height: 240, depth: false, filter:- `src/systems/minimap.ts` — the heart of the example: creates the target,paints it every frame with the `Draw` API, cycles its resolution on **R**,- and keeps the quad's `Sprite.texture` pointed at the current `textureId`.+ and keeps the quad's `Sprite.texture` pointed at the current `texture`.- `src/systems/setup.ts` — spawns the world (ground-dot grid, border frame),the orbiting shapes, and the minimap quad (`flipY: true`).- `src/systems/orbit.ts` — drifts each shape along its ellipse.@@ -123,7 +123,7 @@ const rt = RenderTexture.create({ width: 384, height: 240, depth: false, filter:引擎支持的确切流程(诚实说明):**相机不能以 RenderTexture 为渲染目标**,它也**不会替你重绘场景**;`begin`/`end` 之间用即时 `Draw` API 画什么,-什么就落进离屏目标,视图投影矩阵由你自己提供。得到的 `textureId` 是普通+什么就落进离屏目标,视图投影矩阵由你自己提供。得到的 `texture` 是普通纹理句柄,可赋给 `Sprite.texture` 等任何吃纹理的地方。- 彩色方块沿椭圆轨道漂移,大部分时间在相机视野之外;右上角小地图每帧由diff --git a/examples/render-texture/src/systems/minimap.ts b/examples/render-texture/src/systems/minimap.tsindex 2c5eafcdb..1e24a7250 100644--- a/examples/render-texture/src/systems/minimap.ts+++ b/examples/render-texture/src/systems/minimap.ts@@ -15,8 +15,9 @@ import {// does not re-render the scene for you. What it does is redirect whatever// you draw with the immediate `Draw` API between `begin`/`end` into an// offscreen target, under a view-projection matrix you supply. The handle's-// `textureId` is then an ordinary texture usable anywhere (here: the corner-// quad's `Sprite.texture`).+// `texture` (a resource-table handle) is then an ordinary texture usable+// anywhere (here: the corner quad's `Sprite.texture`); `textureId` is the+// raw device id for low-level paths.//// So the minimap is painted, not re-rendered: each frame this system opens the// target, lays down a background (the pass does NOT clear — the first opaque@@ -68,9 +69,9 @@ export const minimapSystem = defineSystem(}// R cycles the target's resolution. resize() releases the old target- // and returns a NEW handle — the textureId changes, so every consumer- // must be re-pointed at it (done below, where the quad's Sprite is- // synced to rt.textureId every frame).+ // and returns a NEW handle — the texture handle changes, so every+ // consumer must be re-pointed at it (done below, where the quad's+ // Sprite is synced to rt.texture every frame).if (input.isKeyPressed('KeyR')) {resolutionIndex = (resolutionIndex + 1) % MINIMAP_RESOLUTIONS.length;const res = MINIMAP_RESOLUTIONS[resolutionIndex];@@ -78,7 +79,7 @@ export const minimapSystem = defineSystem(}for (const [, sprite] of minimapQuads) {- if (sprite.texture !== rt.textureId) sprite.texture = rt.textureId;+ if (sprite.texture !== rt.texture) sprite.texture = rt.texture;}// --- The offscreen pass -------------------------------------------diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 137b8e82b..57150a852 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -4888,6 +4888,7 @@ _depth: boolean_filter: "linear" | "nearest"_handle: numberheight: number+texture: numbertextureId: numberwidth: number```diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 94dd55f26..7449e68fe 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -4883,6 +4883,7 @@ _depth: boolean_filter: "linear" | "nearest"_handle: numberheight: number+texture: numbertextureId: numberwidth: number```diff --git a/sdk/src/renderTexture.ts b/sdk/src/renderTexture.tsindex eaab82a05..23e634ad1 100644--- a/sdk/src/renderTexture.ts+++ b/sdk/src/renderTexture.ts@@ -1,6 +1,7 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport { Renderer, type RenderTargetHandle } from './renderer';+import { getResourceManager } from './resourceManager';export interface RenderTextureOptions {width: number;@@ -11,7 +12,15 @@ export interface RenderTextureOptions {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;@@ -26,10 +35,16 @@ export const RenderTexture = {const handle = Renderer.createRenderTarget(options.width, options.height, flags);const textureId = Renderer.getTargetTexture(handle);+ // Same channel video frames use: adopt the device texture into the+ // resource table so components can reference it by handle.+ const texture = textureId !== 0+ ? getResourceManager()?.registerExternalTexture(textureId, options.width, options.height) ?? 0+ : 0;return {_handle: handle,textureId,+ texture,width: options.width,height: options.height,_depth: depth,@@ -38,6 +53,7 @@ export const RenderTexture = {},release(rt: RenderTextureHandle): void {+ if (rt.texture !== 0) getResourceManager()?.releaseTexture(rt.texture);Renderer.releaseRenderTarget(rt._handle);},diff --git a/sdk/tests/renderTexture.test.ts b/sdk/tests/renderTexture.test.tsindex ae7def7a0..82754742d 100644--- a/sdk/tests/renderTexture.test.ts+++ b/sdk/tests/renderTexture.test.ts@@ -55,6 +55,7 @@ describe('RenderTexture', () => {expect(rt).toEqual({_handle: 1,textureId: 100,+ texture: 0,width: 512,height: 512,_depth: true,@@ -102,7 +103,7 @@ describe('RenderTexture', () => {return 2;});- const rt = { _handle: 1, textureId: 100, width: 256, height: 256, _depth: true, _filter: 'nearest' as const };+ const rt = { _handle: 1, textureId: 100, texture: 0, width: 256, height: 256, _depth: true, _filter: 'nearest' as const };RenderTexture.resize(rt, 512, 512);expect(callOrder).toEqual(['release', 'create']);@@ -113,12 +114,13 @@ describe('RenderTexture', () => {(Renderer.createRenderTarget as ReturnType<typeof vi.fn>).mockReturnValue(5);(Renderer.getTargetTexture as ReturnType<typeof vi.fn>).mockReturnValue(500);- const rt = { _handle: 1, textureId: 100, width: 256, height: 256, _depth: true, _filter: 'nearest' as const };+ const rt = { _handle: 1, textureId: 100, texture: 0, width: 256, height: 256, _depth: true, _filter: 'nearest' as const };const resized = RenderTexture.resize(rt, 1024, 768);expect(resized).toEqual({_handle: 5,textureId: 500,+ texture: 0,width: 1024,height: 768,_depth: true,@@ -127,13 +129,13 @@ describe('RenderTexture', () => {});it('preserves depth=false and filter=linear through resize', () => {- const rt = { _handle: 1, textureId: 100, width: 64, height: 64, _depth: false, _filter: 'linear' as const };+ const rt = { _handle: 1, textureId: 100, texture: 0, width: 64, height: 64, _depth: false, _filter: 'linear' as const };RenderTexture.resize(rt, 128, 128);expect(Renderer.createRenderTarget).toHaveBeenCalledWith(128, 128, 2);});it('preserves depth=true and filter=nearest through resize', () => {- const rt = { _handle: 1, textureId: 100, width: 64, height: 64, _depth: true, _filter: 'nearest' as const };+ const rt = { _handle: 1, textureId: 100, texture: 0, width: 64, height: 64, _depth: true, _filter: 'nearest' as const };RenderTexture.resize(rt, 128, 128);expect(Renderer.createRenderTarget).toHaveBeenCalledWith(128, 128, 1);});