Skip to content

Commit 52d42af

Browse files
committed
Added pixelRatio parameter to camera and the frustum objects
1 parent fd7ec91 commit 52d42af

18 files changed

+200
-47
lines changed

Apps/Sandcastle/gallery/Star Burst.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@
9393

9494
var drawingBufferWidth = scene.drawingBufferWidth;
9595
var drawingBufferHeight = scene.drawingBufferHeight;
96+
var pixelRatio = scene.pixelRatio;
9697

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

100-
var dimensions = camera.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, new Cesium.Cartesian2());
101+
var dimensions = camera.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, new Cesium.Cartesian2());
101102
Cesium.Cartesian2.multiplyByScalar(offset, Cesium.Cartesian2.maximumComponent(dimensions), offset);
102103

103104
var labelOffset;

CHANGES.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
==========
33

4+
### 1.63 - 2019-11-01
5+
6+
##### Additions :tada:
7+
* Added `pixelRatio` parameter to `Camera.getPixelSize`, `OrthographicFrustum.getPixelDimensions`, `OrthographicOffCenterFrustum.getPixelDimensions`, `PerspectiveFrustum.getPixelDimensions`, and `PerspectiveOffCenterFrustum.getPixelDimensions`. Pass in `scene.pixelRatio` for css pixel sizes or `1.0` for native device pixel sizes.
8+
9+
##### Fixes :wrench:
10+
* Fixed css pixel usage for `BillboardCollection`, `Model`, `Primitive`, and `PointPrimtiveCollection`. [#8113](https://github.com/AnalyticalGraphicsInc/cesium/issues/8113)
11+
412
### 1.62 - 2019-10-01
513

614
##### Deprecated :hourglass_flowing_sand:

Source/Core/OrthographicFrustum.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,23 @@ define([
209209
*
210210
* @param {Number} drawingBufferWidth The width of the drawing buffer.
211211
* @param {Number} drawingBufferHeight The height of the drawing buffer.
212+
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
212213
* @param {Number} distance The distance to the near plane in meters.
213214
* @param {Cartesian2} result The object onto which to store the result.
214215
* @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.
215216
*
216217
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
217218
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
219+
* @exception {DeveloperError} pixelRatio must be greater than zero.
218220
*
219221
* @example
220222
* // Example 1
221223
* // Get the width and height of a pixel.
222-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, new Cesium.Cartesian2());
224+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, 0.0, new Cesium.Cartesian2());
223225
*/
224-
OrthographicFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
226+
OrthographicFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result) {
225227
update(this);
226-
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result);
228+
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result);
227229
};
228230

