﻿// 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 meshes
shader MeshVelocity : PositionStream4, TransformationBase, ScreenPositionBase, VelocityStream
{
    cbuffer PerDraw
    {
        float4x4 PreviousWorldViewProjection;
    }

    // The previous position in screen space
    stage stream float4 PreviousPosition;

    stage override void VSMain()
    {
        base.VSMain();
        
        // Calculate previous world position
        streams.PreviousPosition = mul(streams.Position, PreviousWorldViewProjection);
    }
    
    stage override void PSMain()
    {
        // Calculate screen space velocity
        float2 position = streams.ScreenPosition.xy / streams.ScreenPosition.w;
        float2 positionLast = streams.PreviousPosition.xy / streams.PreviousPosition.w;
        streams.velocity = position - positionLast;

        base.PSMain();

        //streams.ColorTarget = float4(abs(velocity.xy), 0.0f, 0.0f) * 1.0f;

        //float l = length(velocity.xy);
        //streams.ColorTarget = float4(l.xxx, 0.0f) * 15.0f;
    }
};
