關於viewer
var viewer = new Cesium.Viewer('cesiumContainer', {
animation: false,
baseLayerPicker: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
timeline: false,
navigationHelpButton: false,
fullscreenButton: false,
infoBox: false,
selectionIndicator: false,
zoomIndicatorContainer: false,
navigation: false,
})
window.viewer = viewer
var scene = viewer.scene
scene.lightSource.ambientLightColor = new Cesium.Color(0.4, 0.4, 0.4, 0.4);
viewer.scene.colorCorrection.show = true;
viewer.scene.colorCorrection.saturation = 1;
viewer.scene.colorCorrection.brightness = 0.4;
viewer.scene.colorCorrection.contrast = 1;
viewer.scene.colorCorrection.hue = 0;
viewer.scene.bloomEffect.show = true;
viewer.scene.hdrEnabled = true;
viewer._cesiumWidget._creditContainer.style.display = 'none'
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
關於layers
Cesium.when(promise, (layers) => {
layers.forEach((res, i) => {
res.selectedColor = ''
var hyp = new Cesium.HypsometricSetting();
setHypsometric(res);
function setHypsometric(layer) {
hyp.emissionTextureUrl = "/static/img/speedline.jpg";
hyp.emissionTexCoordUSpeed = 0.25;
layer.hypsometricSetting = {
hypsometricSetting: hyp,
}
};
})
關於事件
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas)
handler.setInputAction(function (e) {
...
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
viewer.pickEvent.addEventListener(function (feature) {
console.log(feature)
})
viewer.scene.camera.changed.addEventListener(function () {
var position = viewer.camera.position
var cartographic = Cesium.Cartographic.fromCartesian(position)
})
實體新增
viewer.entities.add({
name: 'name',
id: 'id',
position: new Cesium.CallbackProperty(function () {
...
return ...
}, false),
billboard: {
image: 'URL',
scale: 0.5,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(57, 210),
},
})
viewer.scene.primitives.add(Cesium.Model.fromGltf({
id:'id',
url:'XXX.glb',
modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84,fixedFrameTransforms),
}))
viewer.scene.primitives.add(new Cesium.Primitive({
id:'id',
geometryInstances:instance,
appearance: new Cesium.MaterialAppearance({
closed:false,
material:material,
}),
show:true,
}))
var tileset = viewer.scene.primitives.add(
new Cesium.Cesium3DTileset({
url: 'XXX.json',
})
)
tileset.readyPromise.then(function (tileset) {
viewer.scene.primitives.add(tileset)
var heightOffset = 2000.0
var boundingSphere = tileset.boundingSphere
var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center)
var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height)
var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, heightOffset)
var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3())
tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
常用方法
viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(),
orientation: {
heading: Cesium.Math.toRadians(50),
pitch: Cesium.Math.toRadians(-5),
roll: 0,
},
})
viewer.camera.flyTo({
destination: new Cesium.Cartesian3.fromDegrees(),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(0),
roll: 0.0,
},
})
關於彈窗
var el = document.getElementById('pbubble');
el.style.display='block'
viewer.scene.postRender.addEventListener(function () {
if (position) {
var canvasHeight = scene.canvas.height
var windowPosition = new Cesium.Cartesian2()
Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, position, windowPosition)
el.style.bottom = canvasHeight - windowPosition.y + 45 + 'px'
el.style.left = windowPosition.x - 70 + 'px'
el.style.visibility = 'visible'
el.innerHTML="總車位12<br>剩餘10"
}
})