Skip to content

Commit

Permalink
Remove more picking code in favor of derived commands. Add dev Sandca…
Browse files Browse the repository at this point in the history
…stle example for picking.
  • Loading branch information
bagnell committed Apr 11, 2018
1 parent 1599d31 commit e5ca775
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 209 deletions.
190 changes: 190 additions & 0 deletions Apps/Sandcastle/gallery/development/Picking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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="Picking.">
<meta name="cesium-sandcastle-labels" content="Development">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl : '../../../Source',
waitSeconds : 60
});
</script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
'use strict';
//Sandcastle_Begin
var viewer = new Cesium.Viewer('cesiumContainer', {
selectionIndicator : false,
infoBox : false
});

var scene = viewer.scene;
var handler;

Sandcastle.addToolbarButton('Pick billboard', function() {
var entity = viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
billboard : {
image : '../images/Cesium_Logo_overlay.png'
}
});

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === entity)) {
entity.billboard.color = Cesium.Color.YELLOW;
} else {
entity.billboard.color = Cesium.Color.WHITE;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.addToolbarButton('Pick label', function() {
var entity = viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
label : {
text : 'Label'
}
});

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === entity)) {
entity.label.fillColor = Cesium.Color.YELLOW;
} else {
entity.label.fillColor = Cesium.Color.WHITE;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.addToolbarButton('Pick point', function() {
var entity = viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
point : {
color : Cesium.Color.WHITE,
pixelSize : 15.0
}
});

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === entity)) {
entity.point.color = Cesium.Color.YELLOW;
} else {
entity.point.color = Cesium.Color.WHITE;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.addToolbarButton('Pick polyline collection', function() {
var polylines = scene.primitives.add(new Cesium.PolylineCollection());
var id = 'line';
var line = polylines.add({
positions : Cesium.Cartesian3.fromDegreesArrayHeights([
-84.0, 50.0, 0.0,
-100.0, 30.0, 0.0
]),
width : 5.0,
id : id
});

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === id)) {
line.material.uniforms.color = Cesium.Color.YELLOW;
} else {
line.material.uniforms.color = Cesium.Color.WHITE;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.addToolbarButton('Pick polyline geometry', function() {
var id = 'line';
var primitive = scene.primitives.add(new Cesium.Primitive({
geometryInstances : new Cesium.GeometryInstance({
geometry : new Cesium.PolylineGeometry({
positions : Cesium.Cartesian3.fromDegreesArrayHeights([
-84.0, 50.0, 0.0,
-100.0, 30.0, 0.0
]),
width : 5.0,
vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT
}),
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE)
},
id : id
}),
appearance : new Cesium.PolylineColorAppearance(),
asynchronous : false
}));

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === id)) {
primitive.getGeometryInstanceAttributes(id).color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.YELLOW);
} else {
primitive.getGeometryInstanceAttributes(id).color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.WHITE);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.addToolbarButton('Pick model', function() {
var position = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
var hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(135), 0, 0);
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);

var entity = viewer.entities.add({
position : position,
orientation : orientation,
model : {
uri : '../../SampleData/models/CesiumAir/Cesium_Air.glb',
minimumPixelSize : 256
}
});

handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var pickedObject = scene.pick(movement.endPosition);
if (Cesium.defined(pickedObject) && (pickedObject.id === entity)) {
entity.model.color = Cesium.Color.YELLOW;
} else {
entity.model.color = Cesium.Color.WHITE;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
});

Sandcastle.reset = function() {
viewer.entities.removeAll();
handler = handler && handler.destroy();
};
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
startup(Cesium);
} else if (typeof require === "function") {
require(["Cesium"], startup);
}
</script>
</body>
</html>
69 changes: 11 additions & 58 deletions Source/Scene/PointCloud3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,26 +735,21 @@ define([
});

var drawUniformMap = uniformMap;
var pickId;
var pickIdDeclaration;

if (hasBatchTable) {
drawUniformMap = batchTable.getUniformMapCallback()(uniformMap);
}

var pickUniformMap;

