gltf模型以文件夹形式存放,内部有纹理图片文件夹和gltf文件,gltf文件以相对路径请求该文件夹下的资源。
注意存放路径
cesium使用时,可以使用entity和Cesium.Model.fromGltf
建议使用前者
1.www.cnblogs.com/Haiszu/p/12…
var z= new Array('./src/1.gltf', './src/2.gltf','./src/3.gltf' );
var model,modelin,modelroot;
var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(经度, 纬度,1)); //gltf数据加载位置——中点
//加载gltf格式数据到cesium,z为模型名称,modelMatrix为模型中心点的经纬度坐标
function model_add(url){
/* let index = url.lastIndexOf("/"); // lastIndexOf("/") 找到最后一个 / 的位置
let fileName = url.substr(index + 1); // substr() 截取剩余的字符,即得文件名xxx.doc*/
model=scene.primitives.add(Cesium.Model.fromGltf({
url : url, //如果为bgltf则为.bgltf
modelMatrix : modelMatrix,
scale : 0.001, //放大倍数
}));
/*获取3D model 的旋转矩阵modelMatrix*/
let m = model.modelMatrix;
//构建一个三阶旋转矩阵。模型旋转一定的角度,fromRotation[Z]来控制旋转轴,toRadians()为旋转角度,转为弧度再参与运算
let m1 = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(-78));
//矩阵计算
Cesium.Matrix4.multiplyByMatrix3(m,m1,m);
//将计算结果再赋值给modelMatrix
model.modelMatrix = m;
}
function mode3D_play(checkbox){
if(checkbox.checked==true){
model.show=true;
}else{
model.show=false;
}
}
for(let i=0;i<z.length;i++){
model_add(z[i]);
}
function createPlanes(info) {
var planeEntities = [];
for (var i = 0; i < info.length; i++) {
var position = Cesium.Cartesian3.fromDegrees(
info[i][0],
info[i][1],
info[i][2]
);
var heading = Cesium.Math.toRadians(info[i][3]);
var pitch = 0;
var roll = 0;
var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
var orientation = Cesium.Transforms.headingPitchRollQuaternion(
position,
hpr
);
var descriptionHTML = "";
descriptionHTML +=
"<p>" + (p.lon[0].display || p.lon[0].name) + ":" + info[i][0] + "</p>";
descriptionHTML +=
"<p>" + (p.lat[0].display || p.lat[0].name) + ":" + info[i][1] + "</p>";
descriptionHTML +=
"<p>" +
(p.height[0].display || p.height[0].name) +
":" +
info[i][2] +
"</p>";
descriptionHTML +=
"<p>" +
(p.head[0].display || p.head[0].name) +
":" +
info[i][3] +
"</p>";
var minimumPixelSize = 80;
var maximumScale = 4000;
if (
audioList[info[i][6]] ==
adress + "/api/rest//upload/web/gltfs/towers/scene.gltf"
) {
minimumPixelSize = 40;
maximumScale = 1700;
}
var entity = viewer.entities.add({
name: info[i][4],
position: position,
orientation: orientation,
model: {
uri: audioList[info[i][6]],
minimumPixelSize: minimumPixelSize, //最小的模型像素
maximumScale: maximumScale, //最大的模型像素
runAnimations: true, //是否显示动画
clampAnimations: true, //是否保持最后一针的动画
//color:Cesium.Color.RED,//颜色
color: Cesium.Color.fromAlpha(Cesium.Color.RED, parseFloat(1.0)), //包含透明度的颜色
colorBlendMode: Cesium.ColorBlendMode["MIX"], //常用的有三个HIGHLIGHT,REPLACE,MIX
colorBlendAmount: 0.1, //这个属性必须是MIX混合属性才能生效,见colorBlendMode
},
description: descriptionHTML,
});
planeEntities.push(entity);
// var PolylineTrailBase = viewer.entities.add({
// name: "line",
// polyline: {
// show: true,
// positions: Cesium.Cartesian3.fromDegreesArrayHeights(info[i][5]),
// width: 3,
// material: Cesium.Color.RED.withAlpha(1),
// },
// });
if(info[i][5]!=-2){
var PolylineTrail = viewer.entities.add({
name: "line:" + info[i][4],
polyline: {
show: true,
positions: Cesium.Cartesian3.fromDegreesArrayHeights(info[i][5]),
width: 2,
material: makeColor(i),
},
});
planeEntities.push(PolylineTrail);
}
}
return planeEntities;
}
audioList数组为gltf文件路径数组。