| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- export default class SmokePartical1 {
- range; // 粒子的分布半径
- center; // 粒子的分布中心
- life; // 粒子的存活时间,毫秒
- createTime; // 粒子创建时间
- updateTime; // 上次更新时间
- size; // 粒子大小
- opacityFactor; // 粒子透系数
- opacity; //粒子透明度
- scale; // 粒子缩放量
- scaleFactor; //粒子放大系数
- position; // 粒子位置
- speed; //粒子扩散速度
- constructor(range = 10, center = { x: 0, y: 0, z: 0 }) {
- this.range = range; // 粒子的分布半径
- this.center = center; // 粒子的分布中心
- this.life = 400; // 粒子的存活时间,毫秒
- this.createTime = Date.now(); // 粒子创建时间
- this.updateTime = Date.now(); // 上次更新时间
- this.size = 3; // 粒子大小
- // 粒子透明度,及系数
- this.opacityFactor = 0.8;
- this.opacity = 1 * this.opacityFactor;
- // 粒子放大量,及放大系数
- this.scaleFactor = 10.5;
- this.scale = (this.scaleFactor * (this.updateTime - this.createTime)) / this.life; // 初始1,到达生命周期时为3
- // 粒子位置
- this.position = {
- x: Math.random() * 2 * this.range + this.center.x - this.range,
- y: this.center.y,
- z: Math.random() * 2 * this.range + this.center.z - this.range,
- };
- // 水平方向的扩散
- let speedAround = Math.random() * 10;
- if (speedAround < 5) speedAround -= 5;
- if (speedAround > 5) speedAround += 1;
- // 粒子的扩散速度
- this.speed = {
- x: speedAround,
- y: -this.life * 14,
- z: speedAround,
- };
- }
- // 更新粒子
- update() {
- const now = Date.now();
- const time = now - this.updateTime;
- // 更新位置
- this.position.x += (this.speed.x * time) / 2000;
- this.position.y += (this.speed.y * time) / 2000;
- this.position.z += (this.speed.z * time) / 2000;
- // 计算粒子透明度
- this.opacity = 1 - (now - this.createTime) / this.life;
- this.opacity *= this.opacityFactor;
- if (this.opacity < 0) this.opacity = 0;
- // 计算放大量
- this.scale = this.scaleFactor * ((now - this.createTime) / this.life) + 0.01;
- if (this.scale > 0.01 + this.scaleFactor) this.scale = 0.01 + this.scaleFactor;
- // 重置更新时间
- this.updateTime = now;
- }
- }
|