Files
esengine/packages/particle/src/Particle.ts

234 lines
5.2 KiB
TypeScript
Raw Normal View History

feat(particle): 添加完整粒子系统和粒子编辑器 (#284) * feat(editor-core): 添加用户系统自动注册功能 - IUserCodeService 新增 registerSystems/unregisterSystems/getRegisteredSystems 方法 - UserCodeService 实现系统检测、实例化和场景注册逻辑 - ServiceRegistry 在预览开始时注册用户系统,停止时移除 - 热更新时自动重新加载用户系统 - 更新 System 脚本模板添加 @ECSSystem 装饰器 * feat(editor-core): 添加编辑器脚本支持(Inspector/Gizmo) - registerEditorExtensions 实际注册用户 Inspector 和 Gizmo - 添加 unregisterEditorExtensions 方法 - ServiceRegistry 在项目加载时编译并加载编辑器脚本 - 项目关闭时自动清理编辑器扩展 - 添加 Inspector 和 Gizmo 脚本创建模板 * feat(particle): 添加粒子系统和粒子编辑器 新增两个包: - @esengine/particle: 粒子系统核心库 - @esengine/particle-editor: 粒子编辑器 UI 粒子系统功能: - ECS 组件架构,支持播放/暂停/重置控制 - 7种发射形状:点、圆、环、矩形、边缘、线、锥形 - 5个动画模块:颜色渐变、缩放曲线、速度控制、旋转、噪声 - 纹理动画模块支持精灵表动画 - 3种混合模式:Normal、Additive、Multiply - 11个内置预设:火焰、烟雾、爆炸、雨、雪等 - 对象池优化,支持粒子复用 编辑器功能: - 实时 Canvas 预览,支持全屏和鼠标跟随 - 点击触发爆发效果(用于测试爆炸类特效) - 渐变编辑器:可视化颜色关键帧编辑 - 曲线编辑器:支持缩放曲线和缓动函数 - 预设浏览器:快速应用内置预设 - 模块开关:独立启用/禁用各个模块 - Vector2 样式输入(重力 X/Y) * feat(particle): 完善粒子系统核心功能 1. Burst 定时爆发系统 - BurstConfig 接口支持时间、数量、循环次数、间隔 - 运行时自动处理定时爆发 - 支持无限循环爆发 2. 速度曲线模块 (VelocityOverLifetimeModule) - 6种曲线类型:Constant、Linear、EaseIn、EaseOut、EaseInOut、Custom - 自定义关键帧曲线支持 - 附加速度 X/Y - 轨道速度和径向速度 3. 碰撞边界模块 (CollisionModule) - 矩形和圆形边界类型 - 3种碰撞行为:Kill、Bounce、Wrap - 反弹系数和最小速度阈值 - 反弹时生命损失 * feat(particle): 添加力场模块、碰撞模块和世界/本地空间支持 - 新增 ForceFieldModule 支持风力、吸引点、漩涡、湍流四种力场类型 - 新增 SimulationSpace 枚举支持世界空间和本地空间切换 - ParticleSystemComponent 集成力场模块和空间模式 - 粒子编辑器添加 Collision 和 ForceField 模块的 UI 编辑支持 - 新增 Vortex、Leaves、Bouncing 三个预设展示新功能 - 编辑器预览实现完整的碰撞和力场效果 * fix(particle): 移除未使用的 transform 循环变量
2025-12-05 23:03:31 +08:00
/**
*
* Particle data structure
*
* Represents a single particle with all its runtime state.
*
*/
export interface Particle {
/** 是否存活 | Whether particle is alive */
alive: boolean;
/** 位置X | Position X */
x: number;
/** 位置Y | Position Y */
y: number;
/** 速度X | Velocity X */
vx: number;
/** 速度Y | Velocity Y */
vy: number;
/** 加速度X | Acceleration X */
ax: number;
/** 加速度Y | Acceleration Y */
ay: number;
/** 旋转角度(弧度)| Rotation (radians) */
rotation: number;
/** 角速度 | Angular velocity */
angularVelocity: number;
/** 缩放X | Scale X */
scaleX: number;
/** 缩放Y | Scale Y */
scaleY: number;
/** 颜色R (0-1) | Color R (0-1) */
r: number;
/** 颜色G (0-1) | Color G (0-1) */
g: number;
/** 颜色B (0-1) | Color B (0-1) */
b: number;
/** 透明度 (0-1) | Alpha (0-1) */
alpha: number;
/** 当前生命时间(秒)| Current lifetime (seconds) */
age: number;
/** 总生命时间(秒)| Total lifetime (seconds) */
lifetime: number;
/** 初始缩放X | Initial scale X */
startScaleX: number;
/** 初始缩放Y | Initial scale Y */
startScaleY: number;
/** 初始颜色R | Initial color R */
startR: number;
/** 初始颜色G | Initial color G */
startG: number;
/** 初始颜色B | Initial color B */
startB: number;
/** 初始透明度 | Initial alpha */
startAlpha: number;
/** 自定义数据槽 | Custom data slot */
userData?: any;
}
/**
*
* Create a new particle
*/
export function createParticle(): Particle {
return {
alive: false,
x: 0,
y: 0,
vx: 0,
vy: 0,
ax: 0,
ay: 0,
rotation: 0,
angularVelocity: 0,
scaleX: 1,
scaleY: 1,
r: 1,
g: 1,
b: 1,
alpha: 1,
age: 0,
lifetime: 1,
startScaleX: 1,
startScaleY: 1,
startR: 1,
startG: 1,
startB: 1,
startAlpha: 1
};
}
/**
*
* Reset particle state
*/
export function resetParticle(p: Particle): void {
p.alive = false;
p.x = 0;
p.y = 0;
p.vx = 0;
p.vy = 0;
p.ax = 0;
p.ay = 0;
p.rotation = 0;
p.angularVelocity = 0;
p.scaleX = 1;
p.scaleY = 1;
p.r = 1;
p.g = 1;
p.b = 1;
p.alpha = 1;
p.age = 0;
p.lifetime = 1;
p.startScaleX = 1;
p.startScaleY = 1;
p.startR = 1;
p.startG = 1;
p.startB = 1;
p.startAlpha = 1;
p.userData = undefined;
}
/**
*
* Particle pool for efficient memory management
*/
export class ParticlePool {
private _particles: Particle[] = [];
private _capacity: number;
private _activeCount: number = 0;
constructor(capacity: number) {
this._capacity = capacity;
for (let i = 0; i < capacity; i++) {
this._particles.push(createParticle());
}
}
/** 池容量 | Pool capacity */
get capacity(): number {
return this._capacity;
}
/** 活跃粒子数 | Active particle count */
get activeCount(): number {
return this._activeCount;
}
/** 所有粒子(包括不活跃的)| All particles (including inactive) */
get particles(): readonly Particle[] {
return this._particles;
}
/**
*
* Get an inactive particle
*/
spawn(): Particle | null {
for (const p of this._particles) {
if (!p.alive) {
p.alive = true;
this._activeCount++;
return p;
}
}
return null;
}
/**
*
* Recycle a particle
*/
recycle(p: Particle): void {
if (p.alive) {
p.alive = false;
this._activeCount--;
}
}
/**
*
* Recycle all particles
*/
recycleAll(): void {
for (const p of this._particles) {
p.alive = false;
}
this._activeCount = 0;
}
/**
*
* Iterate over active particles
*/
forEachActive(callback: (p: Particle, index: number) => void): void {
let index = 0;
for (const p of this._particles) {
if (p.alive) {
callback(p, index++);
}
}
}
/**
*
* Resize the pool
*/
resize(newCapacity: number): void {
if (newCapacity > this._capacity) {
for (let i = this._capacity; i < newCapacity; i++) {
this._particles.push(createParticle());
}
} else if (newCapacity < this._capacity) {
// 回收超出容量的活跃粒子 | Recycle active particles beyond capacity
for (let i = newCapacity; i < this._capacity; i++) {
if (this._particles[i].alive) {
this._activeCount--;
}
}
this._particles.length = newCapacity;
}
this._capacity = newCapacity;
}
}