/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
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 a}from"../../../../../core/accessorSupport/decorators.js";import{set as n}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{length as h}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{earth as m}from"../../../../../geometry/support/Ellipsoid.js";import{TransparentEnvironment as u}from"../TransparentEnvironment.js";import{H as d}from"../../../../../chunks/HazeCompositing.glsl.js";import{HazeCompositingTechnique as c}from"./HazeCompositingTechnique.js";import{HazePassParameters as p,HazeTechnique as f}from"./HazeTechnique.js";import{HazeTechniqueConfiguration as l}from"./HazeTechniqueConfiguration.js";import{createQuadVAO as _}from"../../lib/glUtil3D.js";import{applyTextureResizeModulo as b}from"../../lib/textureUtils.js";import{DepthStencilAttachment as g,ColorAttachment0 as w,PrimitiveType as P}from"../../../../webgl/enums.js";let v=class extends u{constructor(e){super(e),this._compositingPassParameters=new d,this._passParameters=new p,this._hazeConfiguration=new l,this._oldAmount=1,this._newAmount=1,this._amount=this._newAmount}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===this.produces);if(!this.bindParameters.mainDepth)return i;const r=this.techniques.getCompiled(f,this._hazeConfiguration),s=!!this._hazeConfiguration.reduced&&this.techniques.getCompiled(c);if(null==r||null==s)return i;const o=this.renderingContext,a=i.getAttachment(g);if(this._update(),!s)return i.detachDepth(),o.bindFramebuffer(i.fbo),o.bindTechnique(r,this.bindParameters,this._passParameters),o.setDrawBuffers([w]),this._renderCommon(o),i.attachDepth(a),i;const n=o.getViewport(),u=this.camera,d=h(u.eye)-m.radius;let p;const l=m.atmosphereHeight;if(d<l){const e=Math.min(1,Math.max(0,d/l));p=t(.4,.5,e)}else{const e=Math.min(1,Math.max(0,(d-l)/(15*l)));p=t(.5,1,e)}const _=this.renderingContext.parameters.maxTextureSize,P=b(Math.round(p*u.fullViewport[2]),_),v=b(Math.round(p*u.fullViewport[3]),_);o.setViewport(0,0,P,v);const C=this.fboCache.acquire(P,v,"haze",5);return o.bindFramebuffer(C.fbo),o.clearFramebuffer([0,0,0,1],!0,!0),o.bindTechnique(r,this.bindParameters,this._passParameters),this._renderCommon(o),o.setViewport(n.x,n.y,n.width,n.height),this._compositingPassParameters.color=C.getTexture(),i.detachDepth(),o.bindFramebuffer(i.fbo),o.bindTechnique(s,this.bindParameters,this._compositingPassParameters),o.setDrawBuffers([w]),o.screen.draw(),i.attachDepth(a),C.release(),i}_renderCommon(e){this._vao??=_(e,1),e.bindVAO(this._vao),e.drawArrays(P.TRIANGLE_STRIP,0,4)}_update(){const e=this.view.basemapTerrain?.getLowerBoundRadius()??0;n(this._passParameters.radii,e,e+m.atmosphereHeight);const r=h(this.bindParameters.camera.eye);this._passParameters.hazeStrength=t(t(.6,1,i(9500,10500,r-m.radius)),1,this._amount)}};v=e([a("esri.views.3d.webgl-engine.effects.haze.Haze")],v);export{v as Haze};
