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
194 lines (166 loc) · 6.44 KB
/
system.js
File metadata and controls
194 lines (166 loc) · 6.44 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
pc.extend(pc, function () {
var _schema = [
'enabled',
'clearColorBuffer',
'clearColor',
'clearDepthBuffer',
'clearStencilBuffer',
'frustumCulling',
'projection',
'fov',
'orthoHeight',
'nearClip',
'farClip',
'priority',
'rect',
'scissorRect',
'camera',
'aspectRatio',
'horizontalFov',
'model',
'renderTarget',
'calculateTransform',
'calculateProjection',
'cullFaces',
'flipFaces'
];
/**
* @name pc.CameraComponentSystem
* @class Used to add and remove {@link pc.CameraComponent}s from Entities. It also holds an
* array of all active cameras.
* @description Create a new CameraComponentSystem
* @param {pc.Application} app The Application
*
* @property {pc.CameraComponent[]} cameras Holds all the active camera components
* @extends pc.ComponentSystem
*/
var CameraComponentSystem = function (app) {
this.id = 'camera';
this.description = "Renders the scene from the location of the Entity.";
app.systems.add(this.id, this);
this.ComponentType = pc.CameraComponent;
this.DataType = pc.CameraComponentData;
this.schema = _schema;
// holds all the active camera components
this.cameras = [ ];
this.on('beforeremove', this.onBeforeRemove, this);
this.on('remove', this.onRemove, this);
pc.ComponentSystem.on('update', this.onUpdate, this);
};
CameraComponentSystem = pc.inherits(CameraComponentSystem, pc.ComponentSystem);
pc.Component._buildAccessors(pc.CameraComponent.prototype, _schema);
pc.extend(CameraComponentSystem.prototype, {
initializeComponentData: function (component, _data, properties) {
properties = [
'postEffects',
'enabled',
'model',
'camera',
'aspectRatio',
'horizontalFov',
'renderTarget',
'clearColor',
'fov',
'orthoHeight',
'nearClip',
'farClip',
'projection',
'priority',
'clearColorBuffer',
'clearDepthBuffer',
'clearStencilBuffer',
'frustumCulling',
'rect',
'scissorRect',
'calculateTransform',
'calculateProjection',
'cullFaces',
'flipFaces'
];
// duplicate data because we're modifying the data
var data = {};
properties.forEach(function (prop) {
data[prop] = _data[prop];
});
if (data.clearColor && pc.type(data.clearColor) === 'array') {
var c = data.clearColor;
data.clearColor = new pc.Color(c[0], c[1], c[2], c[3]);
}
if (data.rect && pc.type(data.rect) === 'array') {
var rect = data.rect;
data.rect = new pc.Vec4(rect[0], rect[1], rect[2], rect[3]);
}
if (data.scissorRect && pc.type(data.scissorRect) === 'array') {
var scissorRect = data.scissorRect;
data.scissorRect = new pc.Vec4(scissorRect[0], scissorRect[1], scissorRect[2], scissorRect[3]);
}
if (data.activate) {
console.warn("WARNING: activate: Property is deprecated. Set enabled property instead.");
data.enabled = data.activate;
}
data.camera = new pc.Camera();
data._node = component.entity;
var self = component;
data.camera.calculateTransform = function(mat, mode) {
if (!self._calculateTransform)
return null;
return self._calculateTransform(mat, mode);
};
data.camera.calculateProjection = function(mat, mode) {
if (!self._calculateProjection)
return null;
return self._calculateProjection(mat, mode);
};
data.postEffects = new pc.PostEffectQueue(this.app, component);
CameraComponentSystem._super.initializeComponentData.call(this, component, data, properties);
},
onBeforeRemove: function (entity, component) {
this.removeCamera(component);
},
onRemove: function (entity, data) {
data.camera = null;
},
onUpdate: function (dt) {
var components = this.store;
var component, componentData, cam, vrDisplay;
if (this.app.vr) {
for (var id in components) {
component = components[id];
componentData = component.data;
cam = componentData.camera;
vrDisplay = cam.vrDisplay;
if (componentData.enabled && component.entity.enabled && vrDisplay) {
// Change WebVR near/far planes based on the stereo camera
vrDisplay.setClipPlanes(cam._nearClip, cam._farClip);
// update camera node transform from VrDisplay
if (cam._node) {
cam._node.localTransform.copy(vrDisplay.combinedViewInv);
cam._node._dirtyLocal = false;
cam._node._dirtify();
// cam._node.syncHierarchy();
}
}
}
}
},
addCamera: function (camera) {
this.cameras.push(camera);
this.sortCamerasByPriority();
},
removeCamera: function (camera) {
var index = this.cameras.indexOf(camera);
if (index >= 0) {
this.cameras.splice(index, 1);
this.sortCamerasByPriority();
}
},
sortCamerasByPriority: function () {
this.cameras.sort(function (a, b) {
return a.priority - b.priority;
});
}
});
return {
CameraComponentSystem: CameraComponentSystem
};
}());