229231
/**

Source/Core/OrthographicOffCenterFrustum.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,21 @@ define([
283283
*
284284
* @param {Number} drawingBufferWidth The width of the drawing buffer.
285285
* @param {Number} drawingBufferHeight The height of the drawing buffer.
286+
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
286287
* @param {Number} distance The distance to the near plane in meters.
287288
* @param {Cartesian2} result The object onto which to store the result.
288289
* @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.
289290
*
290291
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
291292
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
293+
* @exception {DeveloperError} pixelRatio must be greater than zero.
292294
*
293295
* @example
294296
* // Example 1
295297
* // Get the width and height of a pixel.
296-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, new Cesium.Cartesian2());
298+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, 0.0, new Cesium.Cartesian2());
297299
*/
298-
OrthographicOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
300+
OrthographicOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result) {
299301
update(this);
300302

301303
//>>includeStart('debug', pragmas.debug);
@@ -308,6 +310,12 @@ define([
308310
if (drawingBufferHeight <= 0) {
309311
throw new DeveloperError('drawingBufferHeight must be greater than zero.');
310312
}
313+
if (!defined(pixelRatio)) {
314+
throw new DeveloperError('pixelRatio is required.');
315+
}
316+
if (pixelRatio <= 0) {
317+
throw new DeveloperError('pixelRatio must be greater than zero.');
318+
}
311319
if (!defined(distance)) {
312320
throw new DeveloperError('distance is required.');
313321
}
@@ -318,8 +326,8 @@ define([
318326

319327
var frustumWidth = this.right - this.left;
320328
var frustumHeight = this.top - this.bottom;
321-
var pixelWidth = frustumWidth / drawingBufferWidth;
322-
var pixelHeight = frustumHeight / drawingBufferHeight;
329+
var pixelWidth = pixelRatio * frustumWidth / drawingBufferWidth;
330+
var pixelHeight = pixelRatio * frustumHeight / drawingBufferHeight;
323331

324332
result.x = pixelWidth;
325333
result.y = pixelHeight;

Source/Core/PerspectiveFrustum.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,19 @@ define([
293293
*
294294
* @param {Number} drawingBufferWidth The width of the drawing buffer.
295295
* @param {Number} drawingBufferHeight The height of the drawing buffer.
296+
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
296297
* @param {Number} distance The distance to the near plane in meters.
297298
* @param {Cartesian2} result The object onto which to store the result.
298299
* @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.
299300
*
300301
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
301302
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
303+
* @exception {DeveloperError} pixelRatio must be greater than zero.
302304
*
303305
* @example
304306
* // Example 1
305307
* // Get the width and height of a pixel.
306-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Cesium.Cartesian2());
308+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, 1.0, new Cesium.Cartesian2());
307309
*
308310
* @example
309311
* // Example 2
@@ -314,11 +316,11 @@ define([
314316
* var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
315317
* var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
316318
* var distance = Cesium.Cartesian3.magnitude(toCenterProj);
317-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Cesium.Cartesian2());
319+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, distance, new Cesium.Cartesian2());
318320
*/
319-
PerspectiveFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
321+
PerspectiveFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result) {
320322
update(this);
321-
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result);
323+
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result);
322324
};
323325

324326
/**

Source/Core/PerspectiveOffCenterFrustum.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,19 @@ define([
322322
*
323323
* @param {Number} drawingBufferWidth The width of the drawing buffer.
324324
* @param {Number} drawingBufferHeight The height of the drawing buffer.
325+
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
325326
* @param {Number} distance The distance to the near plane in meters.
326327
* @param {Cartesian2} result The object onto which to store the result.
327328
* @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.
328329
*
329330
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
330331
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
332+
* @exception {DeveloperError} pixelRatio must be greater than zero.
331333
*
332334
* @example
333335
* // Example 1
334336
* // Get the width and height of a pixel.
335-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Cesium.Cartesian2());
337+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, 1.0, new Cesium.Cartesian2());
336338
*
337339
* @example
338340
* // Example 2
@@ -343,9 +345,9 @@ define([
343345
* var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
344346
* var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
345347
* var distance = Cesium.Cartesian3.magnitude(toCenterProj);
346-
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Cesium.Cartesian2());
348+
* var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, scene.pixelRatio, distance, new Cesium.Cartesian2());
347349
*/
348-
PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, result) {
350+
PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, result) {
349351
update(this);
350352

351353
//>>includeStart('debug', pragmas.debug);
@@ -358,6 +360,12 @@ define([
358360
if (drawingBufferHeight <= 0) {
359361
throw new DeveloperError('drawingBufferHeight must be greater than zero.');
360362
}
363+
if (!defined(pixelRatio)) {
364+
throw new DeveloperError('pixelRatio is required');
365+
}
366+
if (pixelRatio <= 0) {
367+
throw new DeveloperError('pixelRatio must be greater than zero.');
368+
}
361369
if (!defined(distance)) {
362370
throw new DeveloperError('distance is required.');
363371
}
@@ -368,9 +376,9 @@ define([
368376

369377
var inverseNear = 1.0 / this.near;
370378
var tanTheta = this.top * inverseNear;
371-
var pixelHeight = 2.0 * distance * tanTheta / drawingBufferHeight;
379+
var pixelHeight = 2.0 * pixelRatio * distance * tanTheta / drawingBufferHeight;
372380
tanTheta = this.right * inverseNear;
373-
var pixelWidth = 2.0 * distance * tanTheta / drawingBufferWidth;
381+
var pixelWidth = 2.0 * pixelRatio * distance * tanTheta / drawingBufferWidth;
374382

375383
result.x = pixelWidth;
376384
result.y = pixelHeight;

Source/Scene/BillboardCollection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ define([
14671467
function updateBoundingVolume(collection, frameState, boundingVolume) {
14681468
var pixelScale = 1.0;
14691469
if (!collection._allSizedInMeters || collection._maxPixelOffset !== 0.0) {
1470-
pixelScale = frameState.camera.getPixelSize(boundingVolume, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight);
1470+
pixelScale = frameState.camera.getPixelSize(boundingVolume, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight, frameState.pixelRatio);
14711471
}
14721472

14731473
var size = pixelScale * collection._maxScale * collection._maxSize * 2.0;

Source/Scene/Camera.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -2670,9 +2670,10 @@ define([
26702670
* @param {BoundingSphere} boundingSphere The bounding sphere in world coordinates.
26712671
* @param {Number} drawingBufferWidth The drawing buffer width.
26722672
* @param {Number} drawingBufferHeight The drawing buffer height.
2673+
* @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
26732674
* @returns {Number} The pixel size in meters.
26742675
*/
2675-
Camera.prototype.getPixelSize = function(boundingSphere, drawingBufferWidth, drawingBufferHeight) {
2676+
Camera.prototype.getPixelSize = function(boundingSphere, drawingBufferWidth, drawingBufferHeight, pixelRatio) {
26762677
//>>includeStart('debug', pragmas.debug);
26772678
if (!defined(boundingSphere)) {
26782679
throw new DeveloperError('boundingSphere is required.');
@@ -2683,10 +2684,13 @@ define([
26832684
if (!defined(drawingBufferHeight)) {
26842685
throw new DeveloperError('drawingBufferHeight is required.');
26852686
}
2687+
if (!defined(pixelRatio)) {
2688+
throw new DeveloperError('pixelRatio is required.');
2689+
}
26862690
//>>includeEnd('debug');
26872691

26882692
var distance = this.distanceToBoundingSphere(boundingSphere);
2689-
var pixelSize = this.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, scratchPixelSize);
2693+
var pixelSize = this.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, pixelRatio, distance, scratchPixelSize);
26902694
return Math.max(pixelSize.x, pixelSize.y);
26912695
};
26922696

Source/Scene/Model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4378,7 +4378,7 @@ define([
43784378
function scaleInPixels(positionWC, radius, frameState) {
43794379
scratchBoundingSphere.center = positionWC;
43804380
scratchBoundingSphere.radius = radius;
4381-
return frameState.camera.getPixelSize(scratchBoundingSphere, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight);
4381+
return frameState.camera.getPixelSize(scratchBoundingSphere, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight, frameState.pixelRatio);
43824382
}
43834383

43844384
var scratchPosition = new Cartesian3();

Source/Scene/Picking.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ define([
139139
Cartesian3.fromElements(origin.z, origin.x, origin.y, origin);
140140
}
141141

142-
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, scratchOrthoPixelSize);
142+
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, 1.0, scratchOrthoPixelSize);
143143

144144
var ortho = scratchOrthoPickingFrustum;
145145
ortho.right = pixelSize.x * 0.5;
@@ -169,7 +169,7 @@ define([
169169
var xDir = x * near * tanTheta;
170170
var yDir = y * near * tanPhi;
171171

172-
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, scratchPerspPixelSize);
172+
var pixelSize = frustum.getPixelDimensions(viewport.width, viewport.height, 1.0, 1.0, scratchPerspPixelSize);
173173
var pickWidth = pixelSize.x * width * 0.5;
174174
var pickHeight = pixelSize.y * height * 0.5;
175175

Source/Scene/PointPrimitiveCollection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ define([
720720
}
721721

722722
function updateBoundingVolume(collection, frameState, boundingVolume) {
723-
var pixelSize = frameState.camera.getPixelSize(boundingVolume, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight);
723+
var pixelSize = frameState.camera.getPixelSize(boundingVolume, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight, frameState.pixelRatio);
724724
var size = pixelSize * collection._maxPixelSize;
725725
boundingVolume.radius += size;
726726
}

Source/Scene/Primitive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ define([
17861786
for (i = 0; i < length; ++i) {
17871787
boundingSphere = primitive._boundingSpheres[i];
17881788
var boundingSphereWC = primitive._boundingSphereWC[i];
1789-
var pixelSizeInMeters = frameState.camera.getPixelSize(boundingSphere, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight);
1789+
var pixelSizeInMeters = frameState.camera.getPixelSize(boundingSphere, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight, frameState.pixelRatio);
17901790
var sizeInMeters = pixelSizeInMeters * pixelSize;
17911791
boundingSphereWC.radius = boundingSphere.radius + sizeInMeters;
17921792
}

Specs/Core/OrthographicFrustumSpec.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,58 @@ describe('Core/OrthographicFrustum', function() {
149149

150150
it('get pixel dimensions throws without canvas height', function() {
151151
expect(function() {
152-
return frustum.getPixelDimensions(1.0, undefined, 0.0, new Cartesian2());
152+
return frustum.getPixelDimensions(1.0, undefined, 1.0, 0.0, new Cartesian2());
153153
}).toThrowDeveloperError();
154154
});
155155

156156
it('get pixel dimensions throws without canvas width', function() {
157157
expect(function() {
158-
return frustum.getPixelDimensions(undefined, 1.0, 0.0, new Cartesian2());
158+
return frustum.getPixelDimensions(undefined, 1.0, 1.0, 0.0, new Cartesian2());
159159
}).toThrowDeveloperError();
160160
});
161161

162162
it('get pixel dimensions throws with canvas width less than or equal to zero', function() {
163163
expect(function() {
164-
return frustum.getPixelDimensions(0.0, 1.0, 0.0, new Cartesian2());
164+
return frustum.getPixelDimensions(0.0, 1.0, 1.0, 0.0, new Cartesian2());
165165
}).toThrowDeveloperError();
166166
});
167167

168168
it('get pixel dimensions throws with canvas height less than or equal to zero', function() {
169169
expect(function() {
170-
return frustum.getPixelDimensions(1.0, 0.0, 0.0, new Cartesian2());
170+
return frustum.getPixelDimensions(1.0, 0.0, 1.0, 0.0, new Cartesian2());
171+
}).toThrowDeveloperError();
172+
});
173+
174+
it('get pixel dimensions throws without pixel ratio', function() {
175+
expect(function() {
176+
return frustum.getPixelDimensions(1.0, 1.0, undefined, 0.0, new Cartesian2());
177+
}).toThrowDeveloperError();
178+
});
179+
180+
it('get pixel dimensions throws with pixel ratio less than or equal to zero', function() {
181+
expect(function() {
182+
return frustum.getPixelDimensions(1.0, 1.0, 0.0, 0.0, new Cartesian2());
171183
}).toThrowDeveloperError();
172184
});
173185

174186
it('get pixel dimensions', function() {
175-
var pixelSize = frustum.getPixelDimensions(1.0, 1.0, 0.0, new Cartesian2());
176-
expect(pixelSize.x).toEqual(2.0);
177-
expect(pixelSize.y).toEqual(2.0);
187+
var dimensions = new Cartesian2(1.0, 1.0);
188+
var pixelRatio = 1.0;
189+
var distance = 1.0;
190+
var pixelSize = frustum.getPixelDimensions(dimensions.x, dimensions.y, pixelRatio, distance, new Cartesian2());
191+
var expected = frustum._offCenterFrustum.getPixelDimensions(dimensions.x, dimensions.y, pixelRatio, distance, new Cartesian2());
192+
expect(pixelSize.x).toEqual(expected.x);
193+
expect(pixelSize.y).toEqual(expected.y);
194+
});
195+
196+
it('get pixel dimensions with pixel ratio', function() {
197+
var dimensions = new Cartesian2(1.0, 1.0);
198+
var pixelRatio = 2.0;
199+
var distance = 1.0;
200+
var pixelSize = frustum.getPixelDimensions(dimensions.x, dimensions.y, pixelRatio, distance, new Cartesian2());
201+
var expected = frustum._offCenterFrustum.getPixelDimensions(dimensions.x, dimensions.y, pixelRatio, distance, new Cartesian2());
202+
expect(pixelSize.x).toEqual(expected.x);
203+
expect(pixelSize.y).toEqual(expected.y);
178204
});
179205

180206
it('equals', function() {

0 commit comments

Comments
 (0)