Files
esengine/thirdparty/rapier.js/src.ts/math.ts
YHH 63f006ab62 feat: 添加跨平台运行时、资产系统和UI适配功能 (#256)
* feat(platform-common): 添加WASM加载器和环境检测API

* feat(rapier2d): 新增Rapier2D WASM绑定包

* feat(physics-rapier2d): 添加跨平台WASM加载器

* feat(asset-system): 添加运行时资产目录和bundle格式

* feat(asset-system-editor): 新增编辑器资产管理包

* feat(editor-core): 添加构建系统和模块管理

* feat(editor-app): 重构浏览器预览使用import maps

* feat(platform-web): 添加BrowserRuntime和资产读取

* feat(engine): 添加材质系统和着色器管理

* feat(material): 新增材质系统和着色器编辑器

* feat(tilemap): 增强tilemap编辑器和动画系统

* feat(modules): 添加module.json配置

* feat(core): 添加module.json和类型定义更新

* chore: 更新依赖和构建配置

* refactor(plugins): 更新插件模板使用ModuleManifest

* chore: 添加第三方依赖库

* chore: 移除BehaviourTree-ai和ecs-astar子模块

* docs: 更新README和文档主题样式

* fix: 修复Rust文档测试和添加rapier2d WASM绑定

* fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题

* feat(ui): 添加UI屏幕适配系统(CanvasScaler/SafeArea)

* fix(ecs-engine-bindgen): 添加缺失的ecs-framework-math依赖

* fix: 添加缺失的包依赖修复CI构建

* fix: 修复CodeQL检测到的代码问题

* fix: 修复构建错误和缺失依赖

* fix: 修复类型检查错误

* fix(material-system): 修复tsconfig配置支持TypeScript项目引用

* fix(editor-core): 修复Rollup构建配置添加tauri external

* fix: 修复CodeQL检测到的代码问题

* fix: 修复CodeQL检测到的代码问题
2025-12-03 22:15:22 +08:00

264 lines
5.0 KiB
TypeScript

import {RawVector, RawRotation} from "./raw";
// #if DIM3
import {RawSdpMatrix3} from "./raw";
// #endif
// #if DIM2
export interface Vector {
x: number;
y: number;
}
/**
* A 2D vector.
*/
export class Vector2 implements Vector {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
export class VectorOps {
public static new(x: number, y: number): Vector {
return new Vector2(x, y);
}
public static zeros(): Vector {
return VectorOps.new(0.0, 0.0);
}
// FIXME: type ram: RawVector?
public static fromRaw(raw: RawVector): Vector | null {
if (!raw) return null;
let res = VectorOps.new(raw.x, raw.y);
raw.free();
return res;
}
public static intoRaw(v: Vector): RawVector {
return new RawVector(v.x, v.y);
}
public static copy(out: Vector, input: Vector) {
out.x = input.x;
out.y = input.y;
}
}
/**
* A rotation angle in radians.
*/
export type Rotation = number;
export class RotationOps {
public static identity(): number {
return 0.0;
}
public static fromRaw(raw: RawRotation): Rotation | null {
if (!raw) return null;
let res = raw.angle;
raw.free();
return res;
}
public static intoRaw(angle: Rotation): RawRotation {
return RawRotation.fromAngle(angle);
}
}
// #endif
// #if DIM3
export interface Vector {
x: number;
y: number;
z: number;
}
/**
* A 3D vector.
*/
export class Vector3 implements Vector {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
}
export class VectorOps {
public static new(x: number, y: number, z: number): Vector {
return new Vector3(x, y, z);
}
public static intoRaw(v: Vector): RawVector {
return new RawVector(v.x, v.y, v.z);
}
public static zeros(): Vector {
return VectorOps.new(0.0, 0.0, 0.0);
}
// FIXME: type ram: RawVector?
public static fromRaw(raw: RawVector): Vector | null {
if (!raw) return null;
let res = VectorOps.new(raw.x, raw.y, raw.z);
raw.free();
return res;
}
public static copy(out: Vector, input: Vector) {
out.x = input.x;
out.y = input.y;
out.z = input.z;
}
}
export interface Rotation {
x: number;
y: number;
z: number;
w: number;
}
/**
* A quaternion.
*/
export class Quaternion implements Rotation {
x: number;
y: number;
z: number;
w: number;
constructor(x: number, y: number, z: number, w: number) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
}
export class RotationOps {
public static identity(): Rotation {
return new Quaternion(0.0, 0.0, 0.0, 1.0);
}
public static fromRaw(raw: RawRotation): Rotation | null {
if (!raw) return null;
let res = new Quaternion(raw.x, raw.y, raw.z, raw.w);
raw.free();
return res;
}
public static intoRaw(rot: Rotation): RawRotation {
return new RawRotation(rot.x, rot.y, rot.z, rot.w);
}
public static copy(out: Rotation, input: Rotation) {
out.x = input.x;
out.y = input.y;
out.z = input.z;
out.w = input.w;
}
}
/**
* A 3D symmetric-positive-definite matrix.
*/
export class SdpMatrix3 {
/**
* Row major list of the upper-triangular part of the symmetric matrix.
*/
elements: Float32Array;
/**
* Matrix element at row 1, column 1.
*/
public get m11(): number {
return this.elements[0];
}
/**
* Matrix element at row 1, column 2.
*/
public get m12(): number {
return this.elements[1];
}
/**
* Matrix element at row 2, column 1.
*/
public get m21(): number {
return this.m12;
}
/**
* Matrix element at row 1, column 3.
*/
public get m13(): number {
return this.elements[2];
}
/**
* Matrix element at row 3, column 1.
*/
public get m31(): number {
return this.m13;
}
/**
* Matrix element at row 2, column 2.
*/
public get m22(): number {
return this.elements[3];
}
/**
* Matrix element at row 2, column 3.
*/
public get m23(): number {
return this.elements[4];
}
/**
* Matrix element at row 3, column 2.
*/
public get m32(): number {
return this.m23;
}
/**
* Matrix element at row 3, column 3.
*/
public get m33(): number {
return this.elements[5];
}
constructor(elements: Float32Array) {
this.elements = elements;
}
}
export class SdpMatrix3Ops {
public static fromRaw(raw: RawSdpMatrix3): SdpMatrix3 {
const sdpMatrix3 = new SdpMatrix3(raw.elements());
raw.free();
return sdpMatrix3;
}
}
// #endif