Cesium快速上手3-Billboard Label PointPrimitives图元使用讲解

Billboard&Cesium.BillboardCollection

面朝屏幕的图片

http://localhost:8080/Apps/Sandcastle/index.html?src=development%2FBillboards.html&label=Development

image.png

//链式返回,new Cesium.BillboardCollection() 生成一个对象,这个对象是另一个scene.primitives.add的参数
//position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)
//position 空间直角坐标系,由经纬度坐标转换而来;这里的经纬度省略了Z的参数
// 可以增加一句 var height = Cesium.defaultValue(444444);
// position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883,height )

function addBillboard() {
    Sandcastle.declare(addBillboard);
//链式返回,new Cesium.BillboardCollection() 生成一个对象,这个对象是另一个scene.primitives.add的参数
    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        image : '../images/Cesium_Logo_overlay.png',
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)
    });
}

sizeInMeters : true, //图像的尺寸被指定成图像实际的尺寸,不随地图的缩放而变换大小

function sizeBillboardInMeters() {
    Sandcastle.declare(sizeBillboardInMeters);

    var center = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
    var heading = Cesium.Math.toRadians(50.0);
    var pitch = Cesium.Math.toRadians(-20.0);
    var range = 100.0;
    viewer.camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, range));

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        image : '../images/Cesium_Logo_overlay.png',
        sizeInMeters : true, //图像的尺寸被指定成图像实际的尺寸
        position : center
    });
}

一次创建多个

function addMultipleBillboards() {
    Sandcastle.declare(addMultipleBillboards);

    var logoUrl = '../images/Cesium_Logo_overlay.png';
    var facilityUrl = '../images/facility.gif';

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        image : logoUrl,
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)
    });
    billboards.add({
        image : facilityUrl,
        position : Cesium.Cartesian3.fromDegrees(-80.50, 35.14)
    });
    billboards.add({
        image : facilityUrl,
        position : Cesium.Cartesian3.fromDegrees(-80.12, 25.46)
    });
}

//scaleByDistance 设置屏幕像素缩放比,150米的时候放大一倍,150000米的时候,缩放到0.5
//Cesium.NearFarScalar四个值,最近的距离,最远的距离,缩放比例范围

function scaleByDistance() {
    Sandcastle.declare(scaleByDistance);

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        image : '../images/facility.gif',
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        scaleByDistance : new Cesium.NearFarScalar(1.5e2, 2.0, 1.5e7, 0.5)
    });
}

translucencyByDistance 改动透明度,地球缩放到很小时,透明度增大

function fadeByDistance() {
    Sandcastle.declare(fadeByDistance);

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        image : '../images/Cesium_Logo_overlay.png',
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        translucencyByDistance : new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e7, 0.2)
    });
}

pixelOffset : new Cesium.Cartesian2(0.0, -facilityHeight), //在原位置上偏移,防止叠在一起看不到了
pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.0e3, 1.0, 1.5e6, 0.0), //随着距离改变偏移量
translucencyByDistance : new Cesium.NearFarScalar(1.0e3, 1.0, 1.5e6, 0.1)//随着距离改变透明度

function offsetByDistance() {
    Sandcastle.declare(offsetByDistance);
    Cesium.when.all([
         Cesium.Resource.createIfNeeded('../images/Cesium_Logo_overlay.png').fetchImage(),
         Cesium.Resource.createIfNeeded('../images/facility.gif').fetchImage()
        ],
        function(images) {
            var billboards = scene.primitives.add(new Cesium.BillboardCollection());

            // As viewer zooms closer to facility billboard,
            // increase pixelOffset on CesiumLogo billboard to this height
            var facilityHeight = images[1].height;

            // colocated billboards, separate as viewer gets closer
            billboards.add({
                image : images[1],
                position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
                horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
                verticalOrigin : Cesium.VerticalOrigin.BOTTOM
            });
            billboards.add({
                image : images[0],
                position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
                horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
                verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                pixelOffset : new Cesium.Cartesian2(0.0, -facilityHeight), //在原位置上偏移,防止叠在一起看不到了
                pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.0e3, 1.0, 1.5e6, 0.0), //随着距离改变偏移量
                translucencyByDistance : new Cesium.NearFarScalar(1.0e3, 1.0, 1.5e6, 0.1)//随着距离改变透明度
            });
    });
}

添加点的公告牌


