// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#include "../BinaryAsset.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Graphics/Models/Config.h"
#include "Engine/Graphics/Models/MaterialSlot.h"
#include "Engine/Streaming/StreamableResource.h"
// Note: we use the first chunk as a header, next is the highest quality lod and then lower ones
//
// Example:
// Chunk 0: Header
// Chunk 1: LOD0
// Chunk 2: LOD1
// ..
// Chunk 15: SDF
#define MODEL_LOD_TO_CHUNK_INDEX(lod) (lod + 1)
class MeshBase;
struct RenderContextBatch;
///
/// Base class for asset types that can contain a model resource.
///
API_CLASS(Abstract, NoSpawn) class FLAXENGINE_API ModelBase : public BinaryAsset, public StreamableResource
{
DECLARE_ASSET_HEADER(ModelBase);
friend MeshBase;
friend class StreamModelLODTask;
protected:
bool _initialized = false;
int32 _loadedLODs = 0;
StreamModelLODTask* _streamingTask = nullptr;
public:
///
/// The Sign Distant Field (SDF) data for the model.
///
API_STRUCT() struct SDFData
{
DECLARE_SCRIPTING_TYPE_MINIMAL(SDFData);
///
/// The SDF volume texture (merged all meshes).
///
API_FIELD() GPUTexture* Texture = nullptr;
///
/// The transformation scale from model local-space to the generated SDF texture space (local-space -> uv).
///
API_FIELD() Float3 LocalToUVWMul;
///
/// Amount of world-units per SDF texture voxel.
///
API_FIELD() float WorldUnitsPerVoxel;
///
/// The transformation offset from model local-space to the generated SDF texture space (local-space -> uv).
///
API_FIELD() Float3 LocalToUVWAdd;
///
/// The maximum distance stored in the SDF texture. Used to rescale normalized SDF into world-units (in model local space).
///
API_FIELD() float MaxDistance;
///
/// The bounding box of the SDF texture in the model local-space.
///
API_FIELD() Float3 LocalBoundsMin;
///
/// The SDF texture resolution scale used for building texture.
///
API_FIELD() float ResolutionScale = 1.0f;
///
/// The bounding box of the SDF texture in the model local-space.
///
API_FIELD() Float3 LocalBoundsMax;
///
/// The model LOD index used for the building.
///
API_FIELD() int32 LOD = 6;
};
protected:
explicit ModelBase(const SpawnParams& params, const AssetInfo* info, StreamingGroup* group)
: BinaryAsset(params, info)
, StreamableResource(group)
{
}
public:
~ModelBase();
///
/// The minimum screen size to draw this model (the bottom limit). Used to cull small models. Set to 0 to disable this feature.
///
API_FIELD() float MinScreenSize = 0.0f;
///
/// The list of material slots.
///
API_FIELD(ReadOnly) Array MaterialSlots;
///
/// Gets the amount of the material slots used by this model asset.
///
API_PROPERTY() int32 GetMaterialSlotsCount() const
{
return MaterialSlots.Count();
}
///
/// Gets a value indicating whether this instance is initialized.
///
FORCE_INLINE bool IsInitialized() const
{
return _initialized;
}
///
/// 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(), GetLODsCount() - 1);
}
///
/// Gets index of the highest resident LOD (it 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;
}
///
/// Gets the amount of loaded model LODs.
///
API_PROPERTY() FORCE_INLINE int32 GetLoadedLODs() const
{
return _loadedLODs;
}
///
/// Determines whether this model can be rendered.
///
FORCE_INLINE bool CanBeRendered() const
{
return _loadedLODs > 0;
}
///
/// Resizes the material slots collection. Updates meshes that were using removed slots.
///
API_FUNCTION() virtual void SetupMaterialSlots(int32 slotsCount);
///
/// Gets the material slot by the name.
///
/// The slot name.
/// The material slot with the given name or null if cannot find it (asset may be not loaded yet).
API_FUNCTION() MaterialSlot* GetSlot(const StringView& name);
///
/// Gets amount of the level of details in the model.
///
virtual int32 GetLODsCount() const = 0;
///
/// Gets the meshes for a particular LOD index.
///
virtual void GetMeshes(Array& meshes, int32 lodIndex = 0) = 0;
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 (it may be missing if failed to get it).
void GetLODData(int32 lodIndex, BytesContainer& data) const;
public:
// [BinaryAsset]
void CancelStreaming() override;
#if USE_EDITOR
void GetReferences(Array& assets, Array& files) const override;
#endif
// [StreamableResource]
int32 GetCurrentResidency() const override;
bool CanBeUpdated() const override;
Task* UpdateAllocation(int32 residency) override;
Task* CreateStreamingTask(int32 residency) override;
void CancelStreamingTasks() override;
protected:
// [BinaryAsset]
void unload(bool isReloading) override;
};