Skip to content

Commit

Permalink
add batching for ground primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
likangning93 committed Mar 29, 2018
1 parent 62ab0ba commit 2cb5132
Show file tree
Hide file tree
Showing 8 changed files with 775 additions and 26 deletions.
22 changes: 11 additions & 11 deletions Apps/Sandcastle/gallery/batchingGroundPrims.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
new Cesium.Cartesian3(-2352875.095159641, -3742564.819171856, 4582173.540953957),
new Cesium.Cartesian3(-2350669.646050987, -3743751.6823160048, 4582334.8406995395)
];

/*
// concave polygon
var redPolygon1 = viewer.entities.add({
name : 'concave polygon on surface',
Expand All @@ -87,7 +87,7 @@
material : '../images/Cesium_Logo_Color.jpg'
}
});

*/
// polygons with non-overlapping extents seem to be batchable without problems
/*
var redPolygon1 = viewer.entities.add({
Expand Down Expand Up @@ -136,7 +136,7 @@
});*/

// nearly overlapping rectangles over mt. st. helens
/*

var latitude = 46.1922;
var longitude = -122.1934;

Expand Down Expand Up @@ -167,7 +167,12 @@
material : Cesium.Color.YELLOW.withAlpha(0.5)
}
});
*/

var checkerboard = new Cesium.CheckerboardMaterialProperty({
evenColor : Cesium.Color.ORANGE,
oddColor : Cesium.Color.YELLOW,
repeat : new Cesium.Cartesian2(14, 14)
});

