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 all 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
29 changes: 29 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,30 @@ 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;
this._cameraNeedSorting = true;
}

sortCameras(): void {
if (this._cameraNeedSorting) {
const activeCameras = this._activeCameras;
activeCameras.sort((a, b) => a.priority - b.priority);
for (let i = 0, n = activeCameras.length; i < n; i++) {
activeCameras.get(i)._cameraIndex = i;
}
this._cameraNeedSorting = false;
}
}

addRenderer(renderer: Renderer) {
renderer._rendererIndex = this._renderers.length;
this._renderers.add(renderer);
Expand Down Expand Up @@ -233,5 +261,6 @@ export class ComponentsManager {
this._onPhysicsUpdateScripts.garbageCollection();
this._onUpdateAnimations.garbageCollection();
this._onUpdateRenderers.garbageCollection();
this._activeCameras.garbageCollection();
}
}
10 changes: 8 additions & 2 deletions packages/core/src/DisorderedArray.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Utils } from "./Utils";

/**
* High-performance unordered array, delete uses exchange method to improve performance, internal capacity only increases.
*/
Expand Down Expand Up @@ -69,7 +71,7 @@ export class DisorderedArray<T> {
forEach(callbackFn: (element: T) => void, swapFn: (element: T, index: number) => void): void {
this._startLoop();
const elements = this._elements;
for (let i = 0; i < this.length; i++) {
for (let i = 0, n = this.length; i < n; i++) {
const element = elements[i];
element && callbackFn(element);
}
Expand All @@ -79,13 +81,17 @@ export class DisorderedArray<T> {
forEachAndClean(callbackFn: (e: T) => void): void {
this._startLoop();
const elements = this._elements;
for (let i = 0; i < this.length; i++) {
for (let i = 0, n = this.length; i < n; i++) {
const element = elements[i];
element && callbackFn(element);
}
this._endLoopAndClear();
}

sort(compareFn: (a: T, b: T) => number): void {
Utils._quickSort(this._elements, 0, this.length, 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
106 changes: 2 additions & 104 deletions packages/core/src/RenderPipeline/RenderQueue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Camera } from "../Camera";
import { Engine } from "../Engine";
import { Layer } from "../Layer";
import { Utils } from "../Utils";
import { RenderQueueType, Shader } from "../shader";
import { ShaderMacroCollection } from "../shader/ShaderMacroCollection";
import { RenderContext } from "./RenderContext";
Expand Down Expand Up @@ -194,7 +195,7 @@ export class RenderQueue {
* Sort the elements.
*/
sort(compareFunc: Function): void {
this._quickSort(this.elements, 0, this.elements.length, compareFunc);
Utils._quickSort(this.elements, 0, this.elements.length, compareFunc);
}

/**
Expand All @@ -204,107 +205,4 @@ export class RenderQueue {
_initSpriteBatcher(engine: Engine): void {
this._spriteBatcher = new SpriteBatcher(engine);
}

/**
* @remarks
* Modified based on v8.
* https://github.com/v8/v8/blob/7.2-lkgr/src/js/array.js
*/
private _quickSort<T>(a: T[], from: number, to: number, compareFunc: Function): void {
while (true) {
// Insertion sort is faster for short arrays.
if (to - from <= 10) {
this._insertionSort(a, from, to, compareFunc);
return;
}
const third_index = (from + to) >> 1;
// Find a pivot as the median of first, last and middle element.
let v0 = a[from];
let v1 = a[to - 1];
let v2 = a[third_index];
const c01 = compareFunc(v0, v1);
if (c01 > 0) {
// v1 < v0, so swap them.
const tmp = v0;
v0 = v1;
v1 = tmp;
} // v0 <= v1.
const c02 = compareFunc(v0, v2);
if (c02 >= 0) {
// v2 <= v0 <= v1.
const tmp = v0;
v0 = v2;
v2 = v1;
v1 = tmp;
} else {
// v0 <= v1 && v0 < v2
const c12 = compareFunc(v1, v2);
if (c12 > 0) {
// v0 <= v2 < v1
const tmp = v1;
v1 = v2;
v2 = tmp;
}
}
// v0 <= v1 <= v2
a[from] = v0;
a[to - 1] = v2;
const pivot = v1;
let low_end = from + 1; // Upper bound of elements lower than pivot.
let high_start = to - 1; // Lower bound of elements greater than pivot.
a[third_index] = a[low_end];
a[low_end] = pivot;

// From low_end to i are elements equal to pivot.
// From i to high_start are elements that haven't been compared yet.
partition: for (let i = low_end + 1; i < high_start; i++) {
let element = a[i];
let order = compareFunc(element, pivot);
if (order < 0) {
a[i] = a[low_end];
a[low_end] = element;
low_end++;
} else if (order > 0) {
do {
high_start--;
if (high_start == i) break partition;
const top_elem = a[high_start];
order = compareFunc(top_elem, pivot);
} while (order > 0);
a[i] = a[high_start];
a[high_start] = element;
if (order < 0) {
element = a[i];
a[i] = a[low_end];
a[low_end] = element;
low_end++;
}
}
}
if (to - high_start < low_end - from) {
this._quickSort(a, high_start, to, compareFunc);
to = low_end;
} else {
this._quickSort(a, from, low_end, compareFunc);
from = high_start;
}
}
}

private _insertionSort<T>(a: T[], from: number, to: number, compareFunc: Function): void {
for (let i = from + 1; i < to; i++) {
let j;
const element = a[i];
for (j = i - 1; j >= from; j--) {
const tmp = a[j];
const order = compareFunc(tmp, element);
if (order > 0) {
a[j + 1] = tmp;
} else {
break;
}
}
a[j + 1] = element;
}
}
}
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
Loading