﻿// 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.

// Computes screen space velocity for backgrounds
shader BackgroundVelocity : ShaderBase, VelocityStream, PositionStream4, ScreenPositionBase
{
    stage float4x4 DeltaMatrix;

    stage stream float4 currentShadingPosition;
    stage stream float4 previousShadingPosition;

    stage override void VSMain()
    {
        streams.ShadingPosition = float4(streams.Position.xyz, 1);
        streams.currentShadingPosition = streams.Position;
        streams.previousShadingPosition = mul(streams.ShadingPosition, DeltaMatrix);
        base.VSMain();
    }

    stage override void PSMain()
    {
        streams.currentShadingPosition /= streams.currentShadingPosition.w;
        streams.previousShadingPosition /= streams.previousShadingPosition.w;
        float2 delta = (streams.currentShadingPosition - streams.previousShadingPosition).xy;
        streams.velocity = delta;
        base.PSMain();
    }
};
