/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{set as t}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as a}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{fadeInTime as e,fadeOutTime as i}from"../../support/flow/constants.js";import{Float2PassUniform as o}from"../core/shaderModules/Float2PassUniform.js";import{FloatPassUniform as l}from"../core/shaderModules/FloatPassUniform.js";import{glsl as m}from"../core/shaderModules/glsl.js";function n(a,n){const{hasAnimation:s,animation:T}=n;if(!s)return;const{attributes:f,varyings:p,vertex:u,fragment:c}=a;f.add("timeStamps","vec4"),p.add("vTimeStamp","float"),p.add("vFirstTime","float"),p.add("vLastTime","float"),p.add("vTransitionType","float"),u.main.add(m`vTimeStamp = timeStamps.x;
vFirstTime = timeStamps.y;
vLastTime = timeStamps.z;
vTransitionType = timeStamps.w;`),3===T&&c.constants.add("decayRate","float",2.3),c.code.add(m`
    float getTrailOpacity(float x) {
      if (x < 0.0) {
        return 0.0;
      }

      ${d(T)}
    }`),c.uniforms.add(new l("timeElapsed",t=>t.timeElapsed),new l("trailLength",t=>t.trailLength),new l("speed",t=>t.animationSpeed),new o("startEndTime",a=>t(r,a.startTime,a.endTime))),c.constants.add("fadeInTime","float",e),c.constants.add("fadeOutTime","float",i),c.constants.add("incomingTransition","int",0),c.constants.add("outgoingTransition","int",2),c.code.add(m`float fadeIn(float x) {
return smoothstep(0.0, fadeInTime, x);
}
float fadeOut(float x) {
return isinf(fadeOutTime) ? 1.0 : smoothstep(fadeOutTime, 0.0, x);
}
void updateAlphaIf(inout float alpha, bool condition, float newAlpha) {
alpha = condition ? min(alpha, newAlpha) : alpha;
}
vec4 animate(vec4 color) {
float startTime = startEndTime[0];
float endTime = startEndTime[1];
float totalTime = vLastTime - vFirstTime;
float actualFadeOutTime = min(fadeOutTime * speed, trailLength);
float longStreamlineThreshold = (fadeInTime + 1.0) * speed + actualFadeOutTime;
bool longStreamline = totalTime > longStreamlineThreshold;
float totalTimeWithFadeOut = longStreamline && actualFadeOutTime != trailLength ? totalTime : totalTime + actualFadeOutTime;
float fadeOutStartTime = longStreamline ? totalTime - actualFadeOutTime : totalTime;
float originTime =  -vFirstTime;
float actualEndTime = int(vTransitionType) == outgoingTransition ? min(endTime, startTime + vLastTime / speed) : endTime;
vec4 animatedColor = color;
if (speed == 0.0) {
float alpha = getTrailOpacity((totalTimeWithFadeOut - (vTimeStamp - vFirstTime)) / trailLength);
updateAlphaIf(alpha, !isinf(actualEndTime), fadeOut(timeElapsed - actualEndTime));
updateAlphaIf(alpha, true, fadeIn(timeElapsed - startTime));
animatedColor.a *= alpha;
return animatedColor;
}
float relativeStartTime = mod(startTime, totalTimeWithFadeOut);
float shiftedTimeElapsed = timeElapsed - relativeStartTime + originTime;
float headRelativeToFirst = mod(shiftedTimeElapsed * speed, totalTimeWithFadeOut);
float vRelativeToHead = headRelativeToFirst - originTime - vTimeStamp;
float vAbsoluteTime = timeElapsed - vRelativeToHead / speed;
if (startTime > timeElapsed) {
return vec4(0.0);
}
float alpha = getTrailOpacity(vRelativeToHead / trailLength);
updateAlphaIf(alpha, true, fadeIn(timeElapsed - startTime));
updateAlphaIf(alpha, !isinf(actualEndTime), fadeOut(timeElapsed - actualEndTime));
updateAlphaIf(alpha, int(vTransitionType) != incomingTransition, step(startTime, vAbsoluteTime));
updateAlphaIf(alpha, headRelativeToFirst > fadeOutStartTime, fadeOut((headRelativeToFirst - fadeOutStartTime) / speed));
alpha *= fadeIn(vTimeStamp - vFirstTime);
animatedColor.a *= alpha;
return animatedColor;
}`)}function d(t){switch(t){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 r=a();export{n as AnimatedLine};
