r3-legacy/src/game-lib-d3-camera.js

206 lines
5.1 KiB
JavaScript
Raw Normal View History

2016-11-14 14:48:37 +01:00
/**
* Creates a camera object
2016-11-18 16:00:13 +01:00
* @param graphics GameLib.D3.Graphics
2016-11-29 12:54:25 +01:00
* @param apiCamera GameLib.D3.API.Camera
2016-11-14 14:48:37 +01:00
* @constructor
*/
2017-01-09 15:20:48 +01:00
GameLib.D3.Camera = function(
2016-11-18 16:00:13 +01:00
graphics,
apiCamera
) {
2016-11-14 14:48:37 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-11-25 14:43:03 +01:00
2017-01-12 17:40:17 +01:00
if (GameLib.Utils.UndefinedOrNull(apiCamera)) {
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,
2017-01-02 17:05:40 +01:00
apiCamera.quaternion,
2017-01-09 15:20:48 +01:00
apiCamera.parentEntity,
apiCamera.eyeSeparation,
apiCamera.focalLength
);
2016-12-15 14:53:39 +01:00
this.position = new GameLib.Vector3(
2016-11-25 14:43:03 +01:00
graphics,
this.position,
this
2016-11-25 14:43:03 +01:00
);
2016-12-15 14:53:39 +01:00
this.quaternion = new GameLib.Quaternion(
graphics,
this.quaternion,
this
);
2016-12-15 14:53:39 +01:00
this.lookAt = new GameLib.Vector3(
2016-12-02 13:00:56 +01:00
graphics,
this.lookAt,
this
2016-12-02 13:00:56 +01:00
);
2016-11-25 14:43:03 +01:00
2016-11-14 14:48:37 +01:00
this.instance = this.createInstance();
2016-11-18 16:00:13 +01:00
this.needsUpdate = false;
2016-11-14 14:48:37 +01:00
} ;
GameLib.D3.Camera.prototype = Object.create(GameLib.D3.API.Camera.prototype);
GameLib.D3.Camera.prototype.constructor = GameLib.D3.Camera;
2017-01-10 17:04:30 +01:00
GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE = 0x1;
GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL = 0x2;
GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3;
2016-11-14 14:48:37 +01:00
/**
* Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.Camera}
*/
2016-11-18 16:00:13 +01:00
GameLib.D3.Camera.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
}
2016-12-02 16:03:03 +01:00
if (!instance) {
2017-01-10 17:04:30 +01:00
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) {
2017-01-09 15:20:48 +01:00
instance = new THREE.PerspectiveCamera(
2016-11-18 16:00:13 +01:00
this.fov,
this.aspect,
this.near,
this.far
);
2017-01-10 17:04:30 +01:00
} else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
2017-01-09 15:20:48 +01:00
instance = new THREE.OrthographicCamera(
2016-11-18 16:00:13 +01:00
this.minX,
this.maxX,
this.minY,
this.maxY,
this.minZ,
this.maxZ
);
2017-01-10 17:04:30 +01:00
} else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
if (!instance) {
instance = new THREE.StereoCamera();
}
2016-11-18 16:00:13 +01:00
}
}
2016-12-02 16:03:03 +01:00
if (update) {
2017-01-10 17:04:30 +01:00
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance.minX = this.minX;
instance.maxX = this.maxX;
instance.minY = this.minY;
instance.maxY = this.maxY;
instance.minZ = this.minZ;
instance.maxZ = this.maxZ;
}
2017-01-12 17:40:17 +01:00
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;
2017-01-10 17:04:30 +01:00
}
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
instance.eyeSeparation = this.eyeSeparation;
instance.focalLength = this.focalLength;
2017-01-12 17:40:17 +01:00
instance.update(instance);
2017-01-10 17:04:30 +01:00
}
2016-11-18 16:00:13 +01:00
}
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;
2016-11-18 16:00:13 +01:00
2017-01-09 15:20:48 +01:00
instance.lookAt(this.lookAt.instance);
2016-11-18 16:00:13 +01:00
instance.updateProjectionMatrix();
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Camera.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
2016-12-09 20:32:09 +01:00
};
/**
* Converts a GameLib.D3.Camera to a new GameLib.D3.API.Camera
* @returns {GameLib.D3.API.Camera}
*/
GameLib.D3.Camera.prototype.toApiCamera = function() {
return new GameLib.D3.API.Camera(
this.id,
this.cameraType,
this.name,
this.fov,
this.aspect,
this.near,
this.far,
this.position.toApiVector(),
this.lookAt.toApiVector(),
this.minX,
this.maxX,
this.minY,
this.maxY,
this.minZ,
this.maxZ,
2017-01-02 17:05:40 +01:00
this.quaternion.toApiQuaternion(),
2017-01-09 15:20:48 +01:00
GameLib.Utils.IdOrNull(this.parentEntity),
this.eyeSeparation,
this.focalLength
2016-12-09 20:32:09 +01:00
);
};
/**
* 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) {
2017-01-05 19:34:28 +01:00
var apiCamera = GameLib.D3.API.Camera.FromObjectCamera(objectCamera);
2016-12-09 20:32:09 +01:00
return new GameLib.D3.Camera(
graphics,
apiCamera
);
};