@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 6.41 kB
JavaScript
import{_ as e}from"../../../../chunks/tslib.es6.js";import s from"../../../../core/Accessor.js";import{createTask as t}from"../../../../core/asyncUtils.js";import r from"../../../../core/Evented.js";import i from"../../../../core/Handles.js";import{clamp as a}from"../../../../core/mathUtils.js";import{isNone as o,isSome as n,applySome as m}from"../../../../core/maybe.js";import{watch as u}from"../../../../core/reactiveUtils.js";import{createScreenPointArray as p,createRenderScreenPointArray as h,screenPointObjectToArray as l}from"../../../../core/screenUtils.js";import{isSVG as _}from"../../../../core/urlUtils.js";import{property as c}from"../../../../core/accessorSupport/decorators/property.js";import"../../../../core/accessorSupport/ensureType.js";import"../../../../core/arrayUtils.js";import{subclass as d}from"../../../../core/accessorSupport/decorators/subclass.js";import{requestImage as g}from"../../../../support/requestImageUtils.js";import{Pos2 as f}from"./DefaultVertexBufferLayouts.js";import{createQuadVAO as T}from"./glUtil3D.js";import{Program as v}from"./Program.js";import{VertexAttribute as y}from"./VertexAttribute.js";import{MagnifierPassParameters as R,build as k}from"../shaders/Magnifier.glsl.js";import{loadMagnifierResources as L}from"../../../magnifier/resources.js";import{PrimitiveType as S,BlendFactor as P,TextureType as x,PixelFormat as E,PixelType as j,TextureWrapMode as A,TextureSamplingMode as M}from"../../../webgl/enums.js";import{makePipelineState as b,simpleBlendingParams as w,defaultColorWriteParams as U}from"../../../webgl/renderState.js";import{Texture as I}from"../../../webgl/Texture.js";let N=class extends s{constructor(){super(...arguments),this._handles=new i,this._magnifier=null,this._imageSources=null,this._imageLoadTask=null,this._resources=null,this._passParameters=new R,this.events=new r,this.attributeLocations=new Map([[y.POSITION,0]]),this._tmpScreenPoint=p(),this._tmpRenderPoint=h()}get updating(){return o(this._imageSources)&&n(this._imageLoadTask)&&!this._imageLoadTask.task.finished}get magnifier(){return this._magnifier}set magnifier(e){if(e===this._magnifier)return;this._handles.removeAll(),this._magnifier=e;const s=()=>{this._updateResourceLoading(),this.events.emit("request-render")};n(this._magnifier)&&this._handles.add(u((()=>m(this._magnifier,(e=>e.version))),s)),s()}get enabled(){return n(this._validMagnifier)}get _validMagnifier(){return n(this._magnifier)&&this._magnifier.visible&&n(this._magnifier.position)&&this._magnifier.size>0?this._magnifier:null}get _factor(){return n(this._magnifier)&&this._magnifier.factor||1}destroy(){this._magnifier=null,this._handles.destroy(),n(this._imageLoadTask)&&(this._imageLoadTask.task.abort(),this._imageLoadTask=null),this._disposeResources()}render(e,s){const t=this._validMagnifier;if(o(t))return;const r=s.camera.pixelRatio,i=Math.ceil(r*t.size);if(this._updateResources(e,i),o(this._resources))return;const n=this._passParameters.textures,m=Math.ceil(1/this._factor*i);n.input.resize(m,m),l(t.position,this._tmpScreenPoint);const u=s.camera.screenToRender(this._tmpScreenPoint,this._tmpRenderPoint),p=s.camera.fullWidth,h=s.camera.fullHeight,_=.5*m,c=.5*m;u[0]=a(u[0],_,p-_-1),u[1]=a(u[1],c,h-c-1);const d=Math.floor(u[0]-_),g=Math.floor(u[1]-c),f=this._resources.program;f.bindTexture("textureInput",n.input),e.gl.copyTexImage2D(n.input.descriptor.target,0,n.input.descriptor.pixelFormat,d,g,m,m,0),this._passParameters.magnifier=t,e.useProgram(f),f.bindPass(this._passParameters,s),e.bindVAO(this._resources.vao),e.setPipelineState(this._resources.pipelineState),e.drawArrays(S.TRIANGLE_STRIP,0,4)}_updateResourceLoading(){const e=this._validMagnifier;if(o(e))return;const s=e.maskUrl,r=e.overlayUrl;!n(this._imageLoadTask)||this._imageLoadTask.maskUrl===s&&this._imageLoadTask.overlayUrl===r||(this._imageLoadTask.task.abort(),this._imageLoadTask=null,this._imageSources=null),n(this._imageSources)||n(this._imageLoadTask)||(this._imageLoadTask={maskUrl:s,overlayUrl:r,task:t((async e=>{const t=o(s)||o(r)?L(e):null,i=n(s)?g(s,{signal:e}):t.then((e=>e.mask)),a=n(r)?g(r,{signal:e}):t.then((e=>e.overlay));this._imageSources={mask:await i,overlay:await a},this._disposeResources(),this.events.emit("request-render")}))},this._imageLoadTask.task.promise.then((()=>this.notifyChange("updating")),(()=>this.notifyChange("updating"))))}_updateResources(e,s){if(!this.enabled)return void this._disposeResources();if(n(this._resources)){if(this._passParameters.textures.size!==s){const t=this._createTextureResources(e,s);if(o(t))return void this._disposeResources();this._disposeTextureResources(this._passParameters.textures),this._passParameters.textures=t}return}const t=this._createTextureResources(e,s);o(t)||(this._resources={program:this._createProgram(e),vao:T(e,f,this.attributeLocations,0,1),pipelineState:b({blending:w(P.ONE,P.ONE_MINUS_SRC_ALPHA),depthTest:null,depthWrite:null,colorWrite:U})},this._passParameters.textures=t)}_disposeResources(){o(this._resources)||(this._disposeTextureResources(this._passParameters.textures),this._resources.program.dispose(),this._resources.vao.dispose(),this._resources=null)}_disposeTextureResources(e){e.mask.dispose(),e.overlay.dispose(),e.input.dispose()}_createTextureResources(e,s){if(o(this._imageSources))return null;this._imageSources.overlay.width=s,this._imageSources.overlay.height=s,this._imageSources.mask.width=s,this._imageSources.mask.height=s;const t=new I(e,{target:x.TEXTURE_2D,pixelFormat:E.RGBA,internalFormat:E.RGBA,dataType:j.UNSIGNED_BYTE,wrapMode:A.CLAMP_TO_EDGE,samplingMode:M.LINEAR,flipped:!0,preMultiplyAlpha:!_(this._imageSources.overlay.src)||!e.driverTest.svgPremultipliesAlpha.result},this._imageSources.overlay),r=new I(e,{target:x.TEXTURE_2D,pixelFormat:E.ALPHA,internalFormat:E.ALPHA,dataType:j.UNSIGNED_BYTE,wrapMode:A.CLAMP_TO_EDGE,samplingMode:M.LINEAR,flipped:!0},this._imageSources.mask);return{input:new I(e,{target:x.TEXTURE_2D,pixelFormat:E.RGBA,internalFormat:E.RGBA,dataType:j.UNSIGNED_BYTE,wrapMode:A.CLAMP_TO_EDGE,samplingMode:M.LINEAR,flipped:!1}),mask:r,overlay:t,size:s}}_createProgram(e){return new v(e,k(),this.attributeLocations)}};e([c()],N.prototype,"_imageSources",void 0),e([c()],N.prototype,"_imageLoadTask",void 0),e([c({readOnly:!0})],N.prototype,"updating",null),N=e([d("esri/views/3d/webgl-engine/lib/MagnifierHelper")],N);export{N as MagnifierHelper};