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 physics init #722

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 5 additions & 8 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IPhysics } from "@oasis-engine/design";
import { ColorSpace } from ".";
import { DynamicTextAtlasManager } from "./2d/dynamic-atlas/DynamicTextAtlasManager";
import { ResourceManager } from "./asset/ResourceManager";
Expand Down Expand Up @@ -178,16 +177,14 @@ export class Engine extends EventDispatcher {
* Create engine.
* @param canvas - The canvas to use for rendering
* @param hardwareRenderer - Graphics API renderer
* @param physics - native physics Engine
*/
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, physics?: IPhysics, settings?: EngineSettings) {
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, settings?: EngineSettings) {
super();
this._hardwareRenderer = hardwareRenderer;
this._hardwareRenderer.init(canvas);
if (physics) {
PhysicsManager._nativePhysics = physics;
this.physicsManager = new PhysicsManager(this);
}

this.physicsManager = new PhysicsManager(this);

this._canvas = canvas;
// @todo delete
engineFeatureManager.addObject(this);
Expand Down Expand Up @@ -274,7 +271,7 @@ export class Engine extends EventDispatcher {
scene._activeCameras.sort((camera1, camera2) => camera1.priority - camera2.priority);

componentsManager.callScriptOnStart();
if (this.physicsManager) {
if (this.physicsManager._initialized) {
componentsManager.callColliderOnUpdate();
this.physicsManager._update(deltaTime / 1000.0);
componentsManager.callColliderOnLateUpdate();
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/input/pointer/PointerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Canvas } from "../../Canvas";
import { Engine } from "../../Engine";
import { Entity } from "../../Entity";
import { CameraClearFlags } from "../../enums/CameraClearFlags";
import { HitResult } from "../../physics";
import { HitResult, PhysicsManager } from "../../physics";
import { PointerPhase } from "../enums/PointerPhase";
import { Pointer } from "./Pointer";

Expand All @@ -20,8 +20,6 @@ export class PointerManager {
_pointers: Pointer[] = [];
/** @internal */
_multiPointerEnabled: boolean = true;
/** @internal */
_enablePhysics: boolean = false;

private _engine: Engine;
private _canvas: Canvas;
Expand Down Expand Up @@ -49,7 +47,6 @@ export class PointerManager {
};
// If there are no compatibility issues, navigator.maxTouchPoints should be used here.
this._pointerPool = new Array<Pointer>(11);
this._enablePhysics = engine.physicsManager ? true : false;
}

/**
Expand All @@ -58,7 +55,7 @@ export class PointerManager {
_update(): void {
this._needOverallPointers && this._overallPointers();
this._nativeEvents.length > 0 && this._handlePointerEvent(this._nativeEvents);
if (this._enablePhysics) {
if (this._engine.physicsManager._initialized) {
const rayCastEntity = this._pointerRayCast();
const { _keyEventCount: keyEventCount } = this;
if (keyEventCount > 0) {
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/physics/PhysicsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ColliderShape } from "./shape/ColliderShape";
export class PhysicsManager {
/** @internal */
static _nativePhysics: IPhysics;
/** @internal */
_initialized: boolean = false;

private _engine: Engine;
private _restTime: number = 0;
Expand Down Expand Up @@ -64,7 +66,7 @@ export class PhysicsManager {
scripts = shape2.collider.entity._scripts;
for (let i = 0, len = scripts.length; i < len; i++) {
const script = scripts.get(i);
script._waitHandlingInValid ||script.onCollisionStay(shape1);
script._waitHandlingInValid || script.onCollisionStay(shape1);
}
};
private _onTriggerEnter = (obj1: number, obj2: number) => {
Expand All @@ -80,7 +82,7 @@ export class PhysicsManager {
scripts = shape2.collider.entity._scripts;
for (let i = 0, len = scripts.length; i < len; i++) {
const script = scripts.get(i);
script._waitHandlingInValid ||script.onTriggerEnter(shape1);
script._waitHandlingInValid || script.onTriggerEnter(shape1);
}
};

Expand Down Expand Up @@ -138,6 +140,17 @@ export class PhysicsManager {

constructor(engine: Engine) {
this._engine = engine;
}

/**
* initialize PhysicsManager.
* @param physics - Physics Engine
*/
initialize(physics: IPhysics): void {
if (PhysicsManager._nativePhysics) {
return;
}
PhysicsManager._nativePhysics = physics;
this._nativePhysicsManager = PhysicsManager._nativePhysics.createPhysicsManager(
this._onContactEnter,
this._onContactExit,
Expand All @@ -146,6 +159,7 @@ export class PhysicsManager {
this._onTriggerExit,
this._onTriggerStay
);
this._initialized = true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/physics-physx/src/PhysXPhysics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PhysXPhysics {
* @param runtimeMode - Runtime mode
* @returns Promise object
*/
public static init(runtimeMode: PhysXRuntimeMode = PhysXRuntimeMode.Auto): Promise<void> {
public static initialize(runtimeMode: PhysXRuntimeMode = PhysXRuntimeMode.Auto): Promise<void> {
const scriptPromise = new Promise((resolve) => {
const script = document.createElement("script");
document.body.appendChild(script);
Expand Down
10 changes: 2 additions & 8 deletions packages/rhi-webgl/src/WebGLEngine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Engine } from "@oasis-engine/core";
import { WebCanvas } from "./WebCanvas";
import { WebGLRenderer, WebGLRendererOptions } from "./WebGLRenderer";
import { IPhysics } from "@oasis-engine/design";

type OffscreenCanvas = any;

Expand All @@ -12,19 +11,14 @@ export class WebGLEngine extends Engine {
/**
* Create an engine suitable for the WebGL platform.
* @param canvas - Native web canvas
* @param physics - Physics Engine
* @param webGLRendererOptions - WebGL renderer options
*/
constructor(
canvas: string | HTMLCanvasElement | OffscreenCanvas,
physics?: IPhysics,
webGLRendererOptions?: WebGLRendererOptions
) {
constructor(canvas: string | HTMLCanvasElement | OffscreenCanvas, webGLRendererOptions?: WebGLRendererOptions) {
const webCanvas = new WebCanvas(
<HTMLCanvasElement | OffscreenCanvas>(typeof canvas === "string" ? document.getElementById(canvas) : canvas)
);
const hardwareRenderer = new WebGLRenderer(webGLRendererOptions);
super(webCanvas, hardwareRenderer, physics);
super(webCanvas, hardwareRenderer);
}

/**
Expand Down