/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{throwIfAborted as t}from"../../../../../core/promiseUtils.js";import{deserializeFlowPaths as e}from"../flowPathsIO.js";import{areStreamlinesCompatible as s,setUniforms as a}from"../utils.js";import{AFlowResources as r}from"./AFlowResources.js";import{getPositions as i}from"../../../../support/flow/utils.js";import{BufferObject as o}from"../../../../webgl/BufferObject.js";import{PrimitiveType as n,DataType as m}from"../../../../webgl/enums.js";import{VertexArrayObject as p}from"../../../../webgl/VertexArrayObject.js";import{fromLayout as l}from"../../../../webgl/VertexAttributeLocations.js";import{VertexBuffer as f}from"../../../../webgl/VertexBuffer.js";import{VertexElementDescriptor as h}from"../../../../webgl/VertexElementDescriptor.js";class c{constructor(t){this._params=t,this.supportsContinuation=!1,this.slideoutDuration=0}get animated(){return this._params.flowSpeed>0}isCompatible(t){return t instanceof c&&s(this._params,t._params)}async load(s,a,r){const{extent:o,size:n}=s;t(r);const m=await this._params.loadImagery(o,n[0],n[1],this._params.timeExtent,r),p=a&&this._params.simulationSettings.continuous?i(a.flowPaths,a.query,s,this._params.flowSpeed):[],{vertexData:l,indexData:f,pathData:h}=await this._params.createFlowMesh("Particles",this._params.simulationSettings,m,{positions:p},r),c=e(h);return new d(l,f,s,c,{color:this._params.color,opacity:this._params.opacity,size:this._params.size})}render(t,e,s){const{context:r}=t,{program:i}=s;r.setFaceCullingEnabled(!1),r.setBlendingEnabled(!0),r.setBlendFunction(1,771),r.useProgram(i),i.setUniform1f("u_time",e.time),i.setUniform1f("u_displayOpacity",e.displayOpacity),i.setUniform1f("u_trailLength",this._params.trailLength),i.setUniform1f("u_flowSpeed",this._params.flowSpeed),i.setUniform1f("u_featheringSize",this._params.featheringSize),i.setUniform1f("u_featheringOffset",this._params.featheringOffset),i.setUniform1f("u_introFade",this._params.introFade?1:0),i.setUniform1f("u_fadeToZero",this._params.fadeToZero?1:0),i.setUniform1f("u_decayRate",this._params.decayRate),i.setUniformMatrix3fv("u_dvsMat3",e.dvsMat3),i.setUniformMatrix3fv("u_displayViewMat3",e.displayViewMat3),a(i,"color","vec4",this._params.color),a(i,"opacity","float",this._params.opacity),a(i,"size","float",this._params.size),r.bindVAO(s.vertexArray),r.drawElements(n.TRIANGLES,s.indexCount,m.UNSIGNED_INT,0)}}const u=[new h("a_xyts0",4,m.FLOAT,0,64),new h("a_xyts1",4,m.FLOAT,16,64),new h("a_typeIdDurationSeed",4,m.FLOAT,32,64),new h("a_extrudeInfo",4,m.FLOAT,48,64)],_={vsPath:"raster/flow/particles",fsPath:"raster/flow/particles",locations:l(u)};class d extends r{constructor(t,e,s,a,r){super(s,a),this._vertexData=t,this._indexData=e,this._values=r}attach(t){const{context:e}=t,s=new f(e,u,this._vertexData),a=o.createIndex(e,35044,this._indexData),r=new p(e,s,a),i=[];"ramp"===this._values.color.kind&&i.push("vvColor"),"ramp"===this._values.opacity.kind&&i.push("vvOpacity"),"ramp"===this._values.size.kind&&i.push("vvSize");const n=t.getProgram(_,i);this.vertexArray=r,this.program=n,this.indexCount=this._indexData.length,this._vertexData=null,this._indexData=null}detach(){this.vertexArray.dispose()}get ready(){return this.program.compiled}}export{c as Particles,d as ParticlesResources};
