cesium3D

Cesium快速上手6-Primitive图元的讲解

2020-04-07  本文已影响0人  彩云飘过

简单示例浏览

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

http://localhost:8080/Apps/Sandcastle/gallery/development%2FBox.html

image.png

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

image.png

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

image.png

原理

image.png
image.png

可以自定义几何体Geometry ,覆盖自定义材质Appearance

创建红色几何体的源代码分析

// Create the viewer.
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;

// Draw a red box and position it on the globe surface.

var dimensions = new Cesium.Cartesian3(400000.0, 300000.0, 500000.0);
// Box geometries are initially centered on the origin.
// We can use a model matrix to position the box on the
// globe surface.
var positionOnEllipsoid = Cesium.Cartesian3.fromDegrees(-105.0, 45.0);
// 移到某个位置,抬高25万米
var boxModelMatrix = Cesium.Matrix4.multiplyByTranslation(
    Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid),
    new Cesium.Cartesian3(0.0, 0.0, dimensions.z * 0.5), new Cesium.Matrix4());
// Create a box geometry.
var boxGeometry = Cesium.BoxGeometry.fromDimensions({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    dimensions : dimensions
});
// Create a geometry instance using the geometry
// and model matrix created above.
var boxGeometryInstance = new Cesium.GeometryInstance({
    geometry : boxGeometry,
    modelMatrix : boxModelMatrix,
    attributes : { //指定顶点的颜色
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 0.5))
    }
});
// Add the geometry instance to primitives.
scene.primitives.add(new Cesium.Primitive({
    geometryInstances : boxGeometryInstance,
    appearance : new Cesium.PerInstanceColorAppearance({ //指定材质的颜色 和顶点颜色相同
        closed: true
    })
}));

实例一 和实例三的关键区别就是


image.png
image.png

// Create a box outline geometry. 建一个只有外线的空白盒子
var boxOutlineGeometry = Cesium.BoxOutlineGeometry.fromDimensions({
    dimensions : dimensions
});

//建一个盒子,有盒子的各个面
var boxGeometry = Cesium.BoxGeometry.fromDimensions({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    dimensions : dimensions
});

比较复杂的几何体

http://localhost:8080/Apps/Sandcastle/index.html?src=development%2FGeometry%20and%20Appearances.html&label=Development

Appearances.png

PrimitiveCollection是一个多层次的结构,删除的时候,谨慎调用scene.primitives.removeall(),可能会把cesium自动加的primitive删掉

image.png
拆分上边复杂的代码

以上所有拆分的代码前边都需要有下边这一段

var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var primitives = scene.primitives;
var solidWhite = Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE);

Combine instances for a rectangle, polygon, ellipse, and circle into a single primitive拆解代码如下

image.png

矩形 在地图上如果跨度纬度比较大的时候,它实际上显示出来是梯形。
改动前 var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
改动后 var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -10.0, 70.0);

image.png
// Combine instances for a rectangle, polygon, ellipse, and circle into a single primitive.

var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
var rectangleInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.RectangleGeometry({
        rectangle : rectangle,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
        stRotation : Cesium.Math.toRadians(45)
    })
});
var rectangleOutlineInstance = new Cesium.GeometryInstance({ //长方形 外边的荧光轮廓线
    geometry : new Cesium.RectangleOutlineGeometry({
        rectangle : rectangle
    }),
    attributes : {
        color : solidWhite
    }
});

var positions = Cesium.Cartesian3.fromDegreesArray([
    -107.0, 27.0,
    -107.0, 22.0,
    -102.0, 23.0,
    -97.0, 21.0,
    -97.0, 25.0
]);

var polygonInstance = new Cesium.GeometryInstance({
    geometry : Cesium.PolygonGeometry.fromPositions({
        positions : positions,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});
var polygonOutlineInstance = new Cesium.GeometryInstance({
    geometry : Cesium.PolygonOutlineGeometry.fromPositions({
        positions : positions
    }),
    attributes : {
        color : solidWhite
    }
});

var center = Cesium.Cartesian3.fromDegrees(-80.0, 25.0);
var semiMinorAxis = 300000.0;
var semiMajorAxis = 500000.0;
var rotation = Cesium.Math.toRadians(-40.0);
var ellipseInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        rotation : rotation,
        stRotation : Cesium.Math.toRadians(22),
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});
var ellipseOutlineInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseOutlineGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        rotation : rotation
    }),
    attributes : {
        color : solidWhite
    }
});

