Cesium源码之Label(二)
我们查看Cesium源码时,有时会发现源码中有大量的includeStart开头的注释,如下图所示。
这里面大多是调试信息,当使用gulp打包时,removePragmas参数设置为true,则会删除includeStart和includeEnd之间的js语句。

源码:
1 /**
2 * A Label draws viewport-aligned text positioned in the 3D scene. This constructor
3 * should not be used directly, instead create labels by calling {@link LabelCollection#add}.
4 *
5 * @alias Label
6 * @internalConstructor
7 * @class
8 *
9 * @exception {DeveloperError} translucencyByDistance.far must be greater than translucencyByDistance.near
10 * @exception {DeveloperError} pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near
11 * @exception {DeveloperError} distanceDisplayCondition.far must be greater than distanceDisplayCondition.near
12 *
13 * @see LabelCollection
14 * @see LabelCollection#add
15 *
16 * @demo {@link https://sandcastle.cesium.com/index.html?src=Labels.html|Cesium Sandcastle Labels Demo}
17 */
18 function Label(options, labelCollection) {
19 options = defaultValue(options, defaultValue.EMPTY_OBJECT);
20
21
22 //>>includeStart('debug', pragmas.debug);
23 //距离摄像机的位置不能小于0
24 // 设置为零时,始终应用深度测试
25 if (
26 defined(options.disableDepthTestDistance) &&
27 options.disableDepthTestDistance < 0.0
28 ) {
29 throw new DeveloperError(
30 "disableDepthTestDistance must be greater than 0.0."
31 );
32 }
33 //>>includeEnd('debug');
34
35 //根据距离变透明的属性
36 var translucencyByDistance = options.translucencyByDistance;
37
38 //根据距离的像素偏移
39 var pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
40
41 //根据距离的大小变化
42 var scaleByDistance = options.scaleByDistance;
43
44 //根据距离的label display
45 var distanceDisplayCondition = options.distanceDisplayCondition;
46
47
48 //从这里开始是判断上述那些属性存不存在、合不合理
49
50 if (defined(translucencyByDistance)) {
51 //>>includeStart('debug', pragmas.debug);
52 //远需大于近
53 if (translucencyByDistance.far <= translucencyByDistance.near) {
54 throw new DeveloperError(
55 "translucencyByDistance.far must be greater than translucencyByDistance.near."
56 );
57 }
58 //>>includeEnd('debug');
59 translucencyByDistance = NearFarScalar.clone(translucencyByDistance);
60 }
61 if (defined(pixelOffsetScaleByDistance)) {
62 //>>includeStart('debug', pragmas.debug);
63 if (pixelOffsetScaleByDistance.far <= pixelOffsetScaleByDistance.near) {
64 throw new DeveloperError(
65 "pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near."
66 );
67 }
68 //>>includeEnd('debug');
69 pixelOffsetScaleByDistance = NearFarScalar.clone(
70 pixelOffsetScaleByDistance
71 );
72 }
73 if (defined(scaleByDistance)) {
74 //>>includeStart('debug', pragmas.debug);
75 if (scaleByDistance.far <= scaleByDistance.near) {
76 throw new DeveloperError(
77 "scaleByDistance.far must be greater than scaleByDistance.near."
78 );
79 }
80 //>>includeEnd('debug');
81 scaleByDistance = NearFarScalar.clone(scaleByDistance);
82 }
83 if (defined(distanceDisplayCondition)) {
84 //>>includeStart('debug', pragmas.debug);
85 if (distanceDisplayCondition.far <= distanceDisplayCondition.near) {
86 throw new DeveloperError(
87 "distanceDisplayCondition.far must be greater than distanceDisplayCondition.near."
88 );
89 }
90 //>>includeEnd('debug');
91 distanceDisplayCondition = DistanceDisplayCondition.clone(
92 distanceDisplayCondition
93 );
94 }
95 //判断上述那些属性存不存在、合不合理到这里结束
96
97 //将options拷贝一份作为自己的私有属性
98 this._renderedText = undefined;
99 this._text = undefined;
100 this._show = defaultValue(options.show, true);
101 this._font = defaultValue(options.font, "30px sans-serif");
102 this._fillColor = Color.clone(defaultValue(options.fillColor, Color.WHITE));
103 this._outlineColor = Color.clone(
104 defaultValue(options.outlineColor, Color.BLACK)
105 );
106 this._outlineWidth = defaultValue(options.outlineWidth, 1.0);
107 this._showBackground = defaultValue(options.showBackground, false);
108 this._backgroundColor = defaultValue(
109 options.backgroundColor,
110 new Color(0.165, 0.165, 0.165, 0.8)
111 );
112 this._backgroundPadding = defaultValue(
113 options.backgroundPadding,
114 new Cartesian2(7, 5)
115 );
116 this._style = defaultValue(options.style, LabelStyle.FILL);
117 this._verticalOrigin = defaultValue(
118 options.verticalOrigin,
119 VerticalOrigin.BASELINE
120 );
121 this._horizontalOrigin = defaultValue(
122 options.horizontalOrigin,
123 HorizontalOrigin.LEFT
124 );
125 this._pixelOffset = Cartesian2.clone(
126 defaultValue(options.pixelOffset, Cartesian2.ZERO)
127 );
128 this._eyeOffset = Cartesian3.clone(
129 defaultValue(options.eyeOffset, Cartesian3.ZERO)
130 );
131 this._position = Cartesian3.clone(
132 defaultValue(options.position, Cartesian3.ZERO)
133 );
134 this._scale = defaultValue(options.scale, 1.0);
135 this._id = options.id;
136 this._translucencyByDistance = translucencyByDistance;
137 this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
138 this._scaleByDistance = scaleByDistance;
139 this._heightReference = defaultValue(
140 options.heightReference,
141 HeightReference.NONE
142 );
143 this._distanceDisplayCondition = distanceDisplayCondition;
144 this._disableDepthTestDistance = options.disableDepthTestDistance;
145
146 this._labelCollection = labelCollection;
147 this._glyphs = [];
148 this._backgroundBillboard = undefined;
149 this._batchIndex = undefined; // Used only by Vector3DTilePoints and BillboardCollection
150
151 this._rebindAllGlyphs = true;
152 this._repositionAllGlyphs = true;
153
154 this._actualClampedPosition = undefined;
155 this._removeCallbackFunc = undefined;
156 this._mode = undefined;
157
158 this._clusterShow = true;
159
160 this.text = defaultValue(options.text, "");
161
162 this._relativeSize = 1.0;
163
164 parseFont(this);
165
166 this._updateClamping();
167 }
前面就是简单的对options进行判断之类的,最重要的就是最后一行的_updateClamping()方法
1 Label.prototype._updateClamping = function () {
2 Billboard._updateClamping(this._labelCollection, this);
3 };
继续点,这个函数对传入的label进行位置变换
1 Billboard._updateClamping = function (collection, owner) {
2 var scene = collection._scene;
3 if (!defined(scene) || !defined(scene.globe)) {
4 //>>includeStart('debug', pragmas.debug);
5 if (owner._heightReference !== HeightReference.NONE) {
6 throw new DeveloperError(
7 "Height reference is not supported without a scene and globe."
8 );
9 }
10 //>>includeEnd('debug');
11 return;
12 }
13
14 var globe = scene.globe;
15 var ellipsoid = globe.ellipsoid;
16 var surface = globe._surface;
17
18 var mode = scene.frameState.mode;
19
20 var modeChanged = mode !== owner._mode;
21 owner._mode = mode;
22
23 if (
24 (owner._heightReference === HeightReference.NONE || modeChanged) &&
25 defined(owner._removeCallbackFunc)
26 ) {
27 owner._removeCallbackFunc();
28 owner._removeCallbackFunc = undefined;
29 owner._clampedPosition = undefined;
30 }
31
32 if (
33 owner._heightReference === HeightReference.NONE ||
34 !defined(owner._position)
35 ) {
36 return;
37 }
38
39 var position = ellipsoid.cartesianToCartographic(owner._position);
40 if (!defined(position)) {
41 owner._actualClampedPosition = undefined;
42 return;
43 }
44
45 if (defined(owner._removeCallbackFunc)) {
46 owner._removeCallbackFunc();
47 }
48
49 function updateFunction(clampedPosition) {
50 if (owner._heightReference === HeightReference.RELATIVE_TO_GROUND) {
51 if (owner._mode === SceneMode.SCENE3D) {
52 var clampedCart = ellipsoid.cartesianToCartographic(
53 clampedPosition,
54 scratchCartographic
55 );
56 clampedCart.height += position.height;
57 ellipsoid.cartographicToCartesian(clampedCart, clampedPosition);
58 } else {
59 clampedPosition.x += position.height;
60 }
61 }
62 owner._clampedPosition = Cartesian3.clone(
63 clampedPosition,
64 owner._clampedPosition
65 );
66 }
67 owner._removeCallbackFunc = surface.updateHeight(position, updateFunction);
68
69 Cartographic.clone(position, scratchCartographic);
70 var height = globe.getHeight(position);
71 if (defined(height)) {
72 scratchCartographic.height = height;
73 }
74
75 ellipsoid.cartographicToCartesian(scratchCartographic, scratchPosition);
76
77 updateFunction(scratchPosition);
78 };
Cesium源码之Label(二)的更多相关文章
- Cesium源码剖析---Clipping Plane
之前就一直有写博客的想法,别人也建议写一写,但一直没有动手写,自己想了一下原因,就一个字:懒.懒.懒.为了改掉这个毛病,决定从今天开始写博客了,一方面对自己掌握的知识做一个梳理,另一方面和大家做一个交 ...
- 【原】FMDB源码阅读(二)
[原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...
- 【原】AFNetworking源码阅读(二)
[原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...
- 【原】SDWebImage源码阅读(二)
[原]SDWebImage源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 解决上一篇遗留的坑 上一篇中对sd_setImageWithURL函数简单分析了一下,还 ...
- YYModel 源码解读(二)之NSObject+YYModel.h (1)
本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...
- Cwinux源码解析(二)
我在我的个人博客上发表了第二篇解析文章.欢迎各位读者批评指正. Cwinux源码解析(二)
- DataTable源码分析(二)
DataTable源码分析(二) ===================== DataTable函数分析 ---------------- DataTable作为整个插件的入口,完成了整个表格的数据初 ...
- 一个普通的 Zepto 源码分析(二) - ajax 模块
一个普通的 Zepto 源码分析(二) - ajax 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块,以 ...
- Zepto源码分析(二)奇淫技巧总结
Zepto源码分析(一)核心代码分析 Zepto源码分析(二)奇淫技巧总结 目录 * 前言 * 短路操作符 * 参数重载(参数个数重载) * 参数重载(参数类型重载) * CSS操作 * 获取属性值的 ...
- Android源码浅析(二)——Ubuntu Root,Git,VMware Tools,安装输入法,主题美化,Dock,安装JDK和配置环境
Android源码浅析(二)--Ubuntu Root,Git,VMware Tools,安装输入法,主题美化,Dock,安装JDK和配置环境 接着上篇,上片主要是介绍了一些安装工具的小知识点Andr ...
随机推荐
- 关于windows7打不开hlp文件的解决方法
前言 其实也不是打不开,而是打开后是这样的. 也就是相当于打不开. 解决方案 安装对应架构版本补丁,重启电脑即可. 下载地址 包含64位和32位. 有能力的还望下载这个 下载地址 给我留点积分,感谢!
- Kafka Connect学习
一.基础介绍 1.概念 2.Debezium 为捕获数据更改(change data capture,CDC)提供了一个低延迟的流式处理平台.可以消费数据库每一个行级别(row-level)的更改. ...
- 【每日一题】2021年12月11日-69. Sqrt(x)/x的平方根
给你一个非负整数 x ,计算并返回 x 的 算术平方根 . 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 . 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或 ...
- 使用python脚本传递参数:(三种方式可收藏)
背景:使用python脚本传递参数在实际工作过程中还是比较常用,以下提供了好几种的实现方式: 一.使用sys.argv的数组传入说明:使用sys.argv必须按照先后的顺序传入对应的参数:sys.ar ...
- python 爬取豆瓣电影评论,并进行词云展示
python 爬取豆瓣电影评论,并进行词云展示 本文旨在提供爬取豆瓣电影<我不是药神>评论和词云展示的代码样例 1.分析URL 2.爬取前10页评论 3.进行词云展示 1.分析URL 我不 ...
- MassTransit 知多少 | 基于MassTransit Courier实现Saga 编排式分布式事务
Saga 模式 Saga 最初出现在1987年Hector Garcaa-Molrna & Kenneth Salem发表的一篇名为<Sagas>的论文里.其核心思想是将长事务拆分 ...
- jmeter json提取器提取某个属性的所有值
json 提取器各字段说明: Variable names:保存的变量名,后面使用${Variable names}引用 JSON Path expressions:调试通过的json path表达 ...
- 2022年7月14日,第四组 周鹏,认识JAVA的第二天(;´д`)ゞ(;д;)
那天,我遇到了JAVA 然后,我失去了头发 无论我用了多少办法 还是放不下那个它 我哭的像个傻瓜 但也没能留住它 如果再有一次从来 我愿为它披上薄纱 愿它安稳有个家 可我终究还是失去了它 失去了原本为 ...
- 【转载】EXCEL VBA 自定义排序的三种方法
何谓自定义排序,就是按指定的顺序对数据源进行排序呗. 共分享了三种方法: 第1种方法是系统自带的OrderCustom,优点是代码简洁,缺点是自定义序列有字符长度限制(255个). 第2种方法是字 ...
- 解决MySQL Connector/ODBC驱动无法安装Error1918
1.问题描述 我在一台windows服务器上安装好mysql之后,再安装mysql的ODBC连接驱动时,报错如下: 2.解决方法 之所以出现安装失败是由于缺少Miscrosoft Visual C++ ...