// 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>
    /// Defines a point light
    /// </summary>
    shader LightPoint : LightUtil, LightStream, PositionStream4
    {
        struct PointLightData
        {
            float3 PositionWS;
            float  InvSquareRadius;
            [Color]
            float3 Color;
        };

        struct PointLightDataInternal
        {
            float3 PositionWS;
            float  InvSquareRadius;
            [Color]
            float3 Color;
        };

        void ProcessLight(PointLightDataInternal light)
        {
            float3 lightVectorNorm;
            float attenuation = ComputeAttenuation(light, streams.PositionWS.xyz, lightVectorNorm);

            streams.lightPositionWS = light.PositionWS;
            streams.lightColor = light.Color;
            streams.lightAttenuation = attenuation;
            streams.lightDirectionWS = lightVectorNorm;
        }

        float ComputeAttenuation(PointLightDataInternal light, float3 position, inout float3 lightVectorNorm)
        {
            float3 lightVector = light.PositionWS - position;
            float lightVectorLength = length(lightVector);
            lightVectorNorm = lightVector / lightVectorLength;

            float lightInvSquareRadius = light.InvSquareRadius;
            return GetDistanceAttenuation(lightVectorLength, lightInvSquareRadius);
        }
    };
}
