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.

feat(cocos): renderer.scene.CameraProjection has Cocos's numbers, so a script that tells an orthographic camera from a perspective one by it gets the right answer

yhhc8239e0c5056
diff --git a/sdk/src/cocos/index.ts b/sdk/src/cocos/index.ts
index b4331dc67..ef339c9a1 100644
--- a/sdk/src/cocos/index.ts
+++ b/sdk/src/cocos/index.ts
@@ -28,6 +28,7 @@ export {
} from './assets';
export { registerBuiltinComponent, type BuiltinAdapter } from './builtins';
export { unsupported } from './unsupported';
+export { renderer } from './renderer';
export { ccenum, js, NodePool, profiler } from './misc';
export { MeshRenderer, ModelRenderer, UIMeshRenderer } from './meshRenderer';
export { Collider, BoxCollider } from './collider';
diff --git a/sdk/src/cocos/renderer.ts b/sdk/src/cocos/renderer.ts
new file mode 100644
index 000000000..46b913b4b
--- /dev/null
+++ b/sdk/src/cocos/renderer.ts
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file cocos/renderer.ts
+ * @brief `cc.renderer`: the render-scene namespace, of which scripts read the constants.
+ *
+ * A script asks `camera.projection == renderer.scene.CameraProjection.ORTHO` to tell the two kinds of camera apart, so the numbers must be
+ * Cocos's: a stand-in there compares unequal to everything and sends an orthographic camera down the perspective path. The scene objects
+ * of Cocos's own renderer (`renderer.scene.Camera`, `Model`) are not here, and say so by name when used.
+ */
+import { unsupported } from './unsupported';
+
+/** An object that answers what it has and a named stand-in for the rest. */
+function withStandIns<T extends object>(path: string, known: T): T {
+ return new Proxy(known, {
+ get(target, key, receiver) {
+ if (typeof key === 'symbol' || key in target) return Reflect.get(target, key, receiver);
+ return unsupported(`${path}.${key}`);
+ },
+ });
+}
+
+const CameraProjection = Object.freeze({ ORTHO: 0, PERSPECTIVE: 1 });
+
+export const renderer = withStandIns('renderer', {
+ scene: withStandIns('renderer.scene', { CameraProjection }),
+});
diff --git a/sdk/tests/cocos/renderer-namespace.test.ts b/sdk/tests/cocos/renderer-namespace.test.ts
new file mode 100644
index 000000000..b6f204498
--- /dev/null
+++ b/sdk/tests/cocos/renderer-namespace.test.ts
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file `renderer.scene.CameraProjection` has Cocos's numbers, and the same as `Camera.ProjectionType`, so a script that compares one with the other
+ * tells an orthographic camera from a perspective one.
+ */
+import { describe, expect, it } from 'vitest';
+import { Camera, renderer } from '../../src/cocos';
+
+describe('renderer.scene.CameraProjection', () => {
+ it('is the numbering a Camera answers its projection in', () => {
+ expect(renderer.scene.CameraProjection.ORTHO).toBe(Camera.ProjectionType.ORTHO);
+ expect(renderer.scene.CameraProjection.PERSPECTIVE).toBe(Camera.ProjectionType.PERSPECTIVE);
+ expect(renderer.scene.CameraProjection.ORTHO).not.toBe(renderer.scene.CameraProjection.PERSPECTIVE);
+ });
+
+ it('still answers a name it does not have, with a stand-in that does not throw', () => {
+ expect(() => new renderer.scene.Camera()).not.toThrow();
+ });
+});