TerrainMacroMaterialComponent_MacroMaterialActivates.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. class MacroMaterialTests:
  7. setup_test = (
  8. "Setup successful",
  9. "Setup failed"
  10. )
  11. material_changed_not_called_when_inactive = (
  12. "OnTerrainMacroMaterialRegionChanged not called successfully",
  13. "OnTerrainMacroMaterialRegionChanged called when component inactive."
  14. )
  15. material_created = (
  16. "MaterialCreated called successfully",
  17. "MaterialCreated failed"
  18. )
  19. material_destroyed = (
  20. "MaterialDestroyed called successfully",
  21. "MaterialDestroyed failed"
  22. )
  23. material_recreated = (
  24. "MaterialCreated called successfully on second test",
  25. "MaterialCreated failed on second test"
  26. )
  27. material_changed_call_on_aabb_change = (
  28. "OnTerrainMacroMaterialRegionChanged called successfully",
  29. "Timed out waiting for OnTerrainMacroMaterialRegionChanged"
  30. )
  31. def TerrainMacroMaterialComponent_MacroMaterialActivates():
  32. """
  33. Summary:
  34. Load an empty level, create a MacroMaterialComponent and check assigning textures results in the correct callbacks.
  35. :return: None
  36. """
  37. import os
  38. import azlmbr.legacy.general as general
  39. import azlmbr.asset as asset
  40. import azlmbr.bus as bus
  41. import azlmbr.math as math
  42. import azlmbr.terrain as terrain
  43. import azlmbr.editor as editor
  44. import editor_python_test_tools.hydra_editor_utils as hydra
  45. from editor_python_test_tools.utils import Report
  46. from editor_python_test_tools.utils import TestHelper as helper
  47. from editor_python_test_tools.editor_entity_utils import EditorEntity
  48. material_created_called = False
  49. material_changed_called = False
  50. material_region_changed_called = False
  51. material_destroyed_called = False
  52. def create_entity_at(entity_name, components_to_add, x, y, z):
  53. entity = EditorEntity.create_editor_entity_at([x, y, z], entity_name)
  54. for component in components_to_add:
  55. entity.add_component(component)
  56. return entity
  57. def on_macro_material_created(args):
  58. nonlocal material_created_called
  59. material_created_called = True
  60. def on_macro_material_changed(args):
  61. nonlocal material_changed_called
  62. material_changed_called = True
  63. def on_macro_material_region_changed(args):
  64. nonlocal material_region_changed_called
  65. material_region_changed_called = True
  66. def on_macro_material_destroyed(args):
  67. nonlocal material_destroyed_called
  68. material_destroyed_called = True
  69. # Open a level.
  70. hydra.open_base_level()
  71. general.idle_wait_frames(1)
  72. # Set up a handler to wait for notifications from the TerrainSystem.
  73. handler = terrain.TerrainMacroMaterialAutomationBusHandler()
  74. handler.connect()
  75. handler.add_callback("OnTerrainMacroMaterialCreated", on_macro_material_created)
  76. handler.add_callback("OnTerrainMacroMaterialChanged", on_macro_material_changed)
  77. handler.add_callback("OnTerrainMacroMaterialRegionChanged", on_macro_material_region_changed)
  78. handler.add_callback("OnTerrainMacroMaterialDestroyed", on_macro_material_destroyed)
  79. macro_material_entity = create_entity_at("macro", ["Terrain Macro Material", "Axis Aligned Box Shape"], 0.0, 0.0, 0.0)
  80. # Check that no macro material callbacks happened. It should be "inactive" as it has no assets assigned.
  81. setup_success = not material_created_called and not material_changed_called and not material_region_changed_called and not material_destroyed_called
  82. Report.result(MacroMaterialTests.setup_test, setup_success)
  83. # Find the aabb component.
  84. aabb_component_type_id_type = azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast, 'FindComponentTypeIdsByEntityType', ["Axis Aligned Box Shape"], 0)[0]
  85. aabb_component_id = azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast, 'GetComponentOfType', macro_material_entity.id, aabb_component_type_id_type).GetValue()
  86. # Change the aabb dimensions
  87. material_region_changed_called = False
  88. box_dimensions_path = "Axis Aligned Box Shape|Box Configuration|Dimensions"
  89. editor.EditorComponentAPIBus(bus.Broadcast, "SetComponentProperty", aabb_component_id, box_dimensions_path, math.Vector3(1.0, 1.0, 1.0))
  90. # Check we don't receive a callback. The macro material component should be inactive as it has no images assigned.
  91. general.idle_wait_frames(1)
  92. Report.result(MacroMaterialTests.material_changed_not_called_when_inactive, material_region_changed_called == False)
  93. # Find the macro material component.
  94. macro_material_id_type = azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast, 'FindComponentTypeIdsByEntityType', ["Terrain Macro Material"], 0)[0]
  95. macro_material_component_id = azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast, 'GetComponentOfType', macro_material_entity.id, macro_material_id_type).GetValue()
  96. # Find a color image asset.
  97. color_image_path = os.path.join("assets", "textures", "image.png.streamingimage")
  98. color_image_asset = asset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetIdByPath", color_image_path, math.Uuid(), False)
  99. # Assign the image to the MacroMaterial component, which should result in a created message.
  100. material_created_called = False
  101. color_texture_path = "Configuration|Color Texture"
  102. editor.EditorComponentAPIBus(bus.Broadcast, "SetComponentProperty", macro_material_component_id, color_texture_path, color_image_asset)
  103. call_result = helper.wait_for_condition(lambda: material_created_called == True, 2.0)
  104. Report.result(MacroMaterialTests.material_created, call_result)
  105. # Find a normal image asset.
  106. normal_image_path = os.path.join("assets", "textures", "normal.png.streamingimage")
  107. normal_image_asset = asset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetIdByPath", normal_image_path, math.Uuid(), False)
  108. # Assign the normal image to the MacroMaterial component, which should result in a created message.
  109. material_created_called = False
  110. material_destroyed_called = False
  111. normal_texture_path = "Configuration|Normal Texture"
  112. editor.EditorComponentAPIBus(bus.Broadcast, "SetComponentProperty", macro_material_component_id, normal_texture_path, normal_image_asset)
  113. # Check the MacroMaterial was destroyed and recreated.
  114. destroyed_call_result = helper.wait_for_condition(lambda: material_destroyed_called == True, 2.0)
  115. Report.result(MacroMaterialTests.material_destroyed, destroyed_call_result)
  116. recreated_call_result = helper.wait_for_condition(lambda: material_created_called == True, 2.0)
  117. Report.result(MacroMaterialTests.material_recreated, recreated_call_result)
  118. # Change the aabb dimensions.
  119. box_dimensions_path = "Axis Aligned Box Shape|Box Configuration|Dimensions"
  120. editor.EditorComponentAPIBus(bus.Broadcast, "SetComponentProperty", aabb_component_id, box_dimensions_path, math.Vector3(1.0, 1.0, 1.0))
  121. # Check that a callback is received.
  122. region_changed_call_result = helper.wait_for_condition(lambda: material_region_changed_called == True, 2.0)
  123. Report.result(MacroMaterialTests.material_changed_call_on_aabb_change, region_changed_call_result)
  124. if __name__ == "__main__":
  125. from editor_python_test_tools.utils import Report
  126. Report.start_test(TerrainMacroMaterialComponent_MacroMaterialActivates)