if (hasBatchTable) {
pickUniformMap = batchTable.getPickUniformMapCallback()(uniformMap);
pickId = batchTable.getPickId();
pickIdDeclaration = batchTable.getPickIdDeclarations();
} else {
content._pickId = context.createPickId({
primitive : content._tileset,
content : content
});

pickUniformMap = combine(uniformMap, {
drawUniformMap = combine(uniformMap, {
czm_pickColor : function() {
return content._pickId.color;
}
});
pickId = 'czm_pickColor';
pickIdDeclaration = 'uniform vec4 czm_pickId;';
}

content._opaqueRenderState = RenderState.fromCache({
Expand Down Expand Up @@ -784,21 +779,9 @@ define([
pass : isTranslucent ? Pass.TRANSLUCENT : Pass.CESIUM_3D_TILE,
owner : content,
castShadows : false,
receiveShadows : false
});

content._pickCommand = new DrawCommand({
boundingVolume : undefined, // Updated in update
cull : false, // Already culled by 3D Tiles
modelMatrix : new Matrix4(),
primitiveType : PrimitiveType.POINTS,
vertexArray : vertexArray,
count : pointsLength,
shaderProgram : undefined, // Updated in createShaders
uniformMap : pickUniformMap,
renderState : isTranslucent ? content._translucentRenderState : content._opaqueRenderState,
pass : isTranslucent ? Pass.TRANSLUCENT : Pass.CESIUM_3D_TILE,
owner : content
receiveShadows : false,
pickId : pickId,
pickIdDeclaration : pickIdDeclaration
});
}

Expand Down Expand Up @@ -1149,16 +1132,6 @@ define([
drawFS = batchTable.getFragmentShaderCallback(false, undefined)(drawFS);
}

var pickVS = vs;
var pickFS = fs;

if (hasBatchTable) {
pickVS = batchTable.getPickVertexShaderCallback('a_batchId')(pickVS);
pickFS = batchTable.getPickFragmentShaderCallback()(pickFS);
} else {
pickFS = ShaderSource.createPickFragmentShaderSource(pickFS, 'uniform');
}

var drawCommand = content._drawCommand;
if (defined(drawCommand.shaderProgram)) {
// Destroy the old shader
Expand All @@ -1171,18 +1144,6 @@ define([
attributeLocations : attributeLocations
});

var pickCommand = content._pickCommand;
if (defined(pickCommand.shaderProgram)) {
// Destroy the old shader
pickCommand.shaderProgram.destroy();
}
pickCommand.shaderProgram = ShaderProgram.fromCache({
context : context,
vertexShaderSource : pickVS,
fragmentShaderSource : pickFS,
attributeLocations : attributeLocations
});

try {
// Check if the shader compiles correctly. If not there is likely a syntax error with the style.
drawCommand.shaderProgram._bind();
Expand Down Expand Up @@ -1329,8 +1290,6 @@ define([
}
}

Matrix4.clone(this._drawCommand.modelMatrix, this._pickCommand.modelMatrix);

var boundingVolume;
if (defined(this._tile._contentBoundingVolume)) {
boundingVolume = this._mode === SceneMode.SCENE3D ? this._tile._contentBoundingVolume.boundingSphere : this._tile._contentBoundingVolume2D.boundingSphere;
Expand All @@ -1339,7 +1298,6 @@ define([
}

this._drawCommand.boundingVolume = boundingVolume;
this._pickCommand.boundingVolume = boundingVolume;
}

this._drawCommand.castShadows = ShadowMode.castShadows(tileset.shadows);
Expand All @@ -1362,12 +1320,9 @@ define([
var commandList = frameState.commandList;

var passes = frameState.passes;
if (passes.render) {
if (passes.render || passes.pick) {
commandList.push(this._drawCommand);
}
if (passes.pick) {
commandList.push(this._pickCommand);
}
};

/**
Expand All @@ -1382,11 +1337,9 @@ define([
*/
PointCloud3DTileContent.prototype.destroy = function() {
var command = this._drawCommand;
var pickCommand = this._pickCommand;
if (defined(command)) {
command.vertexArray = command.vertexArray && command.vertexArray.destroy();
command.shaderProgram = command.shaderProgram && command.shaderProgram.destroy();
pickCommand.shaderProgram = pickCommand.shaderProgram && pickCommand.shaderProgram.destroy();
}
this._batchTable = this._batchTable && this._batchTable.destroy();
return destroyObject(this);
Expand Down
Loading

0 comments on commit e5ca775

Please sign in to comment.