center = Cesium.Cartesian3.fromDegrees(-72.0, 25.0);
var radius = 250000.0;
var circleInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CircleGeometry({
        center : center,
        radius : radius,
        stRotation : Cesium.Math.toRadians(90),
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});
var circleOutlineInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CircleOutlineGeometry({
        center : center,
        radius : radius
    }),
    attributes : {
        color : solidWhite
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [rectangleInstance, polygonInstance, ellipseInstance, circleInstance],
    appearance : new Cesium.EllipsoidSurfaceAppearance({
        material : Cesium.Material.fromType('Stripe')
    })
}));

primitives.add(new Cesium.Primitive({
    geometryInstances : [rectangleOutlineInstance, polygonOutlineInstance, ellipseOutlineInstance, circleOutlineInstance],
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true, //为true 无光照
        translucent : false, //透明配置,false是不透明
        renderState : {
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

**extrudedPolygon, extrudedRectangle, extrudedEllipse, cylinderInstance **
脱模多边形,脱模矩形,脱模dellipse,圆柱状实例

image.png

var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
var center = Cesium.Cartesian3.fromDegrees(-80.0, 25.0);
var semiMinorAxis = 300000.0;
var semiMajorAxis = 500000.0;
var rotation = Cesium.Math.toRadians(-40.0);
// Create extruded rectangle
rectangle = Cesium.Rectangle.fromDegrees(-118.0, 38.0, -116.0, 40.0);
var extrudedHeight = 500000.0; //拉伸高度
var extrudedRectangle = new Cesium.GeometryInstance({
    geometry : new Cesium.RectangleGeometry({
        rectangle : rectangle,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

var extrudedOutlineRectangle = new Cesium.GeometryInstance({
    geometry : new Cesium.RectangleOutlineGeometry({
        rectangle : rectangle,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : solidWhite
    }
});

// Create extruded ellipse
center = Cesium.Cartesian3.fromDegrees(-117.0, 35.0);
semiMinorAxis = 100000.0;
semiMajorAxis = 200000.0;
rotation = Cesium.Math.toRadians(90);
var height = 100000.0;
extrudedHeight = 200000.0;
var extrudedEllipse = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        height : height,
        rotation : rotation,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});
var extrudedOutlineEllipse = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseOutlineGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        height : height,
        rotation : rotation,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : solidWhite
    }
});

// Create extruded polygon
var polygonHierarchy = {
    positions : Cesium.Cartesian3.fromDegreesArray([
        -118.0, 30.0,
        -115.0, 30.0,
        -117.1, 31.1,
        -118.0, 33.0
    ])
};
height = 300000.0;
extrudedHeight = 700000.0;
var extrudedPolygon = new Cesium.GeometryInstance({
    geometry : new Cesium.PolygonGeometry({
        polygonHierarchy : polygonHierarchy,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        extrudedHeight : extrudedHeight,
        height : height
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});
var extrudedOutlinePolygon = new Cesium.GeometryInstance({
    geometry : new Cesium.PolygonOutlineGeometry({
        polygonHierarchy : polygonHierarchy,
        extrudedHeight : extrudedHeight,
        height : height
    }),
    attributes : {
        color : solidWhite
    }
});

// cylinder
var length = 200000.0;
var topRadius = 150000.0;
var bottomRadius = 150000.0;
var modelMatrix = Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
    Cesium.Cartesian3.fromDegrees(-70.0, 45.0)),
    new Cesium.Cartesian3(0.0, 0.0, 100000.0), new Cesium.Matrix4());
var cylinderInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CylinderGeometry({
        length : length,
        topRadius : topRadius,
        bottomRadius : bottomRadius,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
    }),
    modelMatrix : modelMatrix,
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});
var cylinderOutlineInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CylinderOutlineGeometry({
        length : length,
        topRadius : topRadius,
        bottomRadius : bottomRadius
    }),
    modelMatrix : modelMatrix,
    attributes : {
        color : solidWhite
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [extrudedPolygon, extrudedRectangle, extrudedEllipse, cylinderInstance],
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : false,
        closed : true
    })
}));
primitives.add(new Cesium.Primitive({
    geometryInstances : [extrudedOutlineRectangle, extrudedOutlineEllipse, extrudedOutlinePolygon, cylinderOutlineInstance],
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        translucent : false,
        renderState : {
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

box and ellipsoid boxes

image.png
var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
var center = Cesium.Cartesian3.fromDegrees(-80.0, 25.0);
var semiMinorAxis = 300000.0;
var semiMajorAxis = 500000.0;
var rotation = Cesium.Math.toRadians(-40.0);
var radius = 250000.0;
var height = 100000.0;

// Create box and ellipsoid boxes, and use the instance's
// modelMatrix to scale and position them.
var dimensions = new Cesium.Cartesian3(1.0, 1.0, 1.0);
var boxGeometry = Cesium.BoxGeometry.fromDimensions({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    dimensions : dimensions
});
var boxOutlineGeometry = Cesium.BoxOutlineGeometry.fromDimensions({
    dimensions : dimensions
});

var radii = new Cesium.Cartesian3(0.5, 0.5, 1.0);
var ellipsoidGeometry = new Cesium.EllipsoidGeometry({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    radii : radii
});

var ellipsoidOutlineGeometry = new Cesium.EllipsoidOutlineGeometry({
    radii : radii,
    stackPartitions : 6,
    slicePartitions : 5
});

radius = 0.75;
var sphereGeometry = new Cesium.SphereGeometry({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    radius : radius
});

var sphereOutlineGeometry = new Cesium.SphereOutlineGeometry({
    radius : radius,
    stackPartitions : 6,
    slicePartitions : 5
});

var instances = [];
var outlineInstances = [];
var i;
var boxModelMatrix, ellipsoidModelMatrix, sphereModelMatrix;
for (i = 0; i < 5; ++i) {
    height = 100000.0 + (200000.0 * i);
    boxModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-106.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());
    ellipsoidModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-102.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());
    sphereModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-98.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());

    instances.push(new Cesium.GeometryInstance({
        geometry : boxGeometry,
        modelMatrix : boxModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
        }
    }));
    outlineInstances.push(new Cesium.GeometryInstance({
        geometry : boxOutlineGeometry,
        modelMatrix : boxModelMatrix,
        attributes : {
            color : solidWhite
        }
    }));

    instances.push(new Cesium.GeometryInstance({
        geometry : ellipsoidGeometry,
        modelMatrix : ellipsoidModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
        }
    }));
    outlineInstances.push(new Cesium.GeometryInstance({
        geometry : ellipsoidOutlineGeometry,
        modelMatrix : ellipsoidModelMatrix,
        attributes : {
            color : solidWhite
        }
    }));

    instances.push(new Cesium.GeometryInstance({
        geometry : sphereGeometry,
        modelMatrix : sphereModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
        }
    }));
    outlineInstances.push(new Cesium.GeometryInstance({
        geometry : sphereOutlineGeometry,
        modelMatrix : sphereModelMatrix,
        attributes : {
            color : solidWhite
        }
    }));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : instances,
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : false,
        closed : true
    })
}));
primitives.add(new Cesium.Primitive({
    geometryInstances : outlineInstances,
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        translucent : false,
        renderState : {
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

box and ellipsoid boxes 随比例尺变化
// Create box and ellipsoid boxes, and use the instance's
// modelMatrix to scale and position them.

image.png

// Create box and ellipsoid boxes, and use the instance's
// modelMatrix to scale and position them.
var dimensions = new Cesium.Cartesian3(1.0, 1.0, 1.0);
var boxGeometry = Cesium.BoxGeometry.fromDimensions({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    dimensions : dimensions
});

var radii = new Cesium.Cartesian3(0.5, 0.5, 1.0);
var ellipsoidGeometry = new Cesium.EllipsoidGeometry({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    radii : radii
});

var radius = 0.75;
var sphereGeometry = new Cesium.SphereGeometry({
    vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
    radius : radius
});

var instances = [];
var outlineInstances = [];
for (var i = 0; i < 5; ++i) {
   var height = 100000.0 + (200000.0 * i);
    var boxModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-108.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());
   var ellipsoidModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-104.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());
    var sphereModelMatrix = Cesium.Matrix4.multiplyByUniformScale(Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(-100.0, 45.0)), new Cesium.Cartesian3(0.0, 0.0, height), new Cesium.Matrix4()), 90000.0, new Cesium.Matrix4());
    instances.push(new Cesium.GeometryInstance({
        geometry : boxGeometry,
        modelMatrix : boxModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
        }
    }));

    instances.push(new Cesium.GeometryInstance({
        geometry : ellipsoidGeometry,
        modelMatrix : ellipsoidModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
        }
    }));

    instances.push(new Cesium.GeometryInstance({
        geometry : sphereGeometry,
        modelMatrix : sphereModelMatrix,
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
        }
    }));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : instances,
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : true,
        closed : true
    })
}));

