2018-01-29 12:42:46 +01:00
|
|
|
/**
|
|
|
|
* GameLib.D3.Camera
|
|
|
|
* @param graphics GameLib.GraphicsRuntime
|
|
|
|
* @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 = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
GameLib.D3.API.Camera.call(
|
|
|
|
this,
|
|
|
|
apiCamera.id,
|
|
|
|
apiCamera.name,
|
|
|
|
apiCamera.cameraType,
|
|
|
|
apiCamera.aspect,
|
|
|
|
apiCamera.position,
|
|
|
|
apiCamera.lookAt,
|
|
|
|
apiCamera.parentEntity
|
|
|
|
);
|
|
|
|
|
|
|
|
this.position = new GameLib.Vector3(
|
|
|
|
graphics,
|
|
|
|
this.position,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
this.lookAt = new GameLib.Vector3(
|
|
|
|
graphics,
|
|
|
|
this.lookAt,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
var linkedObjects = {};
|
|
|
|
|
|
|
|
switch (apiCamera.cameraType) {
|
|
|
|
case GameLib.D3.API.Camera.CAMERA_TYPE_CUBE :
|
|
|
|
linkedObjects.renderTarget = GameLib.D3.RenderTarget;
|
|
|
|
break;
|
|
|
|
case GameLib.D3.API.Camera.CAMERA_TYPE_STEREO :
|
2018-01-30 15:02:47 +01:00
|
|
|
linkedObjects.main = GameLib.D3.Camera.Perspective;
|
|
|
|
linkedObjects.cameraL = GameLib.D3.Camera.Perspective;
|
|
|
|
linkedObjects.cameraR = GameLib.D3.Camera.Perspective;
|
2018-01-29 12:42:46 +01:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameLib.Component.call(
|
|
|
|
this,
|
|
|
|
linkedObjects
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Camera.prototype = Object.create(GameLib.Component.prototype);
|
|
|
|
GameLib.D3.Camera.prototype.constructor = GameLib.D3.Camera;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a camera instance of 'graphics' type (only THREE for now)
|
|
|
|
* @returns {THREE.Camera}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Camera.prototype.createInstance = function() {
|
|
|
|
|
2018-01-30 15:02:47 +01:00
|
|
|
/**
|
|
|
|
* Not all implementations of camera has a position (Stereo camera gets its position on update)
|
|
|
|
*/
|
|
|
|
// if (GameLib.Utils.Defined(this.instance.position)) {
|
|
|
|
this.instance.position.x = this.position.x;
|
|
|
|
this.instance.position.y = this.position.y;
|
|
|
|
this.instance.position.z = this.position.z;
|
|
|
|
// }
|
2018-01-29 12:42:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Not all implementations of camera has aspect ratio, we, however do have an aspect ratio for all our cameras
|
|
|
|
*/
|
|
|
|
if (GameLib.Utils.Defined(this.instance.aspect)) {
|
|
|
|
this.instance.aspect = this.aspect;
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:02:47 +01:00
|
|
|
//if (GameLib.Utils.Defined(this.instance.lookAt)) {
|
|
|
|
this.instance.lookAt(this.lookAt.instance);
|
|
|
|
this.instance.updateProjectionMatrix();
|
|
|
|
//}
|
2018-01-29 12:42:46 +01:00
|
|
|
|
|
|
|
GameLib.Component.prototype.createInstance.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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') {
|
|
|
|
|
|
|
|
this.remove();
|
|
|
|
|
|
|
|
var args = [this.graphics, {
|
|
|
|
id : this.id,
|
|
|
|
name : this.name
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (this.cameraType === GameLib.D3.API.Camera.CAMERA_TYPE_STEREO) {
|
|
|
|
GameLib.D3.Camera.Stereo.apply(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cameraType === GameLib.D3.API.Camera.CAMERA_TYPE_CUBE) {
|
|
|
|
GameLib.D3.Camera.Cube.apply(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cameraType === GameLib.D3.API.Camera.CAMERA_TYPE_PERSPECTIVE) {
|
|
|
|
GameLib.D3.Camera.Perspective.apply(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cameraType === GameLib.D3.API.Camera.CAMERA_TYPE_ORTHOGRAPHIC) {
|
|
|
|
GameLib.D3.Camera.Orthographic.apply(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property === 'aspect') {
|
|
|
|
if (GameLib.Utils.Defined(this.instance.aspect)) {
|
|
|
|
this.instance.aspect = this.aspect;
|
2018-01-30 15:02:47 +01:00
|
|
|
this.instance.updateProjectionMatrix();
|
2018-01-29 12:42:46 +01:00
|
|
|
} else {
|
|
|
|
console.warn('updating the aspect ratio of this type of camera has no effect.');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property === 'position') {
|
|
|
|
this.instance.position.x = this.position.x;
|
|
|
|
this.instance.position.y = this.position.y;
|
|
|
|
this.instance.position.z = this.position.z;
|
|
|
|
this.instance.updateProjectionMatrix();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
this.instance.updateProjectionMatrix();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.name,
|
|
|
|
this.cameraType,
|
2018-01-30 15:02:47 +01:00
|
|
|
this.aspect,
|
2018-01-29 12:42:46 +01:00
|
|
|
this.position.toApiObject(),
|
|
|
|
this.lookAt.toApiObject(),
|
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Camera.prototype.updateFromInstance = function() {
|
|
|
|
|
|
|
|
this.aspect = this.instance.aspect;
|
|
|
|
|
|
|
|
this.position.x = this.instance.position.x;
|
|
|
|
this.position.y = this.instance.position.y;
|
|
|
|
this.position.z = this.instance.position.z;
|
|
|
|
|
|
|
|
if (this.instance.target) {
|
|
|
|
this.lookAt.x = this.instance.target.position.x;
|
|
|
|
this.lookAt.y = this.instance.target.position.y;
|
|
|
|
this.lookAt.z = this.instance.target.position.z;
|
|
|
|
} else {
|
|
|
|
console.warn('todo: update lookAt somehow...');
|
|
|
|
}
|
|
|
|
};
|