/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import{lerp as t,smoothstep as i}from"../../../../../core/mathUtils.js";import{disposeMaybe as r}from"../../../../../core/maybe.js";import{watch as s,syncAndInitial as o}from"../../../../../core/reactiveUtils.js";import{subclass as n}from"../../../../../core/accessorSupport/decorators.js";import{set as a}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{b as m}from"../../../../../chunks/vec32.js";import{earth as h}from"../../../../../geometry/support/Ellipsoid.js";import{InternalRenderCategory as c}from"../../../webgl.js";import{TransparentEnvironment as d}from"../TransparentEnvironment.js";import{H as u}from"../../../../../chunks/HazeCompositing.glsl.js";import{HazeCompositingTechnique as p}from"./HazeCompositingTechnique.js";import{HazePassParameters as f,HazeTechnique as l}from"./HazeTechnique.js";import{HazeTechniqueConfiguration as _}from"./HazeTechniqueConfiguration.js";import{createQuadVAO as b}from"../../lib/glUtil3D.js";import{applyTextureResizeModulo as g}from"../../lib/textureUtils.js";import{DepthStencilAttachment as w,PrimitiveType as P}from"../../../../webgl/enums.js";let v=class extends d{constructor(e){super(e),this._compositingPassParameters=new u,this._passParameters=new f,this._hazeConfiguration=new _,this._oldAmount=1,this._newAmount=1,this._amount=this._newAmount;const t=e.view.stage.renderView.techniques;t.precompile(l,new _);const i=new _;i.reduced=!0,t.precompile(l,i),t.precompile(p)}initialize(){this.addHandles([s(()=>this.view.environment.atmosphereEnabled,e=>e?this._enable():this._disable(),o),s(()=>this.view.stage?.renderer.renderContext.bind.clouds.fadeFactor??1,e=>this._fade(e),o),s(()=>this.view.environment.weather.type,e=>this._newAmount="rainy"===e?0:1,o),s(()=>this.view.stage.renderer?.highResolutionAtmosphere,e=>this._hazeConfiguration.reduced=!e,o)])}_fade(e){e>=1?(this._amount=this._newAmount,this._oldAmount=this._newAmount):this._amount=e<=0?this._oldAmount:t(this._oldAmount,this._newAmount,e)}destroy(){this._vao=r(this._vao)}render(e){const i=e.find(({name:e})=>e===c.TRANSPARENT_ENVIRONMENT);if(!this.bindParameters.mainDepth)return i;const r=this.techniques.get(l,this._hazeConfiguration);if(!r.compiled)return i;const s=this.renderingContext,o=i.getAttachment(w);if(this._update(),!this._hazeConfiguration.reduced)return i.detachDepth(),s.bindFramebuffer(i.fbo),s.bindTechnique(r,this.bindParameters,this._passParameters),this._renderCommon(s),i.attachDepth(o),i;const n=this.techniques.get(p);if(!n.compiled)return i;const a=s.getViewport(),d=this.camera,u=m(d.eye)-h.radius;let f;const _=h.atmosphereHeight;if(u<_){const e=Math.min(1,Math.max(0,u/_));f=t(.4,.5,e)}else{const e=Math.min(1,Math.max(0,(u-_)/(15*_)));f=t(.5,1,e)}const b=this.renderingContext.parameters.maxTextureSize,P=g(Math.round(f*d.fullViewport[2]),b),v=g(Math.round(f*d.fullViewport[3]),b);s.setViewport(0,0,P,v);const A=this.fboCache.acquire(P,v,"haze",5);return s.bindFramebuffer(A.fbo),s.clearFramebuffer([0,0,0,1],!0,!0),s.bindTechnique(r,this.bindParameters,this._passParameters),this._renderCommon(s),s.setViewport(a.x,a.y,a.width,a.height),this._compositingPassParameters.color=A.getTexture(),i.detachDepth(),s.bindFramebuffer(i.fbo),s.bindTechnique(n,this.bindParameters,this._compositingPassParameters),s.screen.draw(),i.attachDepth(o),A.release(),i}_renderCommon(e){this._vao??=b(e,1),e.bindVAO(this._vao),e.drawArrays(P.TRIANGLE_STRIP,0,4)}_update(){const e=this.view.basemapTerrain?.getLowerBoundRadius()??0;a(this._passParameters.radii,e,e+h.atmosphereHeight);const r=m(this.bindParameters.camera.eye);this._passParameters.hazeStrength=t(t(.6,1,i(9500,10500,r-h.radius)),1,this._amount)}};v=e([n("esri.views.3d.webgl-engine.effects.haze.Haze")],v);export{v as Haze};
