var viewer = new Cesium.Viewer("cesiumContainer");
var scene = viewer.scene;
scene.globe.depthTestAgainstTerrain = true;
if (!scene.pickPositionSupported) {
window.alert(
"This browser does not support pickPosition or getting position from pickFromRay."
);
}
var i;
var drillPick = false;
Sandcastle.addToggleButton("Drill pick", false, function (checked) {
drillPick = checked;
});
var tileset = viewer.scene.primitives.add(
new Cesium.Cesium3DTileset({
url: "../../SampleData/Cesium3DTiles/Tilesets/Tileset/tileset.json",
})
);
tileset.style = new Cesium.Cesium3DTileStyle({
defines: {
alpha: "${id} % 2 === 0 ? 0.5 : 1.0",
},
color: "rgba(255, 255, 255, ${alpha})",
});
viewer.zoomTo(tileset);
var blueCartographic = new Cesium.Cartographic(
-1.3196863177294136,
0.6988508714746624,
0.5
);
var redCartographic = new Cesium.Cartographic(
-1.319671841889412,
0.6989153500784591,
0.0
);
var arrowPositions = [
Cesium.Cartographic.toCartesian(blueCartographic),
Cesium.Cartographic.toCartesian(redCartographic),
];
var arrow = viewer.entities.add({
polyline: {
positions: arrowPositions,
width: 10,
arcType: Cesium.ArcType.NONE,
material: new Cesium.PolylineArrowMaterialProperty(
Cesium.Color.YELLOW
),
},
});
var intersectionMarkers = [];
var pickedFeatures = [];
var objectsToExclude = [];
function reset() {
objectsToExclude = [arrow];
for (i = 0; i < pickedFeatures.length; ++i) {
pickedFeatures[i].color = Cesium.Color.fromAlpha(
Cesium.Color.WHITE,
pickedFeatures[i].color.alpha
);
}
for (i = 0; i < intersectionMarkers.length; ++i) {
viewer.entities.remove(intersectionMarkers[i]);
objectsToExclude.push(intersectionMarkers[i]);
}
pickedFeatures.length = 0;
intersectionMarkers.length = 0;
}
function showIntersections(results) {
for (i = 0; i < results.length; ++i) {
var object = results[i].object;
if (object instanceof Cesium.Cesium3DTileFeature) {
pickedFeatures.push(object);
object.color = Cesium.Color.fromAlpha(
Cesium.Color.RED,
object.color.alpha
);
}
intersectionMarkers.push(
viewer.entities.add({
position: results[i].position,
ellipsoid: {
radii: new Cesium.Cartesian3(3.0, 3.0, 3.0),
material: Cesium.Color.RED,
},
})
);
}
}
function pickFromRay() {
reset();
var start = Cesium.Cartographic.toCartesian(blueCartographic);
var end = Cesium.Cartographic.toCartesian(redCartographic);
console.log(start);
var direction = Cesium.Cartesian3.normalize(
Cesium.Cartesian3.subtract(end, start, new Cesium.Cartesian3()),
new Cesium.Cartesian3()
);
var ray = new Cesium.Ray(start, direction);
var results = [];
if (drillPick) {
console.log("1");
} else {
var result = scene.pickFromRay(ray, objectsToExclude);
if (Cesium.defined(result)) {
console.log(result.exclude);
results = [result];
}
}
arrow.polyline.positions = [
Cesium.Cartographic.toCartesian(blueCartographic),
Cesium.Cartographic.toCartesian(redCartographic),
];
showIntersections(results);
}
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
handler.setInputAction(function (movement) {
var redCartesian = scene.pickPosition(movement.position);
redCartographic = Cesium.Cartographic.fromCartesian(redCartesian);
pickFromRay();
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
window.setTimeout(function () {
pickFromRay();
}, 2000.0);