Skip to content

Commit

Permalink
Re-add per instance bounding spheres.
Browse files Browse the repository at this point in the history
  • Loading branch information
bagnell committed Sep 22, 2016
1 parent 3c3febc commit 9d7f49f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Source/Core/Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ define([
/**
* @private
*/
this.boundingSphereCV = undefined;
this.boundingSphereCV = options.boundingSphereCV;
}

/**
Expand Down
42 changes: 23 additions & 19 deletions Source/Scene/Primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ define([

this._batchTable = undefined;
this._batchTableAttributeIndices = undefined;
this._instanceBoundingSpheres = undefined;
this._instanceBoundingSpheresCV = undefined;
}

defineProperties(Primitive.prototype, {
Expand Down Expand Up @@ -1033,6 +1035,8 @@ define([
primitive._attributeLocations = result.attributeLocations;
primitive.modelMatrix = Matrix4.clone(result.modelMatrix, primitive.modelMatrix);
primitive._pickOffsets = result.pickOffsets;
primitive._instanceBoundingSpheres = result.boundingSpheres;
primitive._instanceBoundingSpheresCV = result.boundingSpheresCV;

if (defined(primitive._geometries) && primitive._geometries.length > 0) {
primitive._state = PrimitiveState.COMBINED;
Expand Down Expand Up @@ -1091,6 +1095,8 @@ define([
primitive._attributeLocations = result.attributeLocations;
primitive.modelMatrix = Matrix4.clone(result.modelMatrix, primitive.modelMatrix);
primitive._pickOffsets = result.pickOffsets;
primitive._instanceBoundingSpheres = result.boundingSpheres;
primitive._instanceBoundingSpheresCV = result.boundingSpheresCV;

if (defined(primitive._geometries) && primitive._geometries.length > 0) {
primitive._state = PrimitiveState.COMBINED;
Expand Down Expand Up @@ -1553,7 +1559,18 @@ define([
};
}

var readOnlyInstanceAttributesScratch = ['boundingSphere', 'boundingSphereCV'];
function createBoundingSphereProperties(primitive, properties, index) {
properties.boundingSphere = {
get : function() {
return primitive._instanceBoundingSpheres[index];
}
};
properties.boundingSphereCV = {
get : function() {
return primitive._instanceBoundingSpheresCV[index];
}
};
}

/**
* Returns the modifiable per-instance attributes for a {@link GeometryInstance}.
Expand Down Expand Up @@ -1603,28 +1620,16 @@ define([
var perInstanceAttributeIndices = this._batchTableAttributeIndices;
attributes = {};
var properties = {};
var hasProperties = false;

for (var name in perInstanceAttributeIndices) {
if (perInstanceAttributeIndices.hasOwnProperty(name)) {
var attributeIndex = perInstanceAttributeIndices[name];

hasProperties = true;
properties[name] = {
get : createGetFunction(batchTable, index, attributeIndex)
};

var createSetter = true;
var readOnlyAttributes = readOnlyInstanceAttributesScratch;
length = readOnlyAttributes.length;
for (var j = 0; j < length; ++j) {
if (name === readOnlyInstanceAttributesScratch[j]) {
createSetter = false;
break;
}
}

readOnlyAttributes = this._readOnlyInstanceAttributes;
var readOnlyAttributes = this._readOnlyInstanceAttributes;
if (createSetter && defined(readOnlyAttributes)) {
length = readOnlyAttributes.length;
for (var k = 0; k < length; ++k) {
Expand All @@ -1641,9 +1646,8 @@ define([
}
}

if (hasProperties) {
defineProperties(attributes, properties);
}
createBoundingSphereProperties(this, properties, index);
defineProperties(attributes, properties);

this._lastPerInstanceAttributeIndex = index;
this._perInstanceAttributeCache[index] = attributes;
Expand Down Expand Up @@ -1705,14 +1709,14 @@ define([
}
this._pickIds = undefined;

this._batchTable = this._batchTable && this._batchTable.destroy();

//These objects may be fairly large and reference other large objects (like Entities)
//We explicitly set them to undefined here so that the memory can be freed
//even if a reference to the destroyed Primitive has been kept around.
this._instanceIds = undefined;
this._perInstanceAttributeCache = undefined;
this._perInstanceAttributeLocations = undefined;
this._attributeLocations = undefined;
this._dirtyAttributes = undefined;

return destroyObject(this);
};
Expand Down
79 changes: 75 additions & 4 deletions Source/Scene/PrimitivePipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ define([
var geometries;
var attributeLocations;
var instances = parameters.instances;
var length = instances.length;

if (instances.length > 0) {
if (length > 0) {
geometries = geometryPipeline(parameters);
if (geometries.length > 0) {
attributeLocations = GeometryPipeline.createAttributeLocations(geometries[0]);
Expand All @@ -291,11 +292,35 @@ define([
pickOffsets = createInstancePickOffsets(instances, geometries);
}

var boundingSpheres = new Array(length);
var boundingSpheresCV = new Array(length);
for (var i = 0; i < length; ++i) {
var instance = instances[i];
var geometry = instance.geometry;
if (defined(geometry)) {
boundingSpheres[i] = geometry.boundingSphere;
boundingSpheresCV[i] = geometry.boundingSphereCV;
}

var eastHemisphereGeometry = instance.eastHemisphereGeometry;
var westHemisphereGeometry = instance.westHemisphereGeometry;
if (defined(eastHemisphereGeometry) && defined(westHemisphereGeometry)) {
if (defined(eastHemisphereGeometry.boundingSphere) && defined(westHemisphereGeometry.boundingSphere)) {
boundingSpheres[i] = BoundingSphere.union(eastHemisphereGeometry.boundingSphere, westHemisphereGeometry.boundingSphere);
}
if (defined(eastHemisphereGeometry.boundingSphereCV) && defined(westHemisphereGeometry.boundingSphereCV)) {
boundingSpheresCV[i] = BoundingSphere.union(eastHemisphereGeometry.boundingSphereCV, westHemisphereGeometry.boundingSphereCV);
}
}
}

return {
geometries : geometries,
modelMatrix : parameters.modelMatrix,
attributeLocations : attributeLocations,
pickOffsets : pickOffsets
pickOffsets : pickOffsets,
boundingSpheres : boundingSpheres,
boundingSpheresCV : boundingSpheresCV
};
};

Expand Down Expand Up @@ -511,6 +536,7 @@ define([
primitiveType : primitiveType,
geometryType : geometryType,
boundingSphere : boundingSphere,
boundingSphereCV : boundingSphereCV,
indices : indices,
attributes : attributes
});
Expand Down Expand Up @@ -614,6 +640,43 @@ define([
};
};

function packBoundingSpheres(boundingSpheres) {
var length = boundingSpheres.length;
var bufferLength = 1 + (BoundingSphere.packedLength + 1) * length;
var buffer = new Float32Array(bufferLength);

var bufferIndex = 0;
buffer[bufferIndex++] = length;

for (var i = 0; i < length; ++i) {
var bs = boundingSpheres[i];
if (!defined(bs)) {
buffer[bufferIndex++] = 0.0;
} else {
buffer[bufferIndex++] = 1.0;
BoundingSphere.pack(boundingSpheres[i], buffer, bufferIndex);
}
bufferIndex += BoundingSphere.packedLength;
}

return buffer;
}

function unpackBoundingSpheres(buffer) {
var result = new Array(buffer[0]);
var count = 0;

var i = 1;
while (i < buffer.length) {
if(buffer[i++] === 1.0) {
result[count++] = BoundingSphere.unpack(buffer, i);
}
i += BoundingSphere.packedLength;
}

return result;
}

/**
* @private
*/
Expand All @@ -622,11 +685,17 @@ define([
transferGeometries(results.geometries, transferableObjects);
}

var packedBoundingSpheres = packBoundingSpheres(results.boundingSpheres);
var packedBoundingSpheresCV = packBoundingSpheres(results.boundingSpheresCV);
transferableObjects.push(packedBoundingSpheres.buffer, packedBoundingSpheresCV.buffer);

return {
geometries : results.geometries,
attributeLocations : results.attributeLocations,
modelMatrix : results.modelMatrix,
pickOffsets : results.pickOffsets
pickOffsets : results.pickOffsets,
boundingSpheres : packedBoundingSpheres,
boundingSpheresCV : packedBoundingSpheresCV
};
};

Expand All @@ -638,7 +707,9 @@ define([
geometries : packedResult.geometries,
attributeLocations : packedResult.attributeLocations,
modelMatrix : packedResult.modelMatrix,
pickOffsets : packedResult.pickOffsets
pickOffsets : packedResult.pickOffsets,
boundingSpheres : unpackBoundingSpheres(packedResult.boundingSpheres),
boundingSpheresCV : unpackBoundingSpheres(packedResult.boundingSpheresCV)
};
};

Expand Down

0 comments on commit 9d7f49f

Please sign in to comment.