/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{set as e}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as t}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{fadeInTime as a,fadeOutTime as i}from"../../support/flow/constants.js";import{Float2PassUniform as o}from"../core/shaderModules/Float2PassUniform.js";import{FloatPassUniform as m}from"../core/shaderModules/FloatPassUniform.js";import{glsl as n}from"../core/shaderModules/glsl.js";function s(t,s){const{hasAnimation:l,animation:T}=s;if(!l)return;const{attributes:f,varyings:v,vertex:c,fragment:p}=t;f.add("timeStamps","vec4"),v.add("vTimeStamp","float"),v.add("vFirstTime","float"),v.add("vLastTime","float"),v.add("vTransitionType","float"),c.main.add(n`vTimeStamp = timeStamps.x;
vFirstTime = timeStamps.y;
vLastTime = timeStamps.z;
vTransitionType = timeStamps.w;`),3===T&&p.constants.add("decayRate","float",2.3),p.code.add(n`
    float getTrailOpacity(float x) {
      ${r(T)}
    }`),p.uniforms.add(new m("timeElapsed",e=>e.timeElapsed),new m("trailLength",e=>e.trailLength),new m("speed",e=>e.animationSpeed),new o("startEndTime",t=>e(d,t.startTime,t.endTime))),p.constants.add("fadeInTime","float",a),p.constants.add("fadeOutTime","float",i),p.constants.add("incomingTransition","int",0),p.constants.add("outgoingTransition","int",2),p.code.add(n`float fadeIn(float x) {
return smoothstep(0.0, fadeInTime, x);
}
float fadeOut(float x) {
return isinf(fadeOutTime) ? 1.0 : smoothstep(fadeOutTime, 0.0, x);
}
vec4 animate(vec4 color) {
float startTime = startEndTime[0];
float endTime = startEndTime[1];
float totalTime = vLastTime - vFirstTime;
float actualEndTime = int(vTransitionType) == outgoingTransition ? min(endTime, startTime + vLastTime / speed) : endTime;
vec4 animatedColor = color;
if (speed == 0.0) {
animatedColor.a *= getTrailOpacity((totalTime - (vTimeStamp - vFirstTime)) / trailLength);
animatedColor.a *= isinf(actualEndTime) ? 1.0 : fadeOut(timeElapsed - actualEndTime);
animatedColor.a *= fadeIn(timeElapsed - startTime);
return animatedColor;
}
float relativeStartTime = mod(startTime, totalTime);
float vHeadRelativeToFirst = mod((timeElapsed - relativeStartTime) * speed - vFirstTime, totalTime);
float vRelativeToHead = vHeadRelativeToFirst + vFirstTime - vTimeStamp;
bool inPreviousCycle = vRelativeToHead < 0.0;
vRelativeToHead += inPreviousCycle ? totalTime : 0.0;
float vAbsoluteTime = timeElapsed - vRelativeToHead / speed;
if (vAbsoluteTime > actualEndTime) {
vRelativeToHead = (timeElapsed - relativeStartTime) * speed - vTimeStamp;
vAbsoluteTime = timeElapsed - vRelativeToHead / speed;
}
animatedColor *= step(startTime, vAbsoluteTime);
animatedColor *= step(vAbsoluteTime, actualEndTime);
animatedColor.a *= isinf(actualEndTime) ? 1.0 : fadeOut(timeElapsed - actualEndTime);
animatedColor.a *= inPreviousCycle ? fadeOut(vHeadRelativeToFirst / speed) : 1.0;
animatedColor.a *= getTrailOpacity(vRelativeToHead / trailLength);
animatedColor.a *= int(vTransitionType) == incomingTransition ? fadeIn(vAbsoluteTime - startTime) : 1.0;
animatedColor.a *= fadeIn(vTimeStamp - vFirstTime);
return animatedColor;
}`)}function r(e){switch(e){case 2:return"return x >= 0.0 && x <= 1.0 ? 1.0 : 0.0;";case 3:return"float cutOff = exp(-decayRate);\n        return (exp(-decayRate * x) - cutOff) / (1.0 - cutOff);";default:return"return 1.0;"}}const d=t();export{s as AnimatedLine};
