一、圆柱绘制代码
上一篇介绍了多边形的绘制,本篇介绍圆柱的绘制,Cesium
提供的cylinder
类不仅仅可以绘制圆柱,还可以绘制正多边形棱柱以及锥体等,具体代码如下(别忘了使用你自己的Token
,基础环境不知道怎么布置的请参考开发环境搭建),绘制的结果如下图所示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Include the CesiumJS JavaScript and CSS files -->
<script src="https://cesium.com/downloads/cesiumjs/releases/1.96/Build/Cesium/Cesium.js"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.96/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
</head>
<body>
<div id="cesiumContainer" style="position:fixed;left:0px;top:0px;width:100%;height:100%;"></div>
<script>
// Your access token can be found at: https://cesium.com/ion/tokens.
// Replace `your_access_token` with your Cesium ion access token.
Cesium.Ion.defaultAccessToken = '你的Token'; //替换成你的Token
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(90, -20, 110, 90);
// Initialize the Cesium Viewer in the HTML element with the `cesiumContainer` ID.
const viewer = new Cesium.Viewer('cesiumContainer', {
geocoder:true,//控制右上角第一个位置的查找工具
homeButton:true,//控制右上角第二个位置的home图标
sceneModePicker:true,//控制右上角第三个位置的选择视角模式,2d,3d
baseLayerPicker:true,//控制右上角第四个位置的图层选择器
navigationHelpButton:true,//控制右上角第五个位置的导航帮助按钮
animation:false,//控制左下角的动画器件
timeline:false,//控制下方时间线
fullscreenButton:false,//右下角全屏按钮
vrButton:false,//右下角vr按钮
shouldAnimate: true,//允许动画同步
infoBox : true, //不显示点击要素之后显示的信息
terrainProvider: Cesium.createWorldTerrain()
});
viewer._cesiumWidget._creditContainer.style.display="none";//取消版权信息
let cylinder_line4 = viewer.entities.add({
name: "cylinder_line4",
position: new Cesium.Cartesian3.fromDegrees(119.999, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: Cesium.Color.RED,
outline: true,
numberOfVerticalLines: 4,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_line8_clamp_to_ground = viewer.entities.add({
name: "cylinder_clamp_to_ground",
position: new Cesium.Cartesian3.fromDegrees(119.999, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: Cesium.Color.RED,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
outline: true,
numberOfVerticalLines: 8,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_top0 = viewer.entities.add({
name: "cylinder_top0",
position: new Cesium.Cartesian3.fromDegrees(119.999, 30.001, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 0.0,
bottomRadius: 20.0,
material: Cesium.Color.RED,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices3 = viewer.entities.add({
name: "cylinder_slices3",
position: new Cesium.Cartesian3.fromDegrees(120, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: Cesium.Color.ORANGE,
outline: true,
outlineWidth: 2.0,
slices: 3,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices3_top0 = viewer.entities.add({
name: "cylinder_slices3_top0",
position: new Cesium.Cartesian3.fromDegrees(120, 30.001, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 0.0,
bottomRadius: 20.0,
material: Cesium.Color.ORANGE,
outline: true,
outlineWidth: 2.0,
slices: 3,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices4 = viewer.entities.add({
name: "cylinder_slices4",
position: new Cesium.Cartesian3.fromDegrees(120.001, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: Cesium.Color.YELLOW,
outline: true,
outlineWidth: 3.0,
slices: 4,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices4_top0 = viewer.entities.add({
name: "cylinder_slices4_top0",
position: new Cesium.Cartesian3.fromDegrees(120.001, 30.001, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 0.0,
bottomRadius: 20.0,
material: Cesium.Color.YELLOW,
outline: true,
outlineWidth: 3.0,
slices: 4,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices6 = viewer.entities.add({
name: "cylinder_slices6",
position: new Cesium.Cartesian3.fromDegrees(120.002, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: new Cesium.Color(0, 1, 0, 0.5),
outline: true,
outlineWidth: 4.0,
slices: 6,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices6_top0 = viewer.entities.add({
name: "cylinder_slices6_top0",
position: new Cesium.Cartesian3.fromDegrees(120.002, 30.001, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 0.0,
bottomRadius: 20.0,
material: new Cesium.Color(0, 1, 0, 0.5),
outline: true,
outlineWidth: 4.0,
slices: 6,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices8 = viewer.entities.add({
name: "cylinder_slices8",
position: new Cesium.Cartesian3.fromDegrees(120.003, 30, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 20.0,
bottomRadius: 20.0,
material: new Cesium.Color(0, 0, 1, 1.0),
outline: true,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 5.0,
slices: 8,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
let cylinder_slices8_top0 = viewer.entities.add({
name: "cylinder_slices8_top0",
position: new Cesium.Cartesian3.fromDegrees(120.003, 30.001, 200), // 点的经纬度坐标
cylinder: {
length: 100,
topRadius: 0.0,
bottomRadius: 20.0,
material: new Cesium.Color(0, 0, 1, 1.0),
outline: true,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 5.0,
slices: 8,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000.0)
},
})
viewer.flyTo(viewer.entities);
</script>
</body>
</html>
二、图解参数
2.1 name
cylinder
的名称,在鼠标点击点弹出的infoBox
中会显示该名称。
2.2 position
圆柱中心点的坐标(注意是中心点
),默认是地心坐标系,可使用我们更熟悉的经纬度来表示,转换方法是 new Cesium.Cartesian3.fromDegrees(经度,纬度,高程)
2.3 cylinder
圆柱的绘制参数
2.3.1 show
是否显示,true
为显示,false
为不显示,默认为显示
2.3.2 length
圆柱的高度,单位为米,可根据position
的高程
来换算圆柱的顶底高程
,如假设高程为200
,length
为100
,则顶底高程分别为250
、150
(200为圆柱中心点
)
2.3.3 topRadius
圆柱的顶面半径,单位为米
,可配合bottomRadius
来控制圆柱的形状,当两者一直时是顶底相同的柱体,否则为不同的柱体
2.3.4 bottomRadius
圆柱的底面半径,单位为米
,可配合topRadius
来控制圆柱的形状,当两者一直时是顶底相同的柱体,否则为不同的柱体
2.3.5 heightReference
柱体的高程参考面,默认值为Cesium.HeightReference.NONE
,表示使用绝对高程,如果想要柱体贴在地表,可设置为Cesium.HeightReference.CLAMP_TO_GROUND
,上图中左侧的两个红色圆柱分别为贴地与不贴地的效果
2.3.6 material
圆柱的样式,颜色也是其中的一种,目前可以先把这个参数当作设置颜色用,默认为白色
,上图中不同柱体的颜色就通过该参数设置。有些颜色Cesium
直接提供了,如红色为Cesium.Color.RED
;我们也可以使用RGBA
的格式(A
表示透明度),如红色可用new Cesium.Color(1, 0, 0, 1)
表示(rgb的取值为0-1,如果是0-255的可以除以255进行归一化)
,上图中带有透明度的绿色对应的值为new Cesium.Color(0, 1, 0, 0.5)
2.3.7 outline
是否显示柱体的边框,true
为显示,false
为不显示,默认为显示
2.3.8 outlineColor
柱体边框的颜色,默认为黑色,值的格式同material
,上图中右侧的白色边框就是由该参数设置得到
2.3.9 outlineWidth
柱体边框的宽度,默认为1
,此参数我修改后不起作用,原因还未搞清楚
2.3.10 numberOfVerticalLines
柱体边框垂向线的数量,默认为16
,上图中上下两个红色的圆柱的线数量分别为4
和8
2.3.11 slices
顶底面正多边形的边数,默认为128
,数量越大,越近似为圆,我们可利用该参数来绘制正多边形的柱体,如上图中的三棱柱、四棱柱、六棱柱、八棱柱等
2.3.12 distanceDisplayCondition
柱体在该视角距离内可见,其他距离不可见,默认为空,即一直可见。如new Cesium.DistanceDisplayCondition(100.0, 2000.0)
表示在视角离柱体的距离为100
到2000
之间时可看到该柱体,其他距离不显示该柱体,这个参数可用来实现类似百度地图那种不同缩放显示不同内容的功能