/** * Creates a camera object * @param graphics GameLib.D3.Graphics * @param apiCamera GameLib.D3.API.Camera * @constructor */ GameLib.D3.Camera = function( graphics, apiCamera ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiCamera)) { apiCamera = {}; } if (apiCamera instanceof GameLib.D3.Camera) { return apiCamera; } GameLib.D3.API.Camera.call( this, apiCamera.id, apiCamera.cameraType, apiCamera.name, apiCamera.fov, apiCamera.aspect, apiCamera.near, apiCamera.far, apiCamera.position, apiCamera.lookAt, apiCamera.minX, apiCamera.maxX, apiCamera.minY, apiCamera.maxY, apiCamera.minZ, apiCamera.maxZ, apiCamera.quaternion, apiCamera.parentEntity, apiCamera.eyeSeparation, apiCamera.focalLength ); if (this.position instanceof GameLib.API.Vector3) { this.position = new GameLib.Vector3( graphics, this.position, this ); } else { console.warn('position not instance of API.Vector3'); throw new Error('position not instance of API.Vector3'); } if (this.quaternion instanceof GameLib.API.Quaternion) { this.quaternion = new GameLib.Quaternion( graphics, this.quaternion, this ); } else { console.warn('quaternion not instance of API.Quaternion'); throw new Error('quaternion not instance of API.Quaternion'); } if (this.lookAt instanceof GameLib.API.Vector3) { this.lookAt = new GameLib.Vector3( graphics, this.lookAt, this ); } else { console.warn('lookAt not instance of API.Vector3'); throw new Error('lookAt not instance of API.Vector3'); } this.instance = this.createInstance(); this.needsUpdate = false; } ; GameLib.D3.Camera.prototype = Object.create(GameLib.D3.API.Camera.prototype); GameLib.D3.Camera.prototype.constructor = GameLib.D3.Camera; GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE = 0x1; GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL = 0x2; GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3; /** * Creates a camera instance of 'graphics' type (only THREE for now) * @returns {THREE.Camera} */ GameLib.D3.Camera.prototype.createInstance = function(update) { var instance = null; if (update) { instance = this.instance; } if (!instance) { if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) { instance = new THREE.PerspectiveCamera( this.fov, this.aspect, this.near, this.far ); } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { instance = new THREE.OrthographicCamera( this.minX, this.maxX, this.maxY, this.minY, this.minZ, this.maxZ ); } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { instance = new THREE.StereoCamera(); } } if (update) { if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { instance.left = this.minX; instance.right = this.maxX; instance.bottom = this.minY; instance.top = this.maxY; instance.near = this.minZ; instance.far = this.maxZ; } if ( this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE || this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO ) { instance.fov = this.fov; instance.aspect = this.aspect; instance.near = this.near; instance.far = this.far; } if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { instance.eyeSeparation = this.eyeSeparation; instance.focalLength = this.focalLength; instance.update(instance); } } if (!instance) { console.log('Unsupported camera type : ' + this.cameraType); throw new Error('Unsupported camera type : ' + this.cameraType); } instance.position.x = this.position.x; instance.position.y = this.position.y; instance.position.z = this.position.z; instance.quaternion.x = this.quaternion.x; instance.quaternion.y = this.quaternion.y; instance.quaternion.z = this.quaternion.z; instance.quaternion.w = this.quaternion.w; instance.lookAt(this.lookAt.instance); instance.updateProjectionMatrix(); return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Camera.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; /** * Resize default * @param width * @param height */ GameLib.D3.Camera.prototype.resize = function(width, height) { camera.aspect = width / height; camera.updateInstance(); }; /** * Converts a GameLib.D3.Camera to a new GameLib.D3.API.Camera * @returns {GameLib.D3.API.Camera} */ GameLib.D3.Camera.prototype.toApiObject = function() { return new GameLib.D3.API.Camera( this.id, this.cameraType, this.name, this.fov, this.aspect, this.near, this.far, this.position.toApiObject(), this.lookAt.toApiObject(), this.minX, this.maxX, this.minY, this.maxY, this.minZ, this.maxZ, this.quaternion.toApiObject(), GameLib.Utils.IdOrNull(this.parentEntity), this.eyeSeparation, this.focalLength ); }; /** * Converts from an Object camera to a GameLib.D3.Camera * @param graphics GameLib.D3.Graphics * @param objectCamera Object * @returns {GameLib.D3.Camera} * @constructor */ GameLib.D3.Camera.FromObjectCamera = function(graphics, objectCamera) { var apiCamera = GameLib.D3.API.Camera.FromObjectCamera(objectCamera); return new GameLib.D3.Camera( graphics, apiCamera ); };