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

Optimization dependent components #1435

Merged
merged 1 commit into from
Mar 28, 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
4 changes: 2 additions & 2 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class MathTemp {

/**
* Camera component, as the entrance to the three-dimensional world.
* @decorator `@dependentComponents(DependentMode.CheckOnly, Transform)`
* @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)`
*/
@dependentComponents(DependentMode.CheckOnly, Transform)
@dependentComponents(Transform, DependentMode.CheckOnly)
export class Camera extends Component {
/** @internal */
private static _inverseViewMatrixProperty = ShaderProperty.getByName("u_viewInvMat");
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/ComponentsDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,25 @@ export class ComponentsDependencies {
}

/**
* Declare dependent components.
* Declare dependent component.
* @param component - Dependent component
* @param dependentMode - Dependent mode
*/
export function dependentComponents(component: ComponentConstructor, dependentMode?: DependentMode);

/**
* Declare dependent components.
* @param components - Dependent components
* @param dependentMode - Dependent mode
*/
export function dependentComponents(dependentMode: DependentMode, ...components: ComponentConstructor[]) {
export function dependentComponents(components: ComponentConstructor[], dependentMode?: DependentMode);

export function dependentComponents(
componentOrComponents: ComponentConstructor | ComponentConstructor[],
dependentMode: DependentMode = DependentMode.CheckOnly
) {
const components = Array.isArray(componentOrComponents) ? componentOrComponents : [componentOrComponents];

return function <T extends ComponentConstructor>(target: T): void {
ComponentsDependencies._dependenciesMap.set(target, { mode: dependentMode, components });
components.forEach((component) => ComponentsDependencies._addInvDependency(component, target));
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-ignore
import { BoundingBox, Matrix, Vector3 } from "@oasis-engine/math";
import { assignmentClone, deepClone, ignoreClone, shallowClone } from "./clone/CloneManager";
import { Component } from "./Component";
import { dependentComponents, DependentMode } from "./ComponentsDependencies";
import { Entity } from "./Entity";
import { Material } from "./material/Material";
import { RenderContext } from "./RenderPipeline/RenderContext";
import { Shader, ShaderProperty } from "./shader";
import { ShaderProperty } from "./shader";
import { ShaderDataGroup } from "./shader/enums/ShaderDataGroup";
import { ShaderData } from "./shader/ShaderData";
import { ShaderMacro } from "./shader/ShaderMacro";
Expand All @@ -14,9 +15,9 @@ import { Transform, TransformModifyFlags } from "./Transform";

/**
* Basis for all renderers.
* @decorator `@dependentComponents(DependentMode.CheckOnly, Transform)`
* @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)`
*/
@dependentComponents(DependentMode.CheckOnly, Transform)
@dependentComponents(Transform, DependentMode.CheckOnly)
export class Renderer extends Component {
private static _tempVector0 = new Vector3();

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/Collider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { ColliderShape } from "./shape/ColliderShape";

/**
* Base class for all colliders.
* @decorator `@dependentComponents(Transform)`
* @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)`
*/
@dependentComponents(DependentMode.CheckOnly, Transform)
@dependentComponents(Transform, DependentMode.CheckOnly)
export class Collider extends Component {
/** @internal */
@ignoreClone
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/joint/Joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Collider } from "../Collider";

/**
* A base class providing common functionality for joints.
* @decorator `@dependentComponents(DependentMode.CheckOnly,Collider)`
* @decorator `@dependentComponents(Collider, DependentMode.CheckOnly)`
*/
@dependentComponents(DependentMode.CheckOnly, Collider)
@dependentComponents(Collider, DependentMode.CheckOnly)
export class Joint extends Component {
protected _connectedCollider = new JointCollider();
protected _collider = new JointCollider();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/CompomentDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("Component dependencies test", function () {
});
});

@dependentComponents(DependentMode.CheckOnly, MeshRenderer)
@dependentComponents(MeshRenderer, DependentMode.CheckOnly)
export class CustomScriptA extends Script {}

export class CustomScriptB extends CustomScriptA {}
4 changes: 2 additions & 2 deletions tests/src/core/Script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ describe("Script", () => {
});

it("Dependent components", () => {
@dependentComponents(DependentMode.CheckOnly, Camera)
@dependentComponents(Camera, DependentMode.CheckOnly)
class CheckScript extends Script {}

@dependentComponents(DependentMode.AutoAdd, Camera)
@dependentComponents(Camera, DependentMode.AutoAdd)
class AutoAddScript extends Script {}

const engine = new WebGLEngine(document.createElement("canvas"));
Expand Down