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(assets): builtin:map-plane is the plane with its texture's v running along +z, and a Cocos plane imports as it, so a map laid over one lands where it was drawn
yhhb49fd4c4ba09
diff --git a/docs/astro/src/content/docs/assets/models.mdx b/docs/astro/src/content/docs/assets/models.mdxindex 48b8bf407..b71dc1742 100644--- a/docs/astro/src/content/docs/assets/models.mdx+++ b/docs/astro/src/content/docs/assets/models.mdx@@ -19,8 +19,8 @@ That import is the subject of this page. For the component that draws the result## Built-in primitivesSeven shapes need no import and no file. **+ Add → Create entity… → Cube** (or Sphere, Capsule, Cylinder,-Cone, Plane, Quad) makes an entity whose `MeshRenderer.mesh` holds a `builtin:` ref, and the-same seven appear at the top of the picker on any `MeshRenderer.mesh` field.+Cone, Plane, Map Plane, Quad) makes an entity whose `MeshRenderer.mesh` holds a `builtin:` ref, and the+same eight appear at the top of the picker on any `MeshRenderer.mesh` field.| Ref | Shape ||---|---|@@ -30,10 +30,11 @@ same seven appear at the top of the picker on any `MeshRenderer.mesh` field.| `builtin:cylinder` | 100-unit upright cylinder, flat caps || `builtin:cone` | 100-unit cone standing on its base || `builtin:plane` | 100-unit square in XZ, facing up — a ground |+| `builtin:map-plane` | the same square with the v of its texture running along +z, as a map seen from above has it (the plane of Cocos Creator) || `builtin:quad` | 100-unit square in XY, facing the camera — a backdrop |They are built in code at load time, so they cost no bytes in a package and a scene that-references one carries no dependency. All seven are the same size as a new `ShapeRenderer`,+references one carries no dependency. All eight are the same size as a new `ShapeRenderer`,so one dropped into a scene is visible without being scaled first; give it the size youwant through the Transform, the way an imported model gets one.diff --git a/docs/astro/src/content/docs/zh-cn/assets/models.mdx b/docs/astro/src/content/docs/zh-cn/assets/models.mdxindex 4d011a70e..0feeb991f 100644--- a/docs/astro/src/content/docs/zh-cn/assets/models.mdx+++ b/docs/astro/src/content/docs/zh-cn/assets/models.mdx@@ -15,8 +15,8 @@ import { Aside } from '@astrojs/starlight/components';## 内置图元-有七个形状不需要导入、也不需要文件。**+ 添加 → 创建实体… → Cube**(以及 Sphere、Capsule、Cylinder、-Cone、Plane、Quad)建出的实体,其 `MeshRenderer.mesh` 里放的是一个 `builtin:` 引用;同样这七个+有八个形状不需要导入、也不需要文件。**+ 添加 → 创建实体… → Cube**(以及 Sphere、Capsule、Cylinder、+Cone、Plane、Map Plane、Quad)建出的实体,其 `MeshRenderer.mesh` 里放的是一个 `builtin:` 引用;同样这八个也排在任何 `MeshRenderer.mesh` 字段选择器的最前面。| 引用 | 形状 |@@ -27,9 +27,10 @@ Cone、Plane、Quad)建出的实体,其 `MeshRenderer.mesh` 里放的是一| `builtin:cylinder` | 100 单位的竖直圆柱,平顶平底 || `builtin:cone` | 100 单位的圆锥,立在底面上 || `builtin:plane` | XZ 平面上 100 单位的方块,朝上 —— 一块地面 |+| `builtin:map-plane` | 同样的方块,贴图的 v 沿 +z 方向,和从上往下看的地图一样(Cocos Creator 的平面) || `builtin:quad` | XY 平面上 100 单位的方块,正对相机 —— 一块背板 |-它们在加载时由代码生成,所以在包里不占任何字节,引用了它的场景也不因此多出依赖。七个都+它们在加载时由代码生成,所以在包里不占任何字节,引用了它的场景也不因此多出依赖。八个都和新建的 `ShapeRenderer` 一样大,所以往场景里放一个不用先缩放就看得见;要多大,通过Transform 给,和导入的模型一样。diff --git a/pipeline/src/import/cocos/components.ts b/pipeline/src/import/cocos/components.tsindex 4c4f3c56d..6facf610d 100644--- a/pipeline/src/import/cocos/components.ts+++ b/pipeline/src/import/cocos/components.ts@@ -615,7 +615,7 @@ const PRIMITIVE_MESHES: Record<string, { ref: string; size: number }> = {a804a: { ref: 'builtin:cube', size: 1 },'17020': { ref: 'builtin:sphere', size: 1 },'38fd2': { ref: 'builtin:cylinder', size: 1 },- '2e76e': { ref: 'builtin:plane', size: 10 },+ '2e76e': { ref: 'builtin:map-plane', size: 10 },fc873: { ref: 'builtin:quad', size: 1 },};const ENGINE_PRIMITIVE_SIZE = 100;diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.tsindex cad5f2aac..271067b8e 100644--- a/pipeline/src/import/cocos/importProject.ts+++ b/pipeline/src/import/cocos/importProject.ts@@ -31,7 +31,7 @@ import { convertPass, isOutlinePass, type PassConversion } from './effectConvert* importer that made less (or differently), and is brought up to date when it is opened. Raise it when the converted output changes in a* way an existing project needs to pick up: a new generated file, a field a document or a manifest now carries.*/-export const COCOS_IMPORT_REVISION = 15;+export const COCOS_IMPORT_REVISION = 16;/** Whether the project at @p projectRoot was written by an importer of this revision. A project that was not converted has nothing to bring up. */export function cocosImportIsCurrent(projectRoot: string): boolean {diff --git a/sdk/src/asset/builtinMeshes.ts b/sdk/src/asset/builtinMeshes.tsindex 57c14399e..3a41ba380 100644--- a/sdk/src/asset/builtinMeshes.ts+++ b/sdk/src/asset/builtinMeshes.ts@@ -66,12 +66,13 @@ function tri(b: Builder, a: number, c: number, d: number): void {b.idx.push(a, c, d);}-/** A flat quad from four corners wound counter-clockwise seen from `n`. */-function quadFace(b: Builder, corners: readonly (readonly number[])[], n: readonly number[]): void {- const a = vertex(b, corners[0]!, 0, 0, n);- const c = vertex(b, corners[1]!, 1, 0, n);- const d = vertex(b, corners[2]!, 1, 1, n);- const e = vertex(b, corners[3]!, 0, 1, n);+/** A flat quad from four corners wound counter-clockwise seen from `n`. `flipV` runs the texture's v the other way along the face. */+function quadFace(b: Builder, corners: readonly (readonly number[])[], n: readonly number[], flipV = false): void {+ const [top, bottom] = flipV ? [1, 0] : [0, 1];+ const a = vertex(b, corners[0]!, 0, top, n);+ const c = vertex(b, corners[1]!, 1, top, n);+ const d = vertex(b, corners[2]!, 1, bottom, n);+ const e = vertex(b, corners[3]!, 0, bottom, n);tri(b, a, c, d);tri(b, a, d, e);}@@ -200,6 +201,14 @@ function plane(size: number): Builder {return b;}+/** The plane with v running along +z, so a picture laid over it reads as a map seen from above with +z towards the viewer: the plane of Cocos. */+function mapPlane(size: number): Builder {+ const b = newBuilder();+ const h = size / 2;+ quadFace(b, [[-h, 0, h], [h, 0, h], [h, 0, -h], [-h, 0, -h]], [0, 1, 0], true);+ return b;+}+function quad(size: number): Builder {const b = newBuilder();const h = size / 2;@@ -267,6 +276,8 @@ export const BUILTIN_MESH_TEMPLATES: readonly BuiltinMeshTemplate[] = [() => cone(UNIT / 2, UNIT)),template('plane', 'Plane', `A ${UNIT}-unit ground square in XZ, facing up.`, false,() => plane(UNIT)),+ template('map-plane', 'Map Plane', `A ${UNIT}-unit ground square in XZ, facing up, whose texture's v runs along +z as a map's does.`, false,+ () => mapPlane(UNIT)),template('quad', 'Quad', `A ${UNIT}-unit square in XY, facing the camera.`, false,() => quad(UNIT)),];diff --git a/sdk/tests/builtin-meshes.test.ts b/sdk/tests/builtin-meshes.test.tsindex 7eceb18fb..dc59cfe8c 100644--- a/sdk/tests/builtin-meshes.test.ts+++ b/sdk/tests/builtin-meshes.test.ts@@ -38,7 +38,7 @@ const cross = (a: Vec, b: Vec): Vec => ({describe('built-in meshes', () => {it('offers every primitive under a builtin: ref', () => {expect(BUILTIN_MESH_TEMPLATES.map((t) => t.id).sort())- .toEqual(['capsule', 'cone', 'cube', 'cylinder', 'plane', 'quad', 'sphere']);+ .toEqual(['capsule', 'cone', 'cube', 'cylinder', 'map-plane', 'plane', 'quad', 'sphere']);for (const t of BUILTIN_MESH_TEMPLATES) {expect(t.ref).toBe(`builtin:${t.id}`);expect(isBuiltinMeshRef(t.ref)).toBe(true);@@ -46,6 +46,25 @@ describe('built-in meshes', () => {}});+ // A texture over the plane of Cocos reads as a map seen from above, v running towards +z; the engine's own plane runs it the other way.+ it('runs the v of the map plane along +z and the v of the plane the other way', () => {+ const vOf = (id: string, z: number): number => {+ const mesh = builtinMeshTemplate(`builtin:${id}`)!.build();+ const view = new DataView(mesh.vertices.buffer, mesh.vertices.byteOffset, mesh.vertices.byteLength);+ const pos = mesh.channels.find((c) => c.semantic === MeshChannel.Position)!;+ const uv = mesh.channels.find((c) => c.semantic === MeshChannel.TexCoord0)!;+ for (let i = 0; i < mesh.vertexCount; i++) {+ const at = i * mesh.vertexStride;+ if (Math.abs(view.getFloat32(at + pos.offset + 8, true) - z) < 1e-3) return view.getFloat32(at + uv.offset + 4, true);+ }+ throw new Error('no vertex there');+ };+ expect(vOf('map-plane', -50)).toBeCloseTo(0, 5);+ expect(vOf('map-plane', 50)).toBeCloseTo(1, 5);+ expect(vOf('plane', -50)).toBeCloseTo(1, 5);+ expect(vOf('plane', 50)).toBeCloseTo(0, 5);+ });+// A misspelled built-in must stay a path, so the loader 404s it by name rather// than the ref quietly resolving to nothing at all.it('does not claim a ref it cannot build', () => {