VertexAnimation.azsl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include <Atom/Features/SrgSemantics.azsli>
  9. #include <Atom/RPI/Math.azsli>
  10. ShaderResourceGroup PassSrg : SRG_PerPass
  11. {
  12. StructuredBuffer<float3> m_sourceVertexData;
  13. RWStructuredBuffer<float3> m_targetVertexData;
  14. StructuredBuffer<float3> m_instanceOffsetData;
  15. float m_frameTime;
  16. uint m_vertexCountPerInstance;
  17. uint m_targetVertexStridePerInstance;
  18. }
  19. class ContinuousSineRng
  20. {
  21. uint m_seed;
  22. float m_timeshift;
  23. void Initialize(float3 position, uint instanceIndex, float timeshift)
  24. {
  25. m_seed = 0;
  26. m_seed ^= asuint(position.x);
  27. Xorshift(m_seed);
  28. m_seed ^= asuint(position.y);
  29. Xorshift(m_seed);
  30. m_seed ^= asuint(position.z);
  31. Xorshift(m_seed);
  32. m_seed ^= instanceIndex;
  33. Xorshift(m_seed);
  34. m_timeshift = timeshift;
  35. }
  36. float GetRandomNormalizedFloat()
  37. {
  38. // Duration in seconds after which the animation repeats
  39. float animationDuration = (Xorshift(m_seed) % 60 + 120) / 60.f;
  40. // The float values passed to the sin-function should not be too large, otherwise the result is not continuous
  41. uint seedSmall = m_seed % 1024;
  42. float timeshiftSmall = (m_timeshift % animationDuration) / animationDuration * TWO_PI;
  43. return sin(seedSmall + timeshiftSmall) * 0.5f + 0.5f;
  44. }
  45. float3 GetRandomUnitSphereDirection()
  46. {
  47. float z = GetRandomNormalizedFloat() * 2.f - 1.f;
  48. float phi = GetRandomNormalizedFloat() * TWO_PI;
  49. float rxy = sqrt(1.f - z * z);
  50. float x = rxy * cos(phi);
  51. float y = rxy * sin(phi);
  52. return normalize(float3(x, y, z));
  53. }
  54. };
  55. [numthreads(64, 1, 1)]
  56. void AnimateVertices(uint3 dispatchId : SV_DispatchThreadID)
  57. {
  58. uint instanceIndex = dispatchId.x / PassSrg::m_vertexCountPerInstance;
  59. uint inputIndex = dispatchId.x % PassSrg::m_vertexCountPerInstance;
  60. uint outputIndex = instanceIndex * PassSrg::m_targetVertexStridePerInstance + inputIndex;
  61. ContinuousSineRng rng;
  62. rng.Initialize(PassSrg::m_sourceVertexData[inputIndex], instanceIndex, PassSrg::m_frameTime);
  63. // Animated the vertices by displacing each vertex in a random direction by a random amount
  64. float3 animationMoveDirection = rng.GetRandomUnitSphereDirection();
  65. float animationMoveAmount = rng.GetRandomNormalizedFloat() * 0.02f;
  66. // Need to move vertices per instance in the compute shader since CLAS does not have a separate transform
  67. float3 vertexBaseLocation = PassSrg::m_instanceOffsetData[instanceIndex] + PassSrg::m_sourceVertexData[inputIndex];
  68. PassSrg::m_targetVertexData[outputIndex] = vertexBaseLocation + animationMoveDirection * animationMoveAmount;
  69. }