UNPKG

@juun-roh/cesium-utils

Version:

Solve common Cesium.js challenges: combine multiple terrain sources, tag and filter entity collections, and add visual highlights.

2 lines (1 loc) 7.04 kB
"use strict";var f=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var v=(n,t)=>{for(var i in t)f(n,i,{get:t[i],enumerable:!0})},O=(n,t,i,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of y(t))!m.call(n,o)&&o!==i&&f(n,o,{get:()=>t[o],enumerable:!(e=p(t,o))||e.enumerable});return n};var w=n=>O(f({},"__esModule",{value:!0}),n);var C={};v(C,{Highlight:()=>_,SilhouetteHighlight:()=>h,SurfaceHighlight:()=>c});module.exports=w(C);var l=require("cesium");var s=require("cesium"),h=class{_color=s.Color.RED;_silhouette;_composite;_stages;_entity;_currentObject;_currentOptions;constructor(t){this._stages=t.scene.postProcessStages,this._silhouette=s.PostProcessStageLibrary.createEdgeDetectionStage(),this._silhouette.uniforms.color=this._color,this._silhouette.uniforms.length=.01,this._silhouette.selected=[],this._composite=s.PostProcessStageLibrary.createSilhouetteStage([this._silhouette]),this._stages.add(this._composite)}get color(){return this._color}set color(t){this._color=t}get currentObject(){return this._currentObject}show(t,i){if((0,s.defined)(t)&&!(this._currentObject===t&&this._optionsEqual(this._currentOptions,i))){this._clearHighlights();try{if(t instanceof s.Cesium3DTileFeature)this._silhouette.uniforms.color=i?.color||this._color,this._silhouette.selected.push(t);else{if(!t.model)return;this._entity=t,t.model.silhouetteSize=new s.ConstantProperty(i?.width||2),t.model.silhouetteColor=new s.ConstantProperty(i?.color||this._color)}this._currentObject=t,this._currentOptions=i?{...i}:void 0}catch(e){console.error("Failed to highlight object:",e),this._currentObject=void 0,this._currentOptions=void 0}}}hide(){this._clearHighlights(),this._currentObject=void 0,this._currentOptions=void 0}destroy(){this.hide(),this._composite&&this._stages.remove(this._composite),this._currentObject=void 0,this._currentOptions=void 0}_optionsEqual(t,i){return!t&&!i?!0:!t||!i?!1:t.outline===i.outline&&t.width===i.width&&s.Color.equals(t.color||this._color,i.color||this._color)}_clearHighlights(){this._silhouette.selected.length>0&&(this._silhouette.selected=[]),this._entity?.model&&(this._entity.model.silhouetteColor=new s.ConstantProperty(s.Color.TRANSPARENT),this._entity.model.silhouetteSize=new s.ConstantProperty(0),this._entity=void 0)}};var r=require("cesium"),c=class{_color=r.Color.RED;_entity;_entities;_currentObject;_currentOptions;constructor(t){this._entities=t.entities,this._entity=this._entities.add(new r.Entity({id:`highlight-entity-${Math.random().toString(36).substring(2)}`,show:!1}))}get color(){return this._color}set color(t){this._color=t}get entity(){return this._entity}get currentObject(){return this._currentObject}show(t,i){if(!(!(0,r.defined)(t)||!this._entity)){if(this._currentObject===t&&this._optionsEqual(this._currentOptions,i))return this._entity;this._clearGeometries();try{if(t instanceof r.Entity&&(t.polygon||t.polyline||t.rectangle))this._update(t,i);else if(t instanceof r.GroundPrimitive)this._update(t,i);else{this._currentObject=void 0,this._currentOptions=void 0;return}return this._currentObject=t,this._currentOptions=i?{...i}:void 0,this._entity.show=!0,this._entity}catch(e){console.error("Failed to highlight object:",e),this._currentObject=void 0,this._currentOptions=void 0;return}}}hide(){this._entity&&(this._entity.show=!1),this._currentObject=void 0,this._currentOptions=void 0}destroy(){this._entities.contains(this._entity)&&this._entities.remove(this._entity),this._currentObject=void 0,this._currentOptions=void 0}_optionsEqual(t,i){return!t&&!i?!0:!t||!i?!1:t.outline===i.outline&&t.width===i.width&&r.Color.equals(t.color||this._color,i.color||this._color)}_clearGeometries(){this._entity.polygon=void 0,this._entity.polyline=void 0,this._entity.rectangle=void 0}_update(t,i={color:this._color,outline:!1,width:2}){if(t instanceof r.Entity){if(t.polygon)if(i.outline){let e=t.polygon.hierarchy?.getValue();if(e&&e.positions){let o;e.positions.length>0&&!r.Cartesian3.equals(e.positions[0],e.positions[e.positions.length-1])?o=[...e.positions,e.positions[0]]:o=e.positions,this._entity.polyline=new r.PolylineGraphics({positions:o,material:i.color,width:i.width||2,clampToGround:!0})}}else{let e=t.polygon.hierarchy?.getValue();e&&(this._entity.polygon=new r.PolygonGraphics({hierarchy:e,material:i.color,heightReference:r.HeightReference.CLAMP_TO_GROUND,classificationType:t.polygon.classificationType?.getValue()||r.ClassificationType.BOTH}))}else if(t.polyline){let e=t.polyline.positions?.getValue();if(e){let o=t.polyline.width?.getValue();this._entity.polyline=new r.PolylineGraphics({positions:e,material:i.color,width:o+(i.width||2),clampToGround:!0})}}else if(t.rectangle)if(i.outline){let e=t.rectangle.coordinates?.getValue();if(e){let o=[r.Cartesian3.fromRadians(e.west,e.north),r.Cartesian3.fromRadians(e.east,e.north),r.Cartesian3.fromRadians(e.east,e.south),r.Cartesian3.fromRadians(e.west,e.south),r.Cartesian3.fromRadians(e.west,e.north)];this._entity.polyline=new r.PolylineGraphics({positions:o,material:i.color,width:i.width||2,clampToGround:!0})}}else{let e=t.rectangle.coordinates?.getValue();e&&(this._entity.rectangle=new r.RectangleGraphics({coordinates:e,material:i.color,heightReference:r.HeightReference.CLAMP_TO_GROUND}))}}else if(t instanceof r.GroundPrimitive){let e=t.geometryInstances,o=Array.isArray(e)?e[0]:e;if(!o.geometry.attributes.position)return;let a=o.geometry.attributes.position.values,d=[];for(let u=0;u<a.length;u+=3)d.push(new r.Cartesian3(a[u],a[u+1],a[u+2]));i.outline?this._entity.polyline=new r.PolylineGraphics({positions:d,material:i.color,width:i.width||2,clampToGround:!0}):this._entity.polygon=new r.PolygonGraphics({hierarchy:new r.PolygonHierarchy(d),material:i.color,heightReference:r.HeightReference.CLAMP_TO_GROUND,classificationType:r.ClassificationType.BOTH})}}};var g=class n{static instances=new WeakMap;_surface;_silhouette;_color=l.Color.RED;constructor(t){this._surface=new c(t),this._silhouette=new h(t),this._surface.color=this._color,this._silhouette.color=this._color}get color(){return this._color}set color(t){this._color=t,this._surface.color=t,this._silhouette.color=t}static getInstance(t){let i=t.container;return n.instances.has(i)||n.instances.set(i,new n(t)),n.instances.get(i)}static releaseInstance(t){let i=t.container,e=n.instances.get(i);e&&(e.hide(),e._surface&&e._surface.destroy(),e._silhouette&&e._silhouette.destroy(),n.instances.delete(i))}show(t,i={color:this._color}){let e=this._getObject(t);if((0,l.defined)(e))return e instanceof l.Cesium3DTileFeature?this._silhouette.show(e,i):e instanceof l.Entity&&e.model?this._silhouette.show(e,i):this._surface.show(e,i)}_getObject(t){if((0,l.defined)(t)){if(t instanceof l.Entity||t instanceof l.Cesium3DTileFeature||t instanceof l.GroundPrimitive)return t;if(t.id instanceof l.Entity)return t.id;if(t.primitive instanceof l.GroundPrimitive)return t.primitive}}hide(){this._surface.hide(),this._silhouette.hide()}},_=g;