a single wall

var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
var center = Cesium.Cartesian3.fromDegrees(-80.0, 25.0);
var semiMinorAxis = 300000.0;
var semiMajorAxis = 500000.0;
var rotation = Cesium.Math.toRadians(-40.0);
var radius = 250000.0;
var height = 100000.0;
var positions =null;

// Create a single wall
positions = Cesium.Cartesian3.fromDegreesArray([
    -95.0, 50.0,
    -85.0, 50.0,
    -75.0, 50.0
]);
var maximumHeights = [500000, 1000000, 500000];
var minimumHeights = [0, 500000, 0];

var wallInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.WallGeometry({
        positions : positions,
        maximumHeights : maximumHeights,
        minimumHeights : minimumHeights,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.7}))
    }
});

var wallOutlineInstance = new Cesium.GeometryInstance({  // 墙的荧光线效果,看上去墙是一段一段的
    geometry : new Cesium.WallOutlineGeometry({
        positions : positions,
        maximumHeights : maximumHeights,
        minimumHeights : minimumHeights
    }),
    attributes : {
        color : new Cesium.ColorGeometryInstanceAttribute(0.7, 0.7, 0.7, 1.0)
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : wallInstance,
    appearance : new Cesium.PerInstanceColorAppearance()
}));

primitives.add(new Cesium.Primitive({
    geometryInstances : wallOutlineInstance,
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        translucent : false,
        renderState : {
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

single wall

image.png
// Create a single wall
var positions = Cesium.Cartesian3.fromDegreesArrayHeights([
    -90.0, 43.0, 100000.0,
    -87.5, 45.0, 100000.0,
    -85.0, 43.0, 100000.0,
    -87.5, 41.0, 100000.0,
    -90.0, 43.0, 100000.0
]);

var wallInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.WallGeometry({
        positions : positions        
    })
});

primitives.add(new Cesium.Primitive({
    geometryInstances : wallInstance,
    appearance : new Cesium.MaterialAppearance({
        material : Cesium.Material.fromType('Checkerboard', {
            repeat : new Cesium.Cartesian2(20.0, 20.0)
        })
    })
}));

简单几何体

image.png
var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 30.0, -85.0, 40.0);
var rectangleInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.RectangleGeometry({
        rectangle : rectangle,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});

var polygonHierarchy = {
    positions : Cesium.Cartesian3.fromDegreesArray([
        -109.0, 30.0,
        -95.0, 30.0,
        -95.0, 40.0,
        -109.0, 40.0
    ]),
    holes : [{
        positions : Cesium.Cartesian3.fromDegreesArray([
            -107.0, 31.0,
            -107.0, 39.0,
            -97.0, 39.0,
            -97.0, 31.0
        ]),
        holes : [{
            positions : Cesium.Cartesian3.fromDegreesArray([
                -105.0, 33.0,
                -99.0, 33.0,
                -99.0, 37.0,
                -105.0, 37.0
            ]),
            holes : [{
                positions : Cesium.Cartesian3.fromDegreesArray([
                    -103.0, 34.0,
                    -101.0, 34.0,
                    -101.0, 36.0,
                    -103.0, 36.0
                ])
            }]
        }]
    }]
};
var polygonInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.PolygonGeometry({
        polygonHierarchy : polygonHierarchy,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});

var center = Cesium.Cartesian3.fromDegrees(-80.0, 35.0);
var semiMinorAxis = 200000.0;
var semiMajorAxis = 500000.0;
var rotation = Cesium.Math.toRadians(30.0);
var ellipseInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        rotation : rotation,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});

