@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 5.69 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{getOrCreateMapValue as t}from"../../../../../core/MapUtils.js";import s from"../../../../../core/PooledArray.js";import{assert as e}from"../../lib/Util.js";import{bufferRangePool as n,mergeAdjacentRanges as a}from"./BufferRange.js";import{DrawCommand as i}from"./DrawCommand.js";import{InstancedCommandVAO as o}from"./InstancedCommandVAO.js";import{InstancedCommandVAOs as r}from"./InstancedCommandVAOs.js";import{defaultHighlightName as h}from"../../../../support/HighlightDefaults.js";class m{constructor(t,s){this.isInstanced=t,this.targetBuffers=s,this.vao=null,this.textureBuffer=null,this.vaoEndElement=0,this.textureEndElement=0,this._instances=new Map,this.hasHiddenInstances=!1,this.hasOccludees=!1,this.drawCommandsDirty=!0,this.highlightNames=new Set,this.drawCommandsDefault=c(),this.drawCommandsHighlights=new Map,this.drawCommandsOccludees=c(),this.drawCommandsShadowHighlightRest=c(),this._instancedCommandVAOs=null;const e={},n={};for(const a of s)e[a]=0,n[a]=[];this._numElements=e,this._holes=n}dispose(){this.isInstanced?(this.vao?.buffer("geometry")?.dispose(),this.vao?.disposeVAOOnly(),this.instancedCommandVAOs?.dispose()):this.vao?.dispose(),this.textureBuffer?.dispose()}getTargetBuffer(t){return"geometry"===t?this.vao?.buffer("geometry"):this.textureBuffer}get numElements(){return this._numElements}get instances(){return this._instances}get writeableInstances(){return this._instances}clearHoles(){for(const t of this.targetBuffers){const s=this._holes[t];for(const t of s)n.release(t);s.length=0}}getEndElement(t){return"geometry"===t?this.vaoEndElement:this.textureEndElement}get holes(){return this._holes}get hasHighlights(){return this.highlightNames.size>0}get instancedCommandVAOs(){return this._instancedCommandVAOs}resetInstanceSummary(){this.hasHiddenInstances=!1,this.hasOccludees=!1,this.highlightNames.clear()}updateIfDrawCommandsDirty(t){if(this.drawCommandsDirty){this.resetInstanceSummary();for(const t of this.instances.values())this.updateDrawState(t);this.updateDrawCommands(t)}}addInstance(t,s){e(!this._instances.has(t)),this._instances.set(t,s);for(const e of this.targetBuffers)this._numElements[e]+=s.targetRanges[e].numElements}deleteInstance(t){const s=this._instances.get(t);if(s){for(const t of this.targetBuffers){const e=s.targetRanges[t];this._numElements[t]-=e.numElements,n.release(e)}this._instances.delete(t)}}updateInstances(){const{targetBuffers:t}=this,s={};for(const e of t)s[e]=0;for(const e of this._instances.values()){for(const n of t)s[n]+=e.targetRanges[n].numElements;this.updateDrawState(e)}this._numElements=s}updateDrawState(t){if(t.isVisible){const{highlightName:s}=t;s&&this.highlightNames.add(s),t.hasOccludees&&(this.hasOccludees=!0)}else this.hasHiddenInstances=!0}updateDrawCommands(t){this._updateDrawCommands(t),this._updateInstancedCommandVAOs()}_updateDrawCommands(t){if(this.drawCommandsDefault.clear(),this.drawCommandsOccludees.clear(),this.drawCommandsDirty=!1,0===this._instances.size)return;const{sortedInstances:s}=this;if(this._updateHighlightDrawCommands(t,s),!this.needsMultipleCommands){const t=this.drawCommandsDefault.pushNew(),s=this.holes.geometry.at(0);return this.vao&&1===this.holes.geometry.length&&s.to===this.vaoEndElement?(t.first=0,void(t.count=s.from)):(t.first=1/0,t.count=0,this._instances.forEach(s=>{t.first=Math.min(t.first,s.targetRanges.geometry.from),t.count=Math.max(t.count,s.targetRanges.geometry.to)}),void(t.count-=t.first))}for(const e of s)e.isVisible&&l(e.hasOccludees?this.drawCommandsOccludees:this.drawCommandsDefault,e)}get sortedInstances(){return Array.from(this._instances.values()).sort(({targetRanges:{geometry:t}},{targetRanges:{geometry:s}})=>t.from===s.from?t.to-s.to:t.from-s.from)}updateHighlights(t){this.highlightNames.clear();const s=this.sortedInstances;for(const e of s)e.updateHighlightOptions(t),e.isVisible&&e.highlightName&&this.highlightNames.add(e.highlightName);this._updateHighlightDrawCommands(t,s),this._updateInstancedCommandVAOs()}mergeHoles(){const t=this._holes;for(const s of this.targetBuffers)a(t[s])}_updateHighlightDrawCommands(s,e){const{drawCommandsHighlights:n,drawCommandsShadowHighlightRest:a}=this;n.clear(),a.clear();for(const i of e){if(i.updateHighlightOptions(s),!i.isVisible)continue;const{highlightName:e}=i;if(e){this.highlightNames.add(e);l(t(n,e,c),i)}e&&e===h||l(a,i)}}_updateInstancedCommandVAOs(){const t=this.vao;if(!this.isInstanced||null==t)return;this._instancedCommandVAOs??=new r;const s=this._instancedCommandVAOs,e=s.commandVAOs.copy();s.clear();const n=s.commandVAOs,a=(s,a)=>{for(const i of s){const{first:s,count:r}=i,h=n.get(s,r)??e.pop(s,r)??new o(t.shallowCloneWithBaseInstances(new Map([["geometry",i.first]])),i.count);n.set(s,r,h),a.push(h)}};a(this.drawCommandsDefault,s.drawCommandsDefault);for(const[i,o]of this.drawCommandsHighlights){const t=new Array;a(o,t),s.drawCommandsHighlights.set(i,t)}a(this.drawCommandsOccludees,s.drawCommandsOccludees),a(this.drawCommandsShadowHighlightRest,s.drawCommandsShadowHighlightRest);for(const i of e.values())i.dispose();e.clear()}get needsMultipleCommands(){return this.hasOccludees||this.hasHighlights||this.hasHiddenInstances}}function d(t){return null!=t.vao}function c(){return new s({allocator:t=>t||new i,deallocator:t=>t})}function l(t,{targetRanges:{geometry:s}}){const e=t.back();if(null==e){const e=t.pushNew();return e.first=s.from,void(e.count=s.numElements)}if(u(e,s)){const t=s.from-e.first+s.numElements;e.count=t}else{const e=t.pushNew();e.first=s.from,e.count=s.numElements}}function u(t,s){return t.first+t.count>=s.from}export{m as PerBufferData,d as hasVao};