/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import{clamp as r}from"../../../../../core/mathUtils.js";import{disposeMaybe as t}from"../../../../../core/maybe.js";import{watch as s}from"../../../../../core/reactiveUtils.js";import{Milliseconds as i}from"../../../../../core/time.js";import{property as a,subclass as o}from"../../../../../core/accessorSupport/decorators.js";import{set as n}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{InternalRenderCategory as m}from"../../../webgl.js";import p from"../../../webgl/RenderNode.js";import{distanceFadeEnd as c,distanceFadeStart as h}from"../../core/shaderLibrary/shading/ScreenSpaceConstants.js";import{g as l}from"../../../../../chunks/SSAO.glsl.js";import{SSAOBlurTechnique as u}from"./SSAOBlurTechnique.js";import{noiseData as d}from"./SSAONoiseData.js";import{SSAOPassParameters as f,BlurDrawParameters as b}from"./SSAOParameters.js";import{SSAOTechnique as w}from"./SSAOTechnique.js";import{DepthStencilAttachment as S}from"../../../../webgl/enums.js";import _ from"../../../../webgl/Texture.js";import{TextureDescriptor as T}from"../../../../webgl/TextureDescriptor.js";const P=2;let g=class extends p{constructor(e){super(e),this.consumes={required:["normals"]},this.produces=m.SSAO,this.isEnabled=()=>!1,this._enableTime=i(0),this._passParameters=new f,this._drawParameters=new b}initialize(){const e=Uint8Array.from(atob(d),e=>e.charCodeAt(0)),r=new T(32);r.wrapMode=33071,r.pixelFormat=6407,r.wrapMode=10497,r.hasMipmap=!0,this._passParameters.noiseTexture=new _(this.renderingContext,r,e),this.techniques.precompile(w),this.techniques.precompile(u),this.addHandles(s(()=>this.isEnabled(),()=>this._enableTime=i(0)))}destroy(){this._passParameters.noiseTexture=t(this._passParameters.noiseTexture)}render(e){const t=e.find(({name:e})=>"normals"===e),s=t?.getTexture(),a=t?.getTexture(S);if(!s||!a)return;const o=this.techniques.get(w),p=this.techniques.get(u);if(!o.compiled||!p.compiled)return this._enableTime=i(performance.now()),void this.requestRender(1);0===this._enableTime&&(this._enableTime=i(performance.now()));const d=this.renderingContext,f=this.view.qualitySettings.fadeDuration,b=this.bindParameters,_=b.camera,T=_.relativeElevation,g=r((c-T)/(c-h),0,1),j=f>0?Math.min(f,performance.now()-this._enableTime)/f:1,q=j*g;this._passParameters.normalTexture=s,this._passParameters.depthTexture=a,this._passParameters.projScale=1/_.computeScreenPixelSizeAtDist(1),this._passParameters.intensity=4*x/l(_)**6*q;const v=_.fullViewport[2],A=_.fullViewport[3],y=this.fboCache.acquire(v,A,"ssao input",2);d.bindFramebuffer(y.fbo),d.setViewport(0,0,v,A),d.bindTechnique(o,b,this._passParameters,this._drawParameters),d.screen.draw();const C=Math.round(v/P),O=Math.round(A/P),V=this.fboCache.acquire(C,O,"ssao blur",0);d.bindFramebuffer(V.fbo),this._drawParameters.colorTexture=y.getTexture(),n(this._drawParameters.blurSize,0,P/A),d.bindTechnique(p,b,this._passParameters,this._drawParameters),d.setViewport(0,0,C,O),d.screen.draw(),y.release();const M=this.fboCache.acquire(C,O,m.SSAO,0);return d.bindFramebuffer(M.fbo),d.setViewport(0,0,v,A),d.setClearColor(1,1,1,0),d.clear(16384),this._drawParameters.colorTexture=V.getTexture(),n(this._drawParameters.blurSize,P/v,0),d.bindTechnique(p,b,this._passParameters,this._drawParameters),d.setViewport(0,0,C,O),d.screen.draw(),d.setViewport4fv(_.fullViewport),V.release(),j<1&&this.requestRender(2),M}};e([a()],g.prototype,"consumes",void 0),e([a()],g.prototype,"produces",void 0),e([a({constructOnly:!0})],g.prototype,"isEnabled",void 0),g=e([o("esri.views.3d.webgl-engine.effects.ssao.SSAO")],g);const x=.5;export{g as SSAO,P as blurSizePixels};
