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

295 lines
8.0 KiB
JavaScript
Raw Normal View History

2016-11-14 14:48:37 +01:00
/**
2017-11-03 13:07:45 +01:00
* Creates a Camera object
* @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 = {};
}
2017-06-16 15:49:53 +02:00
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,
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
);
2017-01-19 17:50:11 +01:00
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');
}
2016-11-25 14:43:03 +01:00
2017-01-19 17:50:11 +01:00
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');
}
2017-01-19 17:50:11 +01:00
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');
}
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
} ;
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}
*/
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;
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
*/
GameLib.D3.Camera.prototype.updateInstance = function(property) {
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();
}
}
if (
property === 'aspect' ||
property === 'minX' ||
property === 'maxX' ||
2017-11-22 16:10:58 +01:00
property === 'minY' ||
property === 'maxY' ||
property === 'minZ' ||
property === 'maxZ' ||
property === 'offsetX' ||
property === 'offsetY' ||
property === 'fov' ||
property === 'near' ||
property === 'far' ||
property === 'eyeSeparation' ||
property === 'focalLength'
) {
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
2017-10-05 14:35:22 +02:00
this.minX = this.aspect * this.minY;
this.maxX = this.aspect * this.maxY;
2017-10-05 14:35:22 +02:00
this.instance.left = this.minX + this.offsetX;
this.instance.right = this.maxX + this.offsetX;
2017-10-05 14:35:22 +02:00
this.instance.bottom = this.minY + this.offsetY;
this.instance.top = this.maxY + this.offsetY;
2017-10-05 14:35:22 +02:00
this.instance.near = this.minZ;
this.instance.far = this.maxZ;
}
2017-06-28 17:09:06 +02: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
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
}
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
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
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
};
/**
* 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
* @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
);
};