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

Fix updateBatchTableBoundingSpheres #4461

Merged
merged 5 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 20 additions & 15 deletions Source/Scene/Primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,8 @@ define([
var scratchBoundingSphereCenter2D = new Cartesian3();

function updateBatchTableBoundingSpheres(primitive, frameState) {
if (!defined(primitive._batchTableAttributeIndices.distanceDisplayCondition) || primitive._batchTableBoundingSpheresUpdated) {
var hasDistanceDisplayCondition = defined(primitive._batchTableAttributeIndices.distanceDisplayCondition);
if (!hasDistanceDisplayCondition && primitive._batchTableBoundingSpheresUpdated) {
return;
}

Expand All @@ -1193,20 +1194,24 @@ define([

for (var i = 0; i < length; ++i) {
var boundingSphere = boundingSpheres[i];
var center = boundingSphere.center;
var radius = boundingSphere.radius;

var encodedCenter = EncodedCartesian3.fromCartesian(center, scratchBoundingSphereCenterEncoded);
batchTable.setBatchedAttribute(i, center3DHighIndex, encodedCenter.high);
batchTable.setBatchedAttribute(i, center3DLowIndex, encodedCenter.low);

var cartographic = ellipsoid.cartesianToCartographic(center, scratchBoundingSphereCartographic);
var center2D = projection.project(cartographic, scratchBoundingSphereCenter2D);
encodedCenter = EncodedCartesian3.fromCartesian(center2D, scratchBoundingSphereCenterEncoded);
batchTable.setBatchedAttribute(i, center2DHighIndex, encodedCenter.high);
batchTable.setBatchedAttribute(i, center2DLowIndex, encodedCenter.low);

batchTable.setBatchedAttribute(i, radiusIndex, radius);
var modelMatrix = defaultValue(primitive.modelMatrix, Matrix4.IDENTITY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

var modelMatrix = primitive.modelMatrix;
if (defined(modelMatrix)) {
   boundingSphere = BoundingSphere.transform(boundingSphere, modelMatrix, boundingSphere);
}

boundingSphere = BoundingSphere.transform(boundingSphere, modelMatrix, boundingSphere);

if (hasDistanceDisplayCondition) {
var center = boundingSphere.center;
var radius = boundingSphere.radius;

var encodedCenter = EncodedCartesian3.fromCartesian(center, scratchBoundingSphereCenterEncoded);
batchTable.setBatchedAttribute(i, center3DHighIndex, encodedCenter.high);
batchTable.setBatchedAttribute(i, center3DLowIndex, encodedCenter.low);

var cartographic = ellipsoid.cartesianToCartographic(center, scratchBoundingSphereCartographic);
var center2D = projection.project(cartographic, scratchBoundingSphereCenter2D);
encodedCenter = EncodedCartesian3.fromCartesian(center2D, scratchBoundingSphereCenterEncoded);
batchTable.setBatchedAttribute(i, center2DHighIndex, encodedCenter.high);
batchTable.setBatchedAttribute(i, center2DLowIndex, encodedCenter.low);
batchTable.setBatchedAttribute(i, radiusIndex, radius);
}
}

primitive._batchTableBoundingSpheresUpdated = true;
Expand Down
71 changes: 71 additions & 0 deletions Specs/Scene/PrimitiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defineSuite([
'Core/Math',
'Core/Matrix4',
'Core/PolygonGeometry',
'Core/CylinderGeometry',
'Core/PrimitiveType',
'Core/Rectangle',
'Core/RectangleGeometry',
Expand Down Expand Up @@ -49,6 +50,7 @@ defineSuite([
CesiumMath,
Matrix4,
PolygonGeometry,
CylinderGeometry,
PrimitiveType,
Rectangle,
RectangleGeometry,
Expand Down Expand Up @@ -761,6 +763,75 @@ defineSuite([
expect(scene.renderForSpecs()).toEqual([0, 0, 0, 255]);
});

it('primitive with display condition properly transforms boundingSphere', function() {
var near = 10000.0;
var far = 1000000.0;
var translation = new Cartesian3(10, 20, 30);

var cylinder = new GeometryInstance({
id : 'cylinder',
vertexFormat : PerInstanceColorAppearance.VERTEX_FORMAT,
geometry : new CylinderGeometry({
length : 10,
topRadius : 10,
bottomRadius : 10
}),
attributes : {
color : new ColorGeometryInstanceAttribute(1.0, 1.0, 0.0, 1.0),
show : new ShowGeometryInstanceAttribute(true),
distanceDisplayCondition : new DistanceDisplayConditionGeometryInstanceAttribute(near, far)
}
});

primitive = new Primitive({
geometryInstances : cylinder,
appearance : new PerInstanceColorAppearance(),
modelMatrix : Matrix4.fromTranslation(translation, new Matrix4()),
asynchronous : false
});

scene.primitives.add(primitive);
scene.frameState.scene3DOnly = true;
scene.renderForSpecs();

var boundingSphere = primitive.getGeometryInstanceAttributes('cylinder').boundingSphere;
var center = boundingSphere.center;
expect(center).toEqual(translation);
});

it('primitive without display condition properly transforms boundingSphere', function() {
var translation = new Cartesian3(10, 20, 30);

var cylinder = new GeometryInstance({
id : 'cylinder',
vertexFormat : PerInstanceColorAppearance.VERTEX_FORMAT,
geometry : new CylinderGeometry({
length : 10,
topRadius : 10,
bottomRadius : 10
}),
attributes : {
color : new ColorGeometryInstanceAttribute(1.0, 1.0, 0.0, 1.0),
show : new ShowGeometryInstanceAttribute(true)
}
});

primitive = new Primitive({
geometryInstances : cylinder,
appearance : new PerInstanceColorAppearance(),
modelMatrix : Matrix4.fromTranslation(translation, new Matrix4()),
asynchronous : false
});

scene.primitives.add(primitive);
scene.frameState.scene3DOnly = true;
scene.renderForSpecs();

var boundingSphere = primitive.getGeometryInstanceAttributes('cylinder').boundingSphere;
var center = boundingSphere.center;
expect(center).toEqual(translation);
});

it('getGeometryInstanceAttributes returns same object each time', function() {
primitive = new Primitive({
geometryInstances : rectangleInstance1,
Expand Down