// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Math/Transform.h"
#include "Engine/Core/Math/BoundingSphere.h"
#include "Engine/Core/Math/Half.h"
#include "Engine/Core/Math/Matrix.h"
///
/// Foliage instanced mesh instance. Packed data with very little of logic. Managed by the foliage chunks and foliage actor itself.
///
API_STRUCT(NoPod, NoDefault) struct FLAXENGINE_API FoliageInstance
{
DECLARE_SCRIPTING_TYPE_MINIMAL(FoliageInstance);
///
/// The local-space transformation of the mesh relative to the foliage actor.
///
API_FIELD() Transform Transform;
///
/// The cached instance bounds (in world space).
///
API_FIELD() BoundingSphere Bounds;
///
/// The previous frame index. In sync with Engine::FrameCount used to detect new frames and rendering gaps to reset state.
///
uint64 DrawStatePrevFrame = 0;
///
/// The foliage type index. Foliage types are hold in foliage actor and shared by instances using the same model.
///
API_FIELD() uint16 Type;
///
/// The lightmap index for the foliage instance. -1 if unused.
///
int8 LightmapTextureIndex = -1;
///
/// The previous frame model LOD index used. It's locked during LOD transition to cache the transition start LOD.
///
char DrawStatePrevLOD = -1;
///
/// The LOD transition timer.
///
byte DrawStateLODTransition = 255;
///
/// Flag used to indicate whether CachedDrawWorld contains valid data.
///
byte CachedDrawWorldValid = 0;
///
/// The per-instance random value from range [0;1].
///
API_FIELD() float Random;
///
/// The cull distance for this instance.
///
float CullDistance;
///
/// Lightmap UVs area that entry occupies (packed Rectangle into Half4).
///
Half4 LightmapUVsArea;
///
/// Cached local-to-world transformation matrix for the instance used during rendering. Relative to the last rendering context origin. Valid only if CachedDrawWorldValid flag is set.
///
Matrix CachedDrawWorld;
public:
bool operator==(const FoliageInstance& v) const
{
return Type == v.Type && Random == v.Random && Transform == v.Transform;
}
///
/// Determines whether this foliage instance has valid lightmap data.
///
FORCE_INLINE bool HasLightmap() const
{
return LightmapTextureIndex != INVALID_INDEX;
}
///
/// Removes the lightmap data from the foliage instance.
///
FORCE_INLINE void RemoveLightmap()
{
LightmapTextureIndex = INVALID_INDEX;
}
};