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
feat(cocos): the colour a script sets on a material instance is the renderer tint
yhheb8ffaa4f2b3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0c9c05c02..3d33c62c0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,7 @@ published separately; it ships inside the editor.- **Scroll areas can be pulled past an end and spring back.** A scroll area whose movement is Elastic now does what the setting always said: a drag past an end is stiffer the further out it goes, letting go returns the content, and a flick that reaches an end bounces off it. It is opt-in per area (`ScrollMovement.Elastic`; Clamped, the default, behaves as before), and the gesture model (`ScrollMotion`, with `ScrollContainer.dragTo` and `getOverscroll`) is exported for scrolling anything else the same way.- **More easing curves.** Sine, quart, quint, expo and circ (each in, out and in-out), bounce in and in-out, and smoothstep and smootherstep join the quad, cubic, back, elastic and out-bounce curves. They are new `EasingType` members after the existing ones, so a saved tween keeps its curve, and they ease the same in a C++ tween and a script value tween.- **World content has a rectangle: `ContentRect`.** A size and the pivot the entity's transform sits on, which the `Sprite` and `Text` on the entity follow and which a `UIMask` on it clips to — no `UINode` needed, so a clipping region or a scroll viewport can stand in the world. `contentRectContains` tests a world point against it.+- **A material colour is the renderer tint.** The engine keeps a mesh colour on the renderer, not in a shader parameter, so `mainColor`, `albedo`, `baseColor` and `color` set on a renderer own material instance write its tint and read back from it.- **Mesh renderers and materials in the Cocos layer.** `MeshRenderer` is a view on the engine mesh renderer: `getSharedMaterial`, `getMaterialInstance` (the engine own per-renderer instance, made once), `material`, `setMaterial`, and `Material.setProperty` / `getProperty` writing through to the engine material (colours 0..255 become 0..1). `ModelRenderer` and `UIMeshRenderer` exist, and `cc.geometry` has `AABB`, `Ray`, `Plane` and the box, ray and plane tests.- **More of `cc`.** `js` (class names and lookup, `isChildClassOf`, `mixin`, `addon`, property definers), `ccenum` and `NodePool` (put a node away, get it back, with the handler component told `unuse` and `reuse`) are in the Cocos compatibility layer.- **The script bundle is not rebuilt for nothing.** The project-script bundle is stamped with every file it read (size and modification time) and the build tool that made it, and Play builds it again only when one of those changed: a project of thousands of scripts goes from a multi-second build at every Play to a check of a few hundred milliseconds.diff --git a/sdk/src/cocos/assets.ts b/sdk/src/cocos/assets.tsindex 2f7a6099d..4f8edbe46 100644--- a/sdk/src/cocos/assets.ts+++ b/sdk/src/cocos/assets.ts@@ -54,14 +54,24 @@ export class Material extends Asset {_handle = 0;private readonly props_ = new Map<string, unknown>();+ /**+ * Where a property goes that is not a shader parameter here: the renderer an instance belongs to holds its colour itself. Answers whether it+ * took the property. @internal+ */+ _take?: (name: string, value: unknown) => boolean;+setProperty(name: string, value: unknown, _passIndex?: number): void {this.props_.set(name, value);+ if (this._take?.(name, value)) return;const uniform = uniformOf(value);if (this._handle > 0 && uniform !== null) EngineMaterial.setUniform(this._handle, name, uniform);}+ /** What the property was last set to. @internal */+ _give?: (name: string) => unknown;+getProperty(name: string, _passIndex?: number): unknown {- return this.props_.get(name);+ return this._give?.(name) ?? this.props_.get(name);}/** The material's passes: there is one, and it has no state a script can read. */diff --git a/sdk/src/cocos/meshRenderer.ts b/sdk/src/cocos/meshRenderer.tsindex d1d5aa773..82ec85c60 100644--- a/sdk/src/cocos/meshRenderer.ts+++ b/sdk/src/cocos/meshRenderer.ts@@ -15,7 +15,7 @@ import type { Entity } from '../types';import { Material } from './assets';import { Component } from './component';import { registerBuiltinComponent } from './builtins';-import { viewAdapter } from './ui/view';+import { toCocosColor, viewAdapter } from './ui/view';/** The layer's material objects, one per engine material, so the same material is the same object. */const wrappers = new Map<number, Material>();@@ -30,6 +30,9 @@ function wrap(handle: number): Material | null {return m;}+/** The names a Cocos effect gives the colour that multiplies what a surface draws. */+const TINT_PROPERTIES = new Set(['mainColor', 'albedo', 'baseColor', 'color']);+/** The base of every renderer that draws a model. */export class ModelRenderer extends Component {}@@ -67,7 +70,21 @@ export class MeshRenderer extends ModelRenderer {this.patch_({ material: this.instance_ });this.source_ = current;}- return wrap(this.instance_);+ const own = wrap(this.instance_)!;+ this.bind_(own);+ return own;+ }++ /** An instance is this renderer's own, so the colour a script gives it is the renderer's tint. */+ private bind_(material: Material): void {+ material._take = (name, value) => {+ if (!TINT_PROPERTIES.has(name) || typeof value !== 'object' || value === null) return false;+ const c = value as { r?: number; g?: number; b?: number; a?: number };+ if (typeof c.r !== 'number') return false;+ this.patch_({ color: { r: c.r / 255, g: c.g! / 255, b: c.b! / 255, a: (c.a ?? 255) / 255 } });+ return true;+ };+ material._give = (name) => (TINT_PROPERTIES.has(name) ? toCocosColor(this.data_().color) : undefined);}setMaterial(material: Material | null, index: number): void {diff --git a/sdk/tests/cocos/mesh-renderer-wasm.test.ts b/sdk/tests/cocos/mesh-renderer-wasm.test.tsindex 305bb6aa9..21822abfe 100644--- a/sdk/tests/cocos/mesh-renderer-wasm.test.ts+++ b/sdk/tests/cocos/mesh-renderer-wasm.test.ts@@ -85,6 +85,20 @@ describe.skipIf(!HAS_WASM)('MeshRenderer on the engine', () => {expect(EngineMaterial.getUniform(shared, 'u_level')).toBe(0); // the shared one did not change});+ it('makes the colour of an instance the renderer tint, and reads it back', () => {+ connect();+ const shared = EngineMaterial.create({ shader: 7 });+ 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)!;+ m.setProperty('mainColor', new Color(255, 128, 0, 255));+ const tint = W().get(n._entity, EngineMeshRenderer).color;+ expect([tint.r, Math.round(tint.g * 255), tint.b, tint.a]).toEqual([1, 128, 0, 1]);+ const back = m.getProperty('mainColor') as Color;+ expect([back.r, back.g, back.b]).toEqual([255, 128, 0]);+ });+it('lets UIMeshRenderer be added without anything behind it', () => {connect();const n = node('ui');diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 343fc8d6c..e6cf6d0d1 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'feat(cocos): the colour a script sets on a material instance is the renderer tint':+ {"note":"A material colour is the renderer tint."},'feat(cocos): MeshRenderer, UIMeshRenderer, material properties and cc.geometry in the compatibility layer':{"note":"Mesh renderers and materials in the Cocos layer."},'feat(cocos): cc.js, ccenum and NodePool exist in the compatibility layer':