@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) • 3.24 kB
JavaScript
"use strict";var c=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var g=(a,e)=>{for(var i in e)c(a,i,{get:e[i],enumerable:!0})},m=(a,e,i,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of d(e))!_.call(a,s)&&s!==i&&c(a,s,{get:()=>e[s],enumerable:!(n=p(e,s))||n.enumerable});return a};var w=a=>m(c({},"__esModule",{value:!0}),a);var S={};g(S,{Sunlight:()=>h});module.exports=w(S);var t=require("cesium"),l=class a{_uniformState;_viewer;_analyzing=!1;_pointEntityId;_objectsToExclude=[];_polylines;_points;constructor(e){this._uniformState=e.scene.context.uniformState,this._viewer=e,this._polylines=e.scene.primitives.add(new t.PolylineCollection),this._points=new t.EntityCollection,e.entities.add(this._points)}get sunPositionWC(){return this._uniformState._sunPositionWC}get sunDirectionWC(){return this._uniformState._sunDirectionWC}get isAnalyzing(){return this._analyzing}virtualSun(e,i=1e4){let n=t.Cartesian3.normalize(t.Cartesian3.subtract(this.sunPositionWC,e,new t.Cartesian3),new t.Cartesian3);return t.Cartesian3.multiplyByScalar(n,i,n),{position:t.Cartesian3.add(e,n,new t.Cartesian3),direction:t.Cartesian3.normalize(t.Cartesian3.subtract(e,this.sunPositionWC,new t.Cartesian3),new t.Cartesian3)}}async analyze(e,i,n){if(!this._pointEntityId)throw new Error("Analyze error boundary hasn't been set. Create Error boundary entity first using createDetectionEllipsoid first.");let s=this._viewer.clock.currentTime.clone();this._analyzing=!0;try{return i instanceof t.JulianDate?await this._analyzeSingleTime(e,i,n):await this._analyzeTimeRange(e,i,n)}finally{this._viewer.clock.currentTime=s,this._viewer.scene.render(),this._analyzing=!1}}clear(){this._objectsToExclude=[],this._points.values.length>0&&this._points.removeAll(),this._polylines.length>0&&this._polylines.removeAll()}setTargetPoint(e,i,n,s=t.Color.LIMEGREEN.withAlpha(.8)){this._pointEntityId&&this._viewer.entities.removeById(this._pointEntityId);let r=n??3,o=this._viewer.entities.add(new t.Entity({ellipsoid:{show:i,radii:new t.Cartesian3(r,r,r),material:i?s:void 0,fill:!0},position:e,id:a.DETECTION_ELLIPSOID_ID}));return this._pointEntityId=o.id,this._viewer.scene.render(),o}async _analyzeSingleTime(e,i,n){this._viewer.clock.currentTime=i,this._viewer.scene.render(i);let s=[e,this.virtualSun(e).position];if(n?.debugShowRays){let o=this._polylines.add({show:!0,positions:s,width:5,material:new t.Material({fabric:{type:"Color",uniforms:{color:t.Color.YELLOW.withAlpha(.5)}}})});this._objectsToExclude.push(o)}let r=await this._pick(e,n?.objectsToExclude);return{timestamp:i.toString(),result:r?r.object?.id?.id===this._pointEntityId:!1}}async _analyzeTimeRange(e,i,n){let s=[],{start:r,end:o,step:y}=i,u=r.clone();for(;t.JulianDate.compare(u,o)<=0;)s.push(await this._analyzeSingleTime(e,u,n)),t.JulianDate.addSeconds(u,y,u);return s}async _pick(e,i,n=.1){let{position:s,direction:r}=this.virtualSun(e),o=new t.Ray(s,r);return await this._viewer.scene.pickFromRayMostDetailed(o,this._getExcludedObjects(i),n)}_getExcludedObjects(e){return[...this._objectsToExclude,...e??[]]}};(e=>e.DETECTION_ELLIPSOID_ID="sunlight-detection-ellipsoid")(l||={});var h=l;