image.png
function addPointBillboards() {
    Sandcastle.declare(addPointBillboards);

    // A white circle is drawn into a 2D canvas.  The canvas is used as
    // a texture for billboards, each of which applies a different color
    // and scale to change the point's appearance.
    //
    // The 2D canvas can draw much more than circles.  See:
    // https://developer.mozilla.org/en/Canvas_tutorial
    var canvas = document.createElement('canvas');
    canvas.width = 16;
    canvas.height = 16;
    var context2D = canvas.getContext('2d');
    context2D.beginPath();
    context2D.arc(8, 8, 8, 0, Cesium.Math.TWO_PI, true);
    context2D.closePath();
    context2D.fillStyle = 'rgb(255, 255, 255)';
    context2D.fill();

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    billboards.add({
        imageId : 'custom canvas point',
        image : canvas,
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        color : Cesium.Color.RED,
        scale : 0.5
    });
    billboards.add({
        imageId : 'custom canvas point',
        image : canvas,
        position : Cesium.Cartesian3.fromDegrees(-80.50, 35.14),
        color : Cesium.Color.BLUE
    });
    billboards.add({
        imageId : 'custom canvas point',
        image : canvas,
        position : Cesium.Cartesian3.fromDegrees(-80.12, 25.46),
        color : Cesium.Color.LIME,
        scale : 2
    });
}

增加marker的公告牌


image.png
function addMarkerBillboards() {
    Sandcastle.declare(addMarkerBillboards);

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());

    // Add several billboards based on the above image in the atlas.
    billboards.add({
        image : '../images/whiteShapes.png',
        imageSubRegion : new Cesium.BoundingRectangle(49, 43, 18, 18),
        position : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        color : Cesium.Color.LIME
    });
    billboards.add({
        image : '../images/whiteShapes.png',
        imageSubRegion : new Cesium.BoundingRectangle(61, 23, 18, 18),
        position : Cesium.Cartesian3.fromDegrees(-84.0, 39.0),
        color : new Cesium.Color(0, 0.5, 1.0, 1.0)
    });
    billboards.add({
        image : '../images/whiteShapes.png',
        imageSubRegion : new Cesium.BoundingRectangle(67, 80, 14, 14),
        position : Cesium.Cartesian3.fromDegrees(-70.0, 41.0),
        color : new Cesium.Color(0.5, 0.9, 1.0, 1.0)
    });
    billboards.add({
        image : '../images/whiteShapes.png',
        imageSubRegion : new Cesium.BoundingRectangle(27, 103, 22, 22),
        position : Cesium.Cartesian3.fromDegrees(-73.0, 37.0),
        color : Cesium.Color.RED
    });
    billboards.add({
        image : '../images/whiteShapes.png',
        imageSubRegion : new Cesium.BoundingRectangle(105, 105, 18, 18),
        position : Cesium.Cartesian3.fromDegrees(-79.0, 35.0),
        color : Cesium.Color.YELLOW
    });
}

在框架体系中增加广告牌,平移旋转缩放,全部围绕着billboards.modelMatrix
平移到某一点后,旋转X轴转到朝东,Y转到朝北,z轴转到正上方。
billboards.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);

function inReferenceFrame() {
    Sandcastle.declare(inReferenceFrame);

    var billboards = scene.primitives.add(new Cesium.BillboardCollection());
    var center = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
    billboards.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);

    var facilityUrl = '../images/facility.gif';

    // center
    billboards.add({
        image : facilityUrl,
        position : new Cesium.Cartesian3(0.0, 0.0, 0.0)
    });
    // east
    billboards.add({
        image : facilityUrl,
        position : new Cesium.Cartesian3(1000000.0, 0.0, 0.0)
    });
    // north
    billboards.add({
        image : facilityUrl,
        position : new Cesium.Cartesian3(0.0, 1000000.0, 0.0)
    });
    // up
    billboards.add({
        image : facilityUrl,
        position : new Cesium.Cartesian3(0.0, 0.0, 1000000.0)
    });
}

特点:
始终面朝屏幕,即使旋转也面朝屏幕
注意创建的集群对象 Cesium.BillboardCollection
为什么不能创建单个Billboard?Entity来解决此问题

Label&Cesium.LabelCollection

面朝屏幕的文字

http://localhost:8080/Apps/Sandcastle/index.html?src=development%2FLabels.html&label=Development
特点:

始终朝向屏幕
注意创建的是集群对象 Cesium.LabelCollection()
Label对象只能用在LabelCollection当中

PointPrimitive&Cesium.PointPrimitiveCollection

http://localhost:8080/Apps/Sandcastle/index.html?src=development%2FPointPrimitives.html&label=Development

你可能感兴趣的:(Cesium快速上手3-Billboard Label PointPrimitives图元使用讲解)