1. 程式人生 > >cesium編程入門(五)繪制形狀

cesium編程入門(五)繪制形狀

cnblogs eve acf gem 場景 編程入門 host 元素 pos

通過Entity添加形狀

先來看一個添加立方體的例子

var viewer = new Cesium.Viewer(‘cesiumContainer‘);
var redBox = **viewer.entities.add**({
  name : ‘Red box with black outline‘,
  position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0),
  box : {
    dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
    material : Cesium.Color.RED.withAlpha(0.5),
    outline : true,
    outlineColor : Cesium.Color.BLACK
  }
});

viewer.zoomTo(viewer.entities);

效果如圖:

技術分享圖片

通過CZML添加

通過CZML也可以添加幾何形狀,而且CZML還可以描述動畫(現在先有個印象,動畫以後介紹)

先來看看官網上的說明,CZML是一種JSON格式的字符串,用於描述與時間有關的動畫場景,CZML包含點、線、地標、模型、和其他的一些圖形元素,並指明了這些元素如何隨時間而變化。某種程度上說, Cesium 和 CZML的關系就像 Google Earth 和 KML。

var czml = [{
    "id" : "document",
    "name" : "box",
    "version" : "1.0"
},{
    "id" : "shape2",
    "name" : "Red box with black outline",
    "position" : {
        "cartographicDegrees" : [-107.0, 40.0, 300000.0]
    },
    "box" : {
        "dimensions" : {
            "cartesian": [400000.0, 300000.0, 500000.0]
        },
        "material" : {
            "solidColor" : {
                "color" : {
                    "rgba" : [255, 0, 0, 128]
                }
            }
        },
        "outline" : true,
        "outlineColor" : {
            "rgba" : [0, 0, 0, 255]
        }
    }
}];

var viewer = new Cesium.Viewer(‘cesiumContainer‘);
var dataSourcePromise = Cesium.CzmlDataSource.load(czml);
viewer.dataSources.add(dataSourcePromise);
viewer.zoomTo(dataSourcePromise);

可以看到單個的形狀和Entity的描述基本一致,只不過是使用JSON語法來描述,關於形狀描述可以參考官方API,下一節列出形狀的相關信息

形狀相關描述

下表來自於官方網站,但是連接改為本地鏈接了

Point 技術分享圖片 entity.point - Reference Build/Documentation
Boxes 技術分享圖片 entity.box - Code example - Reference Build/Documentation
Circles and Ellipses 技術分享圖片 entity.ellipse - Code example - Reference Build/Documentation
Corridor 技術分享圖片 entity.corridor - Code example - Reference Build/Documentation
Cylinder and Cones 技術分享圖片 entity.cylinder - Code example - Reference Build/Documentation
Polygons 技術分享圖片 entity.polygon - Code example - Reference Build/Documentation
Polylines 技術分享圖片 entity.polyline - Code example - Reference Build/Documentation
Polyline Volumes 技術分享圖片 entity.polylineVolume - Code example - Reference Build/Documentation
Rectangles 技術分享圖片 entity.rectangle - Code example - Reference Build/Documentation
Spheres and Ellipsoids 技術分享圖片 entity.ellipsoid - Code example - Reference Build/Documentation
Walls 技術分享圖片 entity.wall - Code example - Reference Build/Documentation

cesium編程入門(五)繪制形狀