var center = Cesium.Cartesian3.fromDegrees(-72.0, 35.0);
var radius = 200000.0;
var circleInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CircleGeometry({
        center : center,
        radius : radius,
        vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
    })
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [rectangleInstance, polygonInstance, ellipseInstance, circleInstance],
    appearance : new Cesium.EllipsoidSurfaceAppearance({
        material : Cesium.Material.fromType('Stripe')
    })
}));

几何体

image.png
// Create extruded rectangle
var rectangle = Cesium.Rectangle.fromDegrees(-110.0, 38.0, -107.0, 40.0);
var height = 700000.0;
var extrudedHeight = 1000000.0;
var rotation = Cesium.Math.toRadians(45.0);
var extrudedRectangle = new Cesium.GeometryInstance({
    geometry : new Cesium.RectangleGeometry({
        rectangle : rectangle,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        height : height,
        rotation : rotation,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

// Create extruded ellipse
var center = Cesium.Cartesian3.fromDegrees(-110.0, 35.0);
var semiMinorAxis = 100000.0;
var semiMajorAxis = 200000.0;
var rotation = Cesium.Math.toRadians(-40.0);
var height = 300000.0;
var extrudedHeight = 700000.0;
var extrudedEllipse = new Cesium.GeometryInstance({
    geometry : new Cesium.EllipseGeometry({
        center : center,
        semiMinorAxis : semiMinorAxis,
        semiMajorAxis : semiMajorAxis,
        rotation : rotation,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        height : height,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

// Create extruded polygon
var polygonHierarchy = {
    positions : Cesium.Cartesian3.fromDegreesArray([
        -113.0, 30.0,
        -110.0, 30.0,
        -110.0, 33.0,
        -111.5, 31.0,
        -113.0, 33.0
    ])
};
var extrudedHeight = 300000.0;
var extrudedPolygon = new Cesium.GeometryInstance({
    geometry : new Cesium.PolygonGeometry({
        polygonHierarchy : polygonHierarchy,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        extrudedHeight : extrudedHeight
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

// cylinder
var length = 400000.0;
var topRadius = 0.0;
var bottomRadius = 200000.0;
var modelMatrix = Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
    Cesium.Cartesian3.fromDegrees(-70.0, 40.0)), new Cesium.Cartesian3(0.0, 0.0, 200000.0), new Cesium.Matrix4());
var cylinderInstance = new Cesium.GeometryInstance({
    geometry : new Cesium.CylinderGeometry({
        length : length,
        topRadius : topRadius,
        bottomRadius : bottomRadius,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
    }),
    modelMatrix : modelMatrix,
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [extrudedPolygon, extrudedRectangle, extrudedEllipse, cylinderInstance],
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : false,
        closed : true
    })
}));

多层摞在一起

image.png
// Combine instances each with a unique color.
// We can combine heterogeneous geometries as we
// do here as long as vertex formats match.
var instances = [];

var center = Cesium.Cartesian3.fromDegrees(-65.0, 35.0);
var radius = 200000.0;
var rectangle = Cesium.Rectangle.fromDegrees(-67.0, 27.0, -63.0, 32.0);
for (var i = 0; i < 5; ++i) {
    var height = 200000.0 * i;

    instances.push(new Cesium.GeometryInstance({
        geometry : new Cesium.CircleGeometry({
            center : center,
            radius : radius,
            height : height,
            vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
        }),
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
        }
    }));

    instances.push(new Cesium.GeometryInstance({
        geometry : new Cesium.RectangleGeometry({
            rectangle : rectangle,
            height : height,
            vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
        }),
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
        }
    }));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : instances,
    appearance : new Cesium.PerInstanceColorAppearance()
}));

彩色的线

image.png
var positions = [];
var colors = [];
 
for ( var i = 0; i < 40; ++i) {
    positions.push(Cesium.Cartesian3.fromDegrees(-100.0 + i, 48.0));
    colors.push(Cesium.Color.fromRandom({alpha : 1.0}));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : new Cesium.GeometryInstance({
        geometry : new Cesium.SimplePolylineGeometry({
            positions : positions,
            colors : colors
        })
    }),
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        renderState : {
            // Override the appearance render state to change the
            // line width on system's that support it (Linx/Mac).
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

a polyline with a material

image.png

// create a polyline with a material
var positions = [];
for ( var i = 0; i < 40; ++i) {
    positions.push(Cesium.Cartesian3.fromDegrees(-100.0 + i, 15.0));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : new Cesium.GeometryInstance({
        geometry : new Cesium.PolylineGeometry({
            positions : positions,
            width : 10.0,
            vertexFormat : Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
        })
    }),
    appearance : new Cesium.PolylineMaterialAppearance({
        material : Cesium.Material.fromType(Cesium.Material.PolylineGlowType)
    })
}));

polyline with per segment colors

image.png

// create a polyline with per segment colors
var positions = [];
var colors = [];
for (var i = 0; i < 40; ++i) {
    positions.push(Cesium.Cartesian3.fromDegrees(-100.0 + i, 12.0));
    colors.push(Cesium.Color.fromRandom({alpha : 1.0}));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : new Cesium.GeometryInstance({
        geometry : new Cesium.PolylineGeometry({
            positions : positions,
            width : 10.0,
            vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT,
            colors : colors
        })
    }),
    appearance : new Cesium.PolylineColorAppearance()
}));

polyline with per vertex colors

image.png

// create a polyline with per vertex colors
var positions = [];
var colors = [];
for (var i = 0; i < 40; ++i) {
    positions.push(Cesium.Cartesian3.fromDegrees(-100.0 + i, 9.0));
    colors.push(Cesium.Color.fromRandom({alpha : 1.0}));
}

primitives.add(new Cesium.Primitive({
    geometryInstances : new Cesium.GeometryInstance({
        geometry : new Cesium.PolylineGeometry({
            positions : positions,
            width : 10.0,
            vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT,
            colors : colors,
            colorsPerVertex : true
        })
    }),
    appearance : new Cesium.PolylineColorAppearance()
}));

摞在一起的几何体

image.png

var positions = Cesium.Cartesian3.fromDegreesArray([
    -120.0, 45.0,
    -125.0, 50.0,
    -125.0, 55.0
]);
var width = 100000;

var corridor = new Cesium.GeometryInstance({
    geometry : new Cesium.CorridorGeometry({
        positions : positions,
        width : width,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

var extrudedCorridor = new Cesium.GeometryInstance({
    geometry : new Cesium.CorridorGeometry({
        positions : positions,
        width : width,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        height : 300000,
        extrudedHeight : 400000
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.7}))
    }
});

var corridorOutline = new Cesium.GeometryInstance({
    geometry : new Cesium.CorridorOutlineGeometry({
        positions : positions,
        width : width,
        height : 700000
    }),
    attributes : {
        color : solidWhite
    }
});

var corridorFill = new Cesium.GeometryInstance({
    geometry : new Cesium.CorridorGeometry({
        positions : positions,
        width : width,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        height : 700000
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.7}))
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [corridor, extrudedCorridor, corridorFill],
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : true,
        closed : true
    })
}));

