cesium创建长方体、立方体的三种方式

5 篇文章 3 订阅
4 篇文章 0 订阅

第一种

viewer.entities.add({
    id: `box`,
    name : `box`,
    position: Cesium.Cartesian3.fromDegrees(经度值, 纬度值, 离地面高度值),
    box : {
        dimensions : new Cesium.Cartesian3(方体长度值, 方体宽度值, 方体高度值),
        material : Cesium.Color.fromCssColorString(“#ffffff”),//转换颜色
        outline : false,
        outlineColor : Cesium.Color.BLACK
    }
});

第二种

let material = Cesium.Material.fromType('Color');
material.uniforms.color = Cesium.Color.fromCssColorString(“#ffffff”);
let center = Cesium.Cartesian3.fromDegrees(经度值, 纬度值, 离地面高度值);
let dimensions = new Cesium.Cartesian3(方体长度值, 方体宽度值, 方体高度值);
let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
let hprRotation = Cesium.Matrix3.fromHeadingPitchRoll(
    new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(90), 0.0, 0.0)// 中心点水平旋转90度
);
let hpr = Cesium.Matrix4.fromRotationTranslation(
    hprRotation,
    new Cesium.Cartesian3(0.0, 0.0, 0.0)// 不平移
);
Cesium.Matrix4.multiply(modelMatrix, hpr, modelMatrix);
viewer.scene.primitives.add(
    new Cesium.Primitive({
    	geometryInstances: new Cesium.GeometryInstance({
        	geometry: Cesium.BoxGeometry.fromDimensions({
                vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
                dimensions: dimensions,
            }),
            modelMatrix: modelMatrix,//提供位置与姿态参数
            id: "BoxGeometry"
        }),
        appearance: new Cesium.EllipsoidSurfaceAppearance({
            aboveGround: false,
            material: material
        })
   	})
);

第三种

let data = [
    {   
        lon1: 112, lat1: 24,
        lon2: 113, lat2: 24,
        lon3: 113, lat3: 25,
        lon4: 112, lat4: 25,
    },
    {   
        lon1: 114, lat1: 24, 
        lon2: 115, lat2: 24,
        lon3: 115, lat3: 25,
        lon4: 114, lat4: 25,
    },
    {   
        lon1: 118, lat1: 24,
        lon2: 117, lat2: 24,
        lon3: 117, lat3: 25,
        lon4: 118, lat4: 25,
    }
];
// 创建长方体对象
let that = this;
let geometryInstances = [];
data.map((item,index) => {
    let instance = new Cesium.GeometryInstance({
        id: `box_${index}`,
        geometry: new Cesium.PolygonGeometry({
            polygonHierarchy: new Cesium.PolygonHierarchy(
                Cesium.Cartesian3.fromDegreesArray([
                    item.lon1, item.lat1,
                    item.lon2, item.lat2,
                    item.lon3, item.lat3,
                    item.lon4, item.lat4,
                ])
            ),
            extrudedHeight: that.getDistance(item.lon1, item.lat1, item.lon2, item.lat2),
       }),
	})
    geometryInstances.push(instance);
})
// 创建材质,在MaterialAppearance中若不添加基础材质,模型将会透明
let material = new Cesium.Material.fromType("Color");
    material.uniforms.color =  Cesium.Color.RED;
    // 自定义材质
let aper = new Cesium.MaterialAppearance({
    material: material,
    translucent: true,
    closed: true,
})
// 加载模型
that.viewer.scene.primitives.add(
    new Cesium.Primitive({
        geometryInstances: geometryInstances,
        appearance: aper,
        releaseGeometryInstances: false,
        compressVertices: false,
    })
)
//获取两个经纬度之间的距离,单位为米
getDistance(lon1, lat1, lon2, lat2){
    let radLat1 = lat1*Math.PI / 180.0;
    let radLat2 = lat2*Math.PI / 180.0;
    let a = radLat1 - radLat2;
    let b = lon1*Math.PI / 180.0 - lon2*Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
    Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s * 6378.137 ;//6378.137是地球半径;
    s = Math.round(s * 10000) / 10000 * 1000;
    return s;//调用return的距离单位为m
},
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值