@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 2.68 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{watch as e,initial as s}from"../../../../core/reactiveUtils.js";import{ContentObjectType as r}from"../../webgl-engine/lib/ContentObjectType.js";import{Object3D as t}from"../../webgl-engine/lib/Object3D.js";import{UpdatePolicy as o}from"../../webgl-engine/lib/UpdatePolicy.js";import{WebGLLayer as i}from"../../webgl-engine/lib/WebGLLayer.js";class c{constructor(e){this._resourceFactory=e,this._resources=null,this._visible=!0,this._attached=!1}destroy(){this._destroyResources()}get object(){return null!=this._resources?this._resources.object:null}get resources(){return null!=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(null==this._resources||!e)return;const s=this._resources.object;this._resources.external.forEach((s=>{s.type!==r.Mesh&&s.type!==r.Line&&s.type!==r.Point||e.remove(s)})),s.removeAllGeometries(),this._resourceFactory.recreateGeometry(this._resources.external,s,this._resources.layer),this._resources.external.forEach((s=>{s.type!==r.Mesh&&s.type!==r.Line&&s.type!==r.Point||e.add(s)}))}_createOrDestroyResources(){this._attached?this._resources||this._createResources():this._destroyResources()}_createResources(){this._destroyResources();const c=this._resourceFactory,a=c.view,h=a._stage;if(!h)return;const u=new i(h,{pickable:!1,updatePolicy:o.SYNC}),l=new t({castShadow:!1}),n=c.createResources(l,u);n.forEach((e=>{h.add(e),e.type===r.Texture&&e.load(h.renderView.renderingContext)})),h.add(l),u.add(l);const _=c.cameraChanged,y=_?e((()=>a.state.camera),(e=>_(e)),s):null;this._resources={layer:u,object:l,external:n,cameraHandle:y},this._syncVisible()}_destroyResources(){if(null==this._resources)return;const e=this._resourceFactory.view._stage;e&&(e.remove(this._resources.object),this._resources.layer.destroy(),this._resources.external.forEach((s=>{e.remove(s),s.type===r.Texture&&s.unload()}))),this._resources.object.dispose(),this._resources.cameraHandle?.remove(),this._resourceFactory.destroyResources(this._resources.external),this._resources=null}_syncVisible(){null!=this._resources&&(this._resources.object.visible=this._visible)}}export{c as VisualElementResources};