/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{lerp as t}from"../../../../../core/mathUtils.js";import{disposeMaybe as r}from"../../../../../core/maybe.js";import{watch as s,syncAndInitial as i,initial as a}from"../../../../../core/reactiveUtils.js";import{subclass as o}from"../../../../../core/accessorSupport/decorators.js";import{set as n}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{set as m,length as h}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{ZEROS as p}from"../../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{earth as c}from"../../../../../geometry/support/Ellipsoid.js";import{InternalRenderCategory as d}from"../../../webgl.js";import{OpaqueEnvironment as u}from"../OpaqueEnvironment.js";import{computeInnerAltitudeFade as l,innerAtmosphereDepth as f}from"./atmosphereUtils.js";import{ChapmanAtmospherePassParameters as b,ChapmanAtmosphereTechnique as g}from"./ChapmanAtmosphereTechnique.js";import{createQuadVAO as _}from"../../lib/glUtil3D.js";import{applyTextureResizeModulo as P}from"../../lib/textureUtils.js";import{A as w}from"../../../../../chunks/AtmosphereCompositing.glsl.js";import{AtmosphereCompositingTechnique as v}from"../../shaders/AtmosphereCompositingTechnique.js";import{DepthStencilAttachment as j,ColorAttachment0 as A,PrimitiveType as x}from"../../../../webgl/enums.js";import C from"../../../../../webscene/background/ColorBackground.js";let T=class extends u{constructor(){super(...arguments),this._compositingPassParameters=new w,this._vao=null,this._passParameters=new b}initialize(){this.addHandles([s(()=>this.view.environment.background,e=>{const t=e instanceof C?e.color.toUnitRGBA():p;m(this._passParameters.backgroundColor,t[0]*t[3],t[1]*t[3],t[2]*t[3])},i),s(()=>this.view.stage?.renderer?.highResolutionAtmosphere,e=>this._passParameters.reduced=!e,i),s(()=>this.view.map.ground.undergroundColor,e=>{this._passParameters.undergroundColor=e?.toUnitRGBA()??p,this.requestRender(1)},a),s(()=>this.view.environment.atmosphereEnabled,e=>e?this._enable():this._disable(),i)])}destroy(){this._vao=r(this._vao)}render(e){const r=e.find(({name:e})=>e===d.OPAQUE_ENVIRONMENT);if(!this.bindParameters.mainDepth)return r;const s=this.renderingContext;this._vao??=_(s,1);const i=r.getAttachment(j);this._update();const a=this.techniques.getCompiled(g),o=this.techniques.getCompiled(v);if(!a||!o)return this.requestRender(1),r;if(!this._passParameters.reduced)return r.detachDepth(),s.bindFramebuffer(r.fbo),s.bindTechnique(a,this.bindParameters,this._passParameters),s.setDrawBuffers([A]),s.bindVAO(this._vao),s.drawArrays(x.TRIANGLE_STRIP,0,4),r.attachDepth(i),r;const n=s.getViewport(),m=this.bindParameters.camera,p=h(m.eye)-c.radius;let u;const l=c.atmosphereHeight;if(p<l){const e=Math.min(1,Math.max(0,p/l));u=t(.2,.3,e)}else{const e=Math.min(1,Math.max(0,(p-l)/(15*l)));u=t(.3,.6,e)}const f=this.renderingContext.parameters.maxTextureSize,b=P(Math.round(u*m.fullViewport[2]),f),w=P(Math.round(u*m.fullViewport[3]),f);s.setViewport(0,0,b,w);const C=this.fboCache.acquire(b,w,"chapman",5);return s.bindFramebuffer(C.fbo),s.clearFramebuffer([0,0,0,1],!0,!0),s.bindTechnique(a,this.bindParameters,this._passParameters),s.bindVAO(this._vao),s.drawArrays(x.TRIANGLE_STRIP,0,4),s.setViewport(n.x,n.y,n.width,n.height),this._compositingPassParameters.color=C.getTexture(),r.detachDepth(),s.bindFramebuffer(r.fbo),s.bindTechnique(o,this.bindParameters,this._compositingPassParameters),s.setDrawBuffers([A]),s.screen.draw(),r.attachDepth(i),C.release(),r}_update(){const e=this.view.basemapTerrain?.getLowerBoundRadius()??0;n(this._passParameters.radii,e,e+c.atmosphereHeight);const t=h(this.bindParameters.camera.eye);this._passParameters.altitudeFade=l(t-e),this._passParameters.innerFadeDistance=2*Math.sqrt((2*e-f)*f)}};T=e([o("esri.views.3d.webgl-engine.effects.atmosphere.ChapmanAtmosphere")],T);export{T as ChapmanAtmosphere};
