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

246 lines
6.3 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 = {};
}
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-01-02 17:05:40 +01:00
apiCamera.quaternion,
2017-01-09 15:20:48 +01:00
apiCamera.parentEntity,
apiCamera.eyeSeparation,
apiCamera.focalLength
);
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,
GameLib.Component.COMPONENT_CAMERA
);
2016-11-18 16:00:13 +01:00
2017-06-16 15:49:53 +02: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) {
2017-06-16 15:49:53 +02:00
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
2016-11-18 16:00:13 +01:00
var instance = null;
if (update) {
instance = this.instance;
}
2016-12-02 16:03:03 +01:00
if (!instance) {
2017-06-16 15:49:53 +02: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-06-16 15:49:53 +02: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.maxY,
this.minY,
2016-11-18 16:00:13 +01:00
this.minZ,
this.maxZ
);
2017-01-10 17:04:30 +01:00
} else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
2017-01-19 17:50:11 +01:00
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.left = this.minX;
instance.right = this.maxX;
instance.bottom = this.minY;
instance.top = this.maxY;
instance.near = this.minZ;
instance.far = this.maxZ;
2017-01-10 17:04:30 +01:00
}
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
}
2017-01-19 17:50:11 +01:00
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;
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
};
/**
* 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-05-16 14:51:57 +02:00
this.quaternion.toApiObject(),
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
*/
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
);
};