// 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.Lights
{
    /// <summary>
    /// Code for attenuating a group of spotlights using angular attenuation.
    /// </summary>
    shader LightSpotAttenuationDefault :
        //SpotLightDataInternalShader,   // Required for "SpotLightDataInternal" // TODO: Revert this line as soon as the shader compiler is fixed.
        LightUtil   // Required for "GetDistanceAttenuation()" and "GetAngularAttenuation()".
    {
        //override float ComputeAttenuation(SpotLightDataInternal light, float3 position, inout float3 lightVectorNorm)
        float ComputeAttenuation(float3 PositionWS, // TODO: Revert to the above line as soon as the shader compiler is fixed.
                                 float3 AngleOffsetAndInvSquareRadius,
                                 float3 DirectionWS,
                                 float3 position,
                                 inout float3 lightVectorNorm)   // This overload is a temporary fix for a compiler error rendering us unable to override "ComputeAttenution()".
        {
            // TODO: There's duplicate code here. See "LightSpotAttenuationRectangular".

            //float3 lightVector = light.PositionWS - position;
            float3 lightVector = PositionWS - position; // TODO: Revert to the above line as soon as the shader compiler is fixed.
            float lightVectorLength = length(lightVector);
            lightVectorNorm = lightVector / lightVectorLength;
            
            //float3 lightAngleOffsetAndInvSquareRadius = light.AngleOffsetAndInvSquareRadius;
            float3 lightAngleOffsetAndInvSquareRadius = AngleOffsetAndInvSquareRadius;  // TODO: Revert to the above line as soon as the shader compiler is fixed.
            float2 lightAngleAndOffset = lightAngleOffsetAndInvSquareRadius.xy;
            float lightInvSquareRadius = lightAngleOffsetAndInvSquareRadius.z;

            // TODO: Add support for disk based Directional light
            //float3 lightDirection = -light.DirectionWS;
            float3 lightDirection = -DirectionWS;   // TODO: Revert to the above line as soon as the shader compiler is fixed.
            
            float attenuation = GetDistanceAttenuation(lightVectorLength, lightInvSquareRadius);
            attenuation *= GetAngularAttenuation(lightVectorNorm, lightDirection, lightAngleAndOffset.x, lightAngleAndOffset.y);
            return attenuation;
        }
    };
}
