Skip to content

Commit

Permalink
Merge pull request #4975 from AnalyticalGraphicsInc/mic-picking
Browse files Browse the repository at this point in the history
ModelInstanceCollection picking
  • Loading branch information
pjcozzi authored Feb 10, 2017
2 parents e4d16ef + 935addc commit 43dc60c
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 68 deletions.
29 changes: 13 additions & 16 deletions Apps/Sandcastle/gallery/development/3D Models Instancing.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
var count = 1024;
var spacing = 0.0002;
var url = '../../SampleData/models/CesiumAir/Cesium_Air.gltf';
var dynamic = false;
var useCollection = true;

var centerLongitude = -123.0744619;
Expand All @@ -67,7 +66,6 @@
var collection = scene.primitives.add(new Cesium.ModelInstanceCollection({
url : url,
instances : instances,
dynamic : dynamic,
debugShowBoundingVolume : debugShowBoundingVolume,
debugWireframe : debugWireframe
}));
Expand Down Expand Up @@ -149,6 +147,19 @@
useCollection ? createCollection(instances) : createModels(instances);
}

// Scale picked instances
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
handler.setInputAction(function(movement) {
var pickedInstance = scene.pick(movement.position);
if (Cesium.defined(pickedInstance)) {
console.log(pickedInstance);
var instance = useCollection ? pickedInstance : pickedInstance.primitive;
var scaleMatrix = Cesium.Matrix4.fromUniformScale(1.1);
var modelMatrix = Cesium.Matrix4.multiply(instance.modelMatrix, scaleMatrix, new Cesium.Matrix4());
instance.modelMatrix = modelMatrix;
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

Sandcastle.addToolbarMenu([ {
text : '1024 instances',
onselect : function() {
Expand Down Expand Up @@ -234,20 +245,6 @@
}
}]);

Sandcastle.addToolbarMenu([ {
text : 'Static',
onselect : function() {
dynamic = false;
reset();
}
}, {
text : 'Dynamic',
onselect : function() {
dynamic = true;
reset();
}
}]);

//Sandcastle_End
Sandcastle.finishedLoading();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/ShaderSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ define([
this.pickColorQualifier = pickColorQualifier;
this.includeBuiltIns = defaultValue(options.includeBuiltIns, true);
}

ShaderSource.prototype.clone = function() {
return new ShaderSource({
sources : this.sources,
Expand Down
21 changes: 16 additions & 5 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2307,21 +2307,32 @@ define([
// Retrieve the compiled shader program to assign index values to attributes
var attributeLocations = {};

var index;
var technique = techniques[materials[primitive.material].technique];
var parameters = technique.parameters;
var attributes = technique.attributes;
var programAttributeLocations = model._rendererResources.programs[technique.program].vertexAttributes;
var program = model._rendererResources.programs[technique.program];
var programVertexAttributes = program.vertexAttributes;
var programAttributeLocations = program._attributeLocations;

// Note: WebGL shader compiler may have optimized and removed some attributes from programAttributeLocations
for (var location in programAttributeLocations){
if (programAttributeLocations.hasOwnProperty(location)) {
var attribute = attributes[location];
var index = programAttributeLocations[location].index;
if (defined(attribute)) {
var parameter = parameters[attribute];
attributeLocations[parameter.semantic] = index;
var vertexAttribute = programVertexAttributes[location];
if (defined(vertexAttribute)) {
index = vertexAttribute.index;
var parameter = parameters[attribute];
attributeLocations[parameter.semantic] = index;
}
} else {
// Pre-created attributes
// Pre-created attributes.
// Some pre-created attributes, like per-instance pickIds, may be compiled out of the draw program
// but should be included in the list of attribute locations for the pick program.
// This is safe to do since programVertexAttributes and programAttributeLocations are equivalent except
// that programVertexAttributes optimizes out unused attributes.
index = programAttributeLocations[location];
attributeLocations[location] = index;
}
}
Expand Down
43 changes: 43 additions & 0 deletions Source/Scene/ModelInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*global define*/
define([
'../Core/defineProperties',
'../Core/Matrix4'
], function(
defineProperties,
Matrix4) {
'use strict';

/**
* @private
*/
function ModelInstance(collection, modelMatrix, instanceId) {
this.primitive = collection;
this._modelMatrix = Matrix4.clone(modelMatrix);
this._instanceId = instanceId;
}

defineProperties(ModelInstance.prototype, {
instanceId : {
get : function() {
return this._instanceId;
}
},
model : {
get : function() {
return this.primitive._model;
}
},
modelMatrix : {
get : function() {
return Matrix4.clone(this._modelMatrix);
},
set : function(value) {
Matrix4.clone(value, this._modelMatrix);
this.primitive.expandBoundingSphere(this._modelMatrix);
this.primitive._dirty = true;
}
}
});

return ModelInstance;
});
Loading

0 comments on commit 43dc60c

Please sign in to comment.