12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535 |
- import BoundingRectangle from "../Core/BoundingRectangle.js";
- import Cartesian2 from "../Core/Cartesian2.js";
- import Cartesian3 from "../Core/Cartesian3.js";
- import Cartesian4 from "../Core/Cartesian4.js";
- import Cartographic from "../Core/Cartographic.js";
- import Color from "../Core/Color.js";
- import createGuid from "../Core/createGuid.js";
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
- import Matrix4 from "../Core/Matrix4.js";
- import NearFarScalar from "../Core/NearFarScalar.js";
- import Resource from "../Core/Resource.js";
- import HeightReference from "./HeightReference.js";
- import HorizontalOrigin from "./HorizontalOrigin.js";
- import SceneMode from "./SceneMode.js";
- import SceneTransforms from "./SceneTransforms.js";
- import VerticalOrigin from "./VerticalOrigin.js";
- function Billboard(options, billboardCollection) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
-
- if (
- defined(options.disableDepthTestDistance) &&
- options.disableDepthTestDistance < 0.0
- ) {
- throw new DeveloperError(
- "disableDepthTestDistance must be greater than or equal to 0.0."
- );
- }
-
- var translucencyByDistance = options.translucencyByDistance;
- var pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
- var scaleByDistance = options.scaleByDistance;
- var distanceDisplayCondition = options.distanceDisplayCondition;
- if (defined(translucencyByDistance)) {
-
- if (translucencyByDistance.far <= translucencyByDistance.near) {
- throw new DeveloperError(
- "translucencyByDistance.far must be greater than translucencyByDistance.near."
- );
- }
-
- translucencyByDistance = NearFarScalar.clone(translucencyByDistance);
- }
- if (defined(pixelOffsetScaleByDistance)) {
-
- if (pixelOffsetScaleByDistance.far <= pixelOffsetScaleByDistance.near) {
- throw new DeveloperError(
- "pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near."
- );
- }
-
- pixelOffsetScaleByDistance = NearFarScalar.clone(
- pixelOffsetScaleByDistance
- );
- }
- if (defined(scaleByDistance)) {
-
- if (scaleByDistance.far <= scaleByDistance.near) {
- throw new DeveloperError(
- "scaleByDistance.far must be greater than scaleByDistance.near."
- );
- }
-
- scaleByDistance = NearFarScalar.clone(scaleByDistance);
- }
- if (defined(distanceDisplayCondition)) {
-
- if (distanceDisplayCondition.far <= distanceDisplayCondition.near) {
- throw new DeveloperError(
- "distanceDisplayCondition.far must be greater than distanceDisplayCondition.near."
- );
- }
-
- distanceDisplayCondition = DistanceDisplayCondition.clone(
- distanceDisplayCondition
- );
- }
- this._show = defaultValue(options.show, true);
- this._position = Cartesian3.clone(
- defaultValue(options.position, Cartesian3.ZERO)
- );
- this._actualPosition = Cartesian3.clone(this._position);
- this._pixelOffset = Cartesian2.clone(
- defaultValue(options.pixelOffset, Cartesian2.ZERO)
- );
- this._translate = new Cartesian2(0.0, 0.0);
- this._eyeOffset = Cartesian3.clone(
- defaultValue(options.eyeOffset, Cartesian3.ZERO)
- );
- this._heightReference = defaultValue(
- options.heightReference,
- HeightReference.NONE
- );
- this._verticalOrigin = defaultValue(
- options.verticalOrigin,
- VerticalOrigin.CENTER
- );
- this._horizontalOrigin = defaultValue(
- options.horizontalOrigin,
- HorizontalOrigin.CENTER
- );
- this._scale = defaultValue(options.scale, 1.0);
- this._color = Color.clone(defaultValue(options.color, Color.WHITE));
- this._rotation = defaultValue(options.rotation, 0.0);
- this._alignedAxis = Cartesian3.clone(
- defaultValue(options.alignedAxis, Cartesian3.ZERO)
- );
- this._width = options.width;
- this._height = options.height;
- this._scaleByDistance = scaleByDistance;
- this._translucencyByDistance = translucencyByDistance;
- this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
- this._sizeInMeters = defaultValue(options.sizeInMeters, false);
- this._distanceDisplayCondition = distanceDisplayCondition;
- this._disableDepthTestDistance = options.disableDepthTestDistance;
- this._id = options.id;
- this._collection = defaultValue(options.collection, billboardCollection);
- this._pickId = undefined;
- this._pickPrimitive = defaultValue(options._pickPrimitive, this);
- this._billboardCollection = billboardCollection;
- this._dirty = false;
- this._index = -1;
- this._batchIndex = undefined;
- this._imageIndex = -1;
- this._imageIndexPromise = undefined;
- this._imageId = undefined;
- this._image = undefined;
- this._imageSubRegion = undefined;
- this._imageWidth = undefined;
- this._imageHeight = undefined;
- this._labelDimensions = undefined;
- this._labelHorizontalOrigin = undefined;
- this._labelTranslate = undefined;
- var image = options.image;
- var imageId = options.imageId;
- if (defined(image)) {
- if (!defined(imageId)) {
- if (typeof image === "string") {
- imageId = image;
- } else if (defined(image.src)) {
- imageId = image.src;
- } else {
- imageId = createGuid();
- }
- }
- this._imageId = imageId;
- this._image = image;
- }
- if (defined(options.imageSubRegion)) {
- this._imageId = imageId;
- this._imageSubRegion = options.imageSubRegion;
- }
- if (defined(this._billboardCollection._textureAtlas)) {
- this._loadImage();
- }
- this._actualClampedPosition = undefined;
- this._removeCallbackFunc = undefined;
- this._mode = SceneMode.SCENE3D;
- this._clusterShow = true;
- this._outlineColor = Color.clone(
- defaultValue(options.outlineColor, Color.BLACK)
- );
- this._outlineWidth = defaultValue(options.outlineWidth, 0.0);
- this._updateClamping();
- }
- var SHOW_INDEX = (Billboard.SHOW_INDEX = 0);
- var POSITION_INDEX = (Billboard.POSITION_INDEX = 1);
- var PIXEL_OFFSET_INDEX = (Billboard.PIXEL_OFFSET_INDEX = 2);
- var EYE_OFFSET_INDEX = (Billboard.EYE_OFFSET_INDEX = 3);
- var HORIZONTAL_ORIGIN_INDEX = (Billboard.HORIZONTAL_ORIGIN_INDEX = 4);
- var VERTICAL_ORIGIN_INDEX = (Billboard.VERTICAL_ORIGIN_INDEX = 5);
- var SCALE_INDEX = (Billboard.SCALE_INDEX = 6);
- var IMAGE_INDEX_INDEX = (Billboard.IMAGE_INDEX_INDEX = 7);
- var COLOR_INDEX = (Billboard.COLOR_INDEX = 8);
- var ROTATION_INDEX = (Billboard.ROTATION_INDEX = 9);
- var ALIGNED_AXIS_INDEX = (Billboard.ALIGNED_AXIS_INDEX = 10);
- var SCALE_BY_DISTANCE_INDEX = (Billboard.SCALE_BY_DISTANCE_INDEX = 11);
- var TRANSLUCENCY_BY_DISTANCE_INDEX = (Billboard.TRANSLUCENCY_BY_DISTANCE_INDEX = 12);
- var PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = (Billboard.PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = 13);
- var DISTANCE_DISPLAY_CONDITION = (Billboard.DISTANCE_DISPLAY_CONDITION = 14);
- var DISABLE_DEPTH_DISTANCE = (Billboard.DISABLE_DEPTH_DISTANCE = 15);
- Billboard.TEXTURE_COORDINATE_BOUNDS = 16;
- var SDF_INDEX = (Billboard.SDF_INDEX = 17);
- Billboard.NUMBER_OF_PROPERTIES = 18;
- function makeDirty(billboard, propertyChanged) {
- var billboardCollection = billboard._billboardCollection;
- if (defined(billboardCollection)) {
- billboardCollection._updateBillboard(billboard, propertyChanged);
- billboard._dirty = true;
- }
- }
- Object.defineProperties(Billboard.prototype, {
-
- show: {
- get: function () {
- return this._show;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (this._show !== value) {
- this._show = value;
- makeDirty(this, SHOW_INDEX);
- }
- },
- },
-
- position: {
- get: function () {
- return this._position;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var position = this._position;
- if (!Cartesian3.equals(position, value)) {
- Cartesian3.clone(value, position);
- Cartesian3.clone(value, this._actualPosition);
- this._updateClamping();
- makeDirty(this, POSITION_INDEX);
- }
- },
- },
-
- heightReference: {
- get: function () {
- return this._heightReference;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var heightReference = this._heightReference;
- if (value !== heightReference) {
- this._heightReference = value;
- this._updateClamping();
- makeDirty(this, POSITION_INDEX);
- }
- },
- },
-
- pixelOffset: {
- get: function () {
- return this._pixelOffset;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var pixelOffset = this._pixelOffset;
- if (!Cartesian2.equals(pixelOffset, value)) {
- Cartesian2.clone(value, pixelOffset);
- makeDirty(this, PIXEL_OFFSET_INDEX);
- }
- },
- },
-
- scaleByDistance: {
- get: function () {
- return this._scaleByDistance;
- },
- set: function (value) {
-
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance."
- );
- }
-
- var scaleByDistance = this._scaleByDistance;
- if (!NearFarScalar.equals(scaleByDistance, value)) {
- this._scaleByDistance = NearFarScalar.clone(value, scaleByDistance);
- makeDirty(this, SCALE_BY_DISTANCE_INDEX);
- }
- },
- },
-
- translucencyByDistance: {
- get: function () {
- return this._translucencyByDistance;
- },
- set: function (value) {
-
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance."
- );
- }
-
- var translucencyByDistance = this._translucencyByDistance;
- if (!NearFarScalar.equals(translucencyByDistance, value)) {
- this._translucencyByDistance = NearFarScalar.clone(
- value,
- translucencyByDistance
- );
- makeDirty(this, TRANSLUCENCY_BY_DISTANCE_INDEX);
- }
- },
- },
-
- pixelOffsetScaleByDistance: {
- get: function () {
- return this._pixelOffsetScaleByDistance;
- },
- set: function (value) {
-
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance."
- );
- }
-
- var pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
- if (!NearFarScalar.equals(pixelOffsetScaleByDistance, value)) {
- this._pixelOffsetScaleByDistance = NearFarScalar.clone(
- value,
- pixelOffsetScaleByDistance
- );
- makeDirty(this, PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX);
- }
- },
- },
-
- eyeOffset: {
- get: function () {
- return this._eyeOffset;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var eyeOffset = this._eyeOffset;
- if (!Cartesian3.equals(eyeOffset, value)) {
- Cartesian3.clone(value, eyeOffset);
- makeDirty(this, EYE_OFFSET_INDEX);
- }
- },
- },
-
- horizontalOrigin: {
- get: function () {
- return this._horizontalOrigin;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (this._horizontalOrigin !== value) {
- this._horizontalOrigin = value;
- makeDirty(this, HORIZONTAL_ORIGIN_INDEX);
- }
- },
- },
-
- verticalOrigin: {
- get: function () {
- return this._verticalOrigin;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (this._verticalOrigin !== value) {
- this._verticalOrigin = value;
- makeDirty(this, VERTICAL_ORIGIN_INDEX);
- }
- },
- },
-
- scale: {
- get: function () {
- return this._scale;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (this._scale !== value) {
- this._scale = value;
- makeDirty(this, SCALE_INDEX);
- }
- },
- },
-
- color: {
- get: function () {
- return this._color;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var color = this._color;
- if (!Color.equals(color, value)) {
- Color.clone(value, color);
- makeDirty(this, COLOR_INDEX);
- }
- },
- },
-
- rotation: {
- get: function () {
- return this._rotation;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (this._rotation !== value) {
- this._rotation = value;
- makeDirty(this, ROTATION_INDEX);
- }
- },
- },
-
- alignedAxis: {
- get: function () {
- return this._alignedAxis;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var alignedAxis = this._alignedAxis;
- if (!Cartesian3.equals(alignedAxis, value)) {
- Cartesian3.clone(value, alignedAxis);
- makeDirty(this, ALIGNED_AXIS_INDEX);
- }
- },
- },
-
- width: {
- get: function () {
- return defaultValue(this._width, this._imageWidth);
- },
- set: function (value) {
- if (this._width !== value) {
- this._width = value;
- makeDirty(this, IMAGE_INDEX_INDEX);
- }
- },
- },
-
- height: {
- get: function () {
- return defaultValue(this._height, this._imageHeight);
- },
- set: function (value) {
- if (this._height !== value) {
- this._height = value;
- makeDirty(this, IMAGE_INDEX_INDEX);
- }
- },
- },
-
- sizeInMeters: {
- get: function () {
- return this._sizeInMeters;
- },
- set: function (value) {
- if (this._sizeInMeters !== value) {
- this._sizeInMeters = value;
- makeDirty(this, COLOR_INDEX);
- }
- },
- },
-
- distanceDisplayCondition: {
- get: function () {
- return this._distanceDisplayCondition;
- },
- set: function (value) {
- if (
- !DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)
- ) {
-
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance."
- );
- }
-
- this._distanceDisplayCondition = DistanceDisplayCondition.clone(
- value,
- this._distanceDisplayCondition
- );
- makeDirty(this, DISTANCE_DISPLAY_CONDITION);
- }
- },
- },
-
- disableDepthTestDistance: {
- get: function () {
- return this._disableDepthTestDistance;
- },
- set: function (value) {
- if (this._disableDepthTestDistance !== value) {
-
- if (defined(value) && value < 0.0) {
- throw new DeveloperError(
- "disableDepthTestDistance must be greater than or equal to 0.0."
- );
- }
-
- this._disableDepthTestDistance = value;
- makeDirty(this, DISABLE_DEPTH_DISTANCE);
- }
- },
- },
-
- id: {
- get: function () {
- return this._id;
- },
- set: function (value) {
- this._id = value;
- if (defined(this._pickId)) {
- this._pickId.object.id = value;
- }
- },
- },
-
- pickPrimitive: {
- get: function () {
- return this._pickPrimitive;
- },
- set: function (value) {
- this._pickPrimitive = value;
- if (defined(this._pickId)) {
- this._pickId.object.primitive = value;
- }
- },
- },
-
- pickId: {
- get: function () {
- return this._pickId;
- },
- },
-
- image: {
- get: function () {
- return this._imageId;
- },
- set: function (value) {
- if (!defined(value)) {
- this._imageIndex = -1;
- this._imageSubRegion = undefined;
- this._imageId = undefined;
- this._image = undefined;
- this._imageIndexPromise = undefined;
- makeDirty(this, IMAGE_INDEX_INDEX);
- } else if (typeof value === "string") {
- this.setImage(value, value);
- } else if (value instanceof Resource) {
- this.setImage(value.url, value);
- } else if (defined(value.src)) {
- this.setImage(value.src, value);
- } else {
- this.setImage(createGuid(), value);
- }
- },
- },
-
- ready: {
- get: function () {
- return this._imageIndex !== -1;
- },
- },
-
- _clampedPosition: {
- get: function () {
- return this._actualClampedPosition;
- },
- set: function (value) {
- this._actualClampedPosition = Cartesian3.clone(
- value,
- this._actualClampedPosition
- );
- makeDirty(this, POSITION_INDEX);
- },
- },
-
- clusterShow: {
- get: function () {
- return this._clusterShow;
- },
- set: function (value) {
- if (this._clusterShow !== value) {
- this._clusterShow = value;
- makeDirty(this, SHOW_INDEX);
- }
- },
- },
-
- outlineColor: {
- get: function () {
- return this._outlineColor;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var outlineColor = this._outlineColor;
- if (!Color.equals(outlineColor, value)) {
- Color.clone(value, outlineColor);
- makeDirty(this, SDF_INDEX);
- }
- },
- },
-
- outlineWidth: {
- get: function () {
- return this._outlineWidth;
- },
- set: function (value) {
- if (this._outlineWidth !== value) {
- this._outlineWidth = value;
- makeDirty(this, SDF_INDEX);
- }
- },
- },
- });
- Billboard.prototype.getPickId = function (context) {
- if (!defined(this._pickId)) {
- this._pickId = context.createPickId({
- primitive: this._pickPrimitive,
- collection: this._collection,
- id: this._id,
- });
- }
- return this._pickId;
- };
- Billboard.prototype._updateClamping = function () {
- Billboard._updateClamping(this._billboardCollection, this);
- };
- var scratchCartographic = new Cartographic();
- var scratchPosition = new Cartesian3();
- Billboard._updateClamping = function (collection, owner) {
- var scene = collection._scene;
- if (!defined(scene) || !defined(scene.globe)) {
-
- if (owner._heightReference !== HeightReference.NONE) {
- throw new DeveloperError(
- "Height reference is not supported without a scene and globe."
- );
- }
-
- return;
- }
- var globe = scene.globe;
- var ellipsoid = globe.ellipsoid;
- var surface = globe._surface;
- var mode = scene.frameState.mode;
- var modeChanged = mode !== owner._mode;
- owner._mode = mode;
- if (
- (owner._heightReference === HeightReference.NONE || modeChanged) &&
- defined(owner._removeCallbackFunc)
- ) {
- owner._removeCallbackFunc();
- owner._removeCallbackFunc = undefined;
- owner._clampedPosition = undefined;
- }
- if (
- owner._heightReference === HeightReference.NONE ||
- !defined(owner._position)
- ) {
- return;
- }
- var position = ellipsoid.cartesianToCartographic(owner._position);
- if (!defined(position)) {
- owner._actualClampedPosition = undefined;
- return;
- }
- if (defined(owner._removeCallbackFunc)) {
- owner._removeCallbackFunc();
- }
- function updateFunction(clampedPosition) {
- if (owner._heightReference === HeightReference.RELATIVE_TO_GROUND) {
- if (owner._mode === SceneMode.SCENE3D) {
- var clampedCart = ellipsoid.cartesianToCartographic(
- clampedPosition,
- scratchCartographic
- );
- clampedCart.height += position.height;
- ellipsoid.cartographicToCartesian(clampedCart, clampedPosition);
- } else {
- clampedPosition.x += position.height;
- }
- }
- owner._clampedPosition = Cartesian3.clone(
- clampedPosition,
- owner._clampedPosition
- );
- }
- owner._removeCallbackFunc = surface.updateHeight(position, updateFunction);
- Cartographic.clone(position, scratchCartographic);
- var height = globe.getHeight(position);
- if (defined(height)) {
- scratchCartographic.height = height;
- }
- ellipsoid.cartographicToCartesian(scratchCartographic, scratchPosition);
- updateFunction(scratchPosition);
- };
- Billboard.prototype._loadImage = function () {
- var atlas = this._billboardCollection._textureAtlas;
- var imageId = this._imageId;
- var image = this._image;
- var imageSubRegion = this._imageSubRegion;
- var imageIndexPromise;
- if (defined(image)) {
- imageIndexPromise = atlas.addImage(imageId, image);
- }
- if (defined(imageSubRegion)) {
- imageIndexPromise = atlas.addSubRegion(imageId, imageSubRegion);
- }
- this._imageIndexPromise = imageIndexPromise;
- if (!defined(imageIndexPromise)) {
- return;
- }
- var that = this;
- imageIndexPromise
- .then(function (index) {
- if (
- that._imageId !== imageId ||
- that._image !== image ||
- !BoundingRectangle.equals(that._imageSubRegion, imageSubRegion)
- ) {
-
- return;
- }
-
- var textureCoordinates = atlas.textureCoordinates[index];
- that._imageWidth = atlas.texture.width * textureCoordinates.width;
- that._imageHeight = atlas.texture.height * textureCoordinates.height;
- that._imageIndex = index;
- that._ready = true;
- that._image = undefined;
- that._imageIndexPromise = undefined;
- makeDirty(that, IMAGE_INDEX_INDEX);
- })
- .otherwise(function (error) {
- console.error("Error loading image for billboard: " + error);
- that._imageIndexPromise = undefined;
- });
- };
- Billboard.prototype.setImage = function (id, image) {
-
- if (!defined(id)) {
- throw new DeveloperError("id is required.");
- }
- if (!defined(image)) {
- throw new DeveloperError("image is required.");
- }
-
- if (this._imageId === id) {
- return;
- }
- this._imageIndex = -1;
- this._imageSubRegion = undefined;
- this._imageId = id;
- this._image = image;
- if (defined(this._billboardCollection._textureAtlas)) {
- this._loadImage();
- }
- };
- Billboard.prototype.setImageSubRegion = function (id, subRegion) {
-
- if (!defined(id)) {
- throw new DeveloperError("id is required.");
- }
- if (!defined(subRegion)) {
- throw new DeveloperError("subRegion is required.");
- }
-
- if (
- this._imageId === id &&
- BoundingRectangle.equals(this._imageSubRegion, subRegion)
- ) {
- return;
- }
- this._imageIndex = -1;
- this._imageId = id;
- this._imageSubRegion = BoundingRectangle.clone(subRegion);
- if (defined(this._billboardCollection._textureAtlas)) {
- this._loadImage();
- }
- };
- Billboard.prototype._setTranslate = function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- var translate = this._translate;
- if (!Cartesian2.equals(translate, value)) {
- Cartesian2.clone(value, translate);
- makeDirty(this, PIXEL_OFFSET_INDEX);
- }
- };
- Billboard.prototype._getActualPosition = function () {
- return defined(this._clampedPosition)
- ? this._clampedPosition
- : this._actualPosition;
- };
- Billboard.prototype._setActualPosition = function (value) {
- if (!defined(this._clampedPosition)) {
- Cartesian3.clone(value, this._actualPosition);
- }
- makeDirty(this, POSITION_INDEX);
- };
- var tempCartesian3 = new Cartesian4();
- Billboard._computeActualPosition = function (
- billboard,
- position,
- frameState,
- modelMatrix
- ) {
- if (defined(billboard._clampedPosition)) {
- if (frameState.mode !== billboard._mode) {
- billboard._updateClamping();
- }
- return billboard._clampedPosition;
- } else if (frameState.mode === SceneMode.SCENE3D) {
- return position;
- }
- Matrix4.multiplyByPoint(modelMatrix, position, tempCartesian3);
- return SceneTransforms.computeActualWgs84Position(frameState, tempCartesian3);
- };
- var scratchCartesian3 = new Cartesian3();
- Billboard._computeScreenSpacePosition = function (
- modelMatrix,
- position,
- eyeOffset,
- pixelOffset,
- scene,
- result
- ) {
-
- var positionWorld = Matrix4.multiplyByPoint(
- modelMatrix,
- position,
- scratchCartesian3
- );
-
- var positionWC = SceneTransforms.wgs84WithEyeOffsetToWindowCoordinates(
- scene,
- positionWorld,
- eyeOffset,
- result
- );
- if (!defined(positionWC)) {
- return undefined;
- }
-
- Cartesian2.add(positionWC, pixelOffset, positionWC);
- return positionWC;
- };
- var scratchPixelOffset = new Cartesian2(0.0, 0.0);
- Billboard.prototype.computeScreenSpacePosition = function (scene, result) {
- var billboardCollection = this._billboardCollection;
- if (!defined(result)) {
- result = new Cartesian2();
- }
-
- if (!defined(billboardCollection)) {
- throw new DeveloperError(
- "Billboard must be in a collection. Was it removed?"
- );
- }
- if (!defined(scene)) {
- throw new DeveloperError("scene is required.");
- }
-
-
- Cartesian2.clone(this._pixelOffset, scratchPixelOffset);
- Cartesian2.add(scratchPixelOffset, this._translate, scratchPixelOffset);
- var modelMatrix = billboardCollection.modelMatrix;
- var position = this._position;
- if (defined(this._clampedPosition)) {
- position = this._clampedPosition;
- if (scene.mode !== SceneMode.SCENE3D) {
-
- var projection = scene.mapProjection;
- var ellipsoid = projection.ellipsoid;
- var cart = projection.unproject(position, scratchCartographic);
- position = ellipsoid.cartographicToCartesian(cart, scratchCartesian3);
- modelMatrix = Matrix4.IDENTITY;
- }
- }
- var windowCoordinates = Billboard._computeScreenSpacePosition(
- modelMatrix,
- position,
- this._eyeOffset,
- scratchPixelOffset,
- scene,
- result
- );
- return windowCoordinates;
- };
- Billboard.getScreenSpaceBoundingBox = function (
- billboard,
- screenSpacePosition,
- result
- ) {
- var width = billboard.width;
- var height = billboard.height;
- var scale = billboard.scale;
- width *= scale;
- height *= scale;
- var x = screenSpacePosition.x;
- if (billboard.horizontalOrigin === HorizontalOrigin.RIGHT) {
- x -= width;
- } else if (billboard.horizontalOrigin === HorizontalOrigin.CENTER) {
- x -= width * 0.5;
- }
- var y = screenSpacePosition.y;
- if (
- billboard.verticalOrigin === VerticalOrigin.BOTTOM ||
- billboard.verticalOrigin === VerticalOrigin.BASELINE
- ) {
- y -= height;
- } else if (billboard.verticalOrigin === VerticalOrigin.CENTER) {
- y -= height * 0.5;
- }
- if (!defined(result)) {
- result = new BoundingRectangle();
- }
- result.x = x;
- result.y = y;
- result.width = width;
- result.height = height;
- return result;
- };
- Billboard.prototype.equals = function (other) {
- return (
- this === other ||
- (defined(other) &&
- this._id === other._id &&
- Cartesian3.equals(this._position, other._position) &&
- this._imageId === other._imageId &&
- this._show === other._show &&
- this._scale === other._scale &&
- this._verticalOrigin === other._verticalOrigin &&
- this._horizontalOrigin === other._horizontalOrigin &&
- this._heightReference === other._heightReference &&
- BoundingRectangle.equals(this._imageSubRegion, other._imageSubRegion) &&
- Color.equals(this._color, other._color) &&
- Cartesian2.equals(this._pixelOffset, other._pixelOffset) &&
- Cartesian2.equals(this._translate, other._translate) &&
- Cartesian3.equals(this._eyeOffset, other._eyeOffset) &&
- NearFarScalar.equals(this._scaleByDistance, other._scaleByDistance) &&
- NearFarScalar.equals(
- this._translucencyByDistance,
- other._translucencyByDistance
- ) &&
- NearFarScalar.equals(
- this._pixelOffsetScaleByDistance,
- other._pixelOffsetScaleByDistance
- ) &&
- DistanceDisplayCondition.equals(
- this._distanceDisplayCondition,
- other._distanceDisplayCondition
- ) &&
- this._disableDepthTestDistance === other._disableDepthTestDistance)
- );
- };
- Billboard.prototype._destroy = function () {
- if (defined(this._customData)) {
- this._billboardCollection._scene.globe._surface.removeTileCustomData(
- this._customData
- );
- this._customData = undefined;
- }
- if (defined(this._removeCallbackFunc)) {
- this._removeCallbackFunc();
- this._removeCallbackFunc = undefined;
- }
- this.image = undefined;
- this._pickId = this._pickId && this._pickId.destroy();
- this._billboardCollection = undefined;
- };
- export default Billboard;
|