2023-01-12
1.sampleTerrainMostDetailed

根據提供的terrainPrivider和點的弧度坐標計算出當前點的高度資訊,
var p = new Cesium.Cartographic.fromCartesian(new Cesium.Cartesian3.fromDegrees(103.8603, 30.704)); let promise = Cesium.sampleTerrainMostDetailed(this.viewer.terrainProvider, [p]) Promise.resolve(promise).then(function(updatedPositions) { console.log(`updatedPositions為:${updatedPositions}`); });
updatePositions為:(1.8127041971090665, 0.535885893532339, 495.32088938674565)
獲得了高度就可以去調整模型的位置了
2.heightReference屬性
var entity6 = this.viewer.entities.add({ id:6, position:new Cesium.Cartesian3.fromDegrees(103.8603, 30.704,0), //設定朝向和翻滾角度 orientation:orientation, model:{ uri:"../../../static/3DModel/higokumaru__honkai_impact_3rd/scene.gltf", show:true, scale: 5.0 , silhouetteColor : Cesium.Color.YELLOW, silhouetteSize : 0, colorBlendMode:Cesium.ColorBlendMode.MIX, colorBlendAmount: 0, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND } })
這樣的前提是需要地形先加載好,不然的話模型會出現在地心
這種情況就是模型加載好了地形沒加載好,找不到橢球就沒法計算高度

3.3dtile矩陣變換
var tileset = new Cesium.Cesium3DTileset({ url: "../../../static/3DModel/sicauOSM/tileset.json", }); this.viewer.scene.primitives.add(tileset); console.log(tileset); //3dtile加載完成后執行,進行位置變化 tileset.readyPromise.then(function(tileset) { //高度偏差,向上是正數,向下是負數 var heightOffset = 500.0; //計算tileset的系結范圍 var boundingSphere = tileset.boundingSphere; //計算中心點位置 /** * fromCartesian 方法是用經緯度和高度定義一個位置 * A position defined by longitude, latitude, and height. */ var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center); //計算中心點位置的地表坐標 /** * Cartesian3 是一個3D點 笛卡爾坐標 * Cartesian2 是螢屏坐標 * cartographic 是橢球坐標 * fromRadians 方法 Returns a Cartesian3 position from longitude and latitude values given in radians(弧度). * @param longitude * @param latitude * @param height * 因為建筑模型沒他所在高度資訊,所以填0 */ var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0); //偏移后的坐標,也就是中心點本應在的高度(海拔) var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, heightOffset); /** * subtract 方法 Computes the componentwise difference of two Cartesians. * 計算兩個笛卡爾坐標的成分差異 * @param 就是兩個要計算的坐標 * @param 第三個引數是要保存的結果 */ var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3()); //tileset.modelMatrix轉換 /** * Creates a Matrix4 instance from a Cartesian3 representing the translation. * @param {Cartesian3} translation - The upper right portion of the matrix representing the translation. * @param { Matrix4} result - The object in which the result will be stored, if undefined a new instance will be created. * Cesium中使用Matrix4作為處理線性變換和位移變換的仿射矩陣 * 三維空間的轉換矩陣通常是3x3的就可以 * 但是為了同時滿足位移的需要增加了一個維度使用4x4的矩陣 */ tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation); /** * 定位到3dtiles的位置,也就是讓攝像頭對準這個區域 * viewBoundingSphere 方法 * Sets the camera so that the current view contains the provided bounding sphere. * @param boundingSphere - The bounding sphere to view, in world coordinates. * @param offset - The offset from the target in the local east-north-up reference frame centered at the target. */ this.viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -20, 0)); });
cesium中經常用到矩陣變換,基礎要打好
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/541870.html
標籤:GIS
上一篇:2022關于賞金獵人的一些事
下一篇:2022關于賞金獵人的一些事
