import type { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { Observable } from "@babylonjs/core/Misc/observable"; import type { DeepImmutable } from "@babylonjs/core/types"; import type { IWasmSpinLock } from "../Misc/IWasmSpinLock"; import type { MmdWasmRuntime } from "../mmdWasmRuntime"; import { MmdWasmRuntimeAnimationEvaluationType } from "../mmdWasmRuntime"; import type { IBulletWasmInstance } from "./Bind/bulletWasmInstance"; import type { Constraint } from "./Bind/constraint"; import type { IPhysicsRuntime } from "./Bind/Impl/IPhysicsRuntime"; import type { IRigidBodyBundleImpl } from "./Bind/Impl/IRigidBodyBundleImpl"; import type { IRigidBodyImpl } from "./Bind/Impl/IRigidBodyImpl"; import type { RigidBody } from "./Bind/rigidBody"; import type { RigidBodyBundle } from "./Bind/rigidBodyBundle"; /** * Options for creating a MmdWasmPhysicsRuntimeImpl */ export interface IMmdWasmPhysicsRuntimeImplCreationOptions { /** * Whether to preserve the back buffer for the motion state (default: false) */ preserveBackBuffer?: boolean; } /** * For access full physics world feature, you need to pass this object to the `MmdWasmPhysicsRuntime.getWorld` method */ export declare class MmdWasmPhysicsRuntimeImpl implements IPhysicsRuntime { /** * Observable that is triggered when the physics world is synchronized * in this observable callback scope, ensure that the physics world is not being evaluated */ readonly onSyncObservable: Observable; /** * Observable that is triggered on each physics tick * in this observable callback scope, physics may be evaluating on the worker thread when evaluation type is Buffered */ readonly onTickObservable: Observable; /** * The Bullet WASM instance */ readonly wasmInstance: IBulletWasmInstance; /** * Spinlock for the runtime to synchronize access to the state * */ readonly lock: IWasmSpinLock; private readonly _physicsWorld; private readonly _runtime; private readonly _gravity; private _usingWasmBackBuffer; private _rigidBodyUsingBackBuffer; private readonly _preserveBackBuffer; private _dynamicShadowCount; private readonly _rigidBodyMap; private readonly _rigidBodyBundleMap; /** * @internal * Create a new instance of the MmdWasmPhysicsRuntimeImpl class * @param runtime The MmdWasmRuntime instance * @param initialGravity The initial gravity vector * @param options The options for creating the impl object */ constructor(runtime: MmdWasmRuntime, initialGravity: DeepImmutable, options?: IMmdWasmPhysicsRuntimeImplCreationOptions); dispose(): void; private _nullCheck; /** @internal */ createRigidBodyImpl(): IRigidBodyImpl; /** @internal */ createRigidBodyBundleImpl(bundle: RigidBodyBundle): IRigidBodyBundleImpl; /** @internal */ beforeStep(): void; /** @internal */ afterStep(): void; onEvaluationTypeChanged(evaluationType: MmdWasmRuntimeAnimationEvaluationType): void; /** * Gets the gravity vector of the physics world (default: (0, -10, 0)) * @returns The gravity vector */ getGravityToRef(result: Vector3): Vector3; /** * Sets the gravity vector of the physics world * * If the runtime evaluation type is Buffered, the gravity will be set after waiting for the lock * @param gravity The gravity vector */ setGravity(gravity: DeepImmutable): void; /** * Adds a rigid body to the physics world * * If the world is not existing, it will be created * * If the runtime evaluation type is Buffered, the rigid body will be added after waiting for the lock * @param rigidBody The rigid body to add * @param worldId The ID of the world to add the rigid body to * @returns True if the rigid body was added successfully, false otherwise */ addRigidBody(rigidBody: RigidBody, worldId: number): boolean; /** * Removes a rigid body from the physics world * * If there are no more rigid bodies in the world, the world will be destroyed automatically * * If the runtime evaluation type is Buffered, the rigid body will be removed after waiting for the lock * @param rigidBody The rigid body to remove * @param worldId The ID of the world to remove the rigid body from * @returns True if the rigid body was removed successfully, false otherwise */ removeRigidBody(rigidBody: RigidBody, worldId: number): boolean; /** * Adds a rigid body bundle to the physics world * * If the world is not existing, it will be created * * If the runtime evaluation type is Buffered, the rigid body bundle will be added after waiting for the lock * @param rigidBodyBundle The rigid body bundle to add * @param worldId The ID of the world to add the rigid body bundle to * @returns True if the rigid body bundle was added successfully, false otherwise */ addRigidBodyBundle(rigidBodyBundle: RigidBodyBundle, worldId: number): boolean; /** * Removes a rigid body bundle from the physics world * * If there are no more rigid body bundles in the world, the world will be destroyed automatically * * If the runtime evaluation type is Buffered, the rigid body bundle will be removed after waiting for the lock * @param rigidBodyBundle The rigid body bundle to remove * @param worldId The ID of the world to remove the rigid body bundle from * @returns True if the rigid body bundle was removed successfully, false otherwise */ removeRigidBodyBundle(rigidBodyBundle: RigidBodyBundle, worldId: number): boolean; /** * Adds a rigid body to all worlds * * rigid body physics mode must be Static or Kinematic * * If the runtime evaluation type is Buffered, the rigid body will be added after waiting for the lock * @param rigidBody The rigid body to add * @returns True if the rigid body was added successfully, false otherwise */ addRigidBodyToGlobal(rigidBody: RigidBody): boolean; /** * Removes a rigid body from all worlds * * This method does not remove the rigid body that is added with `MultiPhysicsRuntime.addRigidBody` * * Only the rigid body that is added with `MultiPhysicsRuntime.addRigidBodyToGlobal` will be removed * * If there are no more rigid bodies in the world, the world will be destroyed automatically * * If the runtime evaluation type is Buffered, the rigid body will be removed after waiting for the lock * @param rigidBody The rigid body to remove * @returns True if the rigid body was removed successfully, false otherwise */ removeRigidBodyFromGlobal(rigidBody: RigidBody): boolean; /** * Adds a rigid body bundle to all worlds * * rigid body bundle physics mode must be Static or Kinematic * * If the runtime evaluation type is Buffered, the rigid body bundle will be added after waiting for the lock * @param rigidBodyBundle The rigid body bundle to add * @returns True if the rigid body bundle was added successfully, false otherwise */ addRigidBodyBundleToGlobal(rigidBodyBundle: RigidBodyBundle): boolean; /** * Removes a rigid body bundle from all worlds * * This method does not remove the rigid body bundle that is added with `MultiPhysicsRuntime.addRigidBodyBundle` * * Only the rigid body bundle that is added with `MultiPhysicsRuntime.addRigidBodyBundleToGlobal` will be removed * * If there are no more rigid body bundles in the world, the world will be destroyed automatically * * If the runtime evaluation type is Buffered, the rigid body bundle will be removed after waiting for the lock * @param rigidBodyBundle The rigid body bundle to remove * @returns True if the rigid body bundle was removed successfully, false otherwise */ removeRigidBodyBundleFromGlobal(rigidBodyBundle: RigidBodyBundle): boolean; /** * Adds a rigid body shadow to the physics world * * In case of Dynamic physics mode, Rigid body firstly needs to be added to the other world * * The worldId must be not equal to the worldId of the rigid body * * Rigid body shadow allows the rigid body to be added to multiple worlds * * If the runtime evaluation type is Buffered, the rigid body shadow will be added after waiting for the lock * @param rigidBody The rigid body to add * @param worldId The ID of the world to add the rigid body as shadow * @returns True if the rigid body shadow was added successfully, false otherwise */ addRigidBodyShadow(rigidBody: RigidBody, worldId: number): boolean; /** * Removes a rigid body shadow from the physics world * * If the runtime evaluation type is Buffered, the rigid body shadow will be removed after waiting for the lock * @param rigidBody The rigid body to remove * @param worldId The ID of the world to remove the rigid body shadow from * @returns True if the rigid body shadow was removed successfully, false otherwise */ removeRigidBodyShadow(rigidBody: RigidBody, worldId: number): boolean; /** * Adds a rigid body bundle shadow to the physics world * * In case of Dynamic physics mode, Rigid body bundle firstly needs to be added to the other world * * and the worldId must be not equal to the worldId of the rigid body bundle * * Rigid body bundle shadow allows the rigid body bundle to be added to multiple worlds * * If the runtime evaluation type is Buffered, the rigid body bundle shadow will be added after waiting for the lock * @param rigidBodyBundle The rigid body bundle to add * @param worldId The ID of the world to add the rigid body bundle as shadow * @returns True if the rigid body bundle shadow was added successfully, false otherwise */ addRigidBodyBundleShadow(rigidBodyBundle: RigidBodyBundle, worldId: number): boolean; /** * Removes a rigid body bundle shadow from the physics world * * If the runtime evaluation type is Buffered, the rigid body bundle shadow will be removed after waiting for the lock * @param rigidBodyBundle The rigid body bundle to remove * @param worldId The ID of the world to remove the rigid body bundle shadow from * @returns True if the rigid body bundle shadow was removed successfully, false otherwise */ removeRigidBodyBundleShadow(rigidBodyBundle: RigidBodyBundle, worldId: number): boolean; /** * Gets the rigid body reference count map */ get rigidBodyReferenceCountMap(): ReadonlyMap; /** * Gets the rigid body bundle reference count map */ get rigidBodyBundleReferenceCountMap(): ReadonlyMap; /** * Adds a constraint to the physics world * * Constraint worldId must be equal to the worldId of the connected rigid bodies * * If the runtime evaluation type is Buffered, the constraint will be added after waiting for the lock * @param constraint The constraint to add * @param worldId The ID of the world to add the constraint to * @param disableCollisionsBetweenLinkedBodies Whether to disable collisions between the linked bodies * @returns True if the constraint was added successfully, false otherwise */ addConstraint(constraint: Constraint, worldId: number, disableCollisionsBetweenLinkedBodies: boolean): boolean; /** * Removes a constraint from the physics world * * If the runtime evaluation type is Buffered, the constraint will be removed after waiting for the lock * @param constraint The constraint to remove * @param worldId The ID of the world to remove the constraint from * @returns True if the constraint was removed successfully, false otherwise */ removeConstraint(constraint: Constraint, worldId: number): boolean; }