一、Entity API添加
var wyoming = viewer.entities.add({
name : 'Wyoming',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([
-109.080842,45.002073,
-105.91517,45.002073,
-104.058488,44.996596,
-104.053011,43.002989,
-104.053011,41.003906,
-105.728954,40.998429,
-107.919731,41.003906,
-109.04798,40.998429,
-111.047063,40.998429,
-111.047063,42.000709,
-111.047063,44.476286,
-111.05254,45.002073]),
height : 0,
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
wyoming.polygon.height = 200000;
wyoming.polygon.extrudedHeight = 250000;
二、Primitive API添加
Primitive由几何形状和外观两部分组成,Primitive支持的形状和外观如下图所示:
1、几何形状(Geometry):定义了Primitive的结构,例如三角形、线条、点等。
2、外观(Appearance ):定义Primitive的着色,包括GLSL(OpenGL着色语言,OpenGL Shading Language)顶点着色器和片段着色器,以及渲染状态。
//绘制单个图形
var scene = viewer.scene;
scene.primitives.add(new Cesium.RectanglePrimitive({
rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),//设置形状
material : Cesium.Material.fromType('Stripe') //设置材质
}));
//绘制多个图形
var scene = viewer.scene;
var instances = [];
for (var lon = -180.0; lon < 180.0; lon += 5.0) {
for (var lat = -85.0; lat < 85.0; lat += 5.0) {
instances.push(new Cesium.GeometryInstance({
geometry : new Cesium.RectangleGeometry({
rectangle : Cesium.Rectangle.fromDegrees(lon, lat, lon + 5.0, lat + 5.0),
vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
}),
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
}
}));
}
}
scene.primitives.add(new Cesium.Primitive({
geometryInstances : instances,
appearance : new Cesium.PerInstanceColorAppearance()
}));
Entity和Primitive API都可以用于绘制图形,区别在于Entity是在Primitive的基础上进行了封装,用法比较简单。Primitive更接近底层,使用起来比较复杂,但效率要高于Entity。