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): MeshRenderer, UIMeshRenderer, material properties and cc.geometry in the compatibility layer
yhh321f2c50d5b5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b0b77f222..0c9c05c02 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.+- **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.- **Two Cocos classes with one name.** Cocos tells classes apart by their script, so two scripts may write the same `@ccclass` name. The converted project gives the second its own component name (`Name_2`, in path order), writes which script that was beside the entry, and the bundler writes the new name into that script decorator.diff --git a/sdk/src/cocos/assets.ts b/sdk/src/cocos/assets.tsindex 283b6f4a6..2f7a6099d 100644--- a/sdk/src/cocos/assets.ts+++ b/sdk/src/cocos/assets.ts@@ -8,6 +8,7 @@* `@property(SpriteFrame)` can say "this field is a sprite frame", and a value held in such a field is the path (loading it is* the asset system's job). They carry no behaviour.*/+import { Material as EngineMaterial } from '../render/material';export class Asset { constructor(public path = '') {} }export class ImageAsset extends Asset {}@@ -28,7 +29,44 @@ export class Prefab extends Asset {_failed = '';}export class AudioClip extends Asset {}-export class Material extends Asset {}+/** 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. */+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;+ if (typeof value !== 'object' || value === null) return null;+ const v = value as Record<string, unknown>;+ if (typeof v.r === 'number' && typeof v.g === 'number' && typeof v.b === 'number') {+ return { x: v.r / 255, y: v.g / 255, z: v.b / 255, w: typeof v.a === 'number' ? v.a / 255 : 1 };+ }+ if (typeof v.x === 'number' && typeof v.y === 'number') {+ return { x: v.x, y: v.y, z: typeof v.z === 'number' ? v.z : 0, w: typeof v.w === 'number' ? v.w : 0 };+ }+ return null;+}++/**+ * `Material`: the engine's material (`handle`) with Cocos's property calls. A value is written to the engine material under the name the+ * script gives it, and kept here so `getProperty` answers; a project effect's own property names are not the engine shader's, so a+ * write the shader does not declare is the engine's to report and ignore.+ */+export class Material extends Asset {+ /** The engine material this stands for; 0 for one the engine has not loaded. @internal */+ _handle = 0;+ private readonly props_ = new Map<string, unknown>();++ setProperty(name: string, value: unknown, _passIndex?: number): void {+ this.props_.set(name, value);+ const uniform = uniformOf(value);+ if (this._handle > 0 && uniform !== null) EngineMaterial.setUniform(this._handle, name, uniform);+ }++ getProperty(name: string, _passIndex?: number): unknown {+ return this.props_.get(name);+ }++ /** The material's passes: there is one, and it has no state a script can read. */+ get passes(): never[] { return []; }+}export class JsonAsset extends Asset { json: unknown = null; }export class TextAsset extends Asset { text = ''; }export class BitmapFont extends Asset {}diff --git a/sdk/src/cocos/geometry.ts b/sdk/src/cocos/geometry.tsnew file mode 100644index 000000000..888075c7d--- /dev/null+++ b/sdk/src/cocos/geometry.ts@@ -0,0 +1,142 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file cocos/geometry.ts+ * @brief `cc.geometry`: the boxes, rays and planes scripts test against, and the intersection tests between them.+ *+ * Plain maths on the layer's `Vec3`; nothing here touches the engine. Only the shapes and tests scripts actually reach for are here.+ */+import { Vec3 } from './math';++/** An axis-aligned box: its centre and half the size along each axis. */+export class AABB {+ center: Vec3;+ halfExtents: Vec3;++ constructor(px = 0, py = 0, pz = 0, hw = 1, hh = 1, hl = 1) {+ this.center = new Vec3(px, py, pz);+ this.halfExtents = new Vec3(hw, hh, hl);+ }++ static create(px = 0, py = 0, pz = 0, hw = 1, hh = 1, hl = 1): AABB { return new AABB(px, py, pz, hw, hh, hl); }+ static clone(a: AABB): AABB { return new AABB(a.center.x, a.center.y, a.center.z, a.halfExtents.x, a.halfExtents.y, a.halfExtents.z); }+ static copy(out: AABB, a: AABB): AABB { out.center.set(a.center); out.halfExtents.set(a.halfExtents); return out; }++ /** The box that spans two corners. */+ static fromPoints(minPos: Vec3, maxPos: Vec3, out: AABB = new AABB()): AABB {+ out.center.set((maxPos.x + minPos.x) * 0.5, (maxPos.y + minPos.y) * 0.5, (maxPos.z + minPos.z) * 0.5);+ out.halfExtents.set((maxPos.x - minPos.x) * 0.5, (maxPos.y - minPos.y) * 0.5, (maxPos.z - minPos.z) * 0.5);+ return out;+ }++ /** The box that holds both. */+ static merge(out: AABB, a: AABB, b: AABB): AABB {+ const min = new Vec3(Math.min(a.center.x - a.halfExtents.x, b.center.x - b.halfExtents.x), Math.min(a.center.y - a.halfExtents.y, b.center.y - b.halfExtents.y), Math.min(a.center.z - a.halfExtents.z, b.center.z - b.halfExtents.z));+ const max = new Vec3(Math.max(a.center.x + a.halfExtents.x, b.center.x + b.halfExtents.x), Math.max(a.center.y + a.halfExtents.y, b.center.y + b.halfExtents.y), Math.max(a.center.z + a.halfExtents.z, b.center.z + b.halfExtents.z));+ return AABB.fromPoints(min, max, out);+ }++ set(px: number, py: number, pz: number, hw: number, hh: number, hl: number): void {+ this.center.set(px, py, pz);+ this.halfExtents.set(hw, hh, hl);+ }++ clone(): AABB { return AABB.clone(this); }+ copy(a: AABB): AABB { return AABB.copy(this, a); }+ getBoundary(minPos: Vec3, maxPos: Vec3): void {+ minPos.set(this.center.x - this.halfExtents.x, this.center.y - this.halfExtents.y, this.center.z - this.halfExtents.z);+ maxPos.set(this.center.x + this.halfExtents.x, this.center.y + this.halfExtents.y, this.center.z + this.halfExtents.z);+ }+}++/** A ray from `o` along `d`. */+export class Ray {+ o: Vec3;+ d: Vec3;++ constructor(ox = 0, oy = 0, oz = 0, dx = 0, dy = 0, dz = -1) {+ this.o = new Vec3(ox, oy, oz);+ this.d = new Vec3(dx, dy, dz);+ }++ static create(ox = 0, oy = 0, oz = 0, dx = 0, dy = 0, dz = -1): Ray { return new Ray(ox, oy, oz, dx, dy, dz); }+ static clone(r: Ray): Ray { return new Ray(r.o.x, r.o.y, r.o.z, r.d.x, r.d.y, r.d.z); }+ static copy(out: Ray, r: Ray): Ray { out.o.set(r.o); out.d.set(r.d); return out; }++ /** The ray from one point towards another. */+ static fromPoints(out: Ray, origin: Vec3, target: Vec3): Ray {+ out.o.set(origin);+ out.d.set(target.x - origin.x, target.y - origin.y, target.z - origin.z).normalize();+ return out;+ }++ /** The point `distance` along the ray. */+ computeHit(out: Vec3, distance: number): void {+ out.set(this.o.x + this.d.x * distance, this.o.y + this.d.y * distance, this.o.z + this.d.z * distance);+ }++ clone(): Ray { return Ray.clone(this); }+ copy(r: Ray): Ray { return Ray.copy(this, r); }+}++/** The plane `n · p = d`. */+export class Plane {+ n: Vec3;+ d: number;++ constructor(nx = 0, ny = 1, nz = 0, d = 0) {+ this.n = new Vec3(nx, ny, nz);+ this.d = d;+ }++ static create(nx = 0, ny = 1, nz = 0, d = 0): Plane { return new Plane(nx, ny, nz, d); }+ static clone(p: Plane): Plane { return new Plane(p.n.x, p.n.y, p.n.z, p.d); }+ static copy(out: Plane, p: Plane): Plane { out.n.set(p.n); out.d = p.d; return out; }++ /** The plane through @p point with the given normal. */+ static fromNormalAndPoint(out: Plane, normal: Vec3, point: Vec3): Plane {+ out.n.set(normal);+ out.d = normal.x * point.x + normal.y * point.y + normal.z * point.z;+ return out;+ }++ clone(): Plane { return Plane.clone(this); }+}++const dot = (a: Vec3, b: Vec3): number => a.x * b.x + a.y * b.y + a.z * b.z;++/** The intersection tests. A ray test answers the distance along the ray, or 0 when it misses. */+export const intersect = {+ aabbWithAABB(a: AABB, b: AABB): boolean {+ return Math.abs(a.center.x - b.center.x) <= a.halfExtents.x + b.halfExtents.x+ && Math.abs(a.center.y - b.center.y) <= a.halfExtents.y + b.halfExtents.y+ && Math.abs(a.center.z - b.center.z) <= a.halfExtents.z + b.halfExtents.z;+ },++ rayPlane(ray: Ray, plane: Plane): number {+ const denom = dot(ray.d, plane.n);+ if (Math.abs(denom) < 1e-12) return 0;+ const t = (plane.d - dot(ray.o, plane.n)) / denom;+ return t < 0 ? 0 : t;+ },++ rayAABB(ray: Ray, aabb: AABB): number {+ let tmin = -Infinity, tmax = Infinity;+ for (const axis of ['x', 'y', 'z'] as const) {+ const lo = aabb.center[axis] - aabb.halfExtents[axis], hi = aabb.center[axis] + aabb.halfExtents[axis];+ const o = ray.o[axis], d = ray.d[axis];+ if (Math.abs(d) < 1e-12) {+ if (o < lo || o > hi) return 0;+ continue;+ }+ const t1 = (lo - o) / d, t2 = (hi - o) / d;+ tmin = Math.max(tmin, Math.min(t1, t2));+ tmax = Math.min(tmax, Math.max(t1, t2));+ }+ if (tmax < Math.max(tmin, 0)) return 0;+ return tmin > 0 ? tmin : tmax > 0 ? tmax : 0;+ },+};++/** `cc.geometry`. */+export const geometry = { AABB, Ray, Plane, intersect };diff --git a/sdk/src/cocos/index.ts b/sdk/src/cocos/index.tsindex bfd7157cd..ca0a04f68 100644--- a/sdk/src/cocos/index.ts+++ b/sdk/src/cocos/index.ts@@ -28,6 +28,8 @@ export {export { registerBuiltinComponent, type BuiltinAdapter } from './builtins';export { unsupported } from './unsupported';export { ccenum, js, NodePool } from './misc';+export { MeshRenderer, ModelRenderer, UIMeshRenderer } from './meshRenderer';+export { geometry } from './geometry';export { UITransform, Label, LabelOutline, LabelShadow, Sprite, UIOpacity, Button, EventHandler, Widget, Layout, Camera, RichText, Mask, Toggle, ToggleContainer, ProgressBar, Slider, BlockInputEvents, ScrollView, Graphics, Line, EditBox, PageView, PageViewIndicator, ScrollBar, SafeArea, WebView, getVisibleSize, setVisibleSize } from './ui';export { Touch, EventTouch } from './input';export { tween, Tween, tweenUtil, easing, updateTweens } from './tween';diff --git a/sdk/src/cocos/meshRenderer.ts b/sdk/src/cocos/meshRenderer.tsnew file mode 100644index 000000000..d1d5aa773--- /dev/null+++ b/sdk/src/cocos/meshRenderer.ts@@ -0,0 +1,97 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file cocos/meshRenderer.ts+ * @brief `MeshRenderer`, `ModelRenderer` and `UIMeshRenderer`: views on the engine's mesh renderer, and the material calls scripts make through it.+ *+ * What a script does with a mesh renderer is almost always its material: `getSharedMaterial(0)` to set a property for every renderer that+ * shares it, `getMaterialInstance(0)` to set one for this renderer alone. The engine's renderer holds one material handle, so a+ * renderer has one material; an instance is the engine's own (`Material.createInstance`), made once and kept on the renderer.+ */+import { MeshRenderer as EngineMeshRenderer } from '../ecs/component';+import { Material as EngineMaterial } from '../render/material';+import type { World } from '../ecs/world';+import type { Entity } from '../types';+import { Material } from './assets';+import { Component } from './component';+import { registerBuiltinComponent } from './builtins';+import { 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>();+function wrap(handle: number): Material | null {+ if (handle <= 0) return null;+ let m = wrappers.get(handle);+ if (!m) {+ m = new Material();+ m._handle = handle;+ wrappers.set(handle, m);+ }+ return m;+}++/** The base of every renderer that draws a model. */+export class ModelRenderer extends Component {}++export class MeshRenderer extends ModelRenderer {+ private instance_ = 0;++ private data_() { return this._world.get(this._entity, EngineMeshRenderer); }+ private patch_(fields: Record<string, unknown>): void {+ this._world.set(this._entity, EngineMeshRenderer, { ...this.data_(), ...fields } as never);+ }++ /** Whether the renderer draws. */+ get enabled(): boolean { return this.data_().enabled; }+ set enabled(v: boolean) { this.patch_({ enabled: v }); }++ /** The material every renderer sharing it draws with. */+ get sharedMaterial(): Material | null { return wrap(this.instance_ > 0 ? this.sourceOf_() : this.data_().material); }+ get sharedMaterials(): Material[] { const m = this.sharedMaterial; return m ? [m] : []; }+ /** This renderer's own material: the instance when one was made, else the shared one. */+ get material(): Material | null { return wrap(this.data_().material); }+ set material(m: Material | null) { this.setMaterial(m, 0); }+ get materials(): Material[] { const m = this.material; return m ? [m] : []; }+ set materials(list: Material[]) { this.setMaterial(list[0] ?? null, 0); }++ getSharedMaterial(index: number): Material | null { return index === 0 ? this.sharedMaterial : null; }+ getRenderMaterial(index: number): Material | null { return index === 0 ? this.material : null; }++ /** A material of this renderer alone, made from the shared one the first time and kept after. */+ getMaterialInstance(index: number): Material | null {+ if (index !== 0) return null;+ const current = this.data_().material;+ if (current <= 0) return null;+ if (this.instance_ <= 0 || this.instance_ !== current) {+ this.instance_ = EngineMaterial.createInstance(current);+ this.patch_({ material: this.instance_ });+ this.source_ = current;+ }+ return wrap(this.instance_);+ }++ setMaterial(material: Material | null, index: number): void {+ if (index !== 0) return;+ this.instance_ = 0;+ this.source_ = 0;+ this.patch_({ material: material ? material._handle : 0 });+ }+ setSharedMaterial(material: Material | null, index: number): void { this.setMaterial(material, index); }+ setMaterialInstance(material: Material | null, index: number): void { this.setMaterial(material, index); }++ get receiveShadow(): boolean { return true; }+ set receiveShadow(_v: boolean) { /* shadows are the scene's lighting, not a renderer's switch */ }+ get shadowCastingMode(): number { return this.data_().castShadows ? 1 : 0; }+ set shadowCastingMode(v: number) { this.patch_({ castShadows: v !== 0 }); }++ private source_ = 0;+ private sourceOf_(): number { return this.source_; }+}++/** `UIMeshRenderer`: draws a mesh inside the UI. The engine's UI draws meshes as part of the scene, so this is a place for the call to land. */+export class UIMeshRenderer extends ModelRenderer {}++const hasMesh = (world: World, entity: Entity): boolean => world.has(entity, EngineMeshRenderer);+const addMesh = (world: World, entity: Entity): void => { world.insert(entity, EngineMeshRenderer, {}); };+registerBuiltinComponent(MeshRenderer, ['cc.MeshRenderer', 'MeshRenderer'], viewAdapter(MeshRenderer, hasMesh, addMesh));+registerBuiltinComponent(UIMeshRenderer, ['cc.UIMeshRenderer', 'UIMeshRenderer'], viewAdapter(UIMeshRenderer, () => true, () => { /* nothing stands behind it */ }));diff --git a/sdk/tests/cocos/geometry.test.ts b/sdk/tests/cocos/geometry.test.tsnew file mode 100644index 000000000..39574bf0b--- /dev/null+++ b/sdk/tests/cocos/geometry.test.ts@@ -0,0 +1,57 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, expect, it } from 'vitest';+import { Vec3 } from '../../src/cocos/math';+import { geometry } from '../../src/cocos/geometry';++const { AABB, Ray, Plane, intersect } = geometry;++describe('geometry.AABB', () => {+ it('is a centre and half sizes, from corners or by itself', () => {+ const box = AABB.fromPoints(new Vec3(0, 0, 0), new Vec3(4, 2, 6));+ expect([box.center.x, box.center.y, box.center.z]).toEqual([2, 1, 3]);+ expect([box.halfExtents.x, box.halfExtents.y, box.halfExtents.z]).toEqual([2, 1, 3]);+ expect(new AABB().halfExtents.x).toBe(1);+ const min = new Vec3(), max = new Vec3();+ box.getBoundary(min, max);+ expect([min.x, max.z]).toEqual([0, 6]);+ });++ it('merges into the box that holds both', () => {+ const merged = AABB.merge(new AABB(), new AABB(0, 0, 0, 1, 1, 1), new AABB(10, 0, 0, 1, 1, 1));+ expect(merged.center.x).toBe(5);+ expect(merged.halfExtents.x).toBe(6);+ });+});++describe('geometry.intersect', () => {+ it('tells boxes that overlap (touching counts) from ones that do not', () => {+ expect(intersect.aabbWithAABB(new AABB(0, 0, 0, 1, 1, 1), new AABB(1.5, 0, 0, 1, 1, 1))).toBe(true);+ expect(intersect.aabbWithAABB(new AABB(0, 0, 0, 1, 1, 1), new AABB(2, 0, 0, 1, 1, 1))).toBe(true);+ expect(intersect.aabbWithAABB(new AABB(0, 0, 0, 1, 1, 1), new AABB(2.1, 0, 0, 1, 1, 1))).toBe(false);+ });++ it('answers how far along a ray a plane is, and 0 for a miss', () => {+ const plane = Plane.fromNormalAndPoint(new Plane(), new Vec3(0, 1, 0), new Vec3(0, 5, 0));+ expect(intersect.rayPlane(new Ray(0, 0, 0, 0, 1, 0), plane)).toBe(5);+ expect(intersect.rayPlane(new Ray(0, 0, 0, 1, 0, 0), plane)).toBe(0); // parallel+ expect(intersect.rayPlane(new Ray(0, 0, 0, 0, -1, 0), plane)).toBe(0); // behind+ });++ it('answers how far along a ray a box is, from outside and from inside', () => {+ const box = new AABB(0, 0, -10, 1, 1, 1);+ expect(intersect.rayAABB(new Ray(0, 0, 0, 0, 0, -1), box)).toBe(9);+ expect(intersect.rayAABB(new Ray(5, 0, 0, 0, 0, -1), box)).toBe(0);+ expect(intersect.rayAABB(new Ray(0, 0, -10, 0, 0, -1), box)).toBe(1); // starts inside: where it leaves+ });+});++describe('geometry.Ray', () => {+ it('points from one place to another, and finds a point along itself', () => {+ const ray = Ray.fromPoints(new Ray(), new Vec3(0, 0, 0), new Vec3(0, 0, -4));+ expect([ray.d.x, ray.d.y, ray.d.z]).toEqual([0, 0, -1]);+ const hit = new Vec3();+ ray.computeHit(hit, 3);+ expect(hit.z).toBe(-3);+ });+});diff --git a/sdk/tests/cocos/mesh-renderer-wasm.test.ts b/sdk/tests/cocos/mesh-renderer-wasm.test.tsnew file mode 100644index 000000000..305bb6aa9--- /dev/null+++ b/sdk/tests/cocos/mesh-renderer-wasm.test.ts@@ -0,0 +1,93 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A script's MeshRenderer: its material is the engine's, a shared one is shared and an instance is this renderer's own, and a+ * property set on either reaches the engine material.+ */+import { afterEach, beforeAll, describe, expect, it } from 'vitest';+import type { ESEngineModule } from '../../src/wasm';+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 { HAS_WASM, loadWasmModule } from '../helpers/loadWasm';++describe.skipIf(!HAS_WASM)('MeshRenderer on the engine', () => {+ let module: ESEngineModule;+ let registry: InstanceType<ESEngineModule['Registry']> | undefined;+ let world: World | undefined;+ const W = (): World => world!;++ beforeAll(async () => { ensureBuiltinComponentsRegistered(); module = await loadWasmModule(); });++ function connect(): void {+ setDefaultContext(new AppContext());+ ensureBuiltinComponentsRegistered();+ registry = new module.Registry();+ world = new World();+ world.connectCpp(registry, module);+ setCurrentWorld(world);+ }+ afterEach(() => {+ setCurrentWorld(null);+ world?.disconnectCpp();+ registry?.delete();+ registry = undefined;+ world = undefined;+ });++ const node = (name: string): Node => { const n = new Node(name); W().insert(n._entity, Transform); return n; };++ it('is the engine renderer on its node, the same view each time', () => {+ connect();+ const n = node('m');+ expect(n.getComponent(MeshRenderer)).toBeNull();+ const r = n.addComponent(MeshRenderer)!;+ expect(W().has(n._entity, EngineMeshRenderer)).toBe(true);+ expect(n.getComponent(MeshRenderer)).toBe(r);+ expect(n.getComponent('cc.MeshRenderer')).toBe(r);+ r.enabled = false;+ expect(W().get(n._entity, EngineMeshRenderer).enabled).toBe(false);+ });++ it('shares a material until an instance is asked for, then has one of its own', () => {+ connect();+ const shared = EngineMaterial.create({ shader: 7, uniforms: { u_tint: { x: 1, y: 1, z: 1, w: 1 } } });+ const a = node('a'), b = node('b');+ for (const n of [a, b]) { n.addComponent(MeshRenderer); W().set(n._entity, EngineMeshRenderer, { ...W().get(n._entity, EngineMeshRenderer), material: shared }); }+ const ra = a.getComponent(MeshRenderer)!, rb = b.getComponent(MeshRenderer)!;++ expect(ra.getSharedMaterial(0)).toBe(rb.getSharedMaterial(0));+ expect(ra.material!._handle).toBe(shared);++ const mine = ra.getMaterialInstance(0)!;+ expect(mine._handle).not.toBe(shared);+ expect(W().get(a._entity, EngineMeshRenderer).material).toBe(mine._handle);+ expect(ra.getMaterialInstance(0)).toBe(mine); // made once+ expect(ra.getSharedMaterial(0)!._handle).toBe(shared); // the shared one is still the shared one+ expect(rb.material!._handle).toBe(shared); // and the other renderer is untouched+ });++ it('writes a property through to the engine material, colours in 0..1, and remembers it', () => {+ connect();+ const shared = EngineMaterial.create({ shader: 7, uniforms: { u_tint: { x: 1, y: 1, z: 1, w: 1 }, u_level: 0 } });+ const n = node('p');+ const r = n.addComponent(MeshRenderer)!;+ W().set(n._entity, EngineMeshRenderer, { ...W().get(n._entity, EngineMeshRenderer), material: shared });+ const m = r.getMaterialInstance(0)!;+ m.setProperty('u_level', 0.5);+ m.setProperty('u_tint', new Color(255, 0, 128, 255));+ expect(EngineMaterial.getUniform(m._handle, 'u_level')).toBe(0.5);+ const tint = EngineMaterial.getUniform(m._handle, 'u_tint') as { x: number; y: number; z: number; w: number };+ expect([tint.x, tint.y, tint.z, tint.w].map((v) => Math.round(v * 100) / 100)).toEqual([1, 0, 0.5, 1]);+ expect(m.getProperty('u_level')).toBe(0.5);+ expect(EngineMaterial.getUniform(shared, 'u_level')).toBe(0); // the shared one did not change+ });++ it('lets UIMeshRenderer be added without anything behind it', () => {+ connect();+ const n = node('ui');+ expect(n.addComponent(UIMeshRenderer)).toBeInstanceOf(UIMeshRenderer);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 1b0195b18..343fc8d6c 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): 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':{"note":"More of cc."},'feat(bundle): the project script bundle is built again only when a file it read changed, and Cocos scripts that write the same class name each get a component of their own':