-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeParticle.ts
62 lines (56 loc) · 1.44 KB
/
ThreeParticle.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
import {
Object3D,
Sprite,
SpriteMaterial,
Texture,
TextureLoader,
} from "three";
import { Particle } from "@masatomakino/particle-waypoint";
export class ThreeParticle extends Particle {
protected sprite: Sprite;
protected static loader: TextureLoader;
protected parent: Object3D;
init(parent: Object3D, texturePath: string, scale: number): Promise<Sprite> {
if (ThreeParticle.loader == null) {
ThreeParticle.loader = new TextureLoader();
}
this.parent = parent;
return new Promise((resolve, reject) => {
const map = ThreeParticle.loader.load(
texturePath,
(texture: Texture) => {
this.sprite?.scale.set(
texture.image.width * scale,
texture.image.height * scale,
1.0
);
return resolve(this.sprite);
},
undefined,
(err) => {
return reject(err);
}
);
const mat = new SpriteMaterial({
map: map,
depthTest: false,
});
this.sprite = new Sprite(mat);
this.parent.add(this.sprite);
});
}
update(t: number): number {
const n = super.update(t);
const pos = this.path.getPoint(n);
this.sprite.position.set(pos[0], pos[1], pos[2]);
return n;
}
dispose(): void {
super.dispose();
if (this.parent && this.sprite.parent) {
this.sprite.parent.remove(this.sprite);
}
this.parent = null;
this.sprite = null;
}
}