-
Notifications
You must be signed in to change notification settings - Fork 75
/
MeshRenderer.js
82 lines (69 loc) · 2.17 KB
/
MeshRenderer.js
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
import BaseRenderer from './BaseRenderer';
import { PUID } from '../utils';
import { Pool } from '../core';
import { RENDERER_TYPE_MESH as type } from './types';
export default class MeshRenderer extends BaseRenderer {
/**
* @param {object} container - An Object3D container, usually a THREE.Scene
* @param {object} THREE - THREE Api
*/
constructor(container, THREE) {
super(type);
this.container = container;
this._targetPool = new Pool();
this._materialPool = new Pool();
this._body = new THREE.Mesh(
new THREE.BoxGeometry(50, 50, 50),
new THREE.MeshLambertMaterial({ color: '#ff0000' })
);
}
onSystemUpdate() {}
onParticleCreated(particle) {
if (!particle.target) {
//set target
if (!particle.body) particle.body = this._body;
particle.target = this._targetPool.get(particle.body);
//set material
if (particle.useAlpha || particle.useColor) {
particle.target.material.__puid = PUID.id(particle.body.material);
particle.target.material = this._materialPool.get(
particle.target.material
);
}
}
if (particle.target) {
particle.target.position.copy(particle.position);
this.container.add(particle.target);
}
}
onParticleUpdate(particle) {
if (particle.target) {
particle.target.position.copy(particle.position);
particle.target.rotation.set(
particle.rotation.x,
particle.rotation.y,
particle.rotation.z
);
this.scale(particle);
if (particle.useAlpha) {
particle.target.material.opacity = particle.alpha;
particle.target.material.transparent = true;
}
if (particle.useColor) {
particle.target.material.color.copy(particle.color);
}
}
}
scale(particle) {
particle.target.scale.set(particle.scale, particle.scale, particle.scale);
}
onParticleDead(particle) {
if (particle.target) {
if (particle.useAlpha || particle.useColor)
this._materialPool.expire(particle.target.material);
this._targetPool.expire(particle.target);
this.container.remove(particle.target);
particle.target = null;
}
}
}