Files
esengine/thirdparty/rapier.js/src/pipeline/event_queue.rs
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

141 lines
5.1 KiB
Rust

use crate::math::RawVector;
use crate::utils;
use crate::utils::FlatHandle;
use rapier::geometry::{CollisionEvent, ContactForceEvent};
use rapier::pipeline::ChannelEventCollector;
use std::sync::mpsc::Receiver;
use wasm_bindgen::prelude::*;
/// A structure responsible for collecting events generated
/// by the physics engine.
#[wasm_bindgen]
pub struct RawEventQueue {
pub(crate) collector: ChannelEventCollector,
collision_events: Receiver<CollisionEvent>,
contact_force_events: Receiver<ContactForceEvent>,
pub(crate) auto_drain: bool,
}
#[wasm_bindgen]
pub struct RawContactForceEvent(ContactForceEvent);
#[wasm_bindgen]
impl RawContactForceEvent {
/// The first collider involved in the contact.
pub fn collider1(&self) -> FlatHandle {
crate::utils::flat_handle(self.0.collider1.0)
}
/// The second collider involved in the contact.
pub fn collider2(&self) -> FlatHandle {
crate::utils::flat_handle(self.0.collider2.0)
}
/// The sum of all the forces between the two colliders.
pub fn total_force(&self) -> RawVector {
RawVector(self.0.total_force)
}
/// The sum of the magnitudes of each force between the two colliders.
///
/// Note that this is **not** the same as the magnitude of `self.total_force`.
/// Here we are summing the magnitude of all the forces, instead of taking
/// the magnitude of their sum.
pub fn total_force_magnitude(&self) -> f32 {
self.0.total_force_magnitude
}
/// The world-space (unit) direction of the force with strongest magnitude.
pub fn max_force_direction(&self) -> RawVector {
RawVector(self.0.max_force_direction)
}
/// The magnitude of the largest force at a contact point of this contact pair.
pub fn max_force_magnitude(&self) -> f32 {
self.0.max_force_magnitude
}
}
// #[wasm_bindgen]
// /// The proximity state of a sensor collider and another collider.
// pub enum RawIntersection {
// /// The sensor is intersecting the other collider.
// Intersecting = 0,
// /// The sensor is within tolerance margin of the other collider.
// WithinMargin = 1,
// /// The sensor is disjoint from the other collider.
// Disjoint = 2,
// }
#[wasm_bindgen]
impl RawEventQueue {
/// Creates a new event collector.
///
/// # Parameters
/// - `autoDrain`: setting this to `true` is strongly recommended. If true, the collector will
/// be automatically drained before each `world.step(collector)`. If false, the collector will
/// keep all events in memory unless it is manually drained/cleared; this may lead to unbounded use of
/// RAM if no drain is performed.
#[wasm_bindgen(constructor)]
pub fn new(autoDrain: bool) -> Self {
let collision_channel = std::sync::mpsc::channel();
let contact_force_channel = std::sync::mpsc::channel();
let collector = ChannelEventCollector::new(collision_channel.0, contact_force_channel.0);
Self {
collector,
collision_events: collision_channel.1,
contact_force_events: contact_force_channel.1,
auto_drain: autoDrain,
}
}
/// Applies the given javascript closure on each collision event of this collector, then clear
/// the internal collision event buffer.
///
/// # Parameters
/// - `f(handle1, handle2, started)`: JavaScript closure applied to each collision event. The
/// closure should take three arguments: two integers representing the handles of the colliders
/// involved in the collision, and a boolean indicating if the collision started (true) or stopped
/// (false).
pub fn drainCollisionEvents(&mut self, f: &js_sys::Function) {
let this = JsValue::null();
while let Ok(event) = self.collision_events.try_recv() {
match event {
CollisionEvent::Started(co1, co2, _) => {
let h1 = utils::flat_handle(co1.0);
let h2 = utils::flat_handle(co2.0);
let _ = f.call3(
&this,
&JsValue::from(h1),
&JsValue::from(h2),
&JsValue::from_bool(true),
);
}
CollisionEvent::Stopped(co1, co2, _) => {
let h1 = utils::flat_handle(co1.0);
let h2 = utils::flat_handle(co2.0);
let _ = f.call3(
&this,
&JsValue::from(h1),
&JsValue::from(h2),
&JsValue::from_bool(false),
);
}
}
}
}
pub fn drainContactForceEvents(&mut self, f: &js_sys::Function) {
let this = JsValue::null();
while let Ok(event) = self.contact_force_events.try_recv() {
let _ = f.call1(&this, &JsValue::from(RawContactForceEvent(event)));
}
}
/// Removes all events contained by this collector.
pub fn clear(&self) {
while let Ok(_) = self.collision_events.try_recv() {}
}
}