Files
esengine/packages/particle/src/modules/NoiseModule.ts
YHH 32d35ef2ee 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

101 lines
3.1 KiB
TypeScript

import type { Particle } from '../Particle';
import type { IParticleModule } from './IParticleModule';
/**
* 值噪声哈希函数
* Value noise hash function
*
* 使用经典的整数哈希算法生成伪随机值
* Uses classic integer hash algorithm to generate pseudo-random values
*/
export function noiseHash(x: number, y: number): number {
const n = x + y * 57;
const shifted = (n << 13) ^ n;
return ((shifted * (shifted * shifted * 15731 + 789221) + 1376312589) & 0x7fffffff) / 0x7fffffff;
}
/**
* 2D 值噪声函数
* 2D value noise function
*
* 基于双线性插值的简化值噪声实现,返回 [-1, 1] 范围的值
* Simplified value noise using bilinear interpolation, returns value in [-1, 1] range
*/
export function valueNoise2D(x: number, y: number): number {
const ix = Math.floor(x);
const iy = Math.floor(y);
const fx = x - ix;
const fy = y - iy;
const n00 = noiseHash(ix, iy);
const n10 = noiseHash(ix + 1, iy);
const n01 = noiseHash(ix, iy + 1);
const n11 = noiseHash(ix + 1, iy + 1);
// 双线性插值 | Bilinear interpolation
const nx0 = n00 + (n10 - n00) * fx;
const nx1 = n01 + (n11 - n01) * fx;
return (nx0 + (nx1 - nx0) * fy) * 2 - 1;
}
/**
* 噪声模块 - 添加随机扰动
* Noise module - adds random perturbation
*/
export class NoiseModule implements IParticleModule {
readonly name = 'Noise';
enabled = true;
/** 位置噪声强度 | Position noise strength */
positionAmount: number = 0;
/** 速度噪声强度 | Velocity noise strength */
velocityAmount: number = 0;
/** 旋转噪声强度 | Rotation noise strength */
rotationAmount: number = 0;
/** 缩放噪声强度 | Scale noise strength */
scaleAmount: number = 0;
/** 噪声频率 | Noise frequency */
frequency: number = 1;
/** 噪声滚动速度 | Noise scroll speed */
scrollSpeed: number = 1;
private _time: number = 0;
update(p: Particle, dt: number, _normalizedAge: number): void {
this._time += dt * this.scrollSpeed;
// 基于粒子位置和时间的噪声 | Noise based on particle position and time
const noiseX = valueNoise2D(p.x * this.frequency + this._time, p.y * this.frequency);
const noiseY = valueNoise2D(p.x * this.frequency, p.y * this.frequency + this._time);
// 位置噪声 | Position noise
if (this.positionAmount !== 0) {
p.x += noiseX * this.positionAmount * dt;
p.y += noiseY * this.positionAmount * dt;
}
// 速度噪声 | Velocity noise
if (this.velocityAmount !== 0) {
p.vx += noiseX * this.velocityAmount * dt;
p.vy += noiseY * this.velocityAmount * dt;
}
// 旋转噪声 | Rotation noise
if (this.rotationAmount !== 0) {
p.rotation += noiseX * this.rotationAmount * dt;
}
// 缩放噪声 | Scale noise
if (this.scaleAmount !== 0) {
const scaleDelta = noiseX * this.scaleAmount * dt;
p.scaleX += scaleDelta;
p.scaleY += scaleDelta;
}
}
}