// 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.Materials
{
    /// <summary>
    /// Performs a Lambert shading
    /// </summary>
    shader MaterialSurfaceShadingDiffuseLambert<bool TIsEnergyConservative> : IMaterialSurfaceShading, Math
    {
        override float3 ComputeDirectLightContribution()
        {
            var diffuseColor = streams.matDiffuseVisible;
            if (TIsEnergyConservative)
            {
                // Approximation see: http://research.tri-ace.com/Data/course_note_practical_implementation_at_triace.pdf
                diffuseColor *= (1 - streams.matSpecularVisible);
            }
            return diffuseColor / PI * streams.lightColorNdotL * streams.matDiffuseSpecularAlphaBlend.x;
        }

        override float3 ComputeEnvironmentLightContribution()
        {
            // TODO: Check how to factorize this with DirectLight
            var diffuseColor = streams.matDiffuseVisible;
            if (TIsEnergyConservative)
            {
                diffuseColor *= (1 - streams.matSpecularVisible);
            }

            return diffuseColor * streams.envLightDiffuseColor;
        }
    };
}
