@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 2.6 kB
JavaScript
import{isSome as e,isNone as s}from"../../../../core/maybe.js";import{watch as r,initial as t}from"../../../../core/reactiveUtils.js";import{ContentObjectType as o}from"../../webgl-engine/lib/ContentObjectType.js";import{Object3D as i}from"../../webgl-engine/lib/Object3D.js";import{Texture as c}from"../../webgl-engine/lib/Texture.js";import{UpdatePolicy as a}from"../../webgl-engine/lib/UpdatePolicy.js";import{WebGLLayer as h}from"../../webgl-engine/lib/WebGLLayer.js";class u{constructor(e){this._resourceFactory=e,this._resources=null,this._visible=!0,this._attached=!1}destroy(){this._destroyResources()}get object(){return e(this._resources)?this._resources.object:null}get resources(){return e(this._resources)?this._resources.external:null}get visible(){return this._visible}set visible(e){e!==this._visible&&(this._visible=e,this._syncVisible())}get attached(){return this._attached}set attached(e){e!==this._attached&&(this._attached=e,this._createOrDestroyResources())}recreate(){this.attached&&this._createResources()}recreateGeometry(){if(!this._resourceFactory.recreateGeometry)return void this.recreate();const e=this._resourceFactory.view._stage;if(s(this._resources)||!e)return;const r=this._resources.object;this._resources.external.forEach((s=>{s.type!==o.Mesh&&s.type!==o.Line&&s.type!==o.Point||e.remove(s)})),r.removeAllGeometries(),this._resourceFactory.recreateGeometry(this._resources.external,r,this._resources.layer),this._resources.external.forEach((s=>{s.type!==o.Mesh&&s.type!==o.Line&&s.type!==o.Point||e.add(s)}))}_createOrDestroyResources(){this._attached?this._resources||this._createResources():this._destroyResources()}_createResources(){this._destroyResources();const e=this._resourceFactory,s=e.view,o=s._stage;if(!o)return;const u=new h({pickable:!1,updatePolicy:a.SYNC});o.add(u);const _=new i({castShadow:!1}),n=e.createResources(_,u);n.forEach((e=>{o.add(e),e instanceof c&&o.loadImmediate(e)})),o.add(_),u.add(_);const l=e.cameraChanged,y=l?r((()=>s.state.camera),(e=>l(e)),t):null;this._resources={layer:u,object:_,external:n,cameraHandle:y},this._syncVisible()}_destroyResources(){if(s(this._resources))return;const e=this._resourceFactory.view._stage;e&&(e.remove(this._resources.object),e.remove(this._resources.layer),this._resources.external.forEach((s=>e.remove(s)))),this._resources.object.dispose(),this._resources.cameraHandle?.remove(),this._resourceFactory.destroyResources(this._resources.external),this._resources=null}_syncVisible(){s(this._resources)||(this._resources.object.visible=this._visible)}}export{u as VisualElementResources};