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
173 lines (143 loc) · 6.26 KB
/
system.js
File metadata and controls
173 lines (143 loc) · 6.26 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
pc.extend(pc, function () {
'use strict';
var _schema = [ 'enabled' ];
// TODO: remove this once sprites are deployed
var warningShown = false;
/**
* @private
* @name pc.SpriteComponentSystem
* @class Manages creation of {@link pc.SpriteComponent}s.
* @param {pc.Application} app The application
* @extends pc.ComponentSystem
*/
var SpriteComponentSystem = function SpriteComponentSystem(app) {
this.id = 'sprite';
this.app = app;
app.systems.add(this.id, this);
this.ComponentType = pc.SpriteComponent;
this.DataType = pc.SpriteComponentData;
this.schema = _schema;
// default texture - make white so we can tint it with emissive color
this._defaultTexture = new pc.Texture(app.graphicsDevice, {width:1, height:1, format:pc.PIXELFORMAT_R8_G8_B8_A8});
var pixels = this._defaultTexture.lock();
var pixelData = new Uint8Array(4);
pixelData[0] = 255.0;
pixelData[1] = 255.0;
pixelData[2] = 255.0;
pixelData[3] = 255.0;
pixels.set(pixelData);
this._defaultTexture.unlock();
// default material used by sprites
this.defaultMaterial = new pc.StandardMaterial();
this.defaultMaterial.diffuse = new pc.Color(0,0,0,1); // black diffuse color to prevent ambient light being included
this.defaultMaterial.emissive = new pc.Color(0.5,0.5,0.5,1); // use non-white to compile shader correctly
this.defaultMaterial.emissiveMap = this._defaultTexture;
this.defaultMaterial.emissiveMapTint = true;
this.defaultMaterial.opacityMap = this._defaultTexture;
this.defaultMaterial.opacityMapChannel = "a";
this.defaultMaterial.opacityTint = true;
this.defaultMaterial.opacity = 0; // use non-1 opacity to compile shader correctly
this.defaultMaterial.useLighting = false;
this.defaultMaterial.useGammaTonemap = false;
this.defaultMaterial.useFog = false;
this.defaultMaterial.useSkybox = false;
this.defaultMaterial.blendType = pc.BLEND_PREMULTIPLIED;
this.defaultMaterial.depthWrite = false;
this.defaultMaterial.pixelSnap = false;
this.defaultMaterial.cull = pc.CULLFACE_NONE; // don't cull because we might flipX or flipY which uses negative scale on the graph node
this.defaultMaterial.update();
pc.ComponentSystem.on('update', this.onUpdate, this);
this.on('beforeremove', this.onBeforeRemove, this);
};
SpriteComponentSystem = pc.inherits(SpriteComponentSystem, pc.ComponentSystem);
pc.Component._buildAccessors(pc.SpriteComponent.prototype, _schema);
pc.extend(SpriteComponentSystem.prototype, {
initializeComponentData: function (component, data, properties) {
if (data.enabled !== undefined) {
component.enabled = data.enabled;
}
component.type = data.type;
if (data.color !== undefined) {
if (data.color instanceof pc.Color) {
component.color.set(data.color.data[0], data.color.data[1], data.color.data[2], 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);
}
// force update
component.color = component.color;
}
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.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;
SpriteComponentSystem._super.initializeComponentData.call(this, component, data, properties);
if (! warningShown) {
console.warn('The Sprite component is in beta and might change without notice.');
warningShown = true;
}
},
cloneComponent: function (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,
batchGroupId: source.batchGroupId
});
},
onUpdate: function (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: function (entity, component) {
component.onDestroy();
}
});
return {
SpriteComponentSystem: SpriteComponentSystem
};
}());