/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #pragma once #include #include #include #include #include #include #include #include namespace NvCloth { class ActorClothColliders; class ActorClothSkinning; class ClothConstraints; class ClothDebugDisplay; //! Class that applies cloth simulation to Static Meshes and Actors //! by reading their data and modifying the render nodes in real time. class ClothComponentMesh : public LmbrCentral::MeshModificationNotificationBus::Handler , public AZ::TransformNotificationBus::Handler , public Physics::WindNotificationsBus::Handler { public: AZ_RTTI(ClothComponentMesh, "{15A0F10C-6248-4CE4-A6FD-0E2D8AFCFEE8}"); ClothComponentMesh(AZ::EntityId entityId, const ClothConfiguration& config); ~ClothComponentMesh(); AZ_DISABLE_COPY_MOVE(ClothComponentMesh); // Rendering data. // It stores the tangent space information of each vertex, which is calculated every frame. struct RenderData { AZStd::vector m_particles; AZStd::vector m_tangents; AZStd::vector m_bitangents; AZStd::vector m_normals; }; const RenderData& GetRenderData() const; RenderData& GetRenderData(); void UpdateConfiguration(AZ::EntityId entityId, const ClothConfiguration& config); protected: // Functions used to setup and tear down cloth component mesh void Setup(AZ::EntityId entityId, const ClothConfiguration& config); void TearDown(); // ICloth notifications void OnPreSimulation(ClothId clothId, float deltaTime); void OnPostSimulation(ClothId clothId, float deltaTime, const AZStd::vector& updatedParticles); // LmbrCentral::MeshModificationNotificationBus::Handler overrides ... void ModifyMesh(size_t lodIndex, size_t primitiveIndex, IRenderMesh* renderMesh) override; // AZ::TransformNotificationBus::Handler overrides ... void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override; // Physics::WindNotificationsBus::Handler overrides ... void OnGlobalWindChanged() override; void OnWindChanged(const AZ::Aabb& aabb) override; private: // Structure with render data as needed by IRenderMesh. // It stores the final particles that are going to be sent for rendering. struct InternalRenderData { AZStd::vector m_particles; AZStd::vector m_normals; AZStd::vector m_quatTangents; AZStd::vector m_tangents; }; void UpdateSimulationCollisions(); void UpdateSimulationSkinning(); void UpdateSimulationConstraints(); void UpdateRenderData(const AZStd::vector& particles); void CopyInternalRenderData(); bool CreateCloth(); void ApplyConfigurationToCloth(); void MoveCloth(const AZ::Transform& worldTransform); void TeleportCloth(const AZ::Transform& worldTransform); AZ::Vector3 GetWindBusVelocity(); // Entity Id of the cloth component AZ::EntityId m_entityId; // Current position in world space AZ::Vector3 m_worldPosition; // Configuration parameters for cloth simulation ClothConfiguration m_config; // Instance of cloth simulation ICloth* m_cloth = nullptr; // Cloth event handlers ICloth::PreSimulationEvent::Handler m_preSimulationEventHandler; ICloth::PostSimulationEvent::Handler m_postSimulationEventHandler; // Use a double buffer of render data to always have access to the previous frame's data. // The previous frame's data is used to workaround that debug draw is one frame delayed. static const AZ::u32 RenderDataBufferSize = 2; AZ::u32 m_renderDataBufferIndex = 0; AZStd::array m_renderDataBuffer; // Render data as needed by IRenderMesh. InternalRenderData m_internalRenderData; // Vertex mapping between full mesh and simplified mesh used in cloth simulation. // Negative elements means the vertex has been removed. AZStd::vector m_meshRemappedVertices; // Information to map the simulation particles to render mesh nodes. MeshNodeInfo m_meshNodeInfo; // Original cloth information from the mesh. MeshClothInfo m_meshClothInfo; // Cloth Colliders from the character AZStd::unique_ptr m_actorClothColliders; // Cloth Skinning from the character AZStd::unique_ptr m_actorClothSkinning; AZ::u32 m_numberOfClothSkinningUpdates = 0; // Cloth Constraints AZStd::unique_ptr m_clothConstraints; AZStd::vector m_motionConstraints; AZStd::vector m_separationConstraints; AZStd::unique_ptr m_clothDebugDisplay; friend class ClothDebugDisplay; // Give access to data to draw debug information }; } // namespace NvCloth