/*
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}from"../../../core/mathUtils.js";import{disposeMaybe as s}from"../../../core/maybe.js";import{watch as i,syncAndInitial as r,sync as n}from"../../../core/reactiveUtils.js";import{secondsFromMilliseconds as o}from"../../../core/time.js";import{property as a,subclass as d}from"../../../core/accessorSupport/decorators.js";import{getReferenceEllipsoid as c}from"../../../geometry/ellipsoidUtils.js";import{getContinuousIndexArray as m}from"../../../geometry/support/Indices.js";import{InternalRenderCategory as p}from"../webgl.js";import{PrecipitationPassParameters as h,PrecipitationTechnique as f}from"./PrecipitationTechnique.js";import{PrecipitationTechniqueConfiguration as u}from"./PrecipitationTechniqueConfiguration.js";import{glLayout as l}from"../support/buffer/glUtil.js";import{newLayout as _}from"../support/buffer/InterleavedLayout.js";import{TransparentEnvironment as b}from"../webgl-engine/effects/TransparentEnvironment.js";import{VertexArrayObject as y}from"../webgl-engine/lib/VertexArrayObject.js";import{PrimitiveType as w}from"../../webgl/enums.js";import{bindVertexBufferLayout as v}from"../../webgl/Util.js";import{fromLayout as g}from"../../webgl/VertexAttributeLocations.js";import{VertexBuffer as P}from"../../webgl/VertexBuffer.js";let j=class extends b{constructor(e){super(e),this._hasSnowCover=!1,this._rainSpeed=.1,this._snowSpeed=.01,this._numParticles=0,this._passParameters=new h,this._configuration=new u;const{view:t,type:s}=e;this._configuration.type="rainy"===s?0:1,t.stage.renderView.techniques.precompile(f,this._configuration),this._passParameters.time=0,this._passParameters.radius=c(t.spatialReference).radius,this.addHandles([i(()=>t.environment.weather,e=>{e.type===s&&this.addHandles([i(()=>e.precipitation,e=>this._adjustParticles(e),r),i(()=>"snowy"===e.type&&"enabled"===e.snowCover,e=>this._hasSnowCover=e,r)])},r)]),this.addHandles(i(()=>t.stage?.renderer.renderContext.bind.clouds.fadeFactor??1,e=>{this._passParameters.opacity=e,1===e&&(this.removeHandles("opacity"),this.addHandles(i(()=>t.stage?.renderer.renderContext.bind.clouds.opacity??1,e=>this._passParameters.opacity=e,r),"opacity"))},n),"opacity")}initialize(){this.addHandles([i(()=>this.view.environment.atmosphereEnabled,e=>e?this._enable():this._disable(),r)])}_adjustParticles(e){const s=.35,i=e<.5?t(0,s,2*e):t(s,1,2*(e-.5));this._numParticles=A*i}destroy(){this._numParticles=0,this._vao=s(this._vao),this._instanceIdBuffer=s(this._instanceIdBuffer)}fadeOut(e){this.removeAllHandles(),this.addHandles(i(()=>this.renderContext?.bind.clouds.fadeFactor??1,t=>{this._passParameters.opacity=1-t,1===t&&(this.removeAllHandles(),e())}),"opacity")}get snowCover(){return this._hasSnowCover?this._passParameters.opacity:null}updateAnimation(e){const t=("rainy"===this.type?this._rainSpeed:this._snowSpeed)*o(e.time)%1e5;return this._passParameters.time!==t&&(this._passParameters.time=t,!0)}render(e){const t=this.renderingContext,s=Math.round(this._numParticles*this._passParameters.opacity),i=e.find(({name:e})=>e===p.TRANSPARENT_ENVIRONMENT);if(s<1)return i;const r=this.techniques.get(f,this._configuration);if(!r.compiled)return this.requestRender(1),i;const n=t.bindTechnique(r,this.bindParameters,this._passParameters);return this._vao??=I(t),t.bindVAO(this._vao),this._bindInstanceIdBuffer(n.locations),n.assertCompatibleVertexAttributeLocations(this._vao,g(x)),t.drawArraysInstanced(w.TRIANGLES,0,3,s),t.unbindBuffer(34962),i}_bindInstanceIdBuffer(e){const t=this.renderingContext;if(this._instanceIdBuffer)return void t.bindBuffer(this._instanceIdBuffer);const s=m(A);this._instanceIdBuffer=new P(t,x,new Float32Array(s)),v(t,e,this._instanceIdBuffer,0)}};function I(e){const t=new Float32Array([-1,0,1,1,0,-1,1,0,1]);return new y(e,new P(e,l(C),t))}e([a({constructOnly:!0})],j.prototype,"type",void 0),j=e([d("esri.views.3d.environment.Precipitation")],j);const A=25e4,C=_().vec3f("position").freeze(),x=l(_().f32("instanceFeatureAttribute"),1);export{j as Precipitation};
