-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_contour.js
211 lines (187 loc) · 6.18 KB
/
map_contour.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//控制高程菜单开关
function toggleToolbarById(id){
$('#'+id).toggleClass("toolbar-visible")
}
MapControl.prototype.contour_init = function() {
var me = this;
var minHeight = me.opts.altitude_range.min; // approximate dead sea elevation
var maxHeight = me.opts.altitude_range.max; // approximate everest elevation
var contourColor = Cesium.Color.RED.clone();
var contourUniforms = {};
var shadingUniforms = {};
// The viewModel tracks the state of our mini application.
viewModel = {
enableContour: false,
contourSpacing: 100.0,
contourWidth: 1.0,
selectedShading: "none",
changeColor: function () {
contourUniforms.color = Cesium.Color.fromRandom(
{alpha: 1.0},
contourColor
);
},
};
var getElevationContourMaterial = function() {
// Creates a composite material with both elevation shading and contour lines
return new Cesium.Material({
fabric: {
type: "ElevationColorContour",
materials: {
contourMaterial: {
type: "ElevationContour",
},
elevationRampMaterial: {
type: "ElevationRamp",
},
},
components: {
diffuse: "contourMaterial.alpha == 0.0 ? elevationRampMaterial.diffuse : contourMaterial.diffuse",
alpha: "max(contourMaterial.alpha, elevationRampMaterial.alpha)",
},
},
translucent: false,
});
}
var getSlopeContourMaterial = function() {
// Creates a composite material with both slope shading and contour lines
return new Cesium.Material({
fabric: {
type: "SlopeColorContour",
materials: {
contourMaterial: {
type: "ElevationContour",
},
slopeRampMaterial: {
type: "SlopeRamp",
},
},
components: {
diffuse:"contourMaterial.alpha == 0.0 ? slopeRampMaterial.diffuse : contourMaterial.diffuse",
alpha: "max(contourMaterial.alpha, slopeRampMaterial.alpha)",
},
},
translucent: false,
});
}
var getAspectContourMaterial = function() {
// Creates a composite material with both aspect shading and contour lines
return new Cesium.Material({
fabric: {
type: "AspectColorContour",
materials: {
contourMaterial: {
type: "ElevationContour",
},
aspectRampMaterial: {
type: "AspectRamp",
},
},
components: {
diffuse:"contourMaterial.alpha == 0.0 ? aspectRampMaterial.diffuse : contourMaterial.diffuse",
alpha: "max(contourMaterial.alpha, aspectRampMaterial.alpha)",
},
},
translucent: false,
});
}
var getColorRamp = function(selectedShading) {
var elevationRamp = [0.0, 0.045, 0.1, 0.15, 0.37, 0.54, 1.0];
var slopeRamp = [0.0, 0.29, 0.5, Math.sqrt(2) / 2, 0.87, 0.91, 1.0];
var aspectRamp = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0];
var ramp = document.createElement("canvas");
ramp.width = 100;
ramp.height = 1;
var ctx = ramp.getContext("2d");
var values;
if (selectedShading === "elevation") {
values = elevationRamp;
} else if (selectedShading === "slope") {
values = slopeRamp;
} else if (selectedShading === "aspect") {
values = aspectRamp;
}
var grd = ctx.createLinearGradient(0, 0, 100, 0);
grd.addColorStop(values[0], "#000000"); //black
grd.addColorStop(values[1], "#2747E0"); //blue
grd.addColorStop(values[2], "#D33B7D"); //pink
grd.addColorStop(values[3], "#D33038"); //red
grd.addColorStop(values[4], "#FF9742"); //orange
grd.addColorStop(values[5], "#ffd700"); //yellow
grd.addColorStop(values[6], "#ffffff"); //white
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 100, 1);
return ramp;
}
var updateMaterial = function() {
var hasContour = viewModel.enableContour;
var selectedShading = viewModel.selectedShading;
var globe = me.viewer.scene.globe;
var material;
if (hasContour) {
if (selectedShading === "elevation") {
material = getElevationContourMaterial();
shadingUniforms =
material.materials.elevationRampMaterial.uniforms;
shadingUniforms.minimumHeight = minHeight;
shadingUniforms.maximumHeight = maxHeight;
contourUniforms = material.materials.contourMaterial.uniforms;
} else if (selectedShading === "slope") {
material = getSlopeContourMaterial();
shadingUniforms = material.materials.slopeRampMaterial.uniforms;
contourUniforms = material.materials.contourMaterial.uniforms;
} else if (selectedShading === "aspect") {
material = getAspectContourMaterial();
shadingUniforms = material.materials.aspectRampMaterial.uniforms;
contourUniforms = material.materials.contourMaterial.uniforms;
} else {
material = Cesium.Material.fromType("ElevationContour");
contourUniforms = material.uniforms;
}
contourUniforms.width = viewModel.contourWidth;
contourUniforms.spacing = viewModel.contourSpacing;
contourUniforms.color = contourColor;
} else if (selectedShading === "elevation") {
material = Cesium.Material.fromType("ElevationRamp");
shadingUniforms = material.uniforms;
shadingUniforms.minimumHeight = minHeight;
shadingUniforms.maximumHeight = maxHeight;
} else if (selectedShading === "slope") {
material = Cesium.Material.fromType("SlopeRamp");
shadingUniforms = material.uniforms;
} else if (selectedShading === "aspect") {
material = Cesium.Material.fromType("AspectRamp");
shadingUniforms = material.uniforms;
}
if (selectedShading !== "none") {
shadingUniforms.image = getColorRamp(selectedShading);
}
globe.material = material;
}
// Convert the viewModel members into knockout observables.
Cesium.knockout.track(viewModel);
// Bind the viewModel to the DOM elements of the UI that call for it.
var toolbar = document.getElementById(me.opts.toolbar_container);
Cesium.knockout.applyBindings(viewModel, toolbar);
updateMaterial();
Cesium.knockout
.getObservable(viewModel, "enableContour")
.subscribe(function (newValue) {
updateMaterial();
});
Cesium.knockout
.getObservable(viewModel, "contourWidth")
.subscribe(function (newValue) {
contourUniforms.width = parseFloat(newValue);
});
Cesium.knockout
.getObservable(viewModel, "contourSpacing")
.subscribe(function (newValue) {
contourUniforms.spacing = parseFloat(newValue);
});
Cesium.knockout
.getObservable(viewModel, "selectedShading")
.subscribe(function (value) {
updateMaterial();
});
}