// 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>
    /// Converts diffuse color (for metal flakes layer)
    /// </summary>
    shader MaterialSurfaceDiffuseMetalFlakes : MaterialSurfaceDiffuse,
                                               Transformation,
                                               PositionStream4
    {
        compose ComputeColor surfaceToEyeDistanceFactor;

        override void Compute()
        {
            var basePaintColor = streams.matDiffuse;

            base.Compute();

            var distanceFactor = surfaceToEyeDistanceFactor.Compute().r;

            // Interpolate the factors using the surface to camera distance
            float LOD = saturate(distance(Eye.xyz, streams.PositionWS.xyz) * distanceFactor);
            streams.matDiffuse = lerp(streams.matDiffuse, basePaintColor, LOD);

            // Because matDiffuse can be modified when using a metalness, we are storing the colorBase into matColorBase
            // so that we are able to query the original diffuse color without any modifications.
            streams.matColorBase = lerp(streams.matDiffuse, basePaintColor, LOD);
        }
    };
}
