我们查看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 };
标签:debug,defaultValue,var,Label,owner,源码,._,Cesium,options From: https://www.cnblogs.com/LJXXXX/p/17058616.html