Skip to content

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.

fix(cocos): Material.setProperty takes a texture a script hands it by name, from an image or the frame that holds one, so the fog's region mask reaches its shader

yhhfea78b32fafc
diff --git a/sdk/src/cocos/assets.ts b/sdk/src/cocos/assets.ts
index aad57d2fc..aee58d007 100644
--- a/sdk/src/cocos/assets.ts
+++ b/sdk/src/cocos/assets.ts
@@ -65,6 +65,13 @@ export class AudioClip extends Asset {
_bufferId = '';
}
/** A property value as the engine's material takes it: a number, or a vector of up to four. Colours are 0..255 here and 0..1 there. */
+/** A texture a script hands a material (`setProperty('regionMask', texture)`): the engine texture behind a loaded image or the frame that holds one. */
+function textureOf(value: unknown): { __textureRef: true; textureId: number } | null {
+ const image = value instanceof SpriteFrame ? value.texture : value;
+ if (!(image instanceof ImageAsset) || image._handle <= 0) return null;
+ return { __textureRef: true, textureId: image._handle };
+}
+
function uniformOf(value: unknown): number | { x: number; y: number; z: number; w: number } | null {
if (typeof value === 'number') return value;
if (typeof value === 'boolean') return value ? 1 : 0;
@@ -98,7 +105,7 @@ export class Material extends Asset {
setProperty(name: string, value: unknown, _passIndex?: number): void {
this.props_.set(name, value);
if (this._take?.(name, value)) return;
- const uniform = uniformOf(value);
+ const uniform = textureOf(value) ?? uniformOf(value);
if (this._handle > 0 && uniform !== null) EngineMaterial.setUniform(this._handle, name, uniform);
}
diff --git a/sdk/tests/cocos/mesh-renderer-wasm.test.ts b/sdk/tests/cocos/mesh-renderer-wasm.test.ts
index 21822abfe..e610fbb86 100644
--- a/sdk/tests/cocos/mesh-renderer-wasm.test.ts
+++ b/sdk/tests/cocos/mesh-renderer-wasm.test.ts
@@ -10,7 +10,7 @@ import { World } from '../../src/ecs/world';
import { MeshRenderer as EngineMeshRenderer, Transform, ensureBuiltinComponentsRegistered } from '../../src/ecs/component';
import { AppContext, setDefaultContext } from '../../src/ecs/context';
import { Material as EngineMaterial } from '../../src/render/material';
-import { Color, MeshRenderer, Node, UIMeshRenderer, setCurrentWorld } from '../../src/cocos';
+import { Color, MeshRenderer, Node, SpriteFrame, Texture2D, UIMeshRenderer, setCurrentWorld } from '../../src/cocos';
import { HAS_WASM, loadWasmModule } from '../helpers/loadWasm';
describe.skipIf(!HAS_WASM)('MeshRenderer on the engine', () => {
@@ -85,6 +85,25 @@ describe.skipIf(!HAS_WASM)('MeshRenderer on the engine', () => {
expect(EngineMaterial.getUniform(shared, 'u_level')).toBe(0); // the shared one did not change
});
+ it('hands the engine material the texture a script sets by name, from an image or from the frame that holds one', () => {
+ connect();
+ const shared = EngineMaterial.create({ shader: 7, uniforms: {} });
+ const n = node('t');
+ const r = n.addComponent(MeshRenderer)!;
+ W().set(n._entity, EngineMeshRenderer, { ...W().get(n._entity, EngineMeshRenderer), material: shared });
+ const m = r.getMaterialInstance(0)!;
+ const image = new Texture2D('mask');
+ image._handle = 5;
+ m.setProperty('regionMask', image);
+ expect(EngineMaterial.getUniform(m._handle, 'regionMask')).toMatchObject({ __textureRef: true, textureId: 5 });
+ const other = new Texture2D('other');
+ other._handle = 6;
+ const frame = new SpriteFrame('frame');
+ frame.texture = other;
+ m.setProperty('regionMask', frame);
+ expect(EngineMaterial.getUniform(m._handle, 'regionMask')).toMatchObject({ textureId: 6 });
+ });
+
it('makes the colour of an instance the renderer tint, and reads it back', () => {
connect();
const shared = EngineMaterial.create({ shader: 7 });