// Ореолы, штатная корона и скрытие штатного луча.
#if NC_FLARE_KIND == 0
float coreSize = 0.18;
float haloSize = 0.40;
float haloStrength = 0.12;
float redStrength = 0;
float coreStrength = 1.0;
float streakStrength = 0.055;
bool debugCorona = false;
float4 PS(float4 diffuse : COLOR0, float2 uv : TEXCOORD0) : COLOR0 {
    float2 xy = uv * 2 - 1;
    float spriteRadius2 = dot(xy, xy);
    
    if (debugCorona) return float4(0.1, 1, 0.05, diffuse.a * saturate(1-spriteRadius2));
    
    
    float2 gradientU = float2(ddx(uv.x), ddy(uv.x));
    float2 gradientV = float2(ddx(uv.y), ddy(uv.y));
    float aspect = sqrt(max(dot(gradientV,gradientV),0.00000001)
        / max(dot(gradientU,gradientU),0.00000001));
    xy.x *= clamp(aspect,0.25,6);
    float radius2 = dot(xy, xy);
    float size2 = max(coreSize*coreSize, 0.0001);
    float pinpoint = exp(-radius2 / (size2*0.12));
    float lens = exp(-radius2 / size2);
    float halo = exp(-radius2 / max(haloSize*haloSize, 0.0001));
    
    float streak = exp(-xy.x*xy.x/(size2*4)-xy.y*xy.y/(size2*0.035));
    float edge = pow(saturate(1-spriteRadius2), 3);
    
    float red = step(diffuse.g * 3.5, diffuse.r) * step(diffuse.b * 3.5, diffuse.r);
    float strength = lerp(1, redStrength, red);
    float alpha = (pinpoint*coreStrength + lens*0.28 + halo*haloStrength*0.7
        + streak*streakStrength) * edge * strength;
    return float4(diffuse.rgb, diffuse.a * saturate(alpha));
}
technique Corona {
    pass P0 {
        PixelShader = compile ps_3_0 PS();
        AlphaBlendEnable = TRUE; SrcBlend = SRCALPHA; DestBlend = ONE;
        ZWriteEnable = FALSE; AlphaTestEnable = FALSE;
    }
}
#elif NC_FLARE_KIND == 1
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
struct Input { float3 position : POSITION0; float2 uv : TEXCOORD0; float4 color : COLOR0; };
struct Output { float4 position : POSITION0; float2 uv : TEXCOORD0; float4 color : COLOR0; };
Output VS(Input i) {
    Output o; o.position=mul(float4(i.position,1),gWorldViewProjection);
    o.uv=i.uv; o.color=i.color; return o;
}
float4 PS(Output i) : COLOR0 {
    float2 xy=i.uv*2-1;
    float radius=dot(xy,xy);
    float glow=exp(-radius*6)*pow(saturate(1-radius),2);
    return float4(i.color.rgb,i.color.a*glow);
}
technique Glow { pass P0 {
    VertexShader=compile vs_3_0 VS(); PixelShader=compile ps_3_0 PS();
    AlphaBlendEnable=TRUE; SrcBlend=SRCALPHA; DestBlend=ONE;
    ZEnable=TRUE; ZWriteEnable=FALSE; CullMode=NONE; AlphaTestEnable=FALSE;
} }
#elif NC_FLARE_KIND == 2
texture gTexture;

technique TexReplace
{
    pass P0
    {
        Texture[0] = gTexture;
    }
}
#elif NC_FLARE_KIND == 3
float4 PS() : COLOR0 { return 0; }
technique HideStock { pass P0 {
    PixelShader=compile ps_2_0 PS();
    AlphaBlendEnable=TRUE; SrcBlend=SRCALPHA; DestBlend=INVSRCALPHA;
    ZWriteEnable=FALSE;
} }
#endif
