/*
#include <metal_stdlib>
using namespace metal;

namespace Lens_Cross_NS {
   float2 rotateUV(float2 uv, float angle) {
        float s = sin(angle);
        float c = cos(angle);
        float2x2 rotationMatrix = float2x2(c, -s, s, c);
        return rotationMatrix * uv;
    }
    
    // 유사 난수 생성 함수
    float hash(float2 p) {
        float3 p3 = fract(float3(p.xyx) * float3(.1031, .1030, .0973));
        p3 += dot(p3, p3.yzx + 33.33);
        return fract((p3.x + p3.y) * p3.z);
    }
    
    // 위치 기반으로 랜덤 각도 생성 (0 ~ 2π)
    float getRandomAngle(float2 position, float frameCount) {
        //1부터 - 24 각이 더 천천히 변하게
        return hash(position) * 6.28318530718 * frameCount * 0.005; // 2π 값을 직접 사용
    }
  
    float StarRays(float2 uv, float size, float angle) {
        // 입력 UV 좌표를 회전
        float2 rotatedUV = rotateUV(uv, angle);
        
        // 기본 크기 계산
        float centerSize = size * 0.0000001;
        float raysSize = size * 115.0;
        
        float dist = length(uv);
        
        float rayScale = 1.0 / raysSize;
        float2 scaledUV = rotatedUV * rayScale;
        
        // 메인 광선 계산 (회전된 좌표 사용)
        float rays = max(0.0, 1.0 - abs(scaledUV.x * scaledUV.y * 600.0));
        
        // 45도 회전된 광선 추가
        float2 rotated45UV = float2x2(0.7071, -0.7071, 0.7071, 0.7071) * scaledUV;
        rays += max(0.0, 1.0 - abs(rotated45UV.x * rotated45UV.y * 500.0)) * 0.5;
        
        // 중심 글로우
        float centerGlow = exp(-dist * dist / (centerSize * 100.0));
        
        // 광선 감쇠
        float rayFade = smoothstep(raysSize * .8, 0.0, dist);
        
        // 최종 결합
        float star = centerGlow * 0.1 + (rays * rayFade) * 0.4;
        
        return star;
    }
    
    float3 GetStarColor(float2 position, float brightness) {
        float random = hash(position);
        float random2 = hash(position * 2.0);
        float random3 = hash(position * 3.0);
        
        const float3 colors[5] = {
            float3(1.0, 0.9, 0.7),    // 따뜻한 흰색
            float3(0.9, 0.9, 1.0),    // 차가운 흰색
            float3(0.95, 0.8, 0.6),   // 황금빛
            float3(0.8, 0.9, 1.0),    // 푸른빛
            float3(1.0, 0.8, 0.8)     // 붉은빛
        };
        
        int index1 = int(random * 5.0);
        int index2 = (index1 + 1) % 5;
        float blend = fract(random * 5.0);
        
        float3 baseColor = mix(colors[index1], colors[index2], blend);
        
        float3 tint = float3(
            1.0 + 0.2 * sin(random * 6.28318530718),
            1.0 + 0.2 * sin(random2 * 6.28318530718),
            1.0 + 0.2 * sin(random3 * 6.28318530718)
        );
        
        return baseColor * tint * (0.8 + 0.2 * brightness);
    }


        
   float cheap_star(float2 uv, float anim) {
        uv = abs(uv);
        float2 pos = min(uv.xy/uv.yx, anim);
        float p = (2.0 - pos.x - pos.y);
        return (1.0 + p*(p*p-1.5)) / (uv.x + uv.y);
    }
    
    // 별 효과를 그리는 함수
    float4 drawStar(float2 uv, float4 sourceColor, float2 center) {
        // Calculate distance from current pixel to center
        float2 toCenter = uv - center;
        
        // Apply star effect to the center
        float anim = 1.0; // Fixed animation value
        float2 starUV = toCenter * 20.0; // Scale for star size control
        
        // Get star effect using the cheap_star function
        float star_effect = cheap_star(starUV, anim);
        
        // Star color (golden/yellow)
        float3 star_color = float3(1.0, 0.9, 0.7) * star_effect;
        
        // Intensity control - make star visible only near center
        float starIntensity = exp(-length(toCenter) * 50.0); // Exponential falloff
        
        // Combine original image with star effect
        return sourceColor + float4(star_color * starIntensity * 0.1, 0.0);
    }

}

kernel void Lens_Cross(
    texture2d<float, access::sample> input [[texture(0)]],
    texture2d<float, access::write> output [[texture(1)]],
    constant float& radius [[buffer(0)]],
    uint2 gid [[thread_position_in_grid]]
) {
    constexpr sampler textureSampler(filter::linear);

    uint2 textureSize = uint2(output.get_width(), output.get_height());
    if (gid.x >= textureSize.x || gid.y >= textureSize.y) {
        return;
    }

    float2 uv = float2(gid) / float2(textureSize);
    float4 sourceColor = input.sample(textureSampler, uv);
    
     
    //밝은 지점인지 검사
    float brightness = dot(sourceColor.rgb, float3(0.299, 0.587, 0.114));

    // 별 효과 적용
    float2 point = float2(0.25, 0.25); // 중앙 좌표 정의
    float4 finalColor = Lens_Cross_NS::drawStar(uv, sourceColor, point);
   
    output.write(finalColor, gid);
}
