MeshMotionVector.azsli 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #define MATERIALPIPELINE_SHADER_HAS_PIXEL_STAGE 0 // Well, it does have a pixel stage, but not one that can be customized.
  9. #define MATERIALPIPELINE_USES_PREV_VERTEX_POSITION 1
  10. //////////////////////////////////////////////////////////////////////////////////////////////////
  11. #include MATERIAL_TYPE_AZSLI_FILE_PATH
  12. //////////////////////////////////////////////////////////////////////////////////////////////////
  13. #include <scenesrg_all.srgi>
  14. #include <viewsrg_all.srgi>
  15. struct PsOutput
  16. {
  17. float2 m_motion : SV_Target0;
  18. };
  19. VsOutput VertexShader(VsInput IN, uint instanceId : SV_InstanceID)
  20. {
  21. VsSystemValues SV;
  22. SV.m_instanceId = instanceId;
  23. VsOutput OUT = EvaluateVertexGeometry(IN, SV);
  24. return OUT;
  25. }
  26. PsOutput PixelShader(VsOutput IN)
  27. {
  28. PsOutput OUT;
  29. // Current clip position
  30. float4 clipPos = mul(ViewSrg::m_viewProjectionMatrix, float4(IN.m_worldPos, 1.0));
  31. // Reprojected last frame's clip position, for skinned mesh it also implies last key frame
  32. float4 clipPosPrev = mul(ViewSrg::m_viewProjectionPrevMatrix, float4(IN.m_worldPosPrev, 1.0));
  33. float2 motion = (clipPos.xy / clipPos.w - clipPosPrev.xy / clipPosPrev.w) * 0.5;
  34. OUT.m_motion = motion;
  35. // Flip y to line up with uv coordinates
  36. OUT.m_motion.y = -OUT.m_motion.y;
  37. return OUT;
  38. }