Cesium.js怎么压平模型

// 获取模型实例
var model = viewer.scene.primitives.get(0);

// 获取模型的顶点数据
var vertices = model.geometry.attributes.position.values;

// 将顶点数据转换为笛卡尔坐标系
var positions = [];
for (var i = 0; i < vertices.length; i += 3) {
    var position = new Cesium.Cartesian3(vertices[i], vertices[i + 1], vertices[i + 2]);
    positions.push(position);
}

// 计算模型的最低点高度
var minHeight = Number.MAX_VALUE;
for (var i = 0; i < positions.length; i++) {
    var height = viewer.scene.globe.getHeight(positions[i]);
    if (height < minHeight) {
        minHeight = height;
    }
}

// 计算模型的边界框
var boundingSphere = model.boundingSphere;
var boundingBox = Cesium.AxisAlignedBoundingBox.fromBoundingSphere(boundingSphere);

// 将模型的所有顶点高度设置为最低点高度,并将位于边界框外的顶点高度设置为地球表面高度
for (var i = 0; i < positions.length; i++) {
    var position = positions[i];
    if (!Cesium.AxisAlignedBoundingBox.contains(boundingBox, position)) {
        var height = viewer.scene.globe.getHeight(position);
        position.z = height;
    } else {
        position.z = minHeight;
    }
}

// 更新模型的顶点数据
model.geometry.attributes.position.values = Cesium.Cartesian3.packArray(positions);

你可能感兴趣的:(javascript)