r3-legacy/src/r3-d3-camera-orthographic-f...

184 lines
5.1 KiB
JavaScript

/**
* R3.D3.Camera.Orthographic.FixedAspect
* @param apiComponent
* @constructor
*/
R3.D3.Camera.Orthographic.FixedAspect = function(
apiComponent
) {
__RUNTIME_COMPONENT__;
R3.D3.Camera.Orthographic.call(
this,
apiComponent,
true
);
};
R3.D3.Camera.Orthographic.FixedAspect.prototype = Object.create(R3.D3.Camera.Orthographic.prototype);
R3.D3.Camera.Orthographic.FixedAspect.prototype.constructor = R3.D3.Camera.Orthographic.FixedAspect;
/**
* Creates a camera instance
* @returns {*}
*/
R3.D3.Camera.Orthographic.FixedAspect.prototype.createInstance = function() {
/**
* We need to get the viewport width and height
* @type {void|*}
*/
var dimensions = this.getDimensions();
this.instance = this.graphics.OrthographicCamera(
dimensions.left,
dimensions.right,
dimensions.top,
dimensions.bottom,
this.near,
this.far
);
__CREATE_INSTANCE__;
};
/**
* Updates the instance with the current state
*/
R3.D3.Camera.Orthographic.FixedAspect.prototype.updateInstance = function(property) {
if (property === 'aspect') {
var dimensions = this.getDimensions();
this.instance.left = dimensions.left;
this.instance.right = dimensions.right;
this.instance.top = dimensions.top;
this.instance.bottom = dimensions.bottom;
this.instance.updateProjectionMatrix();
return;
}
R3.D3.Camera.Orthographic.prototype.updateInstance.call(this, property);
};
/**
* This calculates the dimensions of the camera based on the following example info:
*
* aspect = width
* ------
* height
*
* width = aspect * height;
* height = width / aspect;
*
* aspect > 1 (width > height) (landscape)
* aspect < 1 (height > width) (portrait)
*
* 4 / 3 = 1.33333 (1920 x 1440)
* 16 / 9 = 1.77777 (1920 x 1080)
*
* w h w h
* 9 / 16 = 0.5625 (1080 x 1920) - required
* 3 / 4 = 0.75 (1440 x 1920) - current
*
* @returns {{left: number, right: number, top: number, bottom: number}}
*/
R3.D3.Camera.Orthographic.FixedAspect.prototype.getDimensions = function() {
var size = R3.Utils.GetWindowSize();
var currentAspect = size.width / size.height;
var width, height;
if (currentAspect > 1) {
/**
* Width is greater than height (landscape)
*/
if (this.aspectRatio < 1) {
/**
* The required aspect is more high than wide - use the full height
*/
width = this.aspectRatio * size.height;
height = size.height;
} else {
/**
* The required aspect is also more wide than high - so we have another two possibilities:
* a) The required aspect is greater than the current aspect - this means the required aspect is less high
* than the current aspect - we can use the full width
*
* b) The required aspect is less than the current aspect - this means the required aspect is higher than
* the current aspect - we need to determine a new width based on the current height
*/
if (this.aspectRatio > currentAspect) {
/**
* a)
*/
width = size.width;
height = width / this.aspectRatio;
} else {
/**
* b)
*/
height = size.height;
width = this.aspectRatio * height;
}
}
} else {
/**
* The height is greater than the width (portrait)
*/
if (this.aspectRatio > 1) {
/**
* The required aspect is landscape in a portrait mode - use the full width and calculate the new height
*/
width = size.width;
height = width / this.aspectRatio;
} else {
/**
* The required aspect is also more high than wide (portrait) - we have again, two possibilities
* a) The required aspect is greater than the current aspect - this means the required aspect does not fit
* the full width of the current aspect - use the full width of the current size and determine a new height
*
* b) The required aspect is less than the current aspect - this means that the required aspect is less wide
* than the current aspect, so we can use the full height of the current size and determine a new width
*/
if (this.aspectRatio > currentAspect) {
width = size.width;
height = width / this.aspectRatio;
} else {
height = size.height;
width = this.aspectRatio * height;
}
}
}
/**
* We now have the correct width and height of of the camera
*/
return {
left : width / -2,
right : width / 2,
top : height / 2,
bottom : height / -2
}
};