加载海量模型时,除了使用3dtiles,还可以使用ModelInstanceCollection来批量加载gltf或者glb格式的三维模型,效果如下:
核心代码如下:
function getInstances() {
var instances = [];
var gridSize = Math.sqrt(10000);
var cLon = data.longitude;
var cLat = data.latitude;
var spacing = 0.01;
var height = 0.0;
for (var y = 0; y < gridSize; ++y) {
for (var x = 0; x < gridSize; ++x) {
var longitude = cLon + spacing * (x - gridSize / 2);
var latitude = cLat + spacing * (y - gridSize / 2);
var position = Cesium.Cartesian3.fromDegrees(
longitude,
latitude,
height
);
var heading = Math.random();
var pitch = Math.random();
var roll = Math.random();
var scale = (Math.random() + 1.0) / 2.0 * 100;
var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
position,
new Cesium.HeadingPitchRoll(heading, pitch, roll)
);
Cesium.Matrix4.multiplyByUniformScale(
modelMatrix,
scale,
modelMatrix
);
instances.push({
modelMatrix: modelMatrix
});
}
}
return instances;
}
function createCollection() {
var instances = getInstances();
instanceCollection = viewer.scene.primitives.add(
new Cesium.ModelInstanceCollection({
url: "./data/model/tree/tree.gltf",
instances: instances
})
);
}