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

HeightReference for Box, Cylinder and Ellipsoid #6932

Merged
merged 7 commits into from
Aug 22, 2018
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
84 changes: 73 additions & 11 deletions Apps/Sandcastle/gallery/Geometry Height Reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="An example for how to use the GeometryHeightProperty to height reference a corridor, ellipse, polygon or rectangle.">
<meta name="description" content="An example for how to use the HeightReference property on different geometry entities.">
<meta name="cesium-sandcastle-labels" content="Geometries">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
Expand Down Expand Up @@ -41,6 +41,20 @@
// instead of showing through it from underground
viewer.scene.globe.depthTestAgainstTerrain = true;

Sandcastle.addToolbarMenu([{
text : 'Polygons',
onselect : function() {
viewer.entities.removeAll();
addPolygons();
}
}, {
text : 'Boxes, Cylinders and Ellipsoids',
onselect : function() {
viewer.entities.removeAll();
addGeometries();
}
}]);

Sandcastle.addToolbarMenu([{
text : 'Terrain Enabled',
onselect : function() {
Expand All @@ -53,11 +67,57 @@
}
}]);

var longitude = 6.850615989890521;
var latitude = 45.89546589994886;
var longitude = 6.950615989890521;
var latitude = 45.79546589994886;
var delta = 0.001;

function addEntity(i, j) {
function addGeometry(i, j) {
var west = longitude + delta * i;
var north = latitude + delta * j + delta;

var type = Math.floor(Math.random() * 3);
if (type === 0) {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(west, north, 0.0),
box : {
dimensions : new Cesium.Cartesian3(40.0, 30.0, 50.0),
material : Cesium.Color.fromRandom({alpha : 1.0}),
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
});
} else if (type === 1) {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(west, north, 0.0),
cylinder : {
length :50.0,
topRadius : 20.0,
bottomRadius : 20.0,
material : Cesium.Color.fromRandom({alpha : 1.0}),
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
});
} else {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(west, north, 0.0),
ellipsoid : {
radii : new Cesium.Cartesian3(20.0, 15.0, 25.0),
material : Cesium.Color.fromRandom({alpha : 1.0}),
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
});
}
}

function addGeometries(){
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
addGeometry(i, j);
}
}
viewer.zoomTo(viewer.entities);
}

function addPolygon(i, j) {
var west = longitude + delta * i;
var east = longitude + delta * i + delta;

Expand All @@ -81,15 +141,17 @@
});
}

// create 16 polygons that are side-by-side
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
addEntity(i, j);
function addPolygons() {
// create 16 polygons that are side-by-side
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
addPolygon(i, j);
}
}
viewer.camera.lookAt(Cesium.Cartesian3.fromDegrees(longitude, latitude, 1500), new Cesium.HeadingPitchRange(-Cesium.Math.PI/2, -Cesium.Math.PI_OVER_FOUR, 2000));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}

