Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix camera crash error in Script #1873

Merged
merged 6 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export class Camera extends Component {
_replacementShader: Shader = null;
/** @internal */
_replacementSubShaderTag: ShaderTagKey = null;
/** @internal */
@ignoreClone
_cameraIndex: number = -1;

private _priority: number = 0;
private _shaderData: ShaderData = new ShaderData(ShaderDataGroup.Camera);
Expand Down Expand Up @@ -202,8 +205,8 @@ export class Camera extends Component {

set priority(value: number) {
if (this._priority !== value) {
if (this._entity._isActiveInScene && this.enabled) {
this.scene._cameraNeedSorting = true;
if (this._phasedActiveInScene) {
this.scene._componentsManager._cameraNeedSorting = true;
}
this._priority = value;
}
Expand Down Expand Up @@ -571,14 +574,14 @@ export class Camera extends Component {
* @inheritdoc
*/
override _onEnableInScene(): void {
this.scene._attachRenderCamera(this);
this.scene._componentsManager.addCamera(this);
}

/**
* @inheritdoc
*/
override _onDisableInScene(): void {
this.scene._detachRenderCamera(this);
this.scene._componentsManager.removeCamera(this);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { Animator } from "./animation";
* The manager of the components.
*/
export class ComponentsManager {
/* @internal */
_cameraNeedSorting: boolean = false;
/** @internal */
_activeCameras: DisorderedArray<Camera> = new DisorderedArray();
/** @internal */
_renderers: DisorderedArray<Renderer> = new DisorderedArray();

Expand All @@ -30,6 +34,25 @@ export class ComponentsManager {
// Delay dispose active/inActive Pool
private _componentsContainerPool: Component[][] = [];

addCamera(camera: Camera) {
camera._cameraIndex = this._activeCameras.length;
this._activeCameras.add(camera);
this._cameraNeedSorting = true;
}

removeCamera(camera: Camera) {
const replaced = this._activeCameras.deleteByIndex(camera._cameraIndex);
replaced && (replaced._cameraIndex = camera._cameraIndex);
camera._cameraIndex = -1;
}

sortCameras(): void {
if (this._cameraNeedSorting) {
this._activeCameras.sort((a, b) => a.priority - b.priority);
this._cameraNeedSorting = false;
}
}

addRenderer(renderer: Renderer) {
renderer._rendererIndex = this._renderers.length;
this._renderers.add(renderer);
Expand Down Expand Up @@ -233,5 +256,6 @@ export class ComponentsManager {
this._onPhysicsUpdateScripts.garbageCollection();
this._onUpdateAnimations.garbageCollection();
this._onUpdateRenderers.garbageCollection();
this._activeCameras.garbageCollection();
}
}
4 changes: 4 additions & 0 deletions packages/core/src/DisorderedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export class DisorderedArray<T> {
this._endLoopAndClear();
}

sort(compareFn: (a: T, b: T) => number): void {
this._elements.sort(compareFn);
}

garbageCollection(): void {
this._elements.length = this.length;
}
Expand Down
31 changes: 19 additions & 12 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IPhysics, IPhysicsManager, IShaderLab } from "@galacean/engine-design";
import { Color } from "@galacean/engine-math/src/Color";
import { SpriteMaskInteraction } from "./2d";
import { Font } from "./2d/text/Font";
import { Camera } from "./Camera";
import { Canvas } from "./Canvas";
import { EngineSettings } from "./EngineSettings";
import { Entity } from "./Entity";
Expand All @@ -25,6 +27,7 @@ import { ParticleBufferUtils } from "./particle/ParticleBufferUtils";
import { PhysicsScene } from "./physics/PhysicsScene";
import { ColliderShape } from "./physics/shape/ColliderShape";
import { IHardwareRenderer } from "./renderingHardwareInterface";
import { CompareFunction } from "./shader";
import { Shader } from "./shader/Shader";
import { ShaderMacro } from "./shader/ShaderMacro";
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection";
Expand All @@ -38,8 +41,6 @@ import { CullMode } from "./shader/enums/CullMode";
import { RenderQueueType } from "./shader/enums/RenderQueueType";
import { RenderState } from "./shader/state/RenderState";
import { Texture2D, Texture2DArray, TextureCube, TextureCubeFace, TextureFormat } from "./texture";
import { CompareFunction } from "./shader";
import { SpriteMaskInteraction } from "./2d";

ShaderPool.init();

Expand Down Expand Up @@ -336,8 +337,9 @@ export class Engine extends EventDispatcher {
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._cameraNeedSorting && scene._sortCameras();
scene._componentsManager.callScriptOnStart();
const componentsManager = scene._componentsManager;
componentsManager.sortCameras();
componentsManager.callScriptOnStart();
}

// Update physics and fire `onPhysicsUpdate`
Expand Down Expand Up @@ -498,11 +500,15 @@ export class Engine extends EventDispatcher {
for (let i = 0, n = scenes.length; i < n; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
const cameras = scene._activeCameras;
const cameraCount = cameras.length;
if (cameraCount > 0) {
for (let i = 0; i < cameraCount; i++) {
const camera = cameras[i];
const cameras = scene._componentsManager._activeCameras;

if (cameras.length === 0) {
Logger.debug("No active camera in scene.");
continue;
}

cameras.forEach(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use forEach

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is DisorderedArray

(camera: Camera) => {
const componentsManager = scene._componentsManager;
componentsManager.callCameraOnBeginRender(camera);
camera.render();
Expand All @@ -512,10 +518,11 @@ export class Engine extends EventDispatcher {
if (this._hardwareRenderer._options._forceFlush) {
this._hardwareRenderer.flush();
}
},
(camera: Camera, index: number) => {
camera._cameraIndex = index;
}
} else {
Logger.debug("No active camera in scene.");
}
);
}
}

Expand Down
39 changes: 0 additions & 39 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Color, Vector3, Vector4 } from "@galacean/engine-math";
import { Background } from "./Background";
import { Camera } from "./Camera";
import { ComponentsManager } from "./ComponentsManager";
import { Engine } from "./Engine";
import { Entity } from "./Entity";
Expand Down Expand Up @@ -44,15 +43,11 @@ export class Scene extends EngineObject {
/** Max Shadow distance. */
shadowDistance: number = 50;

/* @internal */
_cameraNeedSorting: boolean = false;
/* @internal */
_lightManager: LightManager = new LightManager();
/* @internal */
_componentsManager: ComponentsManager = new ComponentsManager();
/** @internal */
_activeCameras: Camera[] = [];
/** @internal */
_isActiveInEngine: boolean = false;
/** @internal */
_sceneManager: SceneManager;
Expand Down Expand Up @@ -401,39 +396,6 @@ export class Scene extends EngineObject {
return null;
}

/**
* @internal
*/
_sortCameras(): void {
this._activeCameras.sort((a, b) => a.priority - b.priority);
this._cameraNeedSorting = false;
}

/**
* @internal
*/
_attachRenderCamera(camera: Camera): void {
const activeCameras = this._activeCameras;
const index = activeCameras.indexOf(camera);
if (index === -1) {
activeCameras.push(camera);
this._cameraNeedSorting = true;
} else {
Logger.warn("Camera already attached.");
}
}

/**
* @internal
*/
_detachRenderCamera(camera: Camera): void {
const activeCameras = this._activeCameras;
const index = activeCameras.indexOf(camera);
if (index !== -1) {
activeCameras.splice(index, 1);
}
}

/**
* @internal
*/
Expand Down Expand Up @@ -514,7 +476,6 @@ export class Scene extends EngineObject {
while (this.rootEntitiesCount > 0) {
this._rootEntities[0].destroy();
}
this._activeCameras.length = 0;
this.background.destroy();
this._ambientLight && this._ambientLight._removeFromScene(this);
this.shaderData._addReferCount(-1);
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/input/pointer/PointerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ export class PointerManager implements IInput {
if (!scene.isActive || scene.destroyed) {
continue;
}
const { _activeCameras: cameras } = scene;
const { _activeCameras: cameras } = scene._componentsManager;
const elements = cameras._elements;

for (let j = cameras.length - 1; j >= 0; j--) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cameras should be elements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's DisorderedArray

const camera = cameras[j];
if (!camera.enabled || camera.renderTarget) {
const camera = elements[j];
if (camera.renderTarget) {
continue;
}
const { x: vpX, y: vpY, z: vpW, w: vpH } = camera.viewport;
Expand Down