forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
257 lines (206 loc) · 8 KB
/
system.js
File metadata and controls
257 lines (206 loc) · 8 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { Color } from '../../../math/color.js';
import {
CULLFACE_NONE,
PIXELFORMAT_R8_G8_B8_A8
} from '../../../graphics/constants.js';
import { Texture } from '../../../graphics/texture.js';
import { BLEND_PREMULTIPLIED, SPRITE_RENDERMODE_SLICED, SPRITE_RENDERMODE_TILED } from '../../../scene/constants.js';
import { StandardMaterial } from '../../../scene/materials/standard-material.js';
import { Component } from '../component.js';
import { ComponentSystem } from '../system.js';
import { SpriteComponent } from './component.js';
import { SpriteComponentData } from './data.js';
const _schema = ['enabled'];
/**
* @class
* @name SpriteComponentSystem
* @augments ComponentSystem
* @classdesc Manages creation of {@link SpriteComponent}s.
* @param {Application} app - The application.
*/
class SpriteComponentSystem extends ComponentSystem {
constructor(app) {
super(app);
this.id = 'sprite';
this.ComponentType = SpriteComponent;
this.DataType = SpriteComponentData;
this.schema = _schema;
// default texture - make white so we can tint it with emissive color
this._defaultTexture = null;
// default material used by sprites
this._defaultMaterial = null;
// material used for 9-slicing in sliced mode
this._default9SlicedMaterialSlicedMode = null;
// material used for 9-slicing in tiled mode
this._default9SlicedMaterialTiledMode = null;
ComponentSystem.bind('update', this.onUpdate, this);
this.on('beforeremove', this.onBeforeRemove, this);
}
get defaultMaterial() {
if (!this._defaultMaterial) {
var texture = new Texture(this.app.graphicsDevice, {
width: 1,
height: 1,
format: PIXELFORMAT_R8_G8_B8_A8
});
var pixels = new Uint8Array(texture.lock());
pixels[0] = pixels[1] = pixels[2] = pixels[3] = 255;
texture.name = 'sprite';
texture.unlock();
var material = new StandardMaterial();
material.diffuse.set(0, 0, 0); // black diffuse color to prevent ambient light being included
material.emissive.set(0.5, 0.5, 0.5); // use non-white to compile shader correctly
material.emissiveMap = texture;
material.emissiveMapTint = true;
material.opacityMap = texture;
material.opacityMapChannel = "a";
material.opacityTint = true;
material.opacity = 0; // use non-1 opacity to compile shader correctly
material.useLighting = false;
material.useGammaTonemap = false;
material.useFog = false;
material.useSkybox = false;
material.blendType = BLEND_PREMULTIPLIED;
material.depthWrite = false;
material.pixelSnap = false;
material.cull = CULLFACE_NONE; // don't cull because we might flipX or flipY which uses negative scale on the graph node
material.update();
this._defaultTexture = texture;
this._defaultMaterial = material;
}
return this._defaultMaterial;
}
set defaultMaterial(material) {
this._defaultMaterial = material;
}
get default9SlicedMaterialSlicedMode() {
if (!this._default9SlicedMaterialSlicedMode) {
var material = this.defaultMaterial.clone();
material.nineSlicedMode = SPRITE_RENDERMODE_SLICED;
material.update();
this._default9SlicedMaterialSlicedMode = material;
}
return this._default9SlicedMaterialSlicedMode;
}
set default9SlicedMaterialSlicedMode(material) {
this._default9SlicedMaterialSlicedMode = material;
}
get default9SlicedMaterialTiledMode() {
if (!this._default9SlicedMaterialTiledMode) {
var material = this.defaultMaterial.clone();
material.nineSlicedMode = SPRITE_RENDERMODE_TILED;
material.update();
this._default9SlicedMaterialTiledMode = material;
}
return this._default9SlicedMaterialTiledMode;
}
set default9SlicedMaterialTiledMode(material) {
this._default9SlicedMaterialTiledMode = material;
}
destroy() {
if (this._defaultTexture) {
this._defaultTexture.destroy();
this._defaultTexture = null;
}
}
initializeComponentData(component, data, properties) {
if (data.enabled !== undefined) {
component.enabled = data.enabled;
}
component.type = data.type;
if (data.layers && Array.isArray(data.layers)) {
component.layers = data.layers.slice(0);
}
if (data.drawOrder !== undefined) {
component.drawOrder = data.drawOrder;
}
if (data.color !== undefined) {
if (data.color instanceof Color) {
component.color.set(data.color.r, data.color.g, data.color.b, data.opacity !== undefined ? data.opacity : 1);
} else {
component.color.set(data.color[0], data.color[1], data.color[2], data.opacity !== undefined ? data.opacity : 1);
}
/* eslint-disable no-self-assign */
// force update
component.color = component.color;
/* eslint-enable no-self-assign */
}
if (data.opacity !== undefined) {
component.opacity = data.opacity;
}
if (data.flipX !== undefined) {
component.flipX = data.flipX;
}
if (data.flipY !== undefined) {
component.flipY = data.flipY;
}
if (data.width !== undefined) {
component.width = data.width;
}
if (data.height !== undefined) {
component.height = data.height;
}
if (data.spriteAsset !== undefined) {
component.spriteAsset = data.spriteAsset;
}
if (data.sprite) {
component.sprite = data.sprite;
}
if (data.frame !== undefined) {
component.frame = data.frame;
}
if (data.clips) {
for (var name in data.clips) {
component.addClip(data.clips[name]);
}
}
if (data.speed !== undefined) {
component.speed = data.speed;
}
if (data.autoPlayClip) {
component.autoPlayClip = data.autoPlayClip;
}
component.batchGroupId = data.batchGroupId === undefined || data.batchGroupId === null ? -1 : data.batchGroupId;
super.initializeComponentData(component, data, properties);
}
cloneComponent(entity, clone) {
var source = entity.sprite;
return this.addComponent(clone, {
enabled: source.enabled,
type: source.type,
spriteAsset: source.spriteAsset,
sprite: source.sprite,
frame: source.frame,
color: source.color.clone(),
opacity: source.opacity,
flipX: source.flipX,
flipY: source.flipY,
speed: source.speed,
clips: source.clips,
autoPlayClip: source.autoPlayClip,
batchGroupId: source.batchGroupId,
drawOrder: source.drawOrder,
layers: source.layers.slice(0)
});
}
onUpdate(dt) {
var components = this.store;
for (var id in components) {
if (components.hasOwnProperty(id)) {
var component = components[id];
// if sprite component is enabled advance its current clip
if (component.data.enabled && component.entity.enabled) {
var sprite = component.entity.sprite;
if (sprite._currentClip) {
sprite._currentClip._update(dt);
}
}
}
}
}
onBeforeRemove(entity, component) {
component.onDestroy();
}
}
Component._buildAccessors(SpriteComponent.prototype, _schema);
export { SpriteComponentSystem };