viewer.camera.lookAt(Cesium.Cartesian3.fromDegrees(longitude, latitude, 500), new Cesium.HeadingPitchRange(Cesium.Math.PI, -Cesium.Math.PI_OVER_FOUR, 2000));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);//Sandcastle_End
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== 'undefined') {
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Change Log
* Added `Matrix4.setScale` for setting the scale on an affine transformation matrix [#6888](https://github.com/AnalyticalGraphicsInc/cesium/pull/6888)
* Added `GeocoderViewModel.destinationFound` for specifying a function that is called upon a successful geocode. The default behavior is to fly to the destination found by the geocoder. [#6915](https://github.com/AnalyticalGraphicsInc/cesium/pull/6915)
* Added optional `width` and `height` to `Scene.drillPick` for specifying a search area.
* Added `heightReference` to `BoxGraphics`, `CylinderGraphics` and `EllipsoidGraphics`, which can be used to clamp these entity types to terrain [#6932](https://github.com/AnalyticalGraphicsInc/cesium/pull/6932)

##### Fixes :wrench:
* Several performance improvements and fixes to the 3D Tiles traversal code. [#6390](https://github.com/AnalyticalGraphicsInc/cesium/pull/6390)
Expand Down
89 changes: 65 additions & 24 deletions Source/DataSources/BoxGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ define([
'../Core/Color',
'../Core/ColorGeometryInstanceAttribute',
'../Core/defined',
'../Core/defineProperties',
'../Core/DeveloperError',
'../Core/DistanceDisplayConditionGeometryInstanceAttribute',
'../Core/GeometryInstance',
'../Core/GeometryOffsetAttribute',
'../Core/Iso8601',
'../Core/OffsetGeometryInstanceAttribute',
'../Core/ShowGeometryInstanceAttribute',
'../Scene/HeightReference',
'../Scene/MaterialAppearance',
'../Scene/PerInstanceColorAppearance',
'./heightReferenceOnEntityPropertyChanged',
'./ColorMaterialProperty',
'./DynamicGeometryUpdater',
'./GeometryUpdater',
Expand All @@ -25,26 +30,35 @@ define([
Color,
ColorGeometryInstanceAttribute,
defined,
defineProperties,
DeveloperError,
DistanceDisplayConditionGeometryInstanceAttribute,
GeometryInstance,
GeometryOffsetAttribute,
Iso8601,
OffsetGeometryInstanceAttribute,
ShowGeometryInstanceAttribute,
HeightReference,
MaterialAppearance,
PerInstanceColorAppearance,
heightReferenceOnEntityPropertyChanged,
ColorMaterialProperty,
DynamicGeometryUpdater,
GeometryUpdater,
Property) {
'use strict';

var defaultOffset = Cartesian3.ZERO;

var offsetScratch = new Cartesian3();
var positionScratch = new Cartesian3();
var scratchColor = new Color();

function BoxGeometryOptions(entity) {
this.id = entity;
this.vertexFormat = undefined;
this.dimensions = undefined;
this.offsetAttribute = undefined;
}

/**
Expand Down Expand Up @@ -73,6 +87,20 @@ define([
BoxGeometryUpdater.prototype.constructor = BoxGeometryUpdater;
}

defineProperties(BoxGeometryUpdater.prototype, {
/**
* Gets the terrain offset property
* @type {TerrainOffsetProperty}
* @memberof BoxGeometryUpdater.prototype
* @readonly
*/
terrainOffsetProperty: {
get: function() {
return this._terrainOffsetProperty;
}
}
});

/**
* Creates the geometry instance which represents the fill of the geometry.
*
Expand All @@ -93,12 +121,16 @@ define([
var entity = this._entity;
var isAvailable = entity.isAvailable(time);

var attributes;

var color;
var show = new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._fillProperty.getValue(time));
var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
var distanceDisplayConditionAttribute = DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition);

var attributes = {
show : show,
distanceDisplayCondition : distanceDisplayConditionAttribute,
color : undefined,
offset: undefined
};
if (this._materialProperty instanceof ColorMaterialProperty) {
var currentColor;
if (defined(this._materialProperty.color) && (this._materialProperty.color.isConstant || isAvailable)) {
Expand All @@ -107,23 +139,16 @@ define([
if (!defined(currentColor)) {
currentColor = Color.WHITE;
}
color = ColorGeometryInstanceAttribute.fromColor(currentColor);
attributes = {
show : show,
distanceDisplayCondition : distanceDisplayConditionAttribute,
color : color
};
} else {
attributes = {
show : show,
distanceDisplayCondition : distanceDisplayConditionAttribute
};
attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
}
if (defined(this._options.offsetAttribute)) {
attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
}

return new GeometryInstance({
id : entity,
geometry : BoxGeometry.fromDimensions(this._options),
modelMatrix : entity.computeModelMatrix(time),
modelMatrix : entity.computeModelMatrixForHeightReference(time, entity.box.heightReference, this._options.dimensions.z * 0.5, this._scene.mapProjection.ellipsoid),
attributes : attributes
});
};
Expand All @@ -150,18 +175,28 @@ define([
var outlineColor = Property.getValueOrDefault(this._outlineColorProperty, time, Color.BLACK, scratchColor);
var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);

var attributes = {
show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition),
offset : undefined
};
if (defined(this._options.offsetAttribute)) {
attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
}

return new GeometryInstance({
id : entity,
geometry : BoxOutlineGeometry.fromDimensions(this._options),
modelMatrix : entity.computeModelMatrix(time),
attributes : {
show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition)
}
modelMatrix : entity.computeModelMatrixForHeightReference(time, entity.box.heightReference, this._options.dimensions.z * 0.5, this._scene.mapProjection.ellipsoid),
attributes : attributes
});
};

BoxGeometryUpdater.prototype._computeCenter = function(time, result) {
return Property.getValueOrUndefined(this._entity.position, time, result);
};

BoxGeometryUpdater.prototype._isHidden = function(entity, box) {
return !defined(box.dimensions) || !defined(entity.position) || GeometryUpdater.prototype._isHidden.call(this, entity, box);
};
Expand All @@ -171,13 +206,16 @@ define([
};

BoxGeometryUpdater.prototype._setStaticOptions = function(entity, box) {
var isColorMaterial = this._materialProperty instanceof ColorMaterialProperty;
var heightReference = Property.getValueOrDefault(box.heightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);

var options = this._options;
options.vertexFormat = isColorMaterial ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
options.vertexFormat = this._materialProperty instanceof ColorMaterialProperty ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
options.dimensions = box.dimensions.getValue(Iso8601.MINIMUM_VALUE, options.dimensions);
options.offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
};

BoxGeometryUpdater.prototype._onEntityPropertyChanged = heightReferenceOnEntityPropertyChanged;

BoxGeometryUpdater.DynamicGeometryUpdater = DynamicBoxGeometryUpdater;

/**
Expand All @@ -199,7 +237,10 @@ define([
};

DynamicBoxGeometryUpdater.prototype._setOptions = function(entity, box, time) {
this._options.dimensions = Property.getValueOrUndefined(box.dimensions, time, this._options.dimensions);
var heightReference = Property.getValueOrDefault(box.heightReference, time, HeightReference.NONE);
var options = this._options;
options.dimensions = Property.getValueOrUndefined(box.dimensions, time, options.dimensions);
options.offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
};

return BoxGeometryUpdater;
Expand Down
12 changes: 12 additions & 0 deletions Source/DataSources/BoxGraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ define([
* @constructor
*
* @param {Object} [options] Object with the following properties:
* @param {Property} [options.heightReference] A Property specifying what the height from the entity position is relative to.
* @param {Property} [options.dimensions] A {@link Cartesian3} Property specifying the length, width, and height of the box.
* @param {Property} [options.show=true] A boolean Property specifying the visibility of the box.
* @param {Property} [options.fill=true] A boolean Property specifying whether the box is filled with the provided material.
Expand All @@ -36,6 +37,7 @@ define([
* @demo {@link https://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Box.html|Cesium Sandcastle Box Demo}
*/
function BoxGraphics(options) {
this._heightReference = undefined;
this._dimensions = undefined;
this._dimensionsSubscription = undefined;
this._show = undefined;
Expand Down Expand Up @@ -72,6 +74,14 @@ define([
}
},

/**
* Gets or sets the Property specifying the {@link HeightReference}.
* @memberof BoxGraphics.prototype
* @type {Property}
* @default HeightReference.NONE
*/
heightReference : createPropertyDescriptor('heightReference'),

/**
* Gets or sets the boolean Property specifying the visibility of the box.
* @memberof BoxGraphics.prototype
Expand Down Expand Up @@ -154,6 +164,7 @@ define([
if (!defined(result)) {
return new BoxGraphics(this);
}
result.heightReference = this.heightReference;
result.dimensions = this.dimensions;
result.show = this.show;
result.material = this.material;
Expand All @@ -179,6 +190,7 @@ define([
}
//>>includeEnd('debug');

this.heightReference = defaultValue(this.heightReference, source.heightReference);
this.dimensions = defaultValue(this.dimensions, source.dimensions);
this.show = defaultValue(this.show, source.show);
this.material = defaultValue(this.material, source.material);
Expand Down
Loading