Skip to content

Commit

Permalink
move texture on conveyor belt mesh (#21)
Browse files Browse the repository at this point in the history
* comment out asserts
* add moving texture to belt
* add conveyor belt asset
* revert comment out asserts; remove debug code
* add entity id; change v->u in texture move
* add material selector to belt physics
* add texture scaling factor
* remove obsolete assets
---------

Signed-off-by: Jan Hanca <[email protected]>
Signed-off-by: Michał Pełka <[email protected]>
  • Loading branch information
jhanca-robotecai authored and michalpelka committed Jun 29, 2023
1 parent 1b4583c commit 7dd700f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gems/ROS2/Code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ly_add_target(
Gem::Atom_RPI.Public
Gem::Atom_Feature_Common.Static
Gem::Atom_Component_DebugCamera.Static
Gem::AtomLyIntegration_CommonFeatures.Editor.Static
Gem::StartingPointInput
Gem::PhysX.Static
Gem::LmbrCentral.API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,59 @@
*
*/
#include "ConveyorBeltComponentKinematic.h"
#include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Math/Matrix3x3.h>
#include <AzCore/Math/Transform.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzFramework/Physics/Common/PhysicsSimulatedBody.h>
#include <AzFramework/Physics/Components/SimulatedBodyComponentBus.h>
#include <AzFramework/Physics/Material/PhysicsMaterialManager.h>
#include <AzFramework/Physics/Material/PhysicsMaterialSlots.h>
#include <AzFramework/Physics/PhysicsSystem.h>
#include <AzFramework/Physics/RigidBodyBus.h>
#include <LmbrCentral/Shape/SplineComponentBus.h>
#include <Source/RigidBodyComponent.h>
namespace ROS2
{
static AZ::Data::AssetId GetDefaultPhysicsMaterialAssetId()
{
// Used for Edit Context.
// When the physics material asset property doesn't have an asset assigned it
// will show "(default)" to indicate that the default material will be used.
if (auto* materialManager = AZ::Interface<Physics::MaterialManager>::Get())
{
if (AZStd::shared_ptr<Physics::Material> defaultMaterial = materialManager->GetDefaultMaterial())
{
return defaultMaterial->GetMaterialAsset().GetId();
}
}
return {};
}

void ConveyorBeltComponentKinematic::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<ConveyorBeltComponentKinematic>()
->Version(1)
->Field("BeltEntityId", &ConveyorBeltComponentKinematic::m_ConveyorEntityId)
->Field("Speed", &ConveyorBeltComponentKinematic::m_speed)
->Field("BeltWidth", &ConveyorBeltComponentKinematic::m_beltWidth)
->Field("SegmentLength", &ConveyorBeltComponentKinematic::m_segmentLength);
->Field("SegmentLength", &ConveyorBeltComponentKinematic::m_segmentLength)
->Field("TextureScale", &ConveyorBeltComponentKinematic::m_textureScale)
->Field("MaterialAsset", &ConveyorBeltComponentKinematic::m_materialAsset);
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<ConveyorBeltComponentKinematic>("Conveyor Belt Component Kinematic", "Conveyor Belt Component")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Category, "ROS2")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
->DataElement(
AZ::Edit::UIHandlers::EntityId,
&ConveyorBeltComponentKinematic::m_ConveyorEntityId,
"Conveyor Belt Entity",
"Entity of the conveyor belt")
->DataElement(
AZ::Edit::UIHandlers::Default, &ConveyorBeltComponentKinematic::m_speed, "Speed", "Speed of the conveyor belt")
->DataElement(AZ::Edit::UIHandlers::Default, &ConveyorBeltComponentKinematic::m_beltWidth, "Width", "Belt width")
Expand All @@ -40,7 +67,21 @@ namespace ROS2
&ConveyorBeltComponentKinematic::m_segmentLength,
"Segment Length",
"Length of simulated segments. Short segments causes larger number individual bodies to simulate and challenges "
"physics engine.");
"physics engine.")
->DataElement(
AZ::Edit::UIHandlers::Default,
&ConveyorBeltComponentKinematic::m_textureScale,
"Texture scale",
"Scale of the texture on conveyor belt.")
->DataElement(
AZ::Edit::UIHandlers::Default,
&ConveyorBeltComponentKinematic::m_materialAsset,
"Material",
"Material asset of the conveyor belt")
->Attribute(AZ::Edit::Attributes::DefaultAsset, &GetDefaultPhysicsMaterialAssetId)
->Attribute(AZ_CRC_CE("EditButton"), "")
->Attribute(AZ_CRC_CE("EditDescription"), "Open in Asset Editor")
->Attribute(AZ_CRC_CE("DisableEditButtonWhenNoAssetSelected"), true);
}
}
}
Expand All @@ -52,7 +93,7 @@ namespace ROS2

void ConveyorBeltComponentKinematic::Activate()
{
m_initilized = false;
m_initialized = false;
AZ::TickBus::Handler::BusConnect();
}

