#version 300 es

precision highp float;

in vec4 aPosition;
in vec2 aTextureCoord;
uniform float u_aspect_ratio;
uniform float uRate;
uniform float uTime;
uniform vec2 uCircleOffset;
uniform float uRectCornerRadius;
uniform float uHighlightWidth;
uniform float uBgBlur;

uniform vec2 uRectPos;
uniform vec2 uRectSize;

out vec2 v_originTexCoord;
out float rateRev;
out float rate2;
out float rate3;
out float radius;
out float bgRate;
out float hlWidth;
out vec2 texCoord;
out vec2 pos;
out vec2 hr;
out vec2 vp1;
out vec2 rectSize;
out float circleRadius;
out float shadowSize;
out vec3 highlightColor;
out vec2 lightDir1;
out vec2 lightDir2;
out float shadowIntensity;// 优化: 阴影强度预计算
out vec2 distortSpeed;// 优化: 扰动速度预计算
out float distortFreq;// 优化: 扰动频率预计算
out float F0;// 优化: 菲涅尔基础反射率
out float specularPower;// 优化: 高光强度系数
out float highlightEdgeSmoothness;// 优化: 高光边缘平滑度
out float glassTransparency;// 优化: 玻璃透明度预计算

void main() {
    gl_Position = aPosition;
    v_originTexCoord = vec2(aTextureCoord.x, 1.0 - aTextureCoord.y);

    rate2 = uRate * uRate;
    rate3 = rate2 * uRate;
    rateRev = 1.0-uRate;

    texCoord = v_originTexCoord;
    texCoord.x *= u_aspect_ratio;

    vec2 posC = vec2(0.5+0.023*sin(uTime), 0.4+0.02*cos(uTime)) + uCircleOffset;
    vec2 posR = uRectPos;
    pos = mix(posC, posR, rate2);

    vp1 = v_originTexCoord - pos;
    vp1.x *= u_aspect_ratio;

    hr = pos;

    radius = mix(0.3, uRectCornerRadius, rate2);

    bgRate = uBgBlur * rateRev;

    rectSize = uRectSize;

    if (rectSize.y < 0.2){
        float sy = 0.3 * rateRev;
        rectSize.y += sy;
    }

    hlWidth = mix(0.006, 0.0028, uRate);

    // Constants that can be calculated in vertex shader
    shadowSize = 0.01;
    highlightColor = vec3(1.);

    float time = uTime/2.;
    lightDir1 = normalize(vec2(-0.56 * cos(time) - 0.7 * sin(time),
    -0.56 * sin(time) + 0.7 * cos(time)));
    lightDir2 = -lightDir1;

    // 优化: 更多的预计算参数
    shadowIntensity = 0.05;// 阴影强度
    distortSpeed = vec2(2.0, -2.0);// 扰动效果的速度
    distortFreq = 20.0;// 扰动频率
    F0 = 0.04;// 玻璃的基础反射率
    specularPower = 2.;// 高光强度指数
    highlightEdgeSmoothness = 1.0;// 高光边缘平滑系数
    glassTransparency = 0.85;// 玻璃透明度
}