/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/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 i}from"./AFlowResources.js";import{getPositions as r}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 f}from"../../../../webgl/VertexAttributeLocations.js";import{VertexBuffer as l}from"../../../../webgl/VertexBuffer.js";import{VertexElementDescriptor as h}from"../../../../webgl/VertexElementDescriptor.js";class u{constructor(t){this._params=t,this.supportsContinuation=!1,this.slideoutDuration=20}get animated(){return this._params.flowSpeed>0}isCompatible(t){return t instanceof u&&s(this._params,t._params)}async load(s,a,i){const{extent:o,size:n}=s;t(i);const m=await this._params.loadImagery(o,n[0],n[1],this._params.timeExtent,i),p=a&&this._params.simulationSettings.continuous?r(a.flowPaths,a.query,s,this._params.flowSpeed):[],{vertexData:f,indexData:l,pathData:h}=await this._params.createFlowMesh("Streamlines",this._params.simulationSettings,m,{positions:p},i),u=e(h);return new c(f,l,s,u,{color:this._params.color,opacity:this._params.opacity,size:this._params.size})}render(t,e,s){const{context:i}=t,{program:r}=s;i.setFaceCullingEnabled(!1),i.setBlendingEnabled(!0),i.setBlendFunction(1,771),i.useProgram(r),r.setUniform1f("u_time",e.time-s.query.time),r.setUniform1f("u_startTime",e.startTime-s.query.time),r.setUniform1f("u_endTime",e.endTime-s.query.time),r.setUniform1f("u_displayOpacity",e.displayOpacity),r.setUniform1f("u_trailLength",this._params.trailLength),r.setUniform1f("u_flowSpeed",this._params.flowSpeed),r.setUniform1f("u_featheringSize",this._params.featheringSize),r.setUniform1f("u_featheringOffset",this._params.featheringOffset),r.setUniform1f("u_introFade",this._params.introFade?1:0),r.setUniform1f("u_fadeToZero",this._params.fadeToZero?1:0),r.setUniform1f("u_decayRate",this._params.decayRate),r.setUniformMatrix3fv("u_dvsMat3",e.dvsMat3),r.setUniformMatrix3fv("u_displayViewMat3",e.displayViewMat3),a(r,"color","vec4",this._params.color),a(r,"opacity","float",this._params.opacity),a(r,"size","float",this._params.size),i.bindVAO(s.vertexArray),i.drawElements(n.TRIANGLES,s.indexCount,m.UNSIGNED_INT,0)}}const _=[new h("a_positionAndSide",3,m.FLOAT,0,36),new h("a_timeInfo",3,m.FLOAT,12,36),new h("a_extrude",2,m.FLOAT,24,36),new h("a_speed",1,m.FLOAT,32,36)],d={vsPath:"raster/flow/streamlines",fsPath:"raster/flow/streamlines",locations:f(_)};class c extends i{constructor(t,e,s,a,i){super(s,a),this._vertexData=t,this._indexData=e,this._values=i}attach(t){const{context:e}=t,s=new l(e,_,this._vertexData),a=o.createIndex(e,35044,this._indexData),i=new p(e,s,a),r=[];"ramp"===this._values.color.kind&&r.push("vvColor"),"ramp"===this._values.opacity.kind&&r.push("vvOpacity"),"ramp"===this._values.size.kind&&r.push("vvSize");const n=t.getProgram(d,r);this.vertexArray=i,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{u as default};
