/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{smoothstep as t,lerp as s}from"../../../../../core/mathUtils.js";import{releaseMaybe as r}from"../../../../../core/maybe.js";import{watch as a,syncAndInitial as i}from"../../../../../core/reactiveUtils.js";import{subclass as n}from"../../../../../core/accessorSupport/decorators.js";import{add as o,normalize as h,dot as m,scale as p,lerp as c,length as g}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as _,fromValues as d}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getReferenceEllipsoid as u}from"../../../../../geometry/ellipsoidUtils.js";import{InternalRenderCategory as l}from"../../../webgl.js";import{heightLimit as f}from"../../../environment/weather.js";import{TransparentEnvironment as P}from"../TransparentEnvironment.js";import{betaRayleigh as w,betaCombined as b}from"../atmosphere/atmosphereUtils.js";import{F as v}from"../../../../../chunks/Fog.glsl.js";import{FogTechnique as y}from"./FogTechnique.js";import{FogTechniqueConfiguration as j}from"./FogTechniqueConfiguration.js";import{DepthStencilAttachment as F,ColorAttachment1 as E}from"../../../../webgl/enums.js";const B=.95,R=1;let T=class extends P{constructor(e){super(e),this._configuration=new j,this._newParameters=new q,this._oldParameters=new q,this._fadedParameters=new q,this._parameters=this._newParameters,this._passParameters=new v;const t=u(e.view.spatialReference);this._planetRadius=t.radius,this._atmosphereRadius=t.radius+t.atmosphereHeight}initialize(){this.addHandles([a(()=>this.view.environment.atmosphereEnabled,()=>this._updateEnabled(),i),a(()=>this.view.environment.weather,()=>this._updateEnabled(),i),a(()=>this._updateFogParameters(),()=>{},i)]),this.addHandles(a(()=>this._fadeFactor,e=>this._fade(e),i))}_updateEnabled(){this.view.environment.atmosphereEnabled&&this.view.environment.weather?this._enable():this._disable()}get _fadeFactor(){return this.view.stage?.renderer.renderContext.bind.clouds.fadeFactor??1}_fade(e){const{_newParameters:t,_oldParameters:s}=this;e>=1?(this._parameters=t,this._oldParameters.copy(this._newParameters)):e<=0?this._parameters=s:(this._fadedParameters.lerp(s,t,e),this._parameters=this._fadedParameters)}_updateFogParameters(){const{weather:e,weatherAvailable:t}=this.view.environment,s="foggy"===e.type||"snowy"===e.type||"rainy"===e.type;this._newParameters.strength=t?e.fogOpacity:0,this._newParameters.amount=s?1:0,"foggy"!==e.type&&"snowy"!==e.type||(this._newParameters.fogBrightness=M),"rainy"===e.type&&(this._newParameters.fogBrightness=H),this._fadeFactor>=1&&this._oldParameters.copy(this._newParameters),this.requestRender(2)}get strength(){return this._parameters.strength}precompile(){this._update(),this._passParameters.amount>0&&this.techniques.precompile(y,this._configuration)}render(e){const t=e.find(({name:e})=>e===l.TRANSPARENT_ENVIRONMENT);if(0===this._parameters.amount)return t;const s=this._getTechnique();if(!s)return t;const a=this.renderingContext,i=t.getAttachment(F);return this._passParameters.emission=t.obtainAttachment(E),t.attachDepth(null),a.bindFramebuffer(t.fbo),a.bindTechnique(s,this.bindParameters,this._passParameters),a.screen.draw(),t.attachDepth(i),this._passParameters.emission&&(t.attachColor(this._passParameters.emission,E),this._passParameters.emission=r(this._passParameters.emission)),t}_getTechnique(){return this._update(),this._passParameters.amount>0?this.techniques.get(y,this._configuration):null}_update(){const e=this.bindParameters.camera;h(x,e.eye);const s=Math.max(0,m(x,this.bindParameters.lighting.mainLight.direction)),r=this._parameters.fogBrightness;p(this._passParameters.color,N,r),p(A,N,.1*r),c(this._passParameters.color,A,this._passParameters.color,s);const a=g(e.eye);this._passParameters.atmosphereC=a**2-this._atmosphereRadius**2,this._passParameters.amount=(1-t(B*f,R*f,Math.abs(a-this._planetRadius)))*this._parameters.amount,this._passParameters.strength=this._parameters.strength,this._configuration.useFloatBlend=this.view.environment.atmosphereEnabled&&this.bindParameters.useFloatBlend.value,this._configuration.hasEmissive=this.bindParameters.hasEmission}get test(){return{parameters:this._passParameters}}};T=e([n("esri.views.3d.webgl-engine.effects.fog.Fog")],T);class q{constructor(){this.fogBrightness=0,this.strength=0,this.amount=0}copy(e){this.fogBrightness=e.fogBrightness,this.strength=e.strength,this.amount=e.amount}lerp(e,t,r){this.fogBrightness=s(e.fogBrightness,t.fogBrightness,r),this.strength=s(e.strength,t.strength,r),this.amount=s(e.amount,t.amount,r)}}const x=_(),A=_(),C=d(3e-4,3e-4,3e-4),N=_();o(N,b,w),o(N,N,C),h(N,N);const U=N.reduce((e,t)=>e+t,0)/3,H=.5/U,M=1.5/U;export{T as Fog,q as FogParameters};
