FastNoiseTest.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <AzTest/AzTest.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/Script/ScriptContext.h>
  14. #include <AzFramework/Components/TransformComponent.h>
  15. #include <FastNoiseGradientComponent.h>
  16. #include <FastNoiseTest.h>
  17. #include <GradientSignalTestHelpers.h>
  18. #include <GradientSignal/Components/GradientTransformComponent.h>
  19. #include <GradientSignal/Ebuses/GradientRequestBus.h>
  20. #include <GradientSignal/Ebuses/GradientTransformModifierRequestBus.h>
  21. #include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
  22. #include <GradientSignal/GradientSampler.h>
  23. #include <LmbrCentral/Shape/BoxShapeComponentBus.h>
  24. class FastNoiseTest : public ::testing::Test
  25. {
  26. };
  27. TEST_F(FastNoiseTest, FastNoise_ComponentCreatesSuccessfully)
  28. {
  29. auto noiseEntity = AZStd::make_unique<AZ::Entity>("noise_entity");
  30. ASSERT_TRUE(noiseEntity != nullptr);
  31. AZStd::unique_ptr<AZ::Component> createdComponent {noiseEntity->CreateComponent<FastNoiseGem::FastNoiseGradientComponent>()};
  32. FastNoiseGem::FastNoiseGradientComponent* noiseComp = noiseEntity->FindComponent<FastNoiseGem::FastNoiseGradientComponent>();
  33. ASSERT_EQ(noiseComp, createdComponent.get());
  34. }
  35. TEST_F(FastNoiseTest, FastNoise_ComponentMatchesConfiguration)
  36. {
  37. auto noiseEntity = AZStd::make_unique<AZ::Entity>("noise_entity");
  38. ASSERT_TRUE(noiseEntity != nullptr);
  39. FastNoiseGem::FastNoiseGradientConfig cfg;
  40. FastNoiseGem::FastNoiseGradientConfig componentConfig;
  41. AZStd::unique_ptr<AZ::Component> transformComponent {noiseEntity->CreateComponent<AzFramework::TransformComponent>()};
  42. AZStd::unique_ptr<AZ::Component> boxShapeComponent {noiseEntity->CreateComponent(LmbrCentral::BoxShapeComponentTypeId)};
  43. AZStd::unique_ptr<AZ::Component> gradientTransformComponent {noiseEntity->CreateComponent<GradientSignal::GradientTransformComponent>()};
  44. AZStd::unique_ptr<AZ::Component> createdNoiseComponent {noiseEntity->CreateComponent<FastNoiseGem::FastNoiseGradientComponent>(cfg)};
  45. FastNoiseGem::FastNoiseGradientComponent* noiseComp = noiseEntity->FindComponent<FastNoiseGem::FastNoiseGradientComponent>();
  46. ASSERT_EQ(noiseComp, createdNoiseComponent.get());
  47. noiseComp->WriteOutConfig(&componentConfig);
  48. ASSERT_EQ(cfg, componentConfig);
  49. }
  50. TEST_F(FastNoiseTest, FastNoise_ComponentEbusWorksSuccessfully)
  51. {
  52. auto noiseEntity = AZStd::make_unique<AZ::Entity>("noise_entity");
  53. ASSERT_TRUE(noiseEntity != nullptr);
  54. AZStd::unique_ptr<AZ::Component> transformComponent {noiseEntity->CreateComponent<AzFramework::TransformComponent>()};
  55. AZStd::unique_ptr<AZ::Component> boxShapeComponent {noiseEntity->CreateComponent(LmbrCentral::BoxShapeComponentTypeId)};
  56. AZStd::unique_ptr<AZ::Component> gradientTransformComponent {noiseEntity->CreateComponent<GradientSignal::GradientTransformComponent>()};
  57. AZStd::unique_ptr<AZ::Component> createdNoiseComponent {noiseEntity->CreateComponent<FastNoiseGem::FastNoiseGradientComponent>()};
  58. noiseEntity->Init();
  59. noiseEntity->Activate();
  60. GradientSignal::GradientSampleParams params;
  61. float sample = -1.0f;
  62. GradientSignal::GradientRequestBus::EventResult(sample, noiseEntity->GetId(),
  63. &GradientSignal::GradientRequestBus::Events::GetValue, params);
  64. ASSERT_TRUE(sample >= 0.0f);
  65. ASSERT_TRUE(sample <= 1.0f);
  66. noiseEntity->Deactivate();
  67. }
  68. TEST_F(FastNoiseTest, FastNoise_VerifyGetValueAndGetValuesMatch)
  69. {
  70. const float shapeHalfBounds = 128.0f;
  71. auto noiseEntity = AZStd::make_unique<AZ::Entity>("noise_entity");
  72. ASSERT_TRUE(noiseEntity != nullptr);
  73. AZStd::unique_ptr<AZ::Component> transformComponent {noiseEntity->CreateComponent<AzFramework::TransformComponent>()};
  74. AZStd::unique_ptr<AZ::Component> gradientTransformComponent {noiseEntity->CreateComponent<GradientSignal::GradientTransformComponent>()};
  75. // Create a Box Shape to map our gradient into
  76. LmbrCentral::BoxShapeConfig boxConfig(AZ::Vector3(shapeHalfBounds * 2.0f));
  77. AZStd::unique_ptr<AZ::Component> boxComponent {noiseEntity->CreateComponent(LmbrCentral::BoxShapeComponentTypeId)};
  78. boxComponent->SetConfiguration(boxConfig);
  79. // Create a Fast Noise component with an adjusted frequency. (The defaults of Perlin noise with frequency=1.0 would cause us
  80. // to always get back the same noise value)
  81. FastNoiseGem::FastNoiseGradientConfig cfg;
  82. cfg.m_frequency = 0.01f;
  83. AZStd::unique_ptr<AZ::Component> createdNoiseComponent {noiseEntity->CreateComponent<FastNoiseGem::FastNoiseGradientComponent>(cfg)};
  84. noiseEntity->Init();
  85. noiseEntity->Activate();
  86. // Create a gradient sampler and run through a series of points to see if they match expectations.
  87. UnitTest::GradientSignalTestHelpers::CompareGetValueAndGetValues(noiseEntity->GetId(), -shapeHalfBounds, shapeHalfBounds);
  88. noiseEntity->Deactivate();
  89. }
  90. // This uses custom test / benchmark hooks so that we can load LmbrCentral and GradientSignal Gems.
  91. AZ_UNIT_TEST_HOOK(new UnitTest::FastNoiseTestEnvironment, UnitTest::FastNoiseBenchmarkEnvironment);