Skip to content

Commit 58ba7b5

Browse files
authored
Merge pull request #6692 from OmarShehata/terrain-drawing-example
Added terrain drawing sandcastle example
2 parents 488ca67 + ee49788 commit 58ba7b5

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
7+
<meta name="description" content="Draw lines and polygons on terrain with mouse clicks.">
8+
<meta name="cesium-sandcastle-labels" content="Showcases">
9+
<title>Cesium Demo</title>
10+
<script type="text/javascript" src="../Sandcastle-header.js"></script>
11+
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
12+
<script type="text/javascript">
13+
require.config({
14+
baseUrl : '../../../Source',
15+
waitSeconds : 60
16+
});
17+
</script>
18+
</head>
19+
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
20+
<style>
21+
@import url(../templates/bucket.css);
22+
</style>
23+
<div id="cesiumContainer" class="fullSize"></div>
24+
<div id="loadingOverlay"><h1>Loading...</h1></div>
25+
<div id="toolbar">
26+
<table class="infoPanel">
27+
<tbody>
28+
<tr>
29+
<td>Left click to add a vertex.</td>
30+
</tr>
31+
<tr>
32+
<td>Right click to start new shape.</td>
33+
</tr>
34+
</tbody>
35+
</table>
36+
</div>
37+
<script id="cesium_sandcastle_script">
38+
function startup(Cesium) {
39+
'use strict';
40+
//Sandcastle_Begin
41+
var viewer = new Cesium.Viewer('cesiumContainer', {
42+
selectionIndicator : false,
43+
infoBox : false,
44+
terrainProvider : Cesium.createWorldTerrain()
45+
});
46+
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
47+
function createPoint(worldPosition) {
48+
var point = viewer.entities.add({
49+
position : worldPosition,
50+
point : {
51+
color : Cesium.Color.WHITE,
52+
pixelSize : 5,
53+
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
54+
}
55+
});
56+
return point;
57+
}
58+
var drawingMode = 'line';
59+
function drawShape(positionData) {
60+
var shape;
61+
if (drawingMode === 'line') {
62+
shape = viewer.entities.add({
63+
polyline : {
64+
positions : positionData,
65+
clampToGround : true,
66+
width : 3
67+
}
68+
});
69+
}
70+
else if (drawingMode === 'polygon') {
71+
shape = viewer.entities.add({
72+
polygon: {
73+
hierarchy: positionData,
74+
outline: true,
75+
material: new Cesium.ColorMaterialProperty(Cesium.Color.WHITE.withAlpha(0.7))
76+
}
77+
});
78+
}
79+
return shape;
80+
}
81+
var activeShapePoints = [];
82+
var activeShape;
83+
var floatingPoint;
84+
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
85+
handler.setInputAction(function(event) {
86+
// We use `viewer.scene.pickPosition` here instead of `viewer.camera.pickEllipsoid` so that
87+
// we get the correct point when mousing over terrain.
88+
var earthPosition = viewer.scene.pickPosition(event.position);
89+
// `earthPosition` will be undefined if our mouse is not over the globe.
90+
if (Cesium.defined(earthPosition)) {
91+
if (activeShapePoints.length === 0) {
92+
floatingPoint = createPoint(earthPosition);
93+
activeShapePoints.push(earthPosition);
94+
var dynamicPositions = new Cesium.CallbackProperty(function () {
95+
return activeShapePoints;
96+
}, false);
97+
activeShape = drawShape(dynamicPositions);
98+
}
99+
activeShapePoints.push(earthPosition);
100+
createPoint(earthPosition);
101+
}
102+
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
103+
104+
handler.setInputAction(function(event) {
105+
if (Cesium.defined(floatingPoint)) {
106+
var newPosition = viewer.scene.pickPosition(event.endPosition);
107+
if (Cesium.defined(newPosition)) {
108+
floatingPoint.position.setValue(newPosition);
109+
activeShapePoints.pop();
110+
activeShapePoints.push(newPosition);
111+
}
112+
}
113+
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
114+
// Redraw the shape so it's not dynamic and remove the dynamic shape.
115+
function terminateShape() {
116+
activeShapePoints.pop();
117+
drawShape(activeShapePoints);
118+
viewer.entities.remove(floatingPoint);
119+
viewer.entities.remove(activeShape);
120+
floatingPoint = undefined;
121+
activeShape = undefined;
122+
activeShapePoints = [];
123+
}
124+
handler.setInputAction(function(event) {
125+
terminateShape();
126+
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
127+
128+
var options = [{
129+
text : 'Draw Lines',
130+
onselect : function() {
131+
terminateShape();
132+
drawingMode = 'line';
133+
}
134+
}, {
135+
text : 'Draw Polygons',
136+
onselect : function() {
137+
terminateShape();
138+
drawingMode = 'polygon';
139+
}
140+
}];
141+
142+
Sandcastle.addToolbarMenu(options);
143+
// Zoom in to an area with mountains
144+
viewer.camera.lookAt(Cesium.Cartesian3.fromDegrees(-122.2058, 46.1955, 1000.0), new Cesium.Cartesian3(5000.0, 5000.0, 5000.0));
145+
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
146+
//Sandcastle_End
147+
Sandcastle.finishedLoading();
148+
}
149+
if (typeof Cesium !== "undefined") {
150+
startup(Cesium);
151+
} else if (typeof require === "function") {
152+
require(["Cesium"], startup);
153+
}
154+
</script>
155+
</body>
156+
</html>
10.3 KB
Loading

0 commit comments

Comments
 (0)