UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) • 27.3 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as e}from"../chunks/tslib.es6.js";import t from"../request.js";import has from"../core/has.js";import s from"../core/Logger.js";import{isAbortError as r}from"../core/promiseUtils.js";import{watch as i,initial as a}from"../core/reactiveUtils.js";import{convertTime as n}from"../core/timeUtils.js";import{property as o}from"../core/accessorSupport/decorators/property.js";import"../core/RandomLCG.js";import{subclass as l}from"../core/accessorSupport/decorators/subclass.js";import{j as h}from"../chunks/vec32.js";import{create as d}from"../core/libs/gl-matrix-2/factories/vec3f64.js";import{renderSRFromViewSR as u}from"../geometry/support/coordinateSystem.js";import{PointIndex as _,PlaneIndex as c}from"../geometry/support/frustum.js";import{WasmCullMode as m,VoxelRequestType as g,UpdateFlags as f,ContainerType as p}from"../libs/vxl/enums.js";import{loadVoxelWASM as y}from"../libs/vxl/VxlModule.js";import{ViewingMode as x}from"../views/ViewingMode.js";import{VoxelGraphic as v}from"../views/3d/layers/VoxelGraphic.js";import{VoxelTarget as b}from"../views/3d/layers/i3s/Intersector.js";import{Frustum as w}from"../views/3d/state/Frustum.js";import{RenderCoordsHelper as T}from"../views/3d/support/RenderCoordsHelper.js";import{SyncRenderPlugin as R}from"../views/3d/webgl-engine/effects/RenderPlugin.js";import{StoreResults as V}from"../views/3d/webgl-engine/lib/IntersectorInterfaces.js";import{IntersectorResult as L}from"../views/3d/webgl-engine/lib/IntersectorResult.js";import{IntersectorType as C}from"../views/3d/webgl-engine/lib/IntersectorType.js";import{RenderFeature as E}from"../views/3d/webgl-engine/lib/RenderFeature.js";import{RenderSlot as F}from"../views/3d/webgl-engine/lib/RenderSlot.js";import{CompareFunction as I,Face as S,StencilOperation as P}from"../views/webgl/enums.js";var U;!function(e){e[e.Lifetime=1]="Lifetime",e[e.RequestResponse=2]="RequestResponse",e[e.Rendering=3]="Rendering",e[e.Error=4]="Error"}(U||(U={}));let k=class extends R{constructor(e){super(e),this._halfIntTexturesAvailable=!1,this._textureFloatLinearAvailable=!1,this._havePreparedWithAllLayers=!1,this._renderPluginContext=null,this._vxlPromise=null,this._vxl=null,this._pluginIsActive=!1,this._moreToLoad=!1,this._viewportWidth=-1,this._viewportHeight=-1,this._newLayers=[],this._layers=new Map,this._rctx=null,this._renderTargetToRestore=null,this._lastFrameWasStationary=!1,this._wasmMemBlockSizes=[512,1024,2048,4096,8192,16384,32768,65536],this._wasmMemBlocks=new Map,this._dbgFlags=new Set,this._captureFrustum=!1,this._frustum=null,this._frustumRenderableId=-1,this._renderCoordsHelper=null,this.produces=new Map([[F.VOXEL,()=>!!this._vxl&&"local"===this.view.viewingMode]]),this.type=C.VOXEL,this.slicePlaneEnabled=!0,this.isGround=!1,this.layerViewUid=[]}_dbg(e,t){this._dbgFlags.has(e)&&(e===U.Error?s.getLogger(this).error(t):s.getLogger(this).warn(t))}_removeRenderPlugin(){this._pluginIsActive&&this.view.stage&&(this._dbg(U.Lifetime,"--removeRenderPlugin--"),this.view.stage.removeRenderPlugin(this)),this._pluginIsActive=!1}initialize(){this._dbg(U.Lifetime,"--initialize--");for(const e of this._wasmMemBlockSizes)this._wasmMemBlocks.set(e,0);this.addHandles([i((()=>this.view.ready),(e=>{e&&"local"===this.view.viewingMode?(this._dbg(U.Lifetime,"view ready status changed to ready on a local view, calling addRenderPlugin"),this.view.stage.addRenderPlugin(this),this._pluginIsActive=!0):(this._dbg(U.Lifetime,"view ready status changed, not ready or not a local view!"),this._removeRenderPlugin())}),a),i((()=>this.view?.qualityProfile),(e=>{this._dbg(U.Rendering,"qualityProfile changed to "+e),this._vxl&&this._vxl.set_quality(this._toWasmQuality(e))}),a),i((()=>this.view?.timeExtent),(()=>{if(this._vxl){const e=this._getTimeArgs(this.view?.timeExtent);this._dbg(U.Rendering,"sceneView timeExtent changed to useTime="+e.hasTime+" st="+e.startTime+" et="+e.endTime),this._vxl.set_scene_time_extent(e.startTime,e.endTime,e.hasTime),this._renderPluginContext.requestRender()}}),a),i((()=>this.view?.stationary),(e=>{this._vxl&&e&&!this._lastFrameWasStationary&&this._renderPluginContext.requestRender()}))])}initializeRenderContext(e){this._dbg(U.Lifetime,"--initializeRenderContext--");const t=e.renderContext.rctx;this._renderPluginContext=e,this._rctx=e.renderContext.rctx,this._halfIntTexturesAvailable=!!this._rctx.capabilities.textureNorm16,this._textureFloatLinearAvailable=this._rctx.capabilities.textureFloatLinear,this._initializeWasm(t.gl)}uninitializeRenderContext(){this._renderPluginContext=null,this._rctx=null,this._dbg(U.Lifetime,"--uninitializeRenderContext--")}_restoreFramebuffer(){if(!this._renderTargetToRestore)return;const e=this._renderTargetToRestore.fbo;if(!!!this._rctx)return void this._dbg(U.Error,"no context in restoreFramebuffer!");this._rctx.bindFramebuffer(e,!0);const t=this._renderTargetToRestore.viewport;this._rctx.setViewport(t.x,t.y,t.width,t.height)}_bindPreviousDepthToSlot(e,t){const s=!!this._rctx,r=!!this._renderTargetToRestore;if(!s||!r)return 0;const i=this._renderTargetToRestore.fbo.depthStencilTexture;return i?(0===t?this._rctx.bindTexture(null,e,!0):this._rctx.bindTexture(i,e,!0),1):(this._dbg(U.Error,"no depth/stencil texture exists!"),0)}_modifyResourceCount(e,t,s){if(!this._rctx)return void this._dbg(U.Error,"modifyAllocation callback has no rendering context!");const r=e;1===s?this._rctx.instanceCounter.increment(r,t):this._rctx.instanceCounter.decrement(r,t)}_setBlendState(e,t,s,r){this._rctx?(this._rctx.setBlendingEnabled(1===e),this._rctx.setBlendFunction(t,s),this._rctx.setBlendEquation(r)):this._dbg(U.Error,"setBlendState callback has no rendering context!")}_setFrontFace(e){this._rctx?this._rctx.setFrontFace(e):this._dbg(U.Error,"setFrontFace callback has no rendering context!")}_setDepthStencilStateFunction(e,t,s){this._rctx?(this._rctx.setDepthFunction(s),this._rctx.setDepthTestEnabled(1===e),this._rctx.setDepthWriteEnabled(1===t),this._rctx.setStencilTestEnabled(!1),this._rctx.setStencilFunction(I.ALWAYS,0,255),this._rctx.setStencilOpSeparate(S.FRONT,P.KEEP,P.INCR,P.KEEP),this._rctx.setStencilOpSeparate(S.BACK,P.KEEP,P.DECR,P.KEEP)):this._dbg(U.Error,"setDepthStencilStateFunction callback has no rendering context!")}_setRasterizerState(e){if(this._rctx)switch(e){case m.None:this._rctx.setFaceCullingEnabled(!1);break;case m.Back:this._rctx.setCullFace(S.BACK),this._rctx.setFaceCullingEnabled(!0);break;case m.Front:this._rctx.setCullFace(S.FRONT),this._rctx.setFaceCullingEnabled(!0)}else this._dbg(U.Error,"setRasterizerState callback has no rendering context!")}_setViewport(e,t,s,r){this._rctx?this._rctx.setViewport(e,t,s,r):this._dbg(U.Error,"setViewport callback has no rendering context!")}_updateMemoryUsage(){this._layers.forEach(((e,t)=>{if(e.needMemoryUsageUpdate){const s=this._vxl.estimate_memory_usage(t);s>=0&&(e.needMemoryUsageUpdate=!1,e.layerView.setUsedMemory(s))}}))}_syncRequestsResponses(){this._layers.forEach(((e,s)=>{const i=[];e.responses.forEach(((t,r)=>{i.push(r),this._dbg(U.RequestResponse,"responding for requestID:"+r+" size:"+t.size),this._vxl.respond(s,r,t),t.requestType!==g.TreeIndex&&t.requestType!==g.Section||(e.needMemoryUsageUpdate=!0)}));const a=e.responses;for(const t of i)a.delete(t);const n=this._vxl.get_new_requests(s),o=e.abortController.signal;for(const l in n){e.outstandingRequestCount+=1,1===e.outstandingRequestCount&&e.layerView.updatingFlagChanged();const s=n[l],i={responseType:"array-buffer",signal:o,query:{...e.layerView.layer.customParameters,token:e.layerView.layer.apiKey}};this._dbg(U.RequestResponse,"making requestID:"+l+" url:"+s.url),t(s.url,i).then((t=>{e.outstandingRequestCount-=1,0===e.outstandingRequestCount&&e.layerView.updatingFlagChanged(),this._dbg(U.RequestResponse,"have response for requestID:"+l);let r=0;if(t.data.byteLength>0){r=this._vxl._malloc(t.data.byteLength);const e=new Uint8Array(this._vxl.HEAPU8.buffer,r,t.data.byteLength),s=new Uint8Array(t.data);for(let r=0;r<t.data.byteLength;++r)e[r]=s[r]}a.set(+l,{responseType:s.responseType,ptr:r,size:t.data.byteLength,success:!0,requestType:s.requestType})})).catch((t=>{e.outstandingRequestCount-=1,0===e.outstandingRequestCount&&e.layerView.updatingFlagChanged(),r(t)||(this._dbg(U.Error,`requestID:${l} failed, error=${t.toString()}`),a.set(+l,{responseType:s.responseType,ptr:0,size:0,success:!1,requestType:s.requestType}))}))}}))}updateWasmCamera(e){this._vxl.set_projection_matrix.apply(this._vxl,e.projectionMatrix),this._vxl.set_view_matrix.apply(this._vxl,e.viewMatrix),this._vxl.set_near_far(e.near,e.far)}isUpdating(e){if(!this._vxl&&this._vxlPromise)return!0;const t=this._layers.get(e);return!!t&&t.outstandingRequestCount>0}getLayerTimes(e){const t=[];return this._layers.forEach(((s,r)=>{if(s.layerView.wasmLayerId===e.wasmLayerId){const s=this._vxl.get_layer_epoch_times(r,e.layer.currentVariableId);for(let e=0;e<s.length;++e)t.push(s[e])}})),t}getCurrentLayerTimeIndex(e){let t=0;return this._layers.forEach(((s,r)=>{s.layerView.wasmLayerId===e.wasmLayerId&&(t=this._vxl.get_layer_current_time_id(r))})),t}setEnabled(e,t){this._layers.forEach(((s,r)=>{s.layerView.wasmLayerId===e.wasmLayerId&&(this._vxl.set_enabled(r,t),s.needMemoryUsageUpdate=!0,this._renderPluginContext.requestRender())}))}setIsInScaleRange(e,t){const s=this._layers.get(e.wasmLayerId);s&&t!==s.isInScaleRange&&(s.isInScaleRange=t,this._vxl.set_is_in_scale_range(e.wasmLayerId,t),s.needMemoryUsageUpdate=!t,this._renderPluginContext.requestRender())}setStaticSections(e,t){const s={mask:f.StaticSections,staticSections:t};return this._doMaskedUIUpdate(e,s,!0)}setCurrentVariable(e,t){const s={mask:f.CurrentVariable,currentVariable:t};return this._doMaskedUIUpdate(e,s,!0)}setRenderMode(e,t){const s={mask:f.RenderMode,renderMode:t};return this._doMaskedUIUpdate(e,s,!0)}setVerticalExaggerationAndOffset(e,t,s,r){const i={mask:f.ExaggerationAndOffset,volStyleDesc:{volumeId:t,verticalExaggeration:s,verticalOffset:r}};return this._doMaskedUIUpdate(e,i,!0)}setVariableStyles(e,t){const s={mask:f.VariableStyles,variableStyles:t};return this._doMaskedUIUpdate(e,s,!0)}setVolumeStyles(e,t){const s={mask:f.VolumeStyles,volumeStyles:t};return this._doMaskedUIUpdate(e,s,!0)}setEnableDynamicSections(e,t){const s={mask:f.ContainerVisibility,containerIsVisible:t,container:p.DynamicSections};return this._doMaskedUIUpdate(e,s,!0)}setEnableIsosurfaces(e,t){const s={mask:f.ContainerVisibility,containerIsVisible:t,container:p.Isosurfaces};return this._doMaskedUIUpdate(e,s,!0)}setEnableSections(e,t){const s={mask:f.ContainerVisibility,containerIsVisible:t,container:p.StaticSections};return this._doMaskedUIUpdate(e,s,!0)}setAnalysisSlice(e,t,s,r){const i={mask:f.AnalysisSlice,analysisSlice:{point:s,normal:r,enabled:t}};return this._doMaskedUIUpdate(e,i,!0)}updateLayerTimeProperties(e){if(!this._vxl)return;const t=this._layers.get(e.wasmLayerId);if(t){const s=t.layerView.layer;let r=0;s.timeOffset&&(r=n(s.timeOffset.value,s.timeOffset.unit,"seconds"));const i=this._getTimeArgs(s.timeExtent);this._vxl.set_layer_time_properties(e.wasmLayerId,i.startTime,i.endTime,i.hasTime,s.useViewTime,r),this._renderPluginContext.requestRender()}}_doMaskedUIUpdate(e,t,s){if(!this._vxl)return!1;let r=!1;return this._layers.forEach(((s,i)=>{if(s.layerView.wasmLayerId===e.wasmLayerId){const e={str:JSON.stringify(t),byteCount:0,ptr:0,isReusable:!1};this._allocateBlock(e)&&(r=1===this._vxl.handle_masked_ui_update(i,e.ptr,e.byteCount),e.isReusable||this._vxl._free(e.ptr))}})),r&&s&&this._renderPluginContext.requestRender(),r}_addTriangleToWasmBuffer(e,t,s,r,i){return e[3*t]=s[0],e[3*t+1]=s[1],e[3*t+2]=s[2],e[3*(t+=1)]=r[0],e[3*t+1]=r[1],e[3*t+2]=r[2],e[3*(t+=1)]=i[0],e[3*t+1]=i[1],e[3*t+2]=i[2],t+=1}_addNormalToWasmBuffer(e,t,s){return e[3*t]=s[0],e[3*t+1]=s[1],e[3*t+2]=s[2],t+=1}_doCaptureFrustum(){if(!this._vxl)return;const e=36,t=e/3,s=this._vxl._malloc(3*e*Float32Array.BYTES_PER_ELEMENT),r=new Float32Array(this._vxl.HEAPF32.buffer,s,3*e),i=this._vxl._malloc(3*t*Float32Array.BYTES_PER_ELEMENT),a=new Float32Array(this._vxl.HEAPF32.buffer,i,e),n=this._frustum.points[_.NEAR_BOTTOM_LEFT],o=this._frustum.points[_.NEAR_BOTTOM_RIGHT],l=this._frustum.points[_.NEAR_TOP_RIGHT],h=this._frustum.points[_.NEAR_TOP_LEFT],d=this._frustum.points[_.FAR_BOTTOM_LEFT],u=this._frustum.points[_.FAR_BOTTOM_RIGHT],m=this._frustum.points[_.FAR_TOP_RIGHT],g=this._frustum.points[_.FAR_TOP_LEFT];let f=0,p=0;const y=this._frustum.planes[c.NEAR];f=this._addTriangleToWasmBuffer(r,f,l,o,n),p=this._addNormalToWasmBuffer(a,p,y),f=this._addTriangleToWasmBuffer(r,f,n,h,l),p=this._addNormalToWasmBuffer(a,p,y);const x=this._frustum.planes[c.FAR];f=this._addTriangleToWasmBuffer(r,f,d,u,m),p=this._addNormalToWasmBuffer(a,p,x),f=this._addTriangleToWasmBuffer(r,f,m,g,d),p=this._addNormalToWasmBuffer(a,p,x);const v=this._frustum.planes[c.TOP];f=this._addTriangleToWasmBuffer(r,f,m,l,h),p=this._addNormalToWasmBuffer(a,p,v),f=this._addTriangleToWasmBuffer(r,f,h,g,m),p=this._addNormalToWasmBuffer(a,p,v);const b=this._frustum.planes[c.BOTTOM];f=this._addTriangleToWasmBuffer(r,f,n,o,u),p=this._addNormalToWasmBuffer(a,p,b),f=this._addTriangleToWasmBuffer(r,f,u,d,n),p=this._addNormalToWasmBuffer(a,p,b);const w=this._frustum.planes[c.LEFT];f=this._addTriangleToWasmBuffer(r,f,h,n,d),p=this._addNormalToWasmBuffer(a,p,w),f=this._addTriangleToWasmBuffer(r,f,d,g,h),p=this._addNormalToWasmBuffer(a,p,w);const T=this._frustum.planes[c.RIGHT];f=this._addTriangleToWasmBuffer(r,f,l,m,u),p=this._addNormalToWasmBuffer(a,p,T),f=this._addTriangleToWasmBuffer(r,f,u,o,l),p=this._addNormalToWasmBuffer(a,p,T),-1!==this._frustumRenderableId&&this._vxl.remove_generic_mesh(this._frustumRenderableId),this._frustumRenderableId=this._vxl.add_generic_mesh(s,3*e,i,e,255,0,0,64),this._vxl._free(s),this._vxl._free(i),this._captureFrustum=!1,this._renderPluginContext.requestRender()}captureFrustum(){null===this._renderCoordsHelper&&(this._renderCoordsHelper=T.create(x.Local,u(!1,this.view.spatialReference))),null===this._frustum&&(this._frustum=new w(this._renderCoordsHelper)),this._captureFrustum=!0,null!==this._renderPluginContext&&this._renderPluginContext.requestRender()}toggleFullVolumeExtentDraw(e){this._vxl&&this._layers.forEach(((t,s)=>{t.layerView.wasmLayerId===e.wasmLayerId&&(this._vxl.toggle_full_volume_extent_draw(s),this._renderPluginContext.requestRender())}))}dropQueryRenderTarget(){this._vxl&&this._vxl.drop_query_rt()}addVoxelLayer(e){if(!this._vxl){const t={layerView:e,resolveCallback:null,rejectCallback:null},s=new Promise(((e,s)=>{t.resolveCallback=e,t.rejectCallback=s}));return this._newLayers.push(t),s}const t=this._addVoxelLayer(e);return t<0?Promise.reject(-1):Promise.resolve(t)}removeVoxelLayer(e){if(!this._vxl){const t=this._newLayers.findIndex((t=>e.uid===t.layerView.uid));t>=0&&(this._newLayers[t].resolveCallback(-1),this._newLayers.splice(t,1));const s=this._newLayers.length;return 0===s&&(this._dbg(U.Lifetime," no voxel layers left after removing a layer, removing RenderPlugin and destroying"),this.destroy()),s}let t=-1;this._layers.forEach(((s,r)=>{if(s.layerView.wasmLayerId===e.wasmLayerId){t=r,s.abortController.abort(),this._vxl.remove_layer(t);const i=this.layerViewUid.indexOf(e.uid);-1!==i&&this.layerViewUid.splice(i,1)}})),t>=0&&this._layers.delete(t);const s=this._layers.size;return 0===s&&(this._dbg(U.Lifetime," no voxel layers left after removing a layer, removing RenderPlugin and destroying"),this.destroy()),s}_getBlockSize(e){for(const t of this._wasmMemBlockSizes)if(e<t)return t;return-1}_allocateBlock(e){e.byteCount=this._vxl.lengthBytesUTF8(e.str)+1;const t=this._getBlockSize(e.byteCount);return t<0?(e.isReusable=!1,e.ptr=this._vxl._malloc(e.byteCount)):(e.isReusable=!0,e.ptr=this._wasmMemBlocks.get(t),0===e.ptr&&(e.ptr=this._vxl._malloc(t),this._wasmMemBlocks.set(t,e.ptr))),0!==e.ptr&&(this._vxl.stringToUTF8(e.str,e.ptr,e.byteCount),!0)}_getTimeArgs(e){let t=-Number.MAX_VALUE,s=Number.MAX_VALUE,r=!1;return null!=e&&(e.isAllTime?r=!0:(null!=e.start&&(r=!0,t=e.start.getTime()/1e3),null!=e.end&&(r=!0,s=e.end.getTime()/1e3))),{startTime:t,endTime:s,hasTime:r}}_addVoxelLayer(e){const t=e.layer;let r=-1;const i=t.getConfiguration();if(i.length<1)return-1;const a={str:i,byteCount:0,ptr:0,isReusable:!1};if(!this._allocateBlock(a))return-1;const o=this._getTimeArgs(t.timeExtent),l=this.view.spatialReference.isWGS84&&t.spatialReference.isWGS84?111319.49079327357:1;let h=0;if(t.timeOffset&&(h=n(t.timeOffset.value,t.timeOffset.unit,"seconds")),r=this._vxl.add_layer(t.serviceRoot,a.ptr,a.byteCount,l,l,o.startTime,o.endTime,o.hasTime,t.useViewTime,h,this._toWasmQuality(this.view.qualityProfile)),a.isReusable||this._vxl._free(a.ptr),r>=0){t.test?.constantUpscaling&&(this._setUpscalingLimits(0,.25,.25),this._setUpscalingLimits(1,.5,.5),this._setUpscalingLimits(2,.75,.75));const i=new AbortController;if(this._layers.set(r,{layerView:e,responses:new Map,outstandingRequestCount:0,abortController:i,needMemoryUsageUpdate:!1,isInScaleRange:!0}),this.layerViewUid.push(e.uid),!this._halfIntTexturesAvailable||has("mac")){const t=[];let r="";for(const s of e.layer.variables)"Int16"!==s.renderingFormat.type&&"UInt16"!==s.renderingFormat.type||(t.push(s.name),s.id===e.layer.currentVariableId&&(r=s.name));""!==r&&s.getLogger(this).error("#addVoxelLayer_error()",e.layer,`The voxel layer '${e.layer.title}' cannot render the current variable '${r}' in this browser`),t.length>0&&s.getLogger(this).warn("#addVoxelLayer_warning()",e.layer,`The voxel layer '${e.layer.title}' cannot render the variables '${t.toString()}' in this browser`)}if(!this._textureFloatLinearAvailable){const t=[];let r="";for(const s of e.layer.variables)"Float32"===s.renderingFormat.type&&(t.push(s.name),s.id===e.layer.currentVariableId&&(r=s.name));""!==r&&s.getLogger(this).error("#addVoxelLayer_error()",e.layer,`The voxel layer '${e.layer.title}' cannot render the current variable '${r}' in this browser`),t.length>0&&s.getLogger(this).warn("#addVoxelLayer_warning()",e.layer,`The voxel layer '${e.layer.title}' cannot render the variables '${t.toString()}' in this browser`)}return has("esri-mobile")&&s.getLogger(this).warnOnce("Mobile support differs across devices. Voxel layer might not display as expected."),r}return-1}prepareRender(e){if(!this._vxl)return;const t=e.bind.camera.viewForward,s=e.bind.camera.eye;this._vxl.update_camera_pos_and_direction(s[0],s[1],s[2],t[0],t[1],t[2]);const r=this._vxl.cull();this._dbg(U.RequestResponse,"missingResourceCount="+r),this._moreToLoad=r>0,this._havePreparedWithAllLayers=0===this._newLayers.length,this._updateMemoryUsage()}acquireTechniques(){return[]}render(e){if(!this._vxl)return;for(const s of this._newLayers){const e=this._addVoxelLayer(s.layerView);-1===e?s.rejectCallback(-1):s.resolveCallback(e)}if(this._newLayers=[],0===this._layers.size)return void this._dbg(U.Error,"No voxel layers but RenderPlugin instance is being asked to render!");this._lastFrameWasStationary=this.view.stationary,this._syncRequestsResponses(),this._beforeDraw(),this._vxl.begin_color_frame(!this.view.stage.renderer.isFeatureEnabled(E.HighResolutionVoxel),e.bind.lighting.mainLight.direction[0],e.bind.lighting.mainLight.direction[1],e.bind.lighting.mainLight.direction[2]);const t=this._renderTargetToRestore.viewport;t.width===this._viewportWidth&&t.height===this._viewportHeight||(this._viewportWidth=t.width,this._viewportHeight=t.height,this._vxl.set_viewport(t.width,t.height),this._layers.forEach((e=>{e.needMemoryUsageUpdate=!0}))),0===t.x&&0===t.y||this._dbg(U.Error,"Unsupported viewport parameters detected!"),this.updateWasmCamera(e.bind.camera),this._captureFrustum&&(this._frustum.update(e.bind.camera),this._doCaptureFrustum()),this._vxl.draw(),this._afterDraw(),(this._moreToLoad||!this._havePreparedWithAllLayers&&this._layers.size>0)&&this._renderPluginContext.requestRender()}destroy(){this._dbg(U.Lifetime,"--destroy--"),this._vxl&&(this._layers.forEach((e=>{e.abortController.abort()})),this._wasmMemBlocks.forEach((e=>{0!==e&&this._vxl._free(e)})),this._vxl.uninitialize_voxel_wasm(),this._removeRenderPlugin(),this._vxl=null)}_initializeWasm(e){return this._vxl?Promise.resolve():(this._vxlPromise||(this._vxlPromise=y(e).then((e=>{if(this._vxl=e,this._vxlPromise=null,this._newLayers.length<=0)return this._dbg(U.Lifetime," no voxel layers left after WASM downloaded, removing RenderPlugin and destroying"),void this.destroy();const t=this._getTimeArgs(this.view?.timeExtent),s=this._vxl.addFunction(this._restoreFramebuffer.bind(this),"v"),r=this._vxl.addFunction(this._setBlendState.bind(this),"viiii"),i=this._vxl.addFunction(this._setFrontFace.bind(this),"vi"),a=this._vxl.addFunction(this._setRasterizerState.bind(this),"vi"),n=this._vxl.addFunction(this._setDepthStencilStateFunction.bind(this),"viii"),o=this._vxl.addFunction(this._setViewport.bind(this),"viiii"),l=this._vxl.addFunction(this._bindPreviousDepthToSlot.bind(this),"iii"),h=this._vxl.addFunction(this._modifyResourceCount.bind(this),"viii"),d=this._halfIntTexturesAvailable&&!has("mac"),u=this._textureFloatLinearAvailable;this._vxl.initialize_voxel_wasm(s,r,i,a,n,o,l,h,t.startTime,t.endTime,t.hasTime,d,u),this._renderPluginContext&&this._renderPluginContext.requestRender()})).catch((()=>{for(const e of this._newLayers)e.rejectCallback(-2);this._dbg(U.Error," WASM failed to download, removing RenderPlugin and destroying"),this.destroy()}))),this._vxlPromise)}pickDepth(e,t,s){if(!this._vxl||!this._rctx||0===this._layers.size)return null;const r=s.viewport[3]-t;if(e<0||e>s.viewport[2]||t<0||t>s.viewport[3])return this._dbg(U.Error,`[js] pickDepth: outOfRange, screenXY=[${e.toFixed(0)}, ${r.toFixed(0)}]]`),null;this._beforeDraw();const i=s.viewForward,a=s.eye;this._vxl.update_camera_pos_and_direction(a[0],a[1],a[2],i[0],i[1],i[2]),this.updateWasmCamera(s),this._vxl.begin_frame();const n=this._vxl.pick_depth(e,r);if(this._afterDraw(),n.success){return n.distanceToCamera}return null}pickObject(e,t,s,r){if(!this._vxl||!this._rctx||0===this._layers.size)return null;const i=Math.round(e),a=Math.round(t);if(i<0||i>s.viewport[2]||a<0||a>s.viewport[3])return this._dbg(U.Error,`[js] pickObject: outOfRange, screenXY=[${i}, ${a}], vp=[${s.viewport.toString()}]`),null;this._beforeDraw();const n=s.viewForward,o=s.eye;this._vxl.update_camera_pos_and_direction(o[0],o[1],o[2],n[0],n[1],n[2]),this.updateWasmCamera(s),this._vxl.begin_frame();let l=null;if(0===r.length)l=this._vxl.pick_object(i,a,0,0);else{const e={str:JSON.stringify({layerIds:r}),byteCount:0,ptr:0,isReusable:!1};this._allocateBlock(e)&&(l=this._vxl.pick_object(i,a,e.ptr,e.byteCount),e.isReusable||this._vxl._free(e.ptr))}return this._afterDraw(),l}async getOtherFieldPopupValues(e,s){for(const r of e){const e=r.gpuResult;if(!e)continue;const i=this._layers.get(e.layerId);if(!i||!i.layerView.layer.url)continue;const a=i.layerView.layer,n={responseType:"array-buffer",signal:i.abortController.signal,query:{...i.layerView.layer.customParameters,token:i.layerView.layer.apiKey}},o=e.voxelSpacePosition,l=[Math.floor(o[0]/32),Math.floor(o[1]/32),Math.floor(o[2]/32)],h=[-4&l[0],-4&l[1],-4&l[2]];let d=0;if(e.epochTime){const t=this._vxl.get_layer_epoch_times(i.layerView.wasmLayerId,a.currentVariableId);for(let s=0;s<t.length;++s)if(t[s]===e.epochTime/1e3){d=s;break}}const u=[],_={varIds:[],ptrs:[],sizes:[]};for(const r of s){const e=a.variables.findIndex((e=>e.name===r));if(-1===e)continue;const s=a.variables.getItemAt(e)?.id;if(null==s)continue;const i=`${a.url}/variables/${s}/${d}/bundles/0/${h[0]}-${h[1]}-${h[2]}`;u.push(t(i,n).then((e=>e.data))),_.varIds.push(s)}const c=await Promise.allSettled(u),m=c.length;for(let t=0;t<m;++t){const e=c[t];if("fulfilled"===e.status){const t=e.value,s=this._vxl._malloc(t.byteLength);new Uint8Array(this._vxl.HEAPU8.buffer,s,t.byteLength).set(new Uint8Array(t)),_.ptrs.push(s),_.sizes.push(t.byteLength)}else _.varIds.splice(t,1)}const g=this._vxl.get_other_field_popup_values(e,_);for(let t=0;t<_.ptrs.length;++t)this._vxl._free(_.ptrs[t]);if(g.continuousValues)for(const t in g.continuousValues)r.attributes[t]=g.continuousValues[t];if(g.uniqueValues)for(const t in g.uniqueValues)r.attributes[t]=g.uniqueValues[t]}return e}_beforeDraw(){this._renderTargetToRestore={fbo:this._rctx.getBoundFramebufferObject(),viewport:this._rctx.getViewport()},this._rctx.setPolygonOffsetFillEnabled(!1),this._rctx.setScissorTestEnabled(!1),this._rctx.setColorMask(!0,!0,!0,!0)}_afterDraw(){this._renderTargetToRestore.fbo=null,this._rctx.externalTextureUnitUpdate(this._vxl.get_texture_units_bound_in_frame(),this._vxl.get_active_texture_unit()),this._rctx.externalVertexArrayObjectUpdate(),this._rctx.externalVertexBufferUpdate(),this._rctx.externalProgramUpdate()}intersect(e,t,s,r,i){if(!this._vxl||!this._rctx||0===this._layers.size||!e.options.selectionMode||e.options.isFiltered||!e.camera)return;if(null==i||i[0]<0||i[0]>e.camera.viewport[2]||i[1]<0||i[1]>e.camera.viewport[3])return this._dbg(U.Error,i?`[js] VoxelWasmPerScene.intersect: outOfRange, screenXY=[${i[0].toFixed(0)}, ${i[1].toFixed(0)}]`:"[js] VoxelWasmPerScene.intersect: outOfRange, screenXY=null"),null;const a=[];this._layers.forEach((t=>{e.options.filteredLayerViewUids.includes(t.layerView.uid)&&a.push(t.layerView.wasmLayerId)}));const n=this.pickObject(i[0],i[1],e.camera,a);if(null==n||-1===n.layerId)return;const o=this._layers.get(n.layerId);if(o){const t=o.layerView.layer.uid,i=n.distanceToCamera/h(s,r),a=d();a[0]=n.worldX,a[1]=n.worldY,a[2]=n.worldZ;const l={},u=o.layerView.layer,_=u.variables.findIndex((e=>e.id===u.currentVariableId));if(_>=0){const e=u.variables.getItemAt(_);null!=e&&(null!=n.continuousValue?l[e.name]=n.continuousValue:null!=n.uniqueValueLabel&&null!=n.uniqueValue?l[e.name]=`${n.uniqueValueLabel} (${n.uniqueValue})`:null!=n.uniqueValue&&(l[e.name]=`${n.uniqueValue}`),e.description.length>0?l["Voxel.CurrentVariable"]=e.description:e.name?.length>0&&(l["Voxel.CurrentVariable"]=e.name))}if(l["Voxel.Position"]=`[${n.voxelSpacePosition.toString()}]`,null!=n.epochTime&&null!=n.nativeTime&&null!=n.nativeTimeUnits){const e=new Date(n.epochTime);l["Voxel.LocalTime"]=e,l["Voxel.SourceTime"]=`${n.nativeTime.toLocaleString()} ${n.nativeTimeUnits}`}null!=n.depth&&(l["Voxel.Depth"]=n.depth);const c=n.faceNormal;l["Voxel.WorldPosition"]=`[${a[0]}, ${a[1]}, ${a[2]}]`;const m=e=>{const s=new b(a,t,(()=>this._createVoxelGraphic(o.layerView.layer,l,n)));e.set(this.type,s,i,c)},g=e.results,f=e.options.store===V.ALL;if((null==g.min.distance||i<g.min.distance)&&m(g.min),(null==g.max.distance||i>g.max.distance)&&m(g.max),f){const t=new L(e.ray);m(t),e.results.all.push(t)}}}_createVoxelGraphic(e,t,s){return new v({layer:e,sourceLayer:e,attributes:t,gpuResult:s})}_toWasmQuality(e){switch(e){case"low":return 0;case"medium":return 1;case"high":return 2}}_setUpscalingLimits(e,t,s){this._vxl&&this._vxl.set_upscaling_limits(e,t,s)}hasHighlight(){return!1}};e([o({constructOnly:!0})],k.prototype,"view",void 0),k=e([l("esri.layers.VoxelWasmPerSceneView")],k);const A=k;export{A as default};