@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) • 2.67 kB
JavaScript
import{Cartesian3 as n,Color as h,Entity as p,EntityCollection as d,JulianDate as u,Material as _,PolylineCollection as g,Ray as m}from"cesium";var o=class c{_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 g),this._points=new d,e.entities.add(this._points)}get sunPositionWC(){return this._uniformState._sunPositionWC}get sunDirectionWC(){return this._uniformState._sunDirectionWC}get isAnalyzing(){return this._analyzing}virtualSun(e,t=1e4){let i=n.normalize(n.subtract(this.sunPositionWC,e,new n),new n);return n.multiplyByScalar(i,t,i),{position:n.add(e,i,new n),direction:n.normalize(n.subtract(e,this.sunPositionWC,new n),new n)}}async analyze(e,t,i){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 t instanceof u?await this._analyzeSingleTime(e,t,i):await this._analyzeTimeRange(e,t,i)}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,t,i,s=h.LIMEGREEN.withAlpha(.8)){this._pointEntityId&&this._viewer.entities.removeById(this._pointEntityId);let a=i??3,r=this._viewer.entities.add(new p({ellipsoid:{show:t,radii:new n(a,a,a),material:t?s:void 0,fill:!0},position:e,id:c.DETECTION_ELLIPSOID_ID}));return this._pointEntityId=r.id,this._viewer.scene.render(),r}async _analyzeSingleTime(e,t,i){this._viewer.clock.currentTime=t,this._viewer.scene.render(t);let s=[e,this.virtualSun(e).position];if(i?.debugShowRays){let r=this._polylines.add({show:!0,positions:s,width:5,material:new _({fabric:{type:"Color",uniforms:{color:h.YELLOW.withAlpha(.5)}}})});this._objectsToExclude.push(r)}let a=await this._pick(e,i?.objectsToExclude);return{timestamp:t.toString(),result:a?a.object?.id?.id===this._pointEntityId:!1}}async _analyzeTimeRange(e,t,i){let s=[],{start:a,end:r,step:y}=t,l=a.clone();for(;u.compare(l,r)<=0;)s.push(await this._analyzeSingleTime(e,l,i)),u.addSeconds(l,y,l);return s}async _pick(e,t,i=.1){let{position:s,direction:a}=this.virtualSun(e),r=new m(s,a);return await this._viewer.scene.pickFromRayMostDetailed(r,this._getExcludedObjects(t),i)}_getExcludedObjects(e){return[...this._objectsToExclude,...e??[]]}};(e=>e.DETECTION_ELLIPSOID_ID="sunlight-detection-ellipsoid")(o||={});var w=o;export{w as Sunlight};