// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. #pragma once #include "ModelBase.h" #include "Engine/Graphics/Models/Config.h" #include "Engine/Graphics/Models/SkeletonData.h" #include "Engine/Graphics/Models/SkinnedModelLOD.h" class StreamSkinnedModelLODTask; /// /// Skinned model asset that contains model object made of meshes that can be rendered on the GPU using skeleton bones skinning. /// API_CLASS(NoSpawn) class FLAXENGINE_API SkinnedModel : public ModelBase { DECLARE_BINARY_ASSET_HEADER(SkinnedModel, 4); friend SkinnedMesh; friend StreamSkinnedModelLODTask; private: int32 _loadedLODs = 0; StreamSkinnedModelLODTask* _streamingTask = nullptr; public: /// /// Model level of details. The first entry is the highest quality LOD0 followed by more optimized versions. /// API_FIELD(ReadOnly) Array> LODs; /// /// The skeleton bones hierarchy. /// SkeletonData Skeleton; public: /// /// Finalizes an instance of the class. /// ~SkinnedModel(); public: /// /// Gets a value indicating whether this instance is initialized. /// FORCE_INLINE bool IsInitialized() const { return LODs.HasItems(); } /// /// Gets the amount of loaded model LODs. /// API_PROPERTY() FORCE_INLINE int32 GetLoadedLODs() const { return _loadedLODs; } /// /// Determines whether the specified index is a valid LOD index. /// /// The index. /// True if the specified index is a valid LOD index, otherwise false. FORCE_INLINE bool IsValidLODIndex(int32 index) const { return Math::IsInRange(index, 0, LODs.Count() - 1); } /// /// Clamps the index of the LOD to be valid for rendering (only loaded LODs). /// /// The index. /// The resident LOD index. FORCE_INLINE int32 ClampLODIndex(int32 index) const { return Math::Clamp(index, HighestResidentLODIndex(), LODs.Count() - 1); } /// /// Gets index of the highest resident LOD (may be equal to LODs.Count if no LOD has been uploaded). Note: LOD=0 is the highest (top quality) /// FORCE_INLINE int32 HighestResidentLODIndex() const { return GetLODsCount() - _loadedLODs; } /// /// Determines whether any LOD has been initialized. /// /// True if any LOD has been initialized, otherwise false. bool HasAnyLODInitialized() const; /// /// Determines whether this model can be rendered. /// /// True if can render that model, otherwise false. FORCE_INLINE bool CanBeRendered() const { return _loadedLODs > 0; } /// /// Gets the skeleton nodes hierarchy. /// API_PROPERTY() const Array& GetNodes() const { return Skeleton.Nodes; } /// /// Gets the skeleton bones hierarchy. /// API_PROPERTY() const Array& GetBones() const { return Skeleton.Bones; } /// /// Finds the node with the given name. /// /// The name of the node. /// The index of the node or -1 if not found. API_FUNCTION() FORCE_INLINE int32 FindNode(const StringView& name) { return Skeleton.FindNode(name); } /// /// Finds the bone with the given name. /// /// The name of the node used by the bone. /// The index of the bone or -1 if not found. API_FUNCTION() FORCE_INLINE int32 FindBone(const StringView& name) { return FindBone(FindNode(name)); } /// /// Finds the bone that is using a given node index. /// /// The index of the node. /// The index of the bone or -1 if not found. API_FUNCTION() FORCE_INLINE int32 FindBone(int32 nodeIndex) { return Skeleton.FindBone(nodeIndex); } /// /// Gets the blend shapes names used by the skinned model meshes (from LOD 0 only). /// API_PROPERTY() Array GetBlendShapes(); public: /// /// Requests the LOD data asynchronously (creates task that will gather chunk data or null if already here). /// /// Index of the LOD. /// Task that will gather chunk data or null if already here. ContentLoadTask* RequestLODDataAsync(int32 lodIndex); /// /// Gets the model LOD data (links bytes). /// /// Index of the LOD. /// The data (may be missing if failed to get it). void GetLODData(int32 lodIndex, BytesContainer& data) const; public: /// /// Determines if there is an intersection between the SkinnedModel and a Ray in given world using given instance. /// /// The ray to test /// World to test /// When the method completes, contains the distance of the intersection (if any valid). /// When the method completes, contains the intersection surface normal vector (if any valid). /// Mesh, or null /// Level Of Detail index /// True whether the two objects intersected bool Intersects(const Ray& ray, const Matrix& world, float& distance, Vector3& normal, SkinnedMesh** mesh, int32 lodIndex = 0); /// /// Gets the model bounding box in custom matrix world space (rig pose transformed by matrix, not animated). /// /// The transformation matrix. /// The Level Of Detail index. /// The bounding box. API_FUNCTION() BoundingBox GetBox(const Matrix& world, int32 lodIndex = 0) const; /// /// Gets the model bounding box in local space (rig pose, not animated). /// /// The Level Of Detail index. /// The bounding box. API_FUNCTION() BoundingBox GetBox(int32 lodIndex = 0) const; public: /// /// Draws the meshes. Binds vertex and index buffers and invokes the draw calls. /// /// The GPU context to draw with. /// The Level Of Detail index. void Render(GPUContext* context, int32 lodIndex = 0) { LODs[lodIndex].Render(context); } /// /// Draws the model. /// /// The rendering context. /// The packed drawing info data. void Draw(RenderContext& renderContext, const SkinnedMesh::DrawInfo& info); public: /// /// Setups the model LODs collection including meshes creation. /// /// The meshes count per lod array (amount of meshes per LOD). /// True if failed, otherwise false. API_FUNCTION() bool SetupLODs(const Span& meshesCountPerLod); /// /// Setups the skinned model skeleton. Uses the same nodes layout for skeleton bones and calculates the offset matrix by auto. /// /// The nodes hierarchy. The first node must be a root one (with parent index equal -1). /// True if failed, otherwise false. API_FUNCTION() bool SetupSkeleton(const Array& nodes); /// /// Setups the skinned model skeleton. /// /// The nodes hierarchy. The first node must be a root one (with parent index equal -1). /// The bones hierarchy. /// If true then the OffsetMatrix for each bone will be auto-calculated by the engine, otherwise the provided values will be used. /// True if failed, otherwise false. API_FUNCTION() bool SetupSkeleton(const Array& nodes, const Array& bones, bool autoCalculateOffsetMatrix); #if USE_EDITOR /// /// Saves this asset to the file. Supported only in Editor. /// /// If you use saving with the GPU mesh data then the call has to be provided from the thread other than the main game thread. /// True if save also GPU mesh buffers, otherwise will keep data in storage unmodified. Valid only if saving the same asset to the same location and it's loaded. /// The custom asset path to use for the saving. Use empty value to save this asset to its own storage location. Can be used to duplicate asset. Must be specified when saving virtual asset. /// True if cannot save data, otherwise false. API_FUNCTION() bool Save(bool withMeshDataFromGpu = false, const StringView& path = StringView::Empty); #endif private: /// /// Initializes this skinned model to an empty collection of meshes. Ensure to init SkeletonData manually after the call. /// /// The meshes count per lod array (amount of meshes per LOD). /// True if failed, otherwise false. bool Init(const Span& meshesCountPerLod); public: // [ModelBase] void SetupMaterialSlots(int32 slotsCount) override; int32 GetLODsCount() const override; void GetMeshes(Array& meshes, int32 lodIndex = 0) override; void InitAsVirtual() override; #if USE_EDITOR void GetReferences(Array& output) const override; #endif // [StreamableResource] int32 GetMaxResidency() const override; int32 GetCurrentResidency() const override; int32 GetAllocatedResidency() const override; bool CanBeUpdated() const override; Task* UpdateAllocation(int32 residency) override; Task* CreateStreamingTask(int32 residency) override; protected: // [ModelBase] LoadResult load() override; void unload(bool isReloading) override; bool init(AssetInitData& initData) override; AssetChunksFlag getChunksToPreload() const override; };