forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
475 lines (428 loc) · 15.6 KB
/
Copy pathcamera.js
File metadata and controls
475 lines (428 loc) · 15.6 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
Object.assign(pc, function () {
// pre-allocated temp variables
var _deviceCoord = new pc.Vec3();
var _far = new pc.Vec3();
var _farW = new pc.Vec3();
var _invViewProjMat = new pc.Mat4();
/**
* @private
* @constructor
* @name pc.Camera
* @classdesc A camera.
*/
var Camera = function () {
this._projection = pc.PROJECTION_PERSPECTIVE;
this._nearClip = 0.1;
this._farClip = 10000;
this._shaderParams = new pc.Vec4();
this._fov = 45;
this._orthoHeight = 10;
this._aspect = 16 / 9;
this._aspectRatioMode = pc.ASPECT_AUTO;
this._horizontalFov = false;
this.frustumCulling = false;
this.cullingMask = 0xFFFFFFFF;
this._renderDepthRequests = 0;
this._projMatDirty = true;
this._projMat = new pc.Mat4();
this._viewMat = new pc.Mat4();
this._viewProjMat = new pc.Mat4();
this.vrDisplay = null;
this._rect = {
x: 0,
y: 0,
width: 1,
height: 1
};
this._scissorRect = {
x: 0,
y: 0,
width: 1,
height: 1
};
this.frustum = new pc.Frustum(this._projMat, this._viewMat);
// Create a full size viewport onto the backbuffer
this.renderTarget = null;
this._depthTarget = null;
// Create the clear options
this._clearOptions = {
color: [0.5, 0.5, 0.5, 1.0],
depth: 1.0,
stencil: 0,
flags: pc.CLEARFLAG_COLOR | pc.CLEARFLAG_DEPTH | pc.CLEARFLAG_STENCIL
};
this._node = null;
this.calculateTransform = null;
this.overrideCalculateTransform = false;
this.calculateProjection = null;
this.overrideCalculateProjection = false;
this._cullFaces = true;
this._flipFaces = false;
this._component = null;
};
Object.assign(Camera.prototype, {
/**
* @private
* @function
* @name pc.Camera#clone
* @description Duplicates a camera node but does not 'deep copy' the hierarchy.
* @returns {pc.Camera} A cloned Camera.
*/
clone: function () {
var clone = new pc.Camera();
clone.projection = this._projection;
clone.nearClip = this._nearClip;
clone.farClip = this._farClip;
clone._shaderParams = this._shaderParams.clone();
clone.fov = this._fov;
clone.aspectRatio = this._aspect;
clone._aspectRatioMode = this._aspectRatioMode;
clone.renderTarget = this.renderTarget;
clone.setClearOptions(this.getClearOptions());
clone.frustumCulling = this.frustumCulling;
clone.cullingMask = this.cullingMask;
return clone;
},
/**
* @private
* @function
* @name pc.Camera#worldToScreen
* @description Convert a point from 3D world space to 2D canvas pixel space.
* @param {pc.Vec3} worldCoord The world space coordinate to transform.
* @param {Number} cw The width of PlayCanvas' canvas element.
* @param {Number} ch The height of PlayCanvas' canvas element.
* @param {pc.Vec3} [screenCoord] 3D vector to receive screen coordinate result.
* @returns {pc.Vec3} The screen space coordinate.
*/
worldToScreen: function (worldCoord, cw, ch, screenCoord) {
if (screenCoord === undefined) {
screenCoord = new pc.Vec3();
}
var projMat = this.getProjectionMatrix();
var wtm = this._node.getWorldTransform();
this._viewMat.copy(wtm).invert();
this._viewProjMat.mul2(projMat, this._viewMat);
this._viewProjMat.transformPoint(worldCoord, screenCoord);
// calculate w co-coord
var wp = worldCoord.data;
var vpm = this._viewProjMat.data;
var w = wp[0] * vpm[3] +
wp[1] * vpm[7] +
wp[2] * vpm[11] +
1 * vpm[15];
screenCoord.x = (screenCoord.x / w + 1) * 0.5 * cw;
screenCoord.y = (1 - screenCoord.y / w) * 0.5 * ch;
return screenCoord;
},
/**
* @private
* @function
* @name pc.Camera#screenToWorld
* @description Convert a point from 2D canvas pixel space to 3D world space.
* @param {Number} x x coordinate on PlayCanvas' canvas element.
* @param {Number} y y coordinate on PlayCanvas' canvas element.
* @param {Number} z The distance from the camera in world space to create the new point.
* @param {Number} cw The width of PlayCanvas' canvas element.
* @param {Number} ch The height of PlayCanvas' canvas element.
* @param {pc.Vec3} [worldCoord] 3D vector to receive world coordinate result.
* @returns {pc.Vec3} The world space coordinate.
*/
screenToWorld: function (x, y, z, cw, ch, worldCoord) {
if (worldCoord === undefined) {
worldCoord = new pc.Vec3();
}
var projMat = this.getProjectionMatrix();
var wtm = this._node.getWorldTransform();
this._viewMat.copy(wtm).invert();
this._viewProjMat.mul2(projMat, this._viewMat);
_invViewProjMat.copy(this._viewProjMat).invert();
if (this._projection === pc.PROJECTION_PERSPECTIVE) {
// Calculate the screen click as a point on the far plane of the
// normalized device coordinate 'box' (z=1)
_far.set(x / cw * 2 - 1, (ch - y) / ch * 2 - 1, 1);
// Transform to world space
_invViewProjMat.transformPoint(_far, _farW);
var w = _far.x * _invViewProjMat.data[3] +
_far.y * _invViewProjMat.data[7] +
_far.z * _invViewProjMat.data[11] +
_invViewProjMat.data[15];
_farW.scale(1 / w);
var alpha = z / this._farClip;
worldCoord.lerp(this._node.getPosition(), _farW, alpha);
} else {
// Calculate the screen click as a point on the far plane of the
// normalized device coordinate 'box' (z=1)
var range = this._farClip - this._nearClip;
_deviceCoord.set(x / cw, (ch - y) / ch, z / range);
_deviceCoord.scale(2);
_deviceCoord.sub(pc.Vec3.ONE);
// Transform to world space
_invViewProjMat.transformPoint(_deviceCoord, worldCoord);
}
return worldCoord;
},
/**
* @private
* @function
* @name pc.Camera#getClearOptions
* @description Retrieves the options used to determine how the camera's render target will be cleared.
* @returns {Object} The options determining the behaviour of render target clears.
*/
getClearOptions: function () {
return this._clearOptions;
},
/**
* @private
* @function
* @name pc.Camera#getProjectionMatrix
* @description Retrieves the projection matrix for the specified camera.
* @returns {pc.Mat4} The camera's projection matrix.
*/
getProjectionMatrix: function () {
if (this._projMatDirty) {
if (this._projection === pc.PROJECTION_PERSPECTIVE) {
this._projMat.setPerspective(this._fov, this._aspect, this._nearClip, this._farClip, this._horizontalFov);
} else {
var y = this._orthoHeight;
var x = y * this._aspect;
this._projMat.setOrtho(-x, x, -y, y, this._nearClip, this._farClip);
}
var n = this._nearClip;
var f = this._farClip;
this._shaderParams.x = 1 / f;
this._shaderParams.y = f;
this._shaderParams.z = (1 - f / n) / 2;
this._shaderParams.w = (1 + f / n) / 2;
this._projMatDirty = false;
}
return this._projMat;
},
getRect: function () {
return this._rect;
},
/**
* @private
* @function
* @name pc.Camera#setClearOptions
* @description Sets the options used to determine how the camera's render target will be cleared.
* @param {Object} options The options determining the behaviour of subsequent render target clears.
* @param {Number[]} options.color The options determining the behaviour of subsequent render target clears.
* @param {Number} options.depth The options determining the behaviour of subsequent render target clears.
* @param {pc.CLEARFLAG} options.flags The options determining the behaviour of subsequent render target clears.
*/
setClearOptions: function (options) {
this._clearOptions.color[0] = options.color[0];
this._clearOptions.color[1] = options.color[1];
this._clearOptions.color[2] = options.color[2];
this._clearOptions.color[3] = options.color[3];
this._clearOptions.depth = options.depth;
this._clearOptions.stencil = options.stencil;
this._clearOptions.flags = options.flags;
},
setRect: function (x, y, width, height) {
this._rect.x = x;
this._rect.y = y;
this._rect.width = width;
this._rect.height = height;
},
setScissorRect: function (x, y, width, height) {
this._scissorRect.x = x;
this._scissorRect.y = y;
this._scissorRect.width = width;
this._scissorRect.height = height;
},
requestDepthMap: function () {
this._renderDepthRequests++;
},
releaseDepthMap: function () {
this._renderDepthRequests--;
}
});
/**
* @private
* @type Number
* @name pc.Camera#aspectRatio
* @description Camera's aspect ratio.
*/
Object.defineProperty(Camera.prototype, 'aspectRatio', {
get: function () {
return this._aspect;
},
set: function (v) {
if (this._aspect !== v) {
this._aspect = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Number
* @name pc.Camera#projection
* @description Camera's projection type, to specify whether projection is orthographic (parallel projection) or perspective. Can be:
* <ul>
* <li>{@link pc.PROJECTION_PERSPECTIVE}</li>
* <li>{@link pc.PROJECTION_ORTHOGRAPHIC}</li>
* </ul>
*/
Object.defineProperty(Camera.prototype, 'projection', {
get: function () {
return this._projection;
},
set: function (v) {
if (this._projection !== v) {
this._projection = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Number
* @name pc.Camera#nearClip
* @description Camera's distance to near clipping plane
*/
Object.defineProperty(Camera.prototype, 'nearClip', {
get: function () {
return this._nearClip;
},
set: function (v) {
if (this._nearClip !== v) {
this._nearClip = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Number
* @name pc.Camera#farClip
* @description Camera's distance to far clipping plane
*/
Object.defineProperty(Camera.prototype, 'farClip', {
get: function () {
return this._farClip;
},
set: function (v) {
if (this._farClip !== v) {
this._farClip = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Number
* @name pc.Camera#fov
* @description Camera's field of view in degrees. This angle is in degrees
* and is measured vertically or horizontally between the sides of camera planes.
* hirozontalFov property defines the fov axis - vertical or horizontal.
*/
Object.defineProperty(Camera.prototype, 'fov', {
get: function () {
return this._fov;
},
set: function (v) {
if (this._fov !== v) {
this._fov = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Boolean
* @name pc.Camera#horizontalFov
* @description Camera's horizontal or vertical field of view.
*/
Object.defineProperty(Camera.prototype, 'horizontalFov', {
get: function () {
return this._horizontalFov;
},
set: function (v) {
if (this._horizontalFov !== v) {
this._horizontalFov = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Number
* @name pc.Camera#orthoHeight
* @description Camera's half height of the orthographics view.
*/
Object.defineProperty(Camera.prototype, 'orthoHeight', {
get: function () {
return this._orthoHeight;
},
set: function (v) {
if (this._orthoHeight !== v) {
this._orthoHeight = v;
this._projMatDirty = true;
}
}
});
/**
* @private
* @type Array
* @name pc.Camera#clearColor
* @description Camera's clear color.
*/
Object.defineProperty(Camera.prototype, 'clearColor', {
get: function () {
return this._clearOptions.color;
},
set: function (v) {
this._clearOptions.color[0] = v[0];
this._clearOptions.color[1] = v[1];
this._clearOptions.color[2] = v[2];
this._clearOptions.color[3] = v[3];
}
});
/**
* @private
* @type Number
* @name pc.Camera#clearDepth
* @description Camera's clear depth value.
*/
Object.defineProperty(Camera.prototype, 'clearDepth', {
get: function () {
return this._clearOptions.depth;
},
set: function (v) {
this._clearOptions.depth = v;
}
});
/**
* @private
* @type Number
* @name pc.Camera#clearStencil
* @description Camera's clear stencil value.
*/
Object.defineProperty(Camera.prototype, 'clearStencil', {
get: function () {
return this._clearOptions.stencil;
},
set: function (v) {
this._clearOptions.stencil = v;
}
});
/**
* @private
* @type Number
* @name pc.Camera#clearFlags
* @description Camera's clear flags bits value.
*/
Object.defineProperty(Camera.prototype, 'clearFlags', {
get: function () {
return this._clearOptions.flags;
},
set: function (v) {
this._clearOptions.flags = v;
}
});
return {
Camera: Camera
};
}());