@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.62 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{_ as e}from"../../../../../chunks/tslib.es6.js";import{createTask as s}from"../../../../../core/asyncUtils.js";import{clamp as i}from"../../../../../core/mathUtils.js";import{disposeMaybe as r}from"../../../../../core/maybe.js";import{watch as t,syncAndInitial as a}from"../../../../../core/reactiveUtils.js";import{createScreenPointArray as o,createRenderScreenPointArray as m,screenPointObjectToArray as n}from"../../../../../core/screenUtils.js";import{isSVG as l}from"../../../../../core/urlUtils.js";import{property as h}from"../../../../../core/accessorSupport/decorators/property.js";import"../../../../../core/has.js";import"../../../../../core/Logger.js";import"../../../../../core/RandomLCG.js";import{subclass as p}from"../../../../../core/accessorSupport/decorators/subclass.js";import{requestImage as u}from"../../../../../support/requestImageUtils.js";import{InternalRenderCategory as c}from"../../../webgl.js";import _ from"../../../webgl/RenderNode.js";import{MagnifierTechnique as g}from"./MagnifierTechnique.js";import{RenderRequestType as d}from"../../lib/basicInterfaces.js";import{Default3D as f}from"../../lib/DefaultVertexAttributeLocations.js";import{createQuadVAO as v,Layout as P}from"../../lib/glUtil3D.js";import{M as T}from"../../../../../chunks/Magnifier.glsl.js";import{loadMagnifierResources as k}from"../../../../magnifier/resources.js";import{PrimitiveType as y,PixelFormat as b,TextureWrapMode as j}from"../../../../webgl/enums.js";import{Texture as R}from"../../../../webgl/Texture.js";import{TextureDescriptor as S}from"../../../../webgl/TextureDescriptor.js";let w=class extends _{constructor(){super(...arguments),this.produces=c.MAGNIFIER,this.consumes={required:[c.MAGNIFIER]},this._imageSources=null,this._imageLoadTask=null,this._passParameters=new T,this._vao=null,this._magnifier=null,this._tmpScreenPoint=o(),this._tmpRenderPoint=m()}initialize(){this.addHandles([t((()=>this.view.magnifier),(e=>this._update(e)),a)])}_update(e){if(e===this._magnifier)return;this.removeAllHandles(),this._magnifier=e;const s=()=>{const e=this._validMagnifier;e?(this._loadResources(e),this.produces=c.MAGNIFIER):this.produces="disabled",this.requestRender()};this._magnifier&&this.addHandles(t((()=>this._magnifier?.version),s)),s()}get _validMagnifier(){return this._magnifier?.visible&&this._magnifier?.position&&this._magnifier?.size>0?this._magnifier:null}get _factor(){return this._magnifier?.factor||1}destroy(){this._magnifier=null,null!=this._imageLoadTask&&(this._imageLoadTask.task.abort(),this._imageLoadTask=null),this._disposeTextures(),this._vao=r(this._vao)}_disposeTextures(){this._passParameters.mask=r(this._passParameters.mask),this._passParameters.overlay=r(this._passParameters.overlay),this._passParameters.input=r(this._passParameters.input)}precompile(){this._imageSources&&this.techniques.precompile(g)}render(e){const s=this._validMagnifier,r=e.find((({name:e})=>e===c.MAGNIFIER));if(null==s)return r;if(null==this._imageSources)return this.requestRender(d.UPDATE),r;const t=this.renderingContext,a=this.camera.pixelRatio,o=Math.ceil(a*s.size);this._vao??=v(t,P.Pos2,f,0,1);const m=this.techniques.get(g);if(this._ensureTextureResources(t,o),!m.compiled||!this._passParameters.input)return this.requestRender(d.UPDATE),r;const l=Math.ceil(1/this._factor*o),h=this._passParameters.input;h.resize(l,l),n(s.position,this._tmpScreenPoint);const p=this.camera.screenToRender(this._tmpScreenPoint,this._tmpRenderPoint),u=this.camera.fullWidth,_=this.camera.fullHeight,T=.5*l,k=.5*l;p[0]=i(p[0],T,u-T-1),p[1]=i(p[1],k,_-k-1);const b=Math.floor(p[0]-T),j=Math.floor(p[1]-k);t.bindFramebuffer(r.fbo);return m.program.bindTexture("textureInput",h),t.gl.copyTexImage2D(h.descriptor.target,0,h.descriptor.pixelFormat,b,j,l,l,0),this._passParameters.magnifier=s,t.bindTechnique(m,this.bindParameters,this._passParameters),t.bindVAO(this._vao),t.drawArrays(y.TRIANGLE_STRIP,0,4),r}_loadResources(e){const{maskUrl:i,overlayUrl:r}=e;this._imageLoadTask?.maskUrl===i&&this._imageLoadTask?.overlayUrl===r||(this._imageLoadTask?.task.abort(),this._imageLoadTask=this._imageSources=null),this._imageSources||this._imageLoadTask||(this._imageLoadTask={maskUrl:i,overlayUrl:r,task:s((async e=>{const s=null==i||null==r?k(e):null,t=null!=i?u(i,{signal:e}):s.then((e=>e.mask)),a=null!=r?u(r,{signal:e}):s.then((e=>e.overlay));this._imageSources={mask:await t,overlay:await a}}))})}_ensureTextureResources(e,s){if(null==this._imageSources||this._passParameters.size===s&&this._passParameters.input&&this._passParameters.mask&&this._passParameters.overlay)return;this._disposeTextures(),this._imageSources.overlay.width=this._imageSources.mask.width=s,this._imageSources.overlay.height=this._imageSources.mask.height=s;const i=new S;i.internalFormat=b.RGBA,i.wrapMode=j.CLAMP_TO_EDGE,i.flipped=!0,i.preMultiplyAlpha=!l(this._imageSources.overlay.src)||!e.driverTest.svgPremultipliesAlpha.result,this._passParameters.overlay=new R(e,i,this._imageSources.overlay),i.pixelFormat=i.internalFormat=b.ALPHA,i.preMultiplyAlpha=!1,this._passParameters.mask=new R(e,i,this._imageSources.mask),i.pixelFormat=i.internalFormat=b.RGBA,i.flipped=!1,this._passParameters.input=new R(e,i)}};e([h()],w.prototype,"produces",void 0),e([h()],w.prototype,"consumes",void 0),e([h()],w.prototype,"_imageSources",void 0),e([h()],w.prototype,"_imageLoadTask",void 0),w=e([p("esri.views.3d.webgl-engine.effects.magnifier.Magnifier")],w);export{w as Magnifier};