Expand All @@ -66,7 +107,7 @@ namespace ROS2
AzPhysics::SceneHandle sceneHandle, AzPhysics::SimulatedBodyHandle handle)
{
AzPhysics::SceneInterface* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
AZ_Assert(sceneInterface, "No scene intreface");
AZ_Assert(sceneInterface, "No scene interface");
auto* body = azdynamic_cast<AzPhysics::RigidBody*>(sceneInterface->GetSimulatedBodyFromHandle(sceneHandle, handle));
AZ_Assert(body, "No valid body found");
if (body)
Expand All @@ -83,15 +124,16 @@ namespace ROS2
AzPhysics::SceneHandle sceneHandle,
float normalizedLocation)
{
auto colliderConfiguration = AZStd::make_shared<Physics::ColliderConfiguration>();
colliderConfiguration->m_materialSlots.SetMaterialAsset(0, m_materialAsset);
auto shapeConfiguration =
AZStd::make_shared<Physics::BoxShapeConfiguration>(AZ::Vector3(m_segmentLength, m_beltWidth, m_segmentWidth));
const auto transform = GetTransformFromSpline(m_splineConsPtr, normalizedLocation);
AzPhysics::RigidBodyConfiguration conveyorSegmentRigidBodyConfig;
conveyorSegmentRigidBodyConfig.m_kinematic = true;
conveyorSegmentRigidBodyConfig.m_position = transform.GetTranslation();
conveyorSegmentRigidBodyConfig.m_orientation = transform.GetRotation();
conveyorSegmentRigidBodyConfig.m_colliderAndShapeData =
AzPhysics::ShapeColliderPair(AZStd::make_shared<Physics::ColliderConfiguration>(), shapeConfiguration);
conveyorSegmentRigidBodyConfig.m_colliderAndShapeData = AzPhysics::ShapeColliderPair(colliderConfiguration, shapeConfiguration);
conveyorSegmentRigidBodyConfig.m_computeCenterOfMass = true;
conveyorSegmentRigidBodyConfig.m_computeInertiaTensor = true;
conveyorSegmentRigidBodyConfig.m_startSimulationEnabled = true;
Expand All @@ -104,7 +146,7 @@ namespace ROS2
return AZStd::make_pair(normalizedLocation, handle);
}

void ConveyorBeltComponentKinematic::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
void ConveyorBeltComponentKinematic::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
AzPhysics::SystemInterface* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
AZ_Assert(physicsSystem, "No physics system");
Expand All @@ -113,9 +155,9 @@ namespace ROS2
AzPhysics::SceneHandle defaultSceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
AZ_Assert(defaultSceneHandle != AzPhysics::InvalidSceneHandle, "Invalid default physics scene handle");

// Initiliazation. Create segments and add to scene, attach post-simulation event, cache pointers,transform etc.
// Initialization. Create segments and add to scene, attach post-simulation event, cache pointers,transform etc.
// Strong assumption is that conveyor belt is not transformed after initialization.
if (!m_initilized)
if (!m_initialized)
{
m_splineTransform = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(m_splineTransform, m_entity->GetId(), &AZ::TransformBus::Events::GetWorldTM);
Expand Down Expand Up @@ -148,7 +190,7 @@ namespace ROS2
}
AZ_Printf("ConveyorBeltComponentKinematic", "Initial Number of segments: %d", m_ConveyorSegments.size());

m_initilized = true;
m_initialized = true;
}
}

Expand Down Expand Up @@ -183,6 +225,23 @@ namespace ROS2
return c.second == AzPhysics::InvalidSimulatedBodyHandle;
}),
m_ConveyorSegments.end());

// move texture
if (m_ConveyorEntityId.IsValid())
{
AZ::Render::MaterialAssignmentId materialId;
AZ::Render::MaterialComponentRequestBus::EventResult(
materialId, m_ConveyorEntityId, &AZ::Render::MaterialComponentRequestBus::Events::FindMaterialAssignmentId, -1, "Belt");

m_textureOffset += deltaTime * m_speed * m_textureScale;

AZ::Render::MaterialComponentRequestBus::Event(
m_ConveyorEntityId,
&AZ::Render::MaterialComponentRequestBus::Events::SetPropertyValueT<float>,
materialId,
"uv.offsetU",
m_textureOffset);
}
}
AZStd::pair<AZ::Vector3, AZ::Vector3> ConveyorBeltComponentKinematic::GetStartAndEndPointOfBelt(AZ::ConstSplinePtr splinePtr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <AzCore/std/containers/deque.h>
#include <AzFramework/Physics/Common/PhysicsEvents.h>
#include <AzFramework/Physics/Common/PhysicsSimulatedBodyEvents.h>
#include <AzFramework/Physics/Material/PhysicsMaterialAsset.h>
#include <AzFramework/Physics/PhysicsSystem.h>

namespace ROS2
Expand Down Expand Up @@ -91,20 +92,29 @@ namespace ROS2
float m_beltWidth = 1.0f;
//! Length of individual segments of the conveyor belt
float m_segmentLength = 1.0f;
//! Material of individual segments of the conveyor belt
AZ::Data::Asset<Physics::MaterialAsset> m_materialAsset;
//! Cache of created segments
AZStd::deque<AZStd::pair<float, AzPhysics::SimulatedBodyHandle>> m_ConveyorSegments;
//! Heigh of belt
static constexpr float m_segmentWidth = 0.1f;
//! Offset of the texture
float m_textureOffset = 0.0f;
//! Scaling factor of the texture
float m_textureScale = 1.0f;
//! Pointer to the spline
AZ::ConstSplinePtr m_splineConsPtr{ nullptr };
//! Real spline length
float m_splineLength{ -1.f };
float m_splineLength = -1.0f;
//! Transform from spline's local frame to world frame
AZ::Transform m_splineTransform;
//! Start and end point of the belt
AZ::Vector3 m_startPoint;
AZ::Vector3 m_endPoint;

bool m_initilized{ false };
//! Conveyor belt entity (used for texture movement)
AZ::EntityId m_ConveyorEntityId;

bool m_initialized{ false };
};
} // namespace ROS2

0 comments on commit 7dd700f

Please sign in to comment.