Skip to content

Commit

Permalink
Start of display conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcozzi committed Nov 22, 2014
1 parent 9d489f0 commit d604157
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
14 changes: 11 additions & 3 deletions Source/DataSources/ModelVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ define([
'../Core/defined',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/Math',
'../Core/Matrix3',
'../Core/Matrix4',
'../Core/Quaternion',
'../Core/Transforms',
'../Scene/DistanceDisplayCondition',
'../Scene/Model',
'../Scene/ModelAnimationLoop',
'./Property'
Expand All @@ -18,10 +20,12 @@ define([
defined,
destroyObject,
DeveloperError,
CesiumMath,
Matrix3,
Matrix4,
Quaternion,
Transforms,
DistanceDisplayCondition,
Model,
ModelAnimationLoop,
Property) {
Expand Down Expand Up @@ -114,7 +118,9 @@ define([
delete modelHash[entity.id];
}
model = Model.fromGltf({
url : uri
url : uri,
displayCondition : new DistanceDisplayCondition(0, 1000.0),
loadOnlyIfDisplayCondition : true
});

model.readyToRender.addEventListener(readyToRender, this);
Expand All @@ -138,9 +144,11 @@ define([
orientation = Property.getValueOrUndefined(entity._orientation, time, orientation);
if (!Cartesian3.equals(position, modelData.position) || !Quaternion.equals(orientation, modelData.orientation)) {
if (!defined(orientation)) {
Transforms.eastNorthUpToFixedFrame(position, scene.globe.ellipsoid, model.modelMatrix);
// Transforms.eastNorthUpToFixedFrame(position, scene.globe.ellipsoid, model.modelMatrix);
Transforms.northEastDownToFixedFrame(position, scene.globe.ellipsoid, model.modelMatrix);
Matrix4.multiplyByMatrix3(model.modelMatrix, Matrix3.fromRotationZ(CesiumMath.toRadians(90.0)), model.modelMatrix);
} else {
Matrix4.fromRotationTranslation(Matrix3.fromQuaternion(orientation, matrix3Scratch), position, model.modelMatrix);
// Matrix4.fromRotationTranslation(Matrix3.fromQuaternion(orientation, matrix3Scratch), position, model.modelMatrix);
}
modelData.position = Cartesian3.clone(position, modelData.position);
modelData.orientation = Quaternion.clone(orientation, modelData.orientation);
Expand Down
73 changes: 73 additions & 0 deletions Source/Scene/DistanceDisplayCondition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*global define*/
define([
'../Core/Cartesian3',
'../Core/defaultValue',
'../Core/defineProperties',
'../Core/Matrix4'
], function(
Cartesian3,
defaultValue,
defineProperties,
Matrix4) {
"use strict";

/**
* DOC_TBA
*
* @alias DistanceDisplayCondition
* @constructor
*/
var DistanceDisplayCondition = function(near, far) {
/**
* DOC_TBA
*/
near = defaultValue(near, 0.0);
this._near = near;
this._near2 = near * near;

/**
* DOC_TBA
*/
far = defaultValue(far, Number.MAX_VALUE);
this._far = far;
this._far2 = far * far;
};

defineProperties(DistanceDisplayCondition.prototype, {
near : {
get : function() {
return this._near;
},
set : function(value) {
this._near = value;
this._near2 = value * value;
}
},

far : {
get : function() {
return this._far;
},
set : function(value) {
this._far = value;
this._far2 = value * value;
}
}
});

var scratchPosition = new Cartesian3();

/**
* DOC_TBA
*/
DistanceDisplayCondition.prototype.isVisible = function(primitive, frameState) {
// TODO: need to consider positions, e.g., for a polyline

// Distance to center of primitive's reference frame
var position = Matrix4.getTranslation(primitive.modelMatrix, scratchPosition);
var distance2 = Cartesian3.distanceSquared(position, frameState.camera.positionWC);
return (distance2 >= this._near2) && (distance2 <= this._far2);
};

return DistanceDisplayCondition;
});
22 changes: 21 additions & 1 deletion Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ define([
* @param {Object} [options.gltf] The object for the glTF JSON.
* @param {String} [options.basePath=''] The base path that paths in the glTF JSON are relative to.
* @param {Boolean} [options.show=true] Determines if the model primitive will be shown.
* @param {DisplayCondition} [options.displayCondition] DOC_TBA
* @param {Boolean} [options.loadOnlyIfDisplayCondition] DOC_TBA
* @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
* @param {Number} [options.scale=1.0] A uniform scale applied to this model.
* @param {Number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
Expand Down Expand Up @@ -193,6 +195,16 @@ define([
*/
this.show = defaultValue(options.show, true);

/**
* DOC_TBA
*/
this.displayCondition = options.displayCondition;

/**
* DOC_TBA
*/
this.loadOnlyIfDisplayCondition = defaultValue(options.loadOnlyIfDisplayCondition, false);

/**
* The 4x4 transformation matrix that transforms the model from model to world coordinates.
* When this is the identity matrix, the model is drawn in world coordinates, i.e., Earth's WGS84 coordinates.
Expand Down Expand Up @@ -481,6 +493,8 @@ define([
* @param {String} options.url The url to the .gltf file.
* @param {Object} [options.headers] HTTP headers to send with the request.
* @param {Boolean} [options.show=true] Determines if the model primitive will be shown.
* @param {DisplayCondition} [options.displayCondition] DOC_TBA
* @param {Boolean} [options.loadOnlyIfDisplayCondition] DOC_TBA
* @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
* @param {Number} [options.scale=1.0] A uniform scale applied to this model.
* @param {Number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
Expand Down Expand Up @@ -2198,6 +2212,12 @@ define([
return;
}

var displayConditionPassed = defined(this.displayCondition) ? this.displayCondition.isVisible(this, frameState) : true;
if (this.loadOnlyIfDisplayCondition && !displayConditionPassed) {
// Don't even try to load until the display condition is true
return;
}

if ((this._state === ModelState.NEEDS_LOAD) && defined(this.gltf)) {
this._state = ModelState.LOADING;
this._boundingSphere = computeBoundingSphere(this.gltf);
Expand All @@ -2224,7 +2244,7 @@ define([
}
}

var show = this.show && (this.scale !== 0.0);
var show = this.show && (this.scale !== 0.0) && displayConditionPassed;

if ((show && this._state === ModelState.LOADED) || justLoaded) {
var animated = this.activeAnimations.update(frameState) || this._cesiumAnimationsDirty;
Expand Down

0 comments on commit d604157

Please sign in to comment.