var rightHandler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
rightHandler.setInputAction(function(movement) {
Expand All @@ -181,17 +186,12 @@
var normalized = Cesium.Cartesian3.normalize(cartesian, cartesian);
console.log(normalized);

viewer.entities.removeAll();
//viewer.entities.removeAll();
viewer.entities.add({
name : lat + ' ' + long,
rectangle : {
coordinates : Cesium.Rectangle.fromDegrees(long - 0.0002, lat - 0.0001, long + 0.0002, lat + 0.0001),
material :
new Cesium.CheckerboardMaterialProperty({
evenColor : Cesium.Color.ORANGE,
oddColor : Cesium.Color.YELLOW,
repeat : new Cesium.Cartesian2(14, 14)
})
material : checkerboard
}
});
}
Expand Down
32 changes: 32 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,38 @@ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

### rbush

https://github.com/mourner/rbush

> MIT License
> Copyright (c) 2016 Vladimir Agafonkin
>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
>
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

### quickselect

https://github.com/mourner/quickselect

No license given, used by rbush

### crunch

https://github.com/BinomialLLC/crunch
Expand Down
70 changes: 70 additions & 0 deletions Source/Core/RectangleRbush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
define([
'../ThirdParty/rbush',
'./Check'
], function(
rbush,
Check) {
'use strict';

/**
* Wrapper around rbush for use with Rectangle types.
* @private
*/
function RectangleRbush() {
this._tree = rbush();
}

function RectangleWithId() {
this.minX = 0.0;
this.minY = 0.0;
this.maxX = 0.0;
this.maxY = 0.0;
this.id = '';
}

function fromRectangleAndId(rectangle, id, result) {
result.minX = rectangle.west;
result.minY = rectangle.south;
result.maxX = rectangle.east;
result.maxY = rectangle.north;
result.id = id;
return result;
}

function idCompare(a, b) {
return a.id === b.id;
}

RectangleRbush.prototype.insert = function(id, rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string('id', id);
Check.typeOf.object('rectangle', rectangle);
//>>includeEnd('debug');

var withId = fromRectangleAndId(rectangle, id, new RectangleWithId());
this._tree.insert(withId);
};

var removalScratch = new RectangleWithId();
RectangleRbush.prototype.remove = function(id, rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string('id', id);
Check.typeOf.object('rectangle', rectangle);
//>>includeEnd('debug');

var withId = fromRectangleAndId(rectangle, id, removalScratch);
this._tree.remove(withId, idCompare);
};

var collisionScratch = new RectangleWithId();
RectangleRbush.prototype.collides = function(rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object('rectangle', rectangle);
//>>includeEnd('debug');

var withId = fromRectangleAndId(rectangle, '', collisionScratch);
return this._tree.collides(withId);
};

return RectangleRbush;
});
4 changes: 1 addition & 3 deletions Source/DataSources/GeometryVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ define([
'./RectangleGeometryUpdater',
'./StaticGeometryColorBatch',
'./StaticGeometryPerMaterialBatch',
'./StaticGroundGeometryColorBatch',
'./StaticGroundGeometryPerMaterialBatch',
'./StaticOutlineGeometryBatch',
'./WallGeometryUpdater'
Expand Down Expand Up @@ -58,7 +57,6 @@ define([
RectangleGeometryUpdater,
StaticGeometryColorBatch,
StaticGeometryPerMaterialBatch,
StaticGroundGeometryColorBatch,
StaticGroundGeometryPerMaterialBatch,
StaticOutlineGeometryBatch,
WallGeometryUpdater) {
Expand Down Expand Up @@ -163,7 +161,7 @@ define([
this._groundMaterialBatches = new Array(numberOfClassificationTypes); // TODO: why is this?

for (i = 0; i < numberOfClassificationTypes; ++i) {
this._groundColorBatches[i] = new StaticGroundGeometryColorBatch(groundPrimitives, PerInstanceColorAppearance, i);
this._groundColorBatches[i] = new StaticGroundGeometryPerMaterialBatch(groundPrimitives, PerInstanceColorAppearance, i);
this._groundMaterialBatches[i] = new StaticGroundGeometryPerMaterialBatch(groundPrimitives, MaterialAppearance, i);
}

Expand Down
36 changes: 24 additions & 12 deletions Source/DataSources/StaticGroundGeometryPerMaterialBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define([
'../Core/DistanceDisplayCondition',
'../Core/DistanceDisplayConditionGeometryInstanceAttribute',
'../Core/ShowGeometryInstanceAttribute',
'../Core/RectangleRbush',
'../Scene/GroundPrimitive',
'./BoundingSphereState',
'./ColorMaterialProperty',
Expand All @@ -19,6 +20,7 @@ define([
DistanceDisplayCondition,
DistanceDisplayConditionGeometryInstanceAttribute,
ShowGeometryInstanceAttribute,
RectangleRbush,
GroundPrimitive,
BoundingSphereState,
ColorMaterialProperty,
Expand All @@ -29,13 +31,13 @@ define([
var distanceDisplayConditionScratch = new DistanceDisplayCondition();

// Encapsulates a Primitive and all the entities that it represents.
function Batch(primitives, appearanceType, materialProperty, shadows) {
function Batch(primitives, appearanceType, materialProperty, shadows, usingSphericalCoordinates) {
this.primitives = primitives; // scene level primitive collection
this.appearanceType = appearanceType;
this.materialProperty = materialProperty;
this.updaters = new AssociativeArray();
this.createPrimitive = true;
this.primitive = undefined;
this.primitive = undefined; // a GroundPrimitive encapsulating all the entities
this.oldPrimitive = undefined;
this.geometry = new AssociativeArray();
this.material = undefined;
Expand All @@ -46,30 +48,34 @@ define([
this.subscriptions = new AssociativeArray();
this.showsUpdated = new AssociativeArray();
this.shadows = shadows;
this.usingSphericalCoordinates = usingSphericalCoordinates;
this.rbush = new RectangleRbush();
}

Batch.prototype.onMaterialChanged = function() {
this.invalidated = true;
};

Batch.prototype.nonOverlapping = function(updater) {
return false;
Batch.prototype.nonOverlapping = function(rectangle) {
return !this.rbush.collides(rectangle);
};

Batch.prototype.isMaterial = function(updater) {
var material = this.materialProperty;
var updaterMaterial = updater.fillMaterialProperty;

if (updaterMaterial === material) {
if (updaterMaterial === material ||
(updaterMaterial instanceof ColorMaterialProperty && material instanceof ColorMaterialProperty)) {
return true;
}
return defined(material) && material.equals(updaterMaterial);
};

Batch.prototype.add = function(time, updater) {
Batch.prototype.add = function(time, updater, geometryInstance) {
var id = updater.id;
this.updaters.set(id, updater);
this.geometry.set(id, updater.createFillGeometryInstance(time));
this.geometry.set(id, geometryInstance);
this.rbush.insert(id, geometryInstance.geometry.rectangle);
if (!updater.hasConstantFill || !updater.fillMaterialProperty.isConstant || !Property.isConstant(updater.distanceDisplayConditionProperty)) {
this.updatersWithAttributes.set(id, updater);
} else {
Expand All @@ -85,8 +91,10 @@ define([

Batch.prototype.remove = function(updater) {
var id = updater.id;
var geometryInstance = this.geometry.get(id);
this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
if (this.updaters.remove(id)) {
this.rbush.remove(id, geometryInstance.geometry.rectangle);
this.updatersWithAttributes.remove(id);
var unsubscribe = this.subscriptions.get(id);
if (defined(unsubscribe)) {
Expand Down Expand Up @@ -309,15 +317,19 @@ define([
StaticGroundGeometryPerMaterialBatch.prototype.add = function(time, updater) {
var items = this._items;
var length = items.length;
for (var i = 0; i < length; i++) {
var geometryInstance = updater.createFillGeometryInstance(time);
var usingSphericalCoordinates = GroundPrimitive.shouldUseSphericalCoordinates(geometryInstance.geometry.rectangle);
for (var i = 0; i < length; ++i) {
var item = items[i];
if (item.isMaterial(updater) && item.nonOverlapping(updater)) {
item.add(time, updater);
if (item.isMaterial(updater) &&
item.nonOverlapping(geometryInstance.geometry.rectangle) &&
item.usingSphericalCoordinates === usingSphericalCoordinates) {
item.add(time, updater, geometryInstance);
return;
}
}
var batch = new Batch(this._primitives, this._appearanceType, updater.fillMaterialProperty, this._shadows);
batch.add(time, updater);
var batch = new Batch(this._primitives, this._appearanceType, updater.fillMaterialProperty, this._shadows, usingSphericalCoordinates);
batch.add(time, updater, geometryInstance);
items.push(batch);
};

Expand Down
17 changes: 17 additions & 0 deletions Source/Scene/GroundPrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ define([
'../Core/Cartesian3',
'../Core/Cartesian4',
'../Core/Cartographic',
'../Core/Check',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
Expand Down Expand Up @@ -32,6 +33,7 @@ define([
Cartesian3,
Cartesian4,
Cartographic,
Check,
defaultValue,
defined,
defineProperties,
Expand Down Expand Up @@ -924,6 +926,21 @@ define([
return destroyObject(this);
};

/**
* Computes whether the given rectangle is wide enough that texture coordinates
* over its area should be computed using spherical extents instead of distance to planes.
*
* @param {Rectangle} rectangle A rectangle
* @private
*/
GroundPrimitive.shouldUseSphericalCoordinates = function(rectangle) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object('rectangle', rectangle);
//>>includeEnd('debug');

return shouldUseSpherical(rectangle);
};

/**
* Texture coordinates for ground primitives are computed either using spherical coordinates for large areas or
* using distance from planes for small areas.
Expand Down
Loading

0 comments on commit 2cb5132

Please sign in to comment.