該問題已經反饋給cesium官方;看看官方怎麼回覆吧;
issue地址:
https://github.com/AnalyticalGraphicsInc/cesium/issues/8556
目前使用SimplePolylineGeometry 來構造instance可以(現知的問題是,線條的寬度無法更改了;瞭解了一下webgl中繪製有寬度的線的原理,看起來PolylineGeometry的處理邏輯遠遠低於SimplePolylineGeometry);
https://www.cnblogs.com/dojo-lzz/p/9219290.html
這樣構造的原因是想點擊primitive,單獨修改polyline的顏色
要做到此,需要在構造instance時指定唯一id
在鼠標點擊事件中,修改primitive的屬性來實現
全部代碼:
var instances = [];
for(var lon = -180.0; lon < 180.0; lon += 5.0) {
for(var lat = -90.0; lat < 90.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)
}),
id:lon+"-"+lat,
attributes:{
color:Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({
alpha:0.5
}))
}
}));
}
}
viewer.scene.primitives.add(new Cesium.Primitive({
geometryInstances:instances, //合併
releaseGeometryInstances:false,
//某些外觀允許每個幾何圖形實例分別指定某個屬性,例如:
appearance:new Cesium.PerInstanceColorAppearance()
}));
viewer.camera.setView({
destination : Cesium.Cartesian3.fromDegrees(105.20, 30.55,5000000)
});
var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(movement) {
var pick = viewer.scene.pick(movement.position);
console.log(pick)
var attributes = pick.primitive.getGeometryInstanceAttributes( pick.id );//獲取某個實例的屬性集
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue( Cesium.Color.fromRandom( {
alpha : 1.0
} ) )
},Cesium.ScreenSpaceEventType.LEFT_CLICK);