fix:update the part of terrian data

This commit is contained in:
user 2024-04-26 15:51:44 +08:00
parent 8ed92a3e84
commit da05acbf28
1 changed files with 334 additions and 10 deletions

View File

@ -15,6 +15,9 @@ onMounted(()=>{
let TDT_IMG_C = "http://{s}.tianditu.gov.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0" +
"&LAYER=img&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
"&style=default&format=tiles&tk=" + TDT_tk;
const cesiumKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyNjI2ZjVjMS0zZTFjLTQ3MDAtYTZlZC02YWFjOWQwNzBjNjIiLCJpZCI6ODM1NTQsImlhdCI6MTY0NTY1NzYzM30.IPqS_sM-s_vzNnK0EAiTy6kpYP5RBYkuMgbqJtKQkPw";
// Cesium.Ion.defaultAccessToken = cesiumKey;
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyNjI2ZjVjMS0zZTFjLTQ3MDAtYTZlZC02YWFjOWQwNzBjNjIiLCJpZCI6ODM1NTQsImlhdCI6MTY0NTY1NzYzM30.IPqS_sM-s_vzNnK0EAiTy6kpYP5RBYkuMgbqJtKQkPw';
viewer = new Cesium.Viewer("cesiumContainer",{
geocoder: false, //
@ -43,17 +46,16 @@ onMounted(()=>{
// maximumLevel: 50,
// show: false
// })
terrain: Cesium.Terrain.fromWorldTerrain({
requestWaterMask: true,
requestVertexNormals: true
})
// terrain: Cesium.Terrain.fromWorldTerrain({
// requestWaterMask: true,
// requestVertexNormals: true
// })
})
// Cesium ion
// viewer.scene.setTerrain(
// new Cesium.Terrain(
// Cesium.CesiumTerrainProvider.fromIonAssetId(1),
// ),
// );
const terrain = new Cesium.Terrain(
Cesium.CesiumTerrainProvider.fromIonAssetId(1),
)
viewer.scene.setTerrain(terrain);
viewer.scene.globe.depthTestAgainstTerrain = false; //
viewer.scene.fxaa = false;
//
@ -72,8 +74,206 @@ onMounted(()=>{
// pitch: Cesium.Math.toRadians(-15.0)
// }
// })
addGeoJson();
//:
var scene = viewer.scene;
var globe = viewer.scene.globe;
//
var tilesToProcess = [];
var quadtree =
viewer.scene.globe._surface._tileProvider._quadtree._tilesToRender;
//
quadtree.forEach(function(tile) {
tilesToProcess.push(tile);
if (tile instanceof Cesium.Cesium3DTileset) return;
});
const MAX_SHORT = 32767;
var terrainProvider=viewer.terrainProvider
// Request
const request = new Cesium.Request({
throttle: true,
throttleByServer: true
});
// terrain.readyEvent.addEventListener(provider => {
// scene.globe.enableLighting = true;
// terrain.provider.errorEvent.addEventListener(error => {
// alert(`Encountered an error while loading terrain tiles! ${error}`);
// });
// requestMultipleTiles(tileCoords, level, provider)
// .then(allTileData => {
// console.log(":", allTileData);
// });
// });
// 使requestMultipleTiles
// terrainProvider.readyPromise.then(() => {
// requestMultipleTiles(tileCoords, level, terrainProvider)
// .then(allTileData => {
// console.log(":", allTileData);
// // .obj
// let objContent = createObjFileContent(allTileData);
// //
// download("terrain.obj", objContent);
// });
// });
//
function requestTileData(x, y, level, terrainProvider) {
return terrainProvider.requestTileGeometry(x, y, level)
.then(terrainData => {
console.log("Terrain tile data for tile " + x + ", " + y + ":", terrainData);
return terrainData; //
})
.catch(error => {
console.error("获取地形数据失败", error);
});
}
//
function requestMultipleTiles(tileCoords, level, terrainProvider) {
let promises = [];
for (let tileCoord of tileCoords) {
let promise = requestTileData(tileCoord.x, tileCoord.y, level, terrainProvider);
promises.push(promise);
}
return Promise.all(promises);
}
// :
const tileCoords = [
{x: 6759, y: 1222},
{x: 6760, y: 1222},
{x: 6761, y: 1222},
{x: 6759, y: 1223},
{x: 6760, y: 1223},
{x: 6761, y: 1223},
//
];
const level = 12; //
//
function processTileData(terrainData, tileCoord, level) {
const rect = new Cesium.GeographicTilingScheme().tileXYToRectangle(tileCoord.x, tileCoord.y, level);
const minimumHeight = terrainData._minimumHeight;
const maximumHeight = terrainData._maximumHeight;
debugger
const indices = terrainData._indices;
const quantizedVertices = terrainData._quantizedVertices;
const vertexCount = quantizedVertices.length / 3;
const vertices = [];
const faces = [];
console.log(quantizedVertices)
for (let i = 0; i < vertexCount; i++) {
const rawU = quantizedVertices[i];
const rawV = quantizedVertices[i + vertexCount];
const rawH = quantizedVertices[i + vertexCount * 2];
const u = rawU / MAX_SHORT;
const v = rawV / MAX_SHORT;
const longitude = Cesium.Math.lerp(rect.west, rect.east, u);
const latitude = Cesium.Math.lerp(rect.south, rect.north, v);
const height = Cesium.Math.lerp(minimumHeight, maximumHeight, rawH / MAX_SHORT);
const carto = new Cesium.Cartographic(longitude, latitude, height);
const cartesian = Cesium.Cartographic.toCartesian(carto);
vertices.push([cartesian.x, cartesian.y, height]);
}
for (let i = 0; i < indices.length; i += 3) {
faces.push([indices[i], indices[i + 1], indices[i + 2]]);
}
console.log("Vertices:", vertices);
console.log("Faces:", faces);
// indices
const flatIndices = [];
for (let i = 0; i < indices.length; i++) {
flatIndices.push(indices[i]);
}
// Uint16Array
const flattenedIndices = new Uint16Array(flatIndices);
const positions = [];
// Cesium.EntityCollection
var entityCollection = new Cesium.EntityCollection();
for(let i=0;i<faces.length;i++){
//
var triangleVertices = [
vertices[faces[i][0]][0], vertices[faces[i][0]][1], vertices[faces[i][0]][2]+3700000,
vertices[faces[i][1]][0], vertices[faces[i][1]][1], vertices[faces[i][1]][2]+3700000,
vertices[faces[i][2]][0], vertices[faces[i][2]][1], vertices[faces[i][2]][2]+3700000];
//
var point1 = new Cesium.Cartesian3(triangleVertices[0], triangleVertices[1], triangleVertices[2]);
var point2 = new Cesium.Cartesian3(triangleVertices[3], triangleVertices[4], triangleVertices[5]);
var point3 = new Cesium.Cartesian3(triangleVertices[6], triangleVertices[7], triangleVertices[8]);
//
var cartographicPoint1 = Cesium.Cartographic.fromCartesian(point1);
var cartographicPoint2 = Cesium.Cartographic.fromCartesian(point2);
var cartographicPoint3 = Cesium.Cartographic.fromCartesian(point3);
//
var longitude1 = Cesium.Math.toDegrees(cartographicPoint1.longitude);
var latitude1 = Cesium.Math.toDegrees(cartographicPoint1.latitude);
console.log(cartographicPoint1.latitude)
var height1 = vertices[faces[i][0]][2];
var longitude2 = Cesium.Math.toDegrees(cartographicPoint2.longitude);
var latitude2 = Cesium.Math.toDegrees(cartographicPoint2.latitude);
var height2 = vertices[faces[i][1]][2];
var longitude3 = Cesium.Math.toDegrees(cartographicPoint3.longitude);
var latitude3 = Cesium.Math.toDegrees(cartographicPoint3.latitude);
var height3 = vertices[faces[i][2]][2];
viewer.entities.add({
name: "三角面",
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights([
longitude1,
latitude1,
height1,
longitude2,
latitude2,
height2,
longitude3,
latitude3,
height3,
]),
//hierarchy: [point1, point2, point3],
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
perPositionHeight: true,
material: Cesium.Color.fromCssColorString("#23B8BA").withAlpha(
0.3
),
outline: true,
outlineColor: Cesium.Color.WHITE,
}
});
}
viewer.zoomTo(viewer.entities);
return { vertices, faces };
}
function requestMultipleTiles(tileCoords, level, terrainProvider) {
let promises = [];
for (let tileCoord of tileCoords) {
let promise = requestTileData(tileCoord.x, tileCoord.y, level, terrainProvider)
.then(terrainData => processTileData(terrainData, tileCoord, level));
promises.push(promise);
}
return Promise.all(promises);
}
}
function draw(type) {
@ -295,6 +495,15 @@ onMounted(()=>{
});
window.viewer = viewer;
window.Cesium = Cesium;
// 线
// viewer.scene.globe._surface._tileProvider._debug.wireframe =true
// ,
viewer.imageryLayers.addImageryProvider(
new Cesium.TileCoordinatesImageryProvider()
);
}
function drawTerrian(){
@ -309,8 +518,10 @@ onMounted(()=>{
fill:true,
material: Cesium.Color.fromRandom()
}),
customQuadtreeTiles: q,
});
})
}
function filterTerrian(){
const positions = window.sourceEntity.polygon.hierarchy._value.positions;
@ -337,6 +548,118 @@ onMounted(()=>{
}
// viewer.entities.
}
function drawTin(){
// Cesium.EntityCollection
var entityCollection = new Cesium.EntityCollection();
const MAX_SHORT = 32767;
viewer.entities.values.forEach(entity=>{
const terrainData = entity._customQuadtreeTiles.data.terrainData._mesh;
const rect = new Cesium.GeographicTilingScheme().tileXYToRectangle(entity._customQuadtreeTiles.x, entity._customQuadtreeTiles.y, entity._customQuadtreeTiles.level);
const minimumHeight = terrainData.minimumHeight;
const maximumHeight = terrainData.maximumHeight;
const indices = terrainData.indices;
const quantizedVertices = terrainData.vertices;
const vertexCount = quantizedVertices.length / 3;
const vertices = [];
const faces = [];
console.log(quantizedVertices)
for (let i = 0; i < vertexCount; i++) {
const rawU = quantizedVertices[i];
const rawV = quantizedVertices[i + vertexCount];
const rawH = quantizedVertices[i + vertexCount * 2];
const u = rawU / MAX_SHORT;
const v = rawV / MAX_SHORT;
const longitude = Cesium.Math.lerp(rect.west, rect.east, u);
const latitude = Cesium.Math.lerp(rect.south, rect.north, v);
const height = Cesium.Math.lerp(minimumHeight, maximumHeight, rawH / MAX_SHORT);
const carto = new Cesium.Cartographic(longitude, latitude, height);
const cartesian = Cesium.Cartographic.toCartesian(carto);
vertices.push([cartesian.x, cartesian.y, height]);
}
for (let i = 0; i < indices.length; i += 3) {
faces.push([indices[i], indices[i + 1], indices[i + 2]]);
}
console.log("Vertices:", vertices);
console.log("Faces:", faces);
// indices
const flatIndices = [];
for (let i = 0; i < indices.length; i++) {
flatIndices.push(indices[i]);
}
// Uint16Array
const flattenedIndices = new Uint16Array(flatIndices);
const positions = [];
for(let i=0;i<faces.length;i++){
//
var triangleVertices = [
vertices[faces[i][0]][0], vertices[faces[i][0]][1], vertices[faces[i][0]][2]+3700000,
vertices[faces[i][1]][0], vertices[faces[i][1]][1], vertices[faces[i][1]][2]+3700000,
vertices[faces[i][2]][0], vertices[faces[i][2]][1], vertices[faces[i][2]][2]+3700000];
//
var point1 = new Cesium.Cartesian3(triangleVertices[0], triangleVertices[1], triangleVertices[2]);
var point2 = new Cesium.Cartesian3(triangleVertices[3], triangleVertices[4], triangleVertices[5]);
var point3 = new Cesium.Cartesian3(triangleVertices[6], triangleVertices[7], triangleVertices[8]);
//
var cartographicPoint1 = Cesium.Cartographic.fromCartesian(point1);
var cartographicPoint2 = Cesium.Cartographic.fromCartesian(point2);
var cartographicPoint3 = Cesium.Cartographic.fromCartesian(point3);
//
var longitude1 = Cesium.Math.toDegrees(cartographicPoint1.longitude);
var latitude1 = Cesium.Math.toDegrees(cartographicPoint1.latitude);
console.log(cartographicPoint1.latitude)
var height1 = vertices[faces[i][0]][2];
var longitude2 = Cesium.Math.toDegrees(cartographicPoint2.longitude);
var latitude2 = Cesium.Math.toDegrees(cartographicPoint2.latitude);
var height2 = vertices[faces[i][1]][2];
var longitude3 = Cesium.Math.toDegrees(cartographicPoint3.longitude);
var latitude3 = Cesium.Math.toDegrees(cartographicPoint3.latitude);
var height3 = vertices[faces[i][2]][2];
entityCollection.add({
name: "三角面",
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights([
longitude1,
latitude1,
height1,
longitude2,
latitude2,
height2,
longitude3,
latitude3,
height3,
]),
//hierarchy: [point1, point2, point3],
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
perPositionHeight: true,
material: Cesium.Color.fromCssColorString("#23B8BA").withAlpha(
0.3
),
outline: true,
outlineColor: Cesium.Color.WHITE,
}
})
}
viewer.dataSources.add(entityCollection);
})
}
function drawPoint(position, config) {
let config_ = config ? config : {};
@ -442,6 +765,7 @@ onMounted(()=>{
<div class="grid-content bg-purple">
<el-button type="primary" @click="drawTerrian()">获取视野内的地形</el-button>
<el-button type="primary" @click="filterTerrian()">过滤地形</el-button>
<el-button type="primary" @click="drawTin()">绘制TIN</el-button>
<!-- <el-button type="primary" @click="draw('polyline')">绘制线</el-button>
<el-button type="primary" @click="draw('polygon')">绘制面</el-button>
<el-button type="primary" @click="clearDrawEntities">清空</el-button> -->