2016-11-14 14:48:37 +01:00
|
|
|
/**
|
2017-11-03 13:07:45 +01:00
|
|
|
* Creates a Camera object
|
2017-12-02 11:49:27 +01:00
|
|
|
* @param graphics GameLib.GraphicsRuntime
|
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 = {};
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:27:36 +01:00
|
|
|
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-10-05 14:35:22 +02:00
|
|
|
apiCamera.offsetX,
|
|
|
|
apiCamera.offsetY,
|
2017-01-02 17:05:40 +01:00
|
|
|
apiCamera.quaternion,
|
2017-01-09 15:20:48 +01:00
|
|
|
apiCamera.eyeSeparation,
|
2017-12-04 13:23:15 +01:00
|
|
|
apiCamera.focalLength,
|
|
|
|
apiCamera.parentEntity
|
2016-12-20 17:27:36 +01:00
|
|
|
);
|
|
|
|
|
2018-01-14 13:01:08 +01:00
|
|
|
this.position = new GameLib.Vector3(
|
|
|
|
graphics,
|
|
|
|
this.position,
|
|
|
|
this
|
|
|
|
);
|
2016-11-25 14:43:03 +01:00
|
|
|
|
2018-01-14 13:01:08 +01:00
|
|
|
this.quaternion = new GameLib.Quaternion(
|
|
|
|
graphics,
|
|
|
|
this.quaternion,
|
|
|
|
this
|
|
|
|
);
|
2016-12-08 19:43:16 +01:00
|
|
|
|
2018-01-14 13:01:08 +01:00
|
|
|
this.lookAt = new GameLib.Vector3(
|
|
|
|
graphics,
|
|
|
|
this.lookAt,
|
|
|
|
this
|
|
|
|
);
|
2016-11-25 14:43:03 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.Component.call(
|
|
|
|
this,
|
2017-12-04 13:23:15 +01:00
|
|
|
GameLib.Component.CAMERA
|
2017-06-16 15:49:53 +02:00
|
|
|
);
|
2016-11-18 16:00:13 +01:00
|
|
|
|
2016-11-14 14:48:37 +01:00
|
|
|
} ;
|
|
|
|
|
2018-01-07 12:40:16 +01:00
|
|
|
GameLib.D3.Camera.prototype = Object.create(GameLib.Component.prototype);
|
2016-12-20 17:27:36 +01:00
|
|
|
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}
|
|
|
|
*/
|
2017-06-28 17:09:06 +02:00
|
|
|
GameLib.D3.Camera.prototype.createInstance = function() {
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-28 17:09:06 +02:00
|
|
|
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) {
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = new THREE.PerspectiveCamera(
|
2017-06-28 17:09:06 +02:00
|
|
|
this.fov,
|
|
|
|
this.aspect,
|
|
|
|
this.near,
|
|
|
|
this.far
|
|
|
|
);
|
|
|
|
} else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = new THREE.OrthographicCamera(
|
2017-10-05 14:35:22 +02:00
|
|
|
this.minX + this.offsetX,
|
|
|
|
this.maxX + this.offsetX,
|
2017-06-28 17:09:06 +02:00
|
|
|
this.maxY,
|
|
|
|
this.minY,
|
|
|
|
this.minZ,
|
|
|
|
this.maxZ
|
|
|
|
);
|
|
|
|
} else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = new THREE.StereoCamera();
|
|
|
|
} else {
|
|
|
|
throw new Error('unsupported camera type : ' + this.cameraType);
|
2017-01-19 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.position.x = this.position.x;
|
|
|
|
this.instance.position.y = this.position.y;
|
|
|
|
this.instance.position.z = this.position.z;
|
2016-12-08 19:43:16 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.quaternion.x = this.quaternion.x;
|
|
|
|
this.instance.quaternion.y = this.quaternion.y;
|
|
|
|
this.instance.quaternion.z = this.quaternion.z;
|
|
|
|
this.instance.quaternion.w = this.quaternion.w;
|
2016-11-18 16:00:13 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.lookAt(this.lookAt.instance);
|
2016-11-18 16:00:13 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.updateProjectionMatrix();
|
2016-11-18 16:00:13 +01:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
GameLib.Component.prototype.createInstance.call(this);
|
2016-11-18 16:00:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the instance with the current state
|
|
|
|
*/
|
2017-11-17 20:25:56 +01:00
|
|
|
GameLib.D3.Camera.prototype.updateInstance = function(property) {
|
2017-09-27 14:38:58 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(property)) {
|
|
|
|
console.warn('no camera property specified for : ' + this.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property === 'cameraType') {
|
|
|
|
if (
|
|
|
|
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL &&
|
|
|
|
!(this.instance instanceof THREE.OrthographicCamera)
|
|
|
|
) {
|
|
|
|
this.createInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE &&
|
|
|
|
!(this.instance instanceof THREE.PerspectiveCamera)
|
|
|
|
) {
|
|
|
|
this.createInstance();
|
|
|
|
}
|
2017-09-27 14:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2017-11-17 20:25:56 +01:00
|
|
|
property === 'aspect' ||
|
|
|
|
property === 'minX' ||
|
|
|
|
property === 'maxX' ||
|
2017-11-22 16:10:58 +01:00
|
|
|
property === 'minY' ||
|
|
|
|
property === 'maxY' ||
|
2017-11-17 20:25:56 +01:00
|
|
|
property === 'minZ' ||
|
|
|
|
property === 'maxZ' ||
|
|
|
|
property === 'offsetX' ||
|
|
|
|
property === 'offsetY' ||
|
|
|
|
property === 'fov' ||
|
|
|
|
property === 'near' ||
|
|
|
|
property === 'far' ||
|
|
|
|
property === 'eyeSeparation' ||
|
|
|
|
property === 'focalLength'
|
2017-09-27 14:38:58 +02:00
|
|
|
) {
|
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
|
2017-10-05 14:35:22 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
this.minX = this.aspect * this.minY;
|
|
|
|
this.maxX = this.aspect * this.maxY;
|
2017-10-05 14:35:22 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
this.instance.left = this.minX + this.offsetX;
|
|
|
|
this.instance.right = this.maxX + this.offsetX;
|
2017-10-05 14:35:22 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
this.instance.bottom = this.minY + this.offsetY;
|
|
|
|
this.instance.top = this.maxY + this.offsetY;
|
2017-10-05 14:35:22 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
this.instance.near = this.minZ;
|
|
|
|
this.instance.far = this.maxZ;
|
|
|
|
}
|
2017-06-28 17:09:06 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (
|
|
|
|
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ||
|
|
|
|
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO
|
|
|
|
) {
|
|
|
|
this.instance.fov = this.fov;
|
|
|
|
this.instance.aspect = this.aspect;
|
|
|
|
this.instance.near = this.near;
|
|
|
|
this.instance.far = this.far;
|
|
|
|
}
|
2017-06-28 17:09:06 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
|
|
|
|
this.instance.eyeSeparation = this.eyeSeparation;
|
|
|
|
this.instance.focalLength = this.focalLength;
|
|
|
|
this.instance.update(this.instance);
|
|
|
|
}
|
2017-06-28 17:09:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (property === 'position') {
|
|
|
|
this.instance.position.x = this.position.x;
|
|
|
|
this.instance.position.y = this.position.y;
|
|
|
|
this.instance.position.z = this.position.z;
|
|
|
|
}
|
2017-06-28 17:09:06 +02:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (property === 'quaternion') {
|
|
|
|
this.instance.quaternion.x = this.quaternion.x;
|
|
|
|
this.instance.quaternion.y = this.quaternion.y;
|
|
|
|
this.instance.quaternion.z = this.quaternion.z;
|
|
|
|
this.instance.quaternion.w = this.quaternion.w;
|
|
|
|
}
|
2017-10-30 19:34:26 +01:00
|
|
|
|
2017-11-17 20:25:56 +01:00
|
|
|
if (property === 'lookAt') {
|
|
|
|
this.lookAt.instance.x = this.lookAt.x;
|
|
|
|
this.lookAt.instance.y = this.lookAt.y;
|
|
|
|
this.lookAt.instance.z = this.lookAt.z;
|
|
|
|
this.instance.lookAt(this.lookAt.instance);
|
|
|
|
}
|
2017-06-28 17:09:06 +02:00
|
|
|
|
|
|
|
this.instance.updateProjectionMatrix();
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
2017-06-01 16:58:22 +02:00
|
|
|
/**
|
|
|
|
* Resize default
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
|
|
|
GameLib.D3.Camera.prototype.resize = function(width, height) {
|
|
|
|
camera.aspect = width / height;
|
|
|
|
camera.updateInstance();
|
|
|
|
};
|
|
|
|
|
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}
|
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.D3.Camera.prototype.toApiObject = function() {
|
2016-12-09 20:32:09 +01:00
|
|
|
|
|
|
|
return new GameLib.D3.API.Camera(
|
|
|
|
this.id,
|
|
|
|
this.cameraType,
|
|
|
|
this.name,
|
|
|
|
this.fov,
|
|
|
|
this.aspect,
|
|
|
|
this.near,
|
|
|
|
this.far,
|
2017-05-16 14:51:57 +02:00
|
|
|
this.position.toApiObject(),
|
|
|
|
this.lookAt.toApiObject(),
|
2016-12-09 20:32:09 +01:00
|
|
|
this.minX,
|
|
|
|
this.maxX,
|
|
|
|
this.minY,
|
|
|
|
this.maxY,
|
|
|
|
this.minZ,
|
|
|
|
this.maxZ,
|
2017-10-05 14:35:22 +02:00
|
|
|
this.offsetX,
|
|
|
|
this.offsetY,
|
2017-05-16 14:51:57 +02:00
|
|
|
this.quaternion.toApiObject(),
|
2017-01-09 15:20:48 +01:00
|
|
|
this.eyeSeparation,
|
2017-12-04 13:23:15 +01:00
|
|
|
this.focalLength,
|
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-11-03 13:07:45 +01:00
|
|
|
* Converts from an Object Camera to a GameLib.D3.Camera
|
2017-12-02 11:49:27 +01:00
|
|
|
* @param graphics GameLib.GraphicsRuntime
|
2016-12-09 20:32:09 +01:00
|
|
|
* @param objectCamera Object
|
|
|
|
* @returns {GameLib.D3.Camera}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-06-14 14:21:57 +02:00
|
|
|
GameLib.D3.Camera.FromObject = function(graphics, objectCamera) {
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
var apiCamera = GameLib.D3.API.Camera.FromObject(objectCamera);
|
2016-12-09 20:32:09 +01:00
|
|
|
|
|
|
|
return new GameLib.D3.Camera(
|
|
|
|
graphics,
|
|
|
|
apiCamera
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|