Files
esengine/thirdparty/rapier.js/src/math.rs

267 lines
6.7 KiB
Rust
Raw Normal View History

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
//! Linear algebra primitives.
#[cfg(feature = "dim3")]
use js_sys::Float32Array;
#[cfg(feature = "dim3")]
use na::{Quaternion, Unit};
#[cfg(feature = "dim3")]
use rapier::math::Real;
use rapier::math::{Point, Rotation, Vector};
#[cfg(feature = "dim3")]
use rapier::parry::utils::SdpMatrix3;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[repr(transparent)]
#[derive(Copy, Clone)]
/// A rotation quaternion.
pub struct RawRotation(pub(crate) Rotation<f32>);
impl From<Rotation<f32>> for RawRotation {
fn from(v: Rotation<f32>) -> Self {
RawRotation(v)
}
}
#[wasm_bindgen]
#[cfg(feature = "dim2")]
/// A unit complex number describing the orientation of a Rapier entity.
impl RawRotation {
/// The identity rotation.
pub fn identity() -> Self {
Self(Rotation::identity())
}
/// The rotation with thegiven angle.
pub fn fromAngle(angle: f32) -> Self {
Self(Rotation::new(angle))
}
/// The imaginary part of this complex number.
#[wasm_bindgen(getter)]
pub fn im(&self) -> f32 {
self.0.im
}
/// The real part of this complex number.
#[wasm_bindgen(getter)]
pub fn re(&self) -> f32 {
self.0.re
}
/// The rotation angle in radians.
#[wasm_bindgen(getter)]
pub fn angle(&self) -> f32 {
self.0.angle()
}
}
#[wasm_bindgen]
#[cfg(feature = "dim3")]
/// A unit quaternion describing the orientation of a Rapier entity.
impl RawRotation {
#[wasm_bindgen(constructor)]
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
RawRotation(Unit::new_unchecked(Quaternion::new(w, x, y, z)))
}
/// The identity quaternion.
pub fn identity() -> Self {
Self(Rotation::identity())
}
/// The `x` component of this quaternion.
#[wasm_bindgen(getter)]
pub fn x(&self) -> f32 {
self.0.i
}
/// The `y` component of this quaternion.
#[wasm_bindgen(getter)]
pub fn y(&self) -> f32 {
self.0.j
}
/// The `z` component of this quaternion.
#[wasm_bindgen(getter)]
pub fn z(&self) -> f32 {
self.0.k
}
/// The `w` component of this quaternion.
#[wasm_bindgen(getter)]
pub fn w(&self) -> f32 {
self.0.w
}
}
#[wasm_bindgen]
#[repr(transparent)]
#[derive(Copy, Clone)]
/// A vector.
pub struct RawVector(pub(crate) Vector<f32>);
impl From<Vector<f32>> for RawVector {
fn from(v: Vector<f32>) -> Self {
RawVector(v)
}
}
impl From<Point<f32>> for RawVector {
fn from(pt: Point<f32>) -> Self {
pt.coords.into()
}
}
#[wasm_bindgen]
impl RawVector {
/// Creates a new vector filled with zeros.
pub fn zero() -> Self {
Self(Vector::zeros())
}
/// Creates a new 2D vector from its two components.
///
/// # Parameters
/// - `x`: the `x` component of this 2D vector.
/// - `y`: the `y` component of this 2D vector.
#[cfg(feature = "dim2")]
#[wasm_bindgen(constructor)]
pub fn new(x: f32, y: f32) -> Self {
Self(Vector::new(x, y))
}
/// Creates a new 3D vector from its two components.
///
/// # Parameters
/// - `x`: the `x` component of this 3D vector.
/// - `y`: the `y` component of this 3D vector.
/// - `z`: the `z` component of this 3D vector.
#[cfg(feature = "dim3")]
#[wasm_bindgen(constructor)]
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self(Vector::new(x, y, z))
}
/// The `x` component of this vector.
#[wasm_bindgen(getter)]
pub fn x(&self) -> f32 {
self.0.x
}
/// Sets the `x` component of this vector.
#[wasm_bindgen(setter)]
pub fn set_x(&mut self, x: f32) {
self.0.x = x
}
/// The `y` component of this vector.
#[wasm_bindgen(getter)]
pub fn y(&self) -> f32 {
self.0.y
}
/// Sets the `y` component of this vector.
#[wasm_bindgen(setter)]
pub fn set_y(&mut self, y: f32) {
self.0.y = y
}
/// The `z` component of this vector.
#[cfg(feature = "dim3")]
#[wasm_bindgen(getter)]
pub fn z(&self) -> f32 {
self.0.z
}
/// Sets the `z` component of this vector.
#[cfg(feature = "dim3")]
#[wasm_bindgen(setter)]
pub fn set_z(&mut self, z: f32) {
self.0.z = z
}
/// Create a new 2D vector from this vector with its components rearranged as `{x, y}`.
#[cfg(feature = "dim2")]
pub fn xy(&self) -> Self {
Self(self.0.xy())
}
/// Create a new 2D vector from this vector with its components rearranged as `{y, x}`.
#[cfg(feature = "dim2")]
pub fn yx(&self) -> Self {
Self(self.0.yx())
}
/// Create a new 2D vector from this vector with its components rearranged as `{z, y}`.
#[cfg(feature = "dim2")]
#[cfg(feature = "dim3")]
pub fn zy(&self) -> Self {
Self(self.0.zy())
}
/// Create a new 3D vector from this vector with its components rearranged as `{x, y, z}`.
///
/// This will effectively return a copy of `this`. This method exist for completeness with the
/// other swizzling functions.
#[cfg(feature = "dim3")]
pub fn xyz(&self) -> Self {
Self(self.0.xyz())
}
/// Create a new 3D vector from this vector with its components rearranged as `{y, x, z}`.
#[cfg(feature = "dim3")]
pub fn yxz(&self) -> Self {
Self(self.0.yxz())
}
/// Create a new 3D vector from this vector with its components rearranged as `{z, x, y}`.
#[cfg(feature = "dim3")]
pub fn zxy(&self) -> Self {
Self(self.0.zxy())
}
/// Create a new 3D vector from this vector with its components rearranged as `{x, z, y}`.
#[cfg(feature = "dim3")]
pub fn xzy(&self) -> Self {
Self(self.0.xzy())
}
/// Create a new 3D vector from this vector with its components rearranged as `{y, z, x}`.
#[cfg(feature = "dim3")]
pub fn yzx(&self) -> Self {
Self(self.0.yzx())
}
/// Create a new 3D vector from this vector with its components rearranged as `{z, y, x}`.
#[cfg(feature = "dim3")]
pub fn zyx(&self) -> Self {
Self(self.0.zyx())
}
}
#[wasm_bindgen]
#[repr(transparent)]
#[derive(Copy, Clone)]
#[cfg(feature = "dim3")]
pub struct RawSdpMatrix3(pub(crate) SdpMatrix3<Real>);
#[cfg(feature = "dim3")]
impl From<SdpMatrix3<Real>> for RawSdpMatrix3 {
fn from(v: SdpMatrix3<Real>) -> Self {
RawSdpMatrix3(v)
}
}
#[wasm_bindgen]
#[cfg(feature = "dim3")]
impl RawSdpMatrix3 {
/// Row major list of the upper-triangular part of the symmetric matrix.
pub fn elements(&self) -> Float32Array {
let m = self.0;
let output = Float32Array::new_with_length(6);
output.copy_from(&[m.m11, m.m12, m.m13, m.m22, m.m23, m.m33]);
output
}
}