-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTurbulenceField.ts
83 lines (74 loc) · 2.84 KB
/
TurbulenceField.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {Behavior} from './Behavior';
import {Particle} from '../Particle';
import {Vector3} from '../math';
import SimplexNoise from '../util/SimplexNoise';
/**
* Apply turbulence to particles.
*/
export class TurbulenceField implements Behavior {
type = 'TurbulenceField';
generator = new SimplexNoise();
timeOffset = new Vector3();
temp = new Vector3();
temp2 = new Vector3();
constructor(
public scale: Vector3,
public octaves: number,
public velocityMultiplier: Vector3,
public timeScale: Vector3
) {
this.timeOffset.x = (Math.random() / this.scale.x) * this.timeScale.x;
this.timeOffset.y = (Math.random() / this.scale.y) * this.timeScale.y;
this.timeOffset.z = (Math.random() / this.scale.z) * this.timeScale.z;
}
initialize(particle: Particle): void {}
update(particle: Particle, delta: number): void {
const x = particle.position.x / this.scale.x;
const y = particle.position.y / this.scale.y;
const z = particle.position.z / this.scale.z;
this.temp.set(0, 0, 0);
let lvl = 1;
for (let i = 0; i < this.octaves; i++) {
this.temp2.set(
this.generator.noise4D(x * lvl, y * lvl, z * lvl, this.timeOffset.x * lvl) / lvl,
this.generator.noise4D(x * lvl, y * lvl, z * lvl, this.timeOffset.y * lvl) / lvl,
this.generator.noise4D(x * lvl, y * lvl, z * lvl, this.timeOffset.z * lvl) / lvl
);
this.temp.add(this.temp2);
lvl *= 2;
}
this.temp.multiply(this.velocityMultiplier);
particle.velocity.addScaledVector(this.temp, delta);
}
toJSON(): any {
return {
type: this.type,
scale: [this.scale.x, this.scale.y, this.scale.z],
octaves: this.octaves,
velocityMultiplier: [this.velocityMultiplier.x, this.velocityMultiplier.y, this.velocityMultiplier.z],
timeScale: [this.timeScale.x, this.timeScale.y, this.timeScale.z],
};
}
frameUpdate(delta: number): void {
this.timeOffset.x += delta * this.timeScale.x;
this.timeOffset.y += delta * this.timeScale.y;
this.timeOffset.z += delta * this.timeScale.z;
}
static fromJSON(json: any): Behavior {
return new TurbulenceField(
new Vector3(json.scale[0], json.scale[1], json.scale[2]),
json.octaves,
new Vector3(json.velocityMultiplier[0], json.velocityMultiplier[1], json.velocityMultiplier[2]),
new Vector3(json.timeScale[0], json.timeScale[1], json.timeScale[2])
);
}
clone(): Behavior {
return new TurbulenceField(
this.scale.clone(),
this.octaves,
this.velocityMultiplier.clone(),
this.timeScale.clone()
);
}
reset(): void {}
}