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

243 lines
6.4 KiB
JavaScript

/**
* 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');
}
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_CAMERA
);
//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() {
var instance = null;
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 (!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() {
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
this.instance.left = this.minX;
this.instance.right = this.maxX;
this.instance.bottom = this.minY;
this.instance.top = this.maxY;
this.instance.near = this.minZ;
this.instance.far = this.maxZ;
}
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;
}
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
this.instance.eyeSeparation = this.eyeSeparation;
this.instance.focalLength = this.focalLength;
this.instance.update(this.instance);
}
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
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;
this.instance.lookAt(this.lookAt.instance);
this.instance.updateProjectionMatrix();
};
/**
* 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.FromObject = function(graphics, objectCamera) {
var apiCamera = GameLib.D3.API.Camera.FromObject(objectCamera);
return new GameLib.D3.Camera(
graphics,
apiCamera
);
};