cesium压平实现代码
时间: 2023-06-29 19:03:08 浏览: 193
以下是Cesium压平实现代码的示例:
```javascript
// 获取模型的顶点
var positions = model.geometry.attributes.position.values;
// 计算模型的中心点
var center = Cesium.BoundingSphere.fromVertices(positions).center;
// 将顶点转换为笛卡尔坐标
for (var i = 0; i < positions.length; i += 3) {
var position = Cesium.Cartesian3.fromArray(positions, i);
positions[i] = position.x - center.x;
positions[i + 1] = position.y - center.y;
positions[i + 2] = position.z - center.z;
}
// 压平模型
Cesium.Matrix4.multiplyByTranslation(
model.modelMatrix,
center,
model.modelMatrix
);
model.readyPromise.then(function (model) {
var boundingSphere = model.boundingSphere;
var height = boundingSphere.radius;
var plane = new Cesium.Plane(
new Cesium.Cartesian3(0, 0, 1),
-height
);
var translation = Cesium.Transforms.eastNorthUpToFixedFrame(
boundingSphere.center
);
var rotation = Cesium.Quaternion.fromRotationMatrix(translation);
var transform = Cesium.Matrix4.fromRotationTranslation(
rotation,
Cesium.Cartesian3.ZERO
);
var inverseTransform = Cesium.Matrix4.inverse(transform, new Cesium.Matrix4());
var planeProjection = new Cesium.Matrix4();
Cesium.Matrix4.multiply(
inverseTransform,
translation,
planeProjection
);
Cesium.Matrix4.multiply(
planeProjection,
transform,
planeProjection
);
Cesium.Matrix4.multiply(
model.modelMatrix,
planeProjection,
model.modelMatrix
);
});
```
这段代码首先获取了3D模型的顶点,然后计算模型的中心点,并将顶点转换为笛卡尔坐标。接下来,通过Cesium的Matrix4.multiplyByTranslation方法将模型转换为以中心点为原点的坐标系。
最后,通过Cesium.Plane对象创建一个平面,将模型投影到该平面上,并将投影后的模型与原始模型进行合并,从而实现压平效果。
阅读全文