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: add showFrustum property to Camera for always-visible frustum
esengine9f58f39427f4
diff --git a/editor/src/launcher/ProjectService.ts b/editor/src/launcher/ProjectService.tsindex c5304bcf4..2c1c18737 100644--- a/editor/src/launcher/ProjectService.ts+++ b/editor/src/launcher/ProjectService.ts@@ -128,6 +128,7 @@ export async function createProject(created: now,modified: now,spineVersion: '4.2',+ designResolution: { width: 1920, height: 1080 },};const projectFilePath = joinPath(projectDir, 'project.esproject');@@ -137,7 +138,7 @@ export async function createProject(}// Create default scene based on template- const defaultScene = createDefaultScene(name, template);+ const defaultScene = createDefaultScene(name, template, config.designResolution!);const sceneFilePath = joinPath(projectDir, 'assets/scenes/main.esscene');if (!(await fs.writeFile(sceneFilePath, JSON.stringify(defaultScene, null, 2)))) {return { success: false, error: 'Failed to write default scene' };@@ -302,7 +303,14 @@ function createProjectFiles(// Default Scene Creation// =============================================================================-function createDefaultScene(projectName: string, _template: ProjectTemplate) {+function createDefaultScene(+ projectName: string,+ _template: ProjectTemplate,+ designResolution: { width: number; height: number },+) {+ const pixelsPerUnit = 100;+ const orthoSize = designResolution.height / 2;+return {version: '1.0',name: 'Main',@@ -327,9 +335,36 @@ function createDefaultScene(projectName: string, _template: ProjectTemplate) {isActive: true,projectionType: 1,fov: 60,- orthoSize: 400,+ orthoSize,nearPlane: 0.1,farPlane: 1000,+ showFrustum: true,+ },+ },+ ],+ },+ {+ id: 2,+ name: 'Canvas',+ parent: null,+ children: [],+ components: [+ {+ type: 'LocalTransform',+ data: {+ position: { x: 0, y: 0, z: 0 },+ rotation: { x: 0, y: 0, z: 0, w: 1 },+ scale: { x: 1, y: 1, z: 1 },+ },+ },+ {+ type: 'Canvas',+ data: {+ designResolution: { x: designResolution.width, y: designResolution.height },+ pixelsPerUnit,+ scaleMode: 1,+ matchWidthOrHeight: 0.5,+ backgroundColor: { x: 0, y: 0, z: 0, w: 1 },},},],diff --git a/editor/src/panels/SceneViewPanel.ts b/editor/src/panels/SceneViewPanel.tsindex 13cc4fdcf..11a8b4838 100644--- a/editor/src/panels/SceneViewPanel.ts+++ b/editor/src/panels/SceneViewPanel.ts@@ -789,6 +789,8 @@ export class SceneViewPanel {this.drawGrid(ctx, w, h);+ this.drawCameraFrustums(ctx);+const selectedEntity = this.store_.selectedEntity;if (selectedEntity !== null && this.store_.isEntityVisible(selectedEntity as number)) {@@ -796,8 +798,6 @@ export class SceneViewPanel {this.drawSelectionBox(ctx);}- this.drawCameraFrustum(ctx);-if (this.gizmoManager_.getActiveId() !== 'select' && getSettingsValue<boolean>('scene.showGizmos')) {this.gizmoManager_.setContext(this.createGizmoContext(ctx));this.gizmoManager_.draw();@@ -847,21 +847,8 @@ export class SceneViewPanel {ctx.restore();}- private drawCameraFrustum(ctx: CanvasRenderingContext2D): void {- const entityData = this.store_.getSelectedEntityData();- if (!entityData) return;-- const camera = entityData.components.find(c => c.type === 'Camera');- if (!camera) return;-- const transform = entityData.components.find(c => c.type === 'LocalTransform');- if (!transform) return;-- const worldTransform = this.store_.getWorldTransform(entityData.id);- const pos = worldTransform.position;-- const orthoSize = (camera.data.orthoSize as number) ?? 400;- const projectionType = (camera.data.projectionType as number) ?? 0;+ private drawCameraFrustums(ctx: CanvasRenderingContext2D): void {+ const selectedEntity = this.store_.selectedEntity;let aspectRatio = 750 / 1650;const canvasEntity = this.store_.scene.entities.find(@@ -875,34 +862,61 @@ export class SceneViewPanel {}}- ctx.save();- ctx.translate(pos.x, -pos.y);+ for (const entity of this.store_.scene.entities) {+ const camera = entity.components.find(c => c.type === 'Camera');+ if (!camera) continue;- if (projectionType === 1) {- const halfHeight = orthoSize;- const halfWidth = halfHeight * aspectRatio;+ const transform = entity.components.find(c => c.type === 'LocalTransform');+ if (!transform) continue;- ctx.strokeStyle = '#ffaa00';- ctx.lineWidth = 2 / this.zoom_;- ctx.setLineDash([8 / this.zoom_, 4 / this.zoom_]);+ const isSelected = entity.id === selectedEntity;+ const showFrustum = (camera.data.showFrustum as boolean) ?? true;++ if (!showFrustum && !isSelected) continue;++ const worldTransform = this.store_.getWorldTransform(entity.id);+ const pos = worldTransform.position;- ctx.strokeRect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);+ const orthoSize = (camera.data.orthoSize as number) ?? 400;+ const projectionType = (camera.data.projectionType as number) ?? 0;- ctx.fillStyle = 'rgba(255, 170, 0, 0.1)';- ctx.fillRect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);+ ctx.save();+ ctx.translate(pos.x, -pos.y);- ctx.setLineDash([]);- ctx.strokeStyle = '#ffaa00';- ctx.lineWidth = 1 / this.zoom_;- ctx.beginPath();- ctx.moveTo(-halfWidth * 0.1, 0);- ctx.lineTo(halfWidth * 0.1, 0);- ctx.moveTo(0, -halfHeight * 0.1);- ctx.lineTo(0, halfHeight * 0.1);- ctx.stroke();- }+ if (projectionType === 1) {+ const halfHeight = orthoSize;+ const halfWidth = halfHeight * aspectRatio;- ctx.restore();+ ctx.strokeStyle = '#ffaa00';+ ctx.lineWidth = 2 / this.zoom_;++ if (isSelected) {+ ctx.globalAlpha = 1;+ ctx.setLineDash([]);+ } else {+ ctx.globalAlpha = 0.5;+ ctx.setLineDash([8 / this.zoom_, 4 / this.zoom_]);+ }++ ctx.strokeRect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);++ ctx.fillStyle = isSelected ? 'rgba(255, 170, 0, 0.1)' : 'rgba(255, 170, 0, 0.05)';+ ctx.fillRect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);++ ctx.setLineDash([]);+ ctx.strokeStyle = '#ffaa00';+ ctx.lineWidth = 1 / this.zoom_;+ ctx.beginPath();+ ctx.moveTo(-halfWidth * 0.1, 0);+ ctx.lineTo(halfWidth * 0.1, 0);+ ctx.moveTo(0, -halfHeight * 0.1);+ ctx.lineTo(0, halfHeight * 0.1);+ ctx.stroke();+ }++ ctx.globalAlpha = 1;+ ctx.restore();+ }}private renderPreview(): void {@@ -929,9 +943,9 @@ export class SceneViewPanel {this.drawEntity(ctx, entity, entity.id === selectedEntity);}- if (selectedEntity !== null && this.store_.isEntityVisible(selectedEntity as number)) {- this.drawCameraFrustum(ctx);+ this.drawCameraFrustums(ctx);+ if (selectedEntity !== null && this.store_.isEntityVisible(selectedEntity as number)) {if (this.gizmoManager_.getActiveId() !== 'select' && getSettingsValue<boolean>('scene.showGizmos')) {this.gizmoManager_.setContext(this.createGizmoContext(ctx));this.gizmoManager_.draw();diff --git a/editor/src/schemas/ComponentSchemas.ts b/editor/src/schemas/ComponentSchemas.tsindex d0020e381..69fd99db3 100644--- a/editor/src/schemas/ComponentSchemas.ts+++ b/editor/src/schemas/ComponentSchemas.ts@@ -96,6 +96,7 @@ export const CameraSchema: ComponentSchema = {{ name: 'orthoSize', type: 'number', min: 0.1 },{ name: 'nearPlane', type: 'number', step: 0.1 },{ name: 'farPlane', type: 'number', step: 1 },+ { name: 'showFrustum', type: 'boolean' },],};@@ -149,6 +150,28 @@ export const UIRectSchema: ComponentSchema = {],};+export const CanvasSchema: ComponentSchema = {+ name: 'Canvas',+ category: 'builtin',+ properties: [+ { name: 'designResolution', type: 'vec2' },+ { name: 'pixelsPerUnit', type: 'number', min: 1, step: 1 },+ {+ name: 'scaleMode',+ type: 'enum',+ options: [+ { label: 'FixedWidth', value: 0 },+ { label: 'FixedHeight', value: 1 },+ { label: 'Expand', value: 2 },+ { label: 'Shrink', value: 3 },+ { label: 'Match', value: 4 },+ ],+ },+ { name: 'matchWidthOrHeight', type: 'number', min: 0, max: 1, step: 0.1 },+ { name: 'backgroundColor', type: 'color' },+ ],+};+export const SpineAnimationSchema: ComponentSchema = {name: 'SpineAnimation',category: 'builtin',@@ -276,6 +299,7 @@ export function registerBuiltinSchemas(options?: BuiltinSchemaOptions): void {registerComponentSchema(CameraSchema);registerComponentSchema(TextSchema);registerComponentSchema(UIRectSchema);+ registerComponentSchema(CanvasSchema);if (enableSpine) {registerComponentSchema(SpineAnimationSchema);}diff --git a/editor/src/types/SceneTypes.ts b/editor/src/types/SceneTypes.tsindex f7a92bc2e..c58906d33 100644--- a/editor/src/types/SceneTypes.ts+++ b/editor/src/types/SceneTypes.ts@@ -52,7 +52,14 @@ export interface ViewportState {// Factory Functions// =============================================================================-export function createEmptyScene(name: string = 'Untitled'): SceneData {+export function createEmptyScene(+ name: string = 'Untitled',+ designResolution?: { width: number; height: number },+): SceneData {+ const res = designResolution ?? { width: 1920, height: 1080 };+ const pixelsPerUnit = 100;+ const orthoSize = res.height / 2;+return {version: '2.0',name,@@ -76,10 +83,38 @@ export function createEmptyScene(name: string = 'Untitled'): SceneData {data: {isActive: true,projectionType: 1,- orthoSize: 400,+ orthoSize,fov: 60,nearPlane: 0.1,farPlane: 1000,+ showFrustum: true,+ },+ },+ ],+ visible: true,+ },+ {+ id: 2,+ name: 'Canvas',+ parent: null,+ children: [],+ components: [+ {+ type: 'LocalTransform',+ data: {+ position: { x: 0, y: 0, z: 0 },+ rotation: { x: 0, y: 0, z: 0, w: 1 },+ scale: { x: 1, y: 1, z: 1 },+ },+ },+ {+ type: 'Canvas',+ data: {+ designResolution: { x: res.width, y: res.height },+ pixelsPerUnit,+ scaleMode: 1,+ matchWidthOrHeight: 0.5,+ backgroundColor: { x: 0, y: 0, z: 0, w: 1 },},},],diff --git a/sdk/src/component.ts b/sdk/src/component.tsindex 8d9b5bef1..861e5aa06 100644--- a/sdk/src/component.ts+++ b/sdk/src/component.ts@@ -152,6 +152,7 @@ export interface CameraData {aspectRatio: number;isActive: boolean;priority: number;+ showFrustum: boolean;}export interface CanvasData {@@ -227,13 +228,14 @@ export const Camera = defineBuiltin<CameraData>('Camera', {farPlane: 1000,aspectRatio: 1.77,isActive: true,- priority: 0+ priority: 0,+ showFrustum: true});export const Canvas = defineBuiltin<CanvasData>('Canvas', {designResolution: { x: 1920, y: 1080 },pixelsPerUnit: 100,- scaleMode: 0,+ scaleMode: 1,matchWidthOrHeight: 0.5,backgroundColor: { x: 0, y: 0, z: 0, w: 1 }});diff --git a/sdk/src/preview/PreviewPlugin.ts b/sdk/src/preview/PreviewPlugin.tsindex 3c6526f5e..3362356e9 100644--- a/sdk/src/preview/PreviewPlugin.ts+++ b/sdk/src/preview/PreviewPlugin.ts@@ -6,7 +6,7 @@import type { App, Plugin } from '../app';import { Assets, assetPlugin } from '../asset';import type { SceneData } from '../scene';-import { LocalTransform, Camera, type LocalTransformData, type CameraData } from '../component';+import { LocalTransform, Camera, Canvas, type LocalTransformData, type CameraData, type CanvasData } from '../component';import { platformFetch } from '../platform';// =============================================================================@@ -77,6 +77,21 @@ export class PreviewPlugin implements Plugin {if (!hasActiveCamera) {console.warn('[PreviewPlugin] No active camera found, creating default camera');++ let orthoSize = 540;+ let aspectRatio = 1920 / 1080;++ const canvasEntities = world.getEntitiesWithComponents([Canvas]);+ for (const entity of canvasEntities) {+ const canvas = world.get(entity, Canvas) as CanvasData;+ if (canvas.designResolution) {+ const ppu = canvas.pixelsPerUnit || 100;+ orthoSize = (canvas.designResolution.y / 2) / ppu;+ aspectRatio = canvas.designResolution.x / canvas.designResolution.y;+ break;+ }+ }+const cameraEntity = world.spawn();const transformData: LocalTransformData = {@@ -90,11 +105,12 @@ export class PreviewPlugin implements Plugin {isActive: true,projectionType: 0,fov: 60,- orthoSize: 540,+ orthoSize,nearPlane: 0.1,farPlane: 1000,- aspectRatio: 1.77,+ aspectRatio,priority: 0,+ showFrustum: true,};world.insert(cameraEntity, Camera, cameraData);}