Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pixelRatio parameter to camera and the frustum objects #8237

Merged
merged 6 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Apps/Sandcastle/gallery/Star Burst.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@

var drawingBufferWidth = scene.drawingBufferWidth;
var drawingBufferHeight = scene.drawingBufferHeight;
var pixelRatio = scene.pixelRatio;

var diff = Cesium.Cartesian3.subtract(entityPosition, camera.positionWC, new Cesium.Cartesian3());
var distance = Cesium.Cartesian3.dot(camera.directionWC, diff);

var dimensions = camera.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, new Cesium.Cartesian2());
var dimensions = camera.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, new Cesium.Cartesian2());
Cesium.Cartesian2.multiplyByScalar(offset, Cesium.Cartesian2.maximumComponent(dimensions), offset);

var labelOffset;
Expand Down
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ Change Log

### 1.63 - 2019-11-01

##### Fixes :wrench:
##### Deprecated :hourglass_flowing_sand:
* `OrthographicFrustum.getPixelDimensions`, `OrthographicOffCenterFrustum.getPixelDimensions`, `PerspectiveFrustum.getPixelDimensions`, and `PerspectiveOffCenterFrustum.getPixelDimensions` now take a `pixelRatio` argument before the `result` argument. The previous function definition will no longer work in 1.65. [#8237](https://github.com/AnalyticalGraphicsInc/cesium/pull/8237)

##### Additions :tada:
* Added `pixelRatio` parameter to `OrthographicFrustum.getPixelDimensions`, `OrthographicOffCenterFrustum.getPixelDimensions`, `PerspectiveFrustum.getPixelDimensions`, and `PerspectiveOffCenterFrustum.getPixelDimensions`. Pass in `scene.pixelRatio` for dimensions in CSS pixel units or `1.0` for dimensions in native device pixel units. [#8237](https://github.com/AnalyticalGraphicsInc/cesium/pull/8237)

##### Fixes :wrench:
* Fixed css pixel usage for polylines, point clouds, models, primitives, and post-processing. [#8113](https://github.com/AnalyticalGraphicsInc/cesium/issues/8113)
* Fixed a bug where `scene.sampleHeightMostDetailed` and `scene.clampToHeightMostDetailed` would not resolve in request render mode. [#8281](https://github.com/AnalyticalGraphicsInc/cesium/issues/8281)
* Fixed seam artifacts when log depth is disabled, `scene.globe.depthTestAgainstTerrain` is false, and primitives are under the globe. [#8205](https://github.com/AnalyticalGraphicsInc/cesium/pull/8205)
* Fix dynamic ellipsoids using `innerRadii`, `minimumClock`, `maximumClock`, `minimumCone` or `maximumCone`. [#8277](https://github.com/AnalyticalGraphicsInc/cesium/pull/8277)
Expand Down
16 changes: 13 additions & 3 deletions Source/Core/OrthographicFrustum.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Cartesian2 from './Cartesian2.js';
import Check from './Check.js';
import defaultValue from './defaultValue.js';
import defined from './defined.js';
import defineProperties from './defineProperties.js';
import deprecationWarning from './deprecationWarning.js';
import DeveloperError from './DeveloperError.js';
import CesiumMath from './Math.js';
import OrthographicOffCenterFrustum from './OrthographicOffCenterFrustum.js';
Expand Down Expand Up @@ -200,20 +202,28 @@ import OrthographicOffCenterFrustum from './OrthographicOffCenterFrustum.js';
* @param {Number} drawingBufferWidth The width of the drawing buffer.
* @param {Number} drawingBufferHeight The height of the drawing buffer.
* @param {Number} distance The distance to the near plane in meters.
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
*
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
* @exception {DeveloperError} pixelRatio must be greater than zero.
*
* @example
* // Example 1
* // Get the width and height of a pixel.
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
*/
OrthographicFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
OrthographicFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
update(this);
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result);

if (pixelRatio instanceof Cartesian2) {
result = pixelRatio;
pixelRatio = 1.0;
deprecationWarning('getPixelDimensions-parameter-change', 'getPixelDimensions now takes a pixelRatio argument before the result argument in Cesium 1.63. The previous function definition will no longer work in 1.65.');
}
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result);
};

/**
Expand Down
24 changes: 20 additions & 4 deletions Source/Core/OrthographicOffCenterFrustum.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Cartesian2 from './Cartesian2.js';
import Cartesian3 from './Cartesian3.js';
import Cartesian4 from './Cartesian4.js';
import CullingVolume from './CullingVolume.js';
import defaultValue from './defaultValue.js';
import defined from './defined.js';
import defineProperties from './defineProperties.js';
import deprecationWarning from './deprecationWarning.js';
import DeveloperError from './DeveloperError.js';
import CesiumMath from './Math.js';
import Matrix4 from './Matrix4.js';
Expand Down Expand Up @@ -272,20 +274,28 @@ import Matrix4 from './Matrix4.js';
* @param {Number} drawingBufferWidth The width of the drawing buffer.
* @param {Number} drawingBufferHeight The height of the drawing buffer.
* @param {Number} distance The distance to the near plane in meters.
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
*
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
* @exception {DeveloperError} pixelRatio must be greater than zero.
*
* @example
* // Example 1
* // Get the width and height of a pixel.
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
*/
OrthographicOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
OrthographicOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
update(this);

if (pixelRatio instanceof Cartesian2) {
result = pixelRatio;
pixelRatio = 1.0;
deprecationWarning('getPixelDimensions-parameter-change', 'getPixelDimensions now takes a pixelRatio argument before the result argument in Cesium 1.63. The previous function definition will no longer work in 1.65.');
}

//>>includeStart('debug', pragmas.debug);
if (!defined(drawingBufferWidth) || !defined(drawingBufferHeight)) {
throw new DeveloperError('Both drawingBufferWidth and drawingBufferHeight are required.');
Expand All @@ -299,15 +309,21 @@ import Matrix4 from './Matrix4.js';
if (!defined(distance)) {
throw new DeveloperError('distance is required.');
}
if (!defined(pixelRatio)) {
throw new DeveloperError('pixelRatio is required.');
}
if (pixelRatio <= 0) {
throw new DeveloperError('pixelRatio must be greater than zero.');
}
if (!defined(result)) {
throw new DeveloperError('A result object is required.');
}
//>>includeEnd('debug');

var frustumWidth = this.right - this.left;
var frustumHeight = this.top - this.bottom;
var pixelWidth = frustumWidth / drawingBufferWidth;
var pixelHeight = frustumHeight / drawingBufferHeight;
var pixelWidth = pixelRatio * frustumWidth / drawingBufferWidth;
var pixelHeight = pixelRatio * frustumHeight / drawingBufferHeight;

result.x = pixelWidth;
result.y = pixelHeight;
Expand Down
19 changes: 15 additions & 4 deletions Source/Core/PerspectiveFrustum.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Cartesian2 from './Cartesian2.js';
import Check from './Check.js';
import defaultValue from './defaultValue.js';
import defined from './defined.js';
import defineProperties from './defineProperties.js';
import deprecationWarning from './deprecationWarning.js';
import DeveloperError from './DeveloperError.js';
import CesiumMath from './Math.js';
import PerspectiveOffCenterFrustum from './PerspectiveOffCenterFrustum.js';
Expand Down Expand Up @@ -284,16 +286,18 @@ import PerspectiveOffCenterFrustum from './PerspectiveOffCenterFrustum.js';
* @param {Number} drawingBufferWidth The width of the drawing buffer.
* @param {Number} drawingBufferHeight The height of the drawing buffer.
* @param {Number} distance The distance to the near plane in meters.
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
*
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
* @exception {DeveloperError} pixelRatio must be greater than zero.
*
* @example
* // Example 1
* // Get the width and height of a pixel.
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
*
* @example
* // Example 2
Expand All @@ -304,11 +308,18 @@ import PerspectiveOffCenterFrustum from './PerspectiveOffCenterFrustum.js';
* var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
* var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
* var distance = Cesium.Cartesian3.magnitude(toCenterProj);
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
*/
PerspectiveFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
PerspectiveFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
update(this);
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result);

if (pixelRatio instanceof Cartesian2) {
result = pixelRatio;
pixelRatio = 1.0;
deprecationWarning('getPixelDimensions-parameter-change', 'getPixelDimensions now takes a pixelRatio argument before the result argument in Cesium 1.63. The previous function definition will no longer work in 1.65.');
}

return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result);
};

/**
Expand Down
26 changes: 21 additions & 5 deletions Source/Core/PerspectiveOffCenterFrustum.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Cartesian2 from './Cartesian2.js';
import Cartesian3 from './Cartesian3.js';
import Cartesian4 from './Cartesian4.js';
import CullingVolume from './CullingVolume.js';
import defaultValue from './defaultValue.js';
import defined from './defined.js';
import defineProperties from './defineProperties.js';
import deprecationWarning from './deprecationWarning.js';
import DeveloperError from './DeveloperError.js';
import CesiumMath from './Math.js';
import Matrix4 from './Matrix4.js';
Expand Down Expand Up @@ -311,16 +313,18 @@ import Matrix4 from './Matrix4.js';
* @param {Number} drawingBufferWidth The width of the drawing buffer.
* @param {Number} drawingBufferHeight The height of the drawing buffer.
* @param {Number} distance The distance to the near plane in meters.
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
*
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
* @exception {DeveloperError} pixelRatio must be greater than zero.
*
* @example
* // Example 1
* // Get the width and height of a pixel.
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
*
* @example
* // Example 2
Expand All @@ -331,11 +335,17 @@ import Matrix4 from './Matrix4.js';
* var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
* var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
* var distance = Cesium.Cartesian3.magnitude(toCenterProj);
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Cesium.Cartesian2());
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
*/
PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
update(this);

if (pixelRatio instanceof Cartesian2) {
result = pixelRatio;
pixelRatio = 1.0;
deprecationWarning('getPixelDimensions-parameter-change', 'getPixelDimensions now takes a pixelRatio argument before the result argument in Cesium 1.63. The previous function definition will no longer work in 1.65.');
}

//>>includeStart('debug', pragmas.debug);
if (!defined(drawingBufferWidth) || !defined(drawingBufferHeight)) {
throw new DeveloperError('Both drawingBufferWidth and drawingBufferHeight are required.');
Expand All @@ -349,16 +359,22 @@ import Matrix4 from './Matrix4.js';
if (!defined(distance)) {
throw new DeveloperError('distance is required.');
}
if (!defined(pixelRatio)) {
throw new DeveloperError('pixelRatio is required');
}
if (pixelRatio <= 0) {
throw new DeveloperError('pixelRatio must be greater than zero.');
}
if (!defined(result)) {
throw new DeveloperError('A result object is required.');
}
//>>includeEnd('debug');

var inverseNear = 1.0 / this.near;
var tanTheta = this.top * inverseNear;
var pixelHeight = 2.0 * distance * tanTheta / drawingBufferHeight;
var pixelHeight = 2.0 * pixelRatio * distance * tanTheta / drawingBufferHeight;
tanTheta = this.right * inverseNear;
var pixelWidth = 2.0 * distance * tanTheta / drawingBufferWidth;
var pixelWidth = 2.0 * pixelRatio * distance * tanTheta / drawingBufferWidth;

result.x = pixelWidth;
result.y = pixelHeight;
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,7 @@ import SceneMode from './SceneMode.js';
//>>includeEnd('debug');

var distance = this.distanceToBoundingSphere(boundingSphere);
var pixelSize = this.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, scratchPixelSize);
var pixelSize = this.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, this._scene.pixelRatio, scratchPixelSize);
return Math.max(pixelSize.x, pixelSize.y);
};

Expand Down
4 changes: 2 additions & 2 deletions Source/Scene/Picking.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ import View from './View.js';
Cartesian3.fromElements(origin.z, origin.x, origin.y, origin);
}

var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, scratchOrthoPixelSize);
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, 1.0, scratchOrthoPixelSize);

var ortho = scratchOrthoPickingFrustum;
ortho.right = pixelSize.x * 0.5;
Expand Down Expand Up @@ -138,7 +138,7 @@ import View from './View.js';
var xDir = x * near * tanTheta;
var yDir = y * near * tanPhi;

var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, scratchPerspPixelSize);
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, 1.0, scratchPerspPixelSize);
var pickWidth = pixelSize.x * width * 0.5;
var pickHeight = pixelSize.y * height * 0.5;

Expand Down
Loading