// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

namespace Stride.Rendering.Images
{
    /// <summary>
    /// Vignetting shader. 
    /// </summary>
    internal shader VignettingShader : ColorTransformShader, Texturing
    {
        // Amount
        float Amount;

        // At which radius from the center the vignetting begins, in [0, 1]
        float RadiusBegin;

        // Color othe vignette
        [Color]
        float3 Color;

        override float4 Compute(float4 color)
        {
            float2 fromCenterVector = streams.TexCoord - float2(0.5, 0.5);
            float squareDistanceToCenter = dot(fromCenterVector, fromCenterVector);
            float distanceToCenter = sqrt(squareDistanceToCenter);

            float vignette = smoothstep(RadiusBegin, 1.0, distanceToCenter / 0.7071); // 0.7071 is sqrt(0.5), at the screen corner
            vignette *= Amount;
            color.rgb = color.rgb * (1.0 - vignette) + vignette * Color;

            return color;
            
        }
    };
}