primitives.add(new Cesium.Primitive({
    geometryInstances : corridorOutline,
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        translucent : false,
        renderState : {
            lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
        }
    })
}));

*摞在一起的几何体2

image.png


function starPositions(arms, rOuter, rInner) {
    var angle = Math.PI / arms;
    var pos = [];
    for ( var i = 0; i < 2 * arms; i++) {
        var r = (i % 2) === 0 ? rOuter : rInner;
        var p = new Cesium.Cartesian2(Math.cos(i * angle) * r, Math.sin(i * angle) * r);
        pos.push(p);
    }
    return pos;
}

var positions = Cesium.Cartesian3.fromDegreesArrayHeights([
    -102.0, 15.0, 100000.0,
    -105.0, 20.0, 200000.0,
    -110.0, 20.0, 100000.0
]);
var polylineVolumeFill = new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineVolumeGeometry({
        polylinePositions : positions,
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        shapePositions : starPositions(7, 30000.0, 20000.0)
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

var polylineVolumeOutline = new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineVolumeOutlineGeometry({
        polylinePositions : positions,
        shapePositions : starPositions(7, 30000.0, 20000.0)
    }),
    attributes : {
        color : solidWhite
    }
});

var polylineVolume = new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineVolumeGeometry({
        polylinePositions : Cesium.Cartesian3.fromDegreesArray([
            -102.0, 15.0,
            -105.0, 20.0,
            -110.0, 20.0
        ]),
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        shapePositions : starPositions(7, 30000, 20000)
    }),
    attributes : {
        color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 1.0}))
    }
});

function computeCircle(radius) {
    var positions = [];
    for (var i = 0; i < 360; i++) {
        var radians = Cesium.Math.toRadians(i);
        positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
    }
    return positions;
}

var tubeGeometry = new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineVolumeGeometry({
        polylinePositions : Cesium.Cartesian3.fromDegreesArray([
            -104.0, 13.0,
            -107.0, 18.0,
            -112.0, 18.0
        ]),
        vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
        shapePositions : computeCircle(40000.0)
    }),
    attributes : {
        color : solidWhite
    }
});

primitives.add(new Cesium.Primitive({
    geometryInstances : [tubeGeometry, polylineVolume, polylineVolumeFill],
    appearance : new Cesium.PerInstanceColorAppearance({
        translucent : false,
        closed : true
    })
}));

primitives.add(new Cesium.Primitive({
    geometryInstances : polylineVolumeOutline,
    appearance : new Cesium.PerInstanceColorAppearance({
        flat : true,
        translucent : false,
        renderState : {
            lineWidth : 1.0
        }
    })
}));
上一篇下一篇

猜你喜欢

热点阅读