Cesium学习笔记(六):几何和外观(Geometry and Appearances)【转】
https://blog.csdn.net/UmGsoil/article/details/74912638
我们先直接来看一个例子
- var viewer = new Cesium.Viewer('cesiumContainer');
- var flag = viewer.entities.add({
- rectangle : {
- coordinates : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
- material : new Cesium.StripeMaterialProperty({
- evenColor: Cesium.Color.WHITE,
- oddColor: Cesium.Color.BLUE,
- repeat: 5
- })
- }
- });
这是我们之前的写法,直接创建一个实体对象
而在这一章,我们将会使用几何和外观来创建实体对象,这样更灵活更有效率
首先,还是先看一下,上面那段代码的改造
- var viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- //创建几何图形
- var instance = new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
- vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
- })
- });
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : instance,
- //使用系统自带的条纹样式
- appearance : new Cesium.EllipsoidSurfaceAppearance({
- material : Cesium.Material.fromType('Stripe')
- })
- }));
这样的写法自然是有优点也有缺点的
优点:
性能 - 当绘制大量静态图元时,直接使用几何形状可以将它们组合成单个几何体,以减少CPU开销并更好地利用GPU。并且组合是在网络上完成的,可以保持UI的响应。
灵活性 - 基元组合几何和外观。通过解耦,我们可以独立地修改。我们可以添加与许多不同外观兼容的新几何体,反之亦然。
低级访问 - 外观提供了接近于渲染器的访问,可以直接使用渲染器的所有细节(Appearances provide close-to-the-metal access to rendering without having to worry about all the details of using the Renderer directly)。外观使其易于:
编写完整的GLSL顶点和片段着色器。
使用自定义渲染状态。
缺点:
代码量增大,并且需要使用者对这方面有更深入的理解。
组合几何可以使用静态数据,不一定是动态数据。
primitives 的抽象级别适合于映射应用程序;几何图形和外观的抽象层次接近传统的3D引擎(Primitives are at the level of abstraction appropriate for mapping apps; geometries and appearances have a level of abstraction closer to a traditional 3D engine)(感觉翻译的不太好的地方都给上了原文)
我们可以用一个primitives画出多个几何图形,这样可以明显能看出性能上的优势
- var viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- var instance = new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
- vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
- })
- });
- var anotherInstance = new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-85.0, 20.0, -75.0, 30.0),
- vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
- })
- });
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : [instance, anotherInstance],
- appearance : new Cesium.EllipsoidSurfaceAppearance({
- material : Cesium.Material.fromType('Stripe')
- })
- }));

对于不同的图形,我们可以单独给它们设置属性,这里,我们使用PerInstanceColorAppearance不同颜色来遮蔽每个实例
- var viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- var instance = new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
- vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
- }),
- attributes : {
- //(红,绿,蓝,透明度)
- color : new Cesium.ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 0.8)
- }
- });
- var anotherInstance = new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-85.0, 20.0, -75.0, 30.0),
- vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
- }),
- attributes : {
- color : new Cesium.ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 0.8) }
- });
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : [instance, anotherInstance],
- appearance : new Cesium.PerInstanceColorAppearance()
- }));

可能这样大家还感觉不出来性能上的优势,那我们可以这样
- var viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- var instances = [];
- //循环创建随机颜色的矩形
- for (var lon = -180.0; lon < 180.0; lon += 5.0) {
- for (var lat = -85.0; lat < 85.0; lat += 5.0) {
- instances.push(new Cesium.GeometryInstance({
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(lon, lat, lon + 5.0, lat + 5.0),
- vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
- }),
- attributes : {
- color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({alpha : 0.5}))
- }
- }));
- }
- }
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : instances,
- appearance : new Cesium.PerInstanceColorAppearance()
- }));
这里画了2592个不同颜色的矩形,而且速度非常快,这就更明显的看出primitives在性能上的优势了

虽然我们是通过一个primitives来创建的,但是我们可以给每一个几何图形一个id,这样我们就可以单独访问他们了、
- var instance = new Cesium.GeometryInstance({
- id : "blue rectangle",
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
- vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
- }),
- attributes : {
- color : new Cesium.ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 0.8)
- }
- });
- var anotherInstance = new Cesium.GeometryInstance({
- id : "red rectangle",
- geometry : new Cesium.RectangleGeometry({
- rectangle : Cesium.Rectangle.fromDegrees(-85.0, 20.0, -75.0, 30.0),
- vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
- }),
- attributes : {
- color : new Cesium.ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 0.8)
- }
- });
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : [instance, anotherInstance],
- appearance : new Cesium.PerInstanceColorAppearance()
- }));
- //获取屏幕事件管理器
- var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
- //监听屏幕输入事件(这里是监听左键点击事件)
- handler.setInputAction(function (movement) {
- var pick = scene.pick(movement.position);
- if (Cesium.defined(pick) ) {
- switch (pick.id)
- {
- case 'blue rectangle':
- console.log('Mouse clicked blue rectangle.');
- break;
- case 'red rectangle':
- console.log('Mouse clicked red rectangle.');
- break;
- }
- }
- }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
然后我点击两个矩形,控制台就输出了相应的log

当只要改变属性,不需要改变几何形状时候还可以把几何图形的创建给提出来
- var viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- //使用同一个几何图形
- var ellipsoidGeometry = new Cesium.EllipsoidGeometry({
- vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
- radii : new Cesium.Cartesian3(300000.0, 200000.0, 150000.0)
- });
- var cyanEllipsoidInstance = new Cesium.GeometryInstance({
- geometry : ellipsoidGeometry,
- //不同的模型矩阵改变了位置
- modelMatrix : Cesium.Matrix4.multiplyByTranslation(
- Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
- new Cesium.Cartesian3(0.0, 0.0, 150000.0),
- new Cesium.Matrix4()
- ),
- //改变了颜色
- attributes : {
- color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.CYAN)
- }
- });
- var orangeEllipsoidInstance = new Cesium.GeometryInstance({
- geometry : ellipsoidGeometry,
- modelMatrix : Cesium.Matrix4.multiplyByTranslation(
- Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
- new Cesium.Cartesian3(0.0, 0.0, 450000.0),
- new Cesium.Matrix4()
- ),
- attributes : {
- color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.ORANGE)
- }
- });
- scene.primitives.add(new Cesium.Primitive({
- geometryInstances : [cyanEllipsoidInstance, orangeEllipsoidInstance],
- appearance : new Cesium.PerInstanceColorAppearance({
- //不透明
- translucent : false,
- closed : true
- })
- }));

在创建完之后,我们依旧可以动态的修改模型的属性,当然,这需要给模型加上一个id
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var ellipsoidGeometry = new Cesium.EllipsoidGeometry({
vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
radii : new Cesium.Cartesian3(300000.0, 200000.0, 150000.0)
});
var cyanEllipsoidInstance = new Cesium.GeometryInstance({
id : 'cyan',
geometry : ellipsoidGeometry,
modelMatrix : Cesium.Matrix4.multiplyByTranslation(
Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
new Cesium.Cartesian3(0.0, 0.0, 150000.0),
new Cesium.Matrix4()
),
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.CYAN)
}
});
var orangeEllipsoidInstance = new Cesium.GeometryInstance({
id : 'orange',
geometry : ellipsoidGeometry,
modelMatrix : Cesium.Matrix4.multiplyByTranslation(
Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
new Cesium.Cartesian3(0.0, 0.0, 450000.0),
new Cesium.Matrix4()
),
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.ORANGE)
}
});
var primitive = scene.primitives.add(new Cesium.Primitive({
geometryInstances : [cyanEllipsoidInstance, orangeEllipsoidInstance],
appearance : new Cesium.PerInstanceColorAppearance({
//不透明
translucent : false,
closed : true
})
}));
setInterval(function() {
var attributes1 = primitive.getGeometryInstanceAttributes('cyan');
attributes1.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromRandom({alpha : 1.0}));
var attributes2 = primitive.getGeometryInstanceAttributes('orange');
attributes2.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromRandom({alpha : 1.0}));
},1000);

下面列举下cesium中的几何图形和外观,要注意的是有些外观和几何是不兼容的
几何图形 描述
BoxGeometry 盒子
BoxOutlineGeometry 只有外部线条的的盒子
CircleGeometry 圆圈或挤压圆
CircleOutlineGeometry 同上(只有线条的圆,后面我就省略了)
CorridorGeometry 垂直于表面的折线,宽度以米为单位,可选择挤压高度
CorridorOutlineGeometry
CylinderGeometry 圆柱体,圆锥体或截锥体
CylinderOutlineGeometry
EllipseGeometry 椭圆或挤出椭圆
EllipseOutlineGeometry
EllipsoidGeometry 椭圆形
EllipsoidOutlineGeometry
RectangleGeometry 矩形或挤压矩形
RectangleOutlineGeometry
PolygonGeometry 具有可选孔或挤出多边形的多边形
PolygonOutlineGeometry
PolylineGeometry 一组宽度为像素的线段
SimplePolylineGeometry
PolylineVolumeGeometry 沿着折线挤压的2D形状
PolylineVolumeOutlineGeometry
SphereGeometry 一个球体
SphereOutlineGeometry
WallGeometry 垂直于地球的墙壁
WallOutlineGeometry
外观 描述
MaterialAppearance 外观与所有几何类型一起使用,并支持材料描述阴影。
EllipsoidSurfaceAppearance 几何像几何平行于地球表面的“Material Appearance”一样,就像一个多边形,并且使用这个假设来通过程序上计算许多顶点属性来节省内存。
PerInstanceColorAppearance 使用每个实例的颜色来遮蔽每个实例。
PolylineMaterialAppearance 支持材料遮蔽Polyline。
PolylineColorAppearance 使用每顶点或每段着色来遮蔽折线。
原文链接:https://blog.csdn.net/UmGsoil/article/details/74912638
Cesium学习笔记(六):几何和外观(Geometry and Appearances)【转】的更多相关文章
- java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)
java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- python3.4学习笔记(六) 常用快捷键使用技巧,持续更新
python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...
- Go语言学习笔记六: 循环语句
Go语言学习笔记六: 循环语句 今天学了一个格式化代码的命令:gofmt -w chapter6.go for循环 for循环有3种形式: for init; condition; increment ...
- 【opencv学习笔记六】图像的ROI区域选择与复制
图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...
- Linux学习笔记(六) 进程管理
1.进程基础 当输入一个命令时,shell 会同时启动一个进程,这种任务与进程分离的方式是 Linux 系统上重要的概念 每个执行的任务都称为进程,在每个进程启动时,系统都会给它指定一个唯一的 ID, ...
- # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)
目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
随机推荐
- 使用ABAP绘制可伸缩矢量图
Jerry去年的文章 动手使用ABAP Channel开发一些小工具,提升日常工作效率 里曾经介绍过一些用ABAP实现的可供娱乐的小程序,比如用古老的HPGL接口在SAPGUI里绘图: 关于如何用SA ...
- Linux内核:关于中断你需要知道的
1.中断处理程序与其他内核函数真正的区别在于,中断处理程序是被内核调用来相应中断的,而它们运行于中断上下文(原子上下文)中,在该上下文中执行的代码不可阻塞.中断就是由硬件打断操作系统. 2.异常与中断 ...
- Windows安装MySQL5.7教程
导读: 我们日常学习可能会需要在本地安装MySQL服务,也遇到过小伙伴探讨关于Windows系统安装MySQL的问题.在这里建议大家安装MySQL5.7版本,当然想尝试8.0版本的同学也可以参考安装. ...
- unittest使用
unittest:单元测试框架主要包含四部分: 1.测试固件(test fixture): 定义:包含执行测试前的准备setUP().测试执行完后的清扫工作tearDown() 注意: setUp() ...
- MySQL Replication--中继日志更新
RELAY LOG相关参数 设置如何保存从节点接收到的主库BINLOG sync_relay_log : 设置如何同步中继日志到中继日志文件. 当sync_relay_log = 0时,则MySQL服 ...
- MySQL MHA候选主库选择
MHA在选择新主库时,会将所有存活的从库分为下面几类: 存活从库数组:挑选所有存活的从库 最新从库数组: 挑选Master_Log_File+Read_Master_Log_Pos最高的从库 优选从库 ...
- gps示例代码
/* main.c */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #incl ...
- SQL模糊查询的四种匹配模式
执行数据库查询时,有完整查询和模糊查询之分,一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 一.四种匹配模式 关于条件,SQL提供了四种匹配模式: 1.% 表 ...
- 数据库迁移Flyway
为什么需要Flyway 日常开发常常会遇到一些这样的场景 小红开发一个模块在本地数据库增加了两个字段,并且改动了dao层的代码提交到git.这时候小黄拉取了代码Run很可能报错. 如果在上线正式环境的 ...
- Java.io.tmpdir介绍
System.getproperty(“java.io.tmpdir”)是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样, 在Windows的缓存目录为:C:\Users\登录用户~1\ ...