|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../../utils/Class'); |
| 8 | +var CONST = require('../const'); |
| 9 | +var GameObject3D = require('../GameObject3D'); |
| 10 | +var Frustum = require('./Frustum'); |
| 11 | +var Vector3 = require('../../math/Vector3'); |
| 12 | +var Vector4 = require('../../math/Vector4'); |
| 13 | +var Matrix4 = require('../../math/Matrix4'); |
| 14 | + |
| 15 | +var tempMatrix = new Matrix4(); |
| 16 | + |
| 17 | +var Camera = new Class({ |
| 18 | + |
| 19 | + Extends: GameObject3D, |
| 20 | + |
| 21 | + initialize: |
| 22 | + |
| 23 | + function Camera () |
| 24 | + { |
| 25 | + GameObject3D.call(this); |
| 26 | + |
| 27 | + this.type = CONST.OBJECT_TYPE.CAMERA; |
| 28 | + |
| 29 | + /** |
| 30 | + * This is the inverse of worldMatrix. |
| 31 | + */ |
| 32 | + this.viewMatrix = new Matrix4(); |
| 33 | + |
| 34 | + /** |
| 35 | + * This is the matrix which contains the projection. |
| 36 | + */ |
| 37 | + this.projectionMatrix = new Matrix4(); |
| 38 | + |
| 39 | + /** |
| 40 | + * This is the matrix which contains the projection. |
| 41 | + */ |
| 42 | + this.projectionMatrixInverse = new Matrix4(); |
| 43 | + |
| 44 | + /** |
| 45 | + * The frustum of the camera. |
| 46 | + */ |
| 47 | + this.frustum = new Frustum(); |
| 48 | + |
| 49 | + /** |
| 50 | + * The factor of gamma. |
| 51 | + */ |
| 52 | + this.gammaFactor = 2.0; |
| 53 | + |
| 54 | + /** |
| 55 | + * Output pixel encoding. |
| 56 | + * @type {TEXEL_ENCODING_TYPE} |
| 57 | + */ |
| 58 | + this.outputEncoding = 'linear'; |
| 59 | + |
| 60 | + /** |
| 61 | + * Where on the screen is the camera rendered in normalized coordinates. |
| 62 | + * @type {Vector4} |
| 63 | + */ |
| 64 | + this.rect = new Vector4(0, 0, 1, 1); |
| 65 | + |
| 66 | + /** |
| 67 | + * When this is set, it checks every frame if objects are in the frustum of the camera before rendering objects. |
| 68 | + * Otherwise objects gets rendered every frame even if it isn't visible. |
| 69 | + */ |
| 70 | + this.frustumCulled = true; |
| 71 | + }, |
| 72 | + |
| 73 | + /** |
| 74 | + * Set view by look at, this func will set quaternion of this camera. |
| 75 | + * |
| 76 | + * @method |
| 77 | + * @param {zen3d.Vector3} target - The target that the camera look at. |
| 78 | + * @param {zen3d.Vector3} up - The up direction of the camera. |
| 79 | + */ |
| 80 | + lookAt: function (target, up) |
| 81 | + { |
| 82 | + tempMatrix.lookAtRH(this.position, target, up); |
| 83 | + |
| 84 | + this.quaternion.setFromRotationMatrix(tempMatrix); |
| 85 | + }, |
| 86 | + |
| 87 | + /** |
| 88 | + * Set orthographic projection matrix. |
| 89 | + * |
| 90 | + * @param {number} left — Camera frustum left plane. |
| 91 | + * @param {number} right — Camera frustum right plane. |
| 92 | + * @param {number} bottom — Camera frustum bottom plane. |
| 93 | + * @param {number} top — Camera frustum top plane. |
| 94 | + * @param {number} near — Camera frustum near plane. |
| 95 | + * @param {number} far — Camera frustum far plane. |
| 96 | + */ |
| 97 | + setOrtho: function (left, right, bottom, top, near, far) |
| 98 | + { |
| 99 | + this.projectionMatrix.set([ |
| 100 | + 2 / (right - left), 0, 0, -(right + left) / (right - left), |
| 101 | + 0, 2 / (top - bottom), 0, -(top + bottom) / (top - bottom), |
| 102 | + 0, 0, -2 / (far - near), -(far + near) / (far - near), |
| 103 | + 0, 0, 0, 1 |
| 104 | + ]); |
| 105 | + |
| 106 | + this.projectionMatrixInverse.getInverse(this.projectionMatrix); |
| 107 | + }, |
| 108 | + |
| 109 | + /** |
| 110 | + * Set perspective projection matrix. |
| 111 | + * @param {number} fov — Camera frustum vertical field of view. |
| 112 | + * @param {number} aspect — Camera frustum aspect ratio. |
| 113 | + * @param {number} near — Camera frustum near plane. |
| 114 | + * @param {number} far — Camera frustum far plane. |
| 115 | + */ |
| 116 | + setPerspective: function (fov, aspect, near, far) |
| 117 | + { |
| 118 | + this.projectionMatrix.set([ |
| 119 | + 1 / (aspect * Math.tan(fov / 2)), 0, 0, 0, |
| 120 | + 0, 1 / (Math.tan(fov / 2)), 0, 0, |
| 121 | + 0, 0, -(far + near) / (far - near), -2 * far * near / (far - near), |
| 122 | + 0, 0, -1, 0 |
| 123 | + ]); |
| 124 | + |
| 125 | + this.projectionMatrixInverse.getInverse(this.projectionMatrix); |
| 126 | + }, |
| 127 | + |
| 128 | + getWorldDirection: function (optionalTarget) |
| 129 | + { |
| 130 | + optionalTarget = optionalTarget || new Vector3(); |
| 131 | + |
| 132 | + var e = this.worldMatrix.val; |
| 133 | + |
| 134 | + return optionalTarget.set(-e[8], -e[9], -e[10]).normalize(); |
| 135 | + }, |
| 136 | + |
| 137 | + updateMatrix: function (force) |
| 138 | + { |
| 139 | + GameObject3D.prototype.updateMatrix.call(this, force); |
| 140 | + |
| 141 | + this.viewMatrix.getInverse(this.worldMatrix); |
| 142 | + |
| 143 | + tempMatrix.multiplyMatrices(this.projectionMatrix, this.viewMatrix); |
| 144 | + |
| 145 | + this.frustum.setFromMatrix(tempMatrix); |
| 146 | + } |
| 147 | + |
| 148 | +}); |
| 149 | + |
| 150 | +module.exports = Camera; |
0 commit comments