SimulationLOD in Mass Framework

How does SimulationLOD work in Mass Framework? Mass Framework is a ECS for managing crowds and traffic in Unreal 5.0 and above.

SimulationLOD with MassGameplay built-in resources

  • SimulationLOD Trait

    • Built-in MassGameplay.
    • Trait located At UE5/Engine/Plugins/Runtime/MassGameplay/Source/MassLOD.
    • SimulationLOD trait shares the same Trait file with LODCollector.
    UCLASS(meta = (DisplayName = "SimulationLOD"))
    class MASSLOD_API UMassSimulationLODTrait : public UMassEntityTraitBase
    {
    	GENERATED_BODY()
    
    	UPROPERTY(Category = "Config", EditAnywhere)
    	FMassSimulationLODParameters Params;
    
    	UPROPERTY(Category = "Config", EditAnywhere)
    	bool bEnableVariableTicking = false;
    
    	UPROPERTY(Category = "Config", EditAnywhere, meta = (EditCondition = "bEnableVariableTicking", EditConditionHides))
    	FMassSimulationVariableTickParameters VariableTickParams;
    
    protected:
    	virtual void BuildTemplate(FMassEntityTemplateBuildContext& BuildContext, const UWorld& World) const override;
    };
    void UMassSimulationLODTrait::BuildTemplate(FMassEntityTemplateBuildContext& BuildContext, const UWorld& World) const
    {
    	BuildContext.RequireFragment<FMassViewerInfoFragment>();
    	BuildContext.RequireFragment<FTransformFragment>();
    
    	FMassSimulationLODFragment& LODFragment = BuildContext.AddFragment_GetRef<FMassSimulationLODFragment>();
    
    	// Start all simulation LOD in the Off 
    	if(Params.bSetLODTags || bEnableVariableTicking)
    	{
    		LODFragment.LOD = EMassLOD::Off;
    		BuildContext.AddTag<FMassOffLODTag>();
    	}
    
    	FMassEntityManager& EntityManager = UE::Mass::Utils::GetEntityManagerChecked(World);
    
    	FConstSharedStruct ParamsFragment = EntityManager.GetOrCreateConstSharedFragment(Params);
    	BuildContext.AddConstSharedFragment(ParamsFragment);
    
    	uint32 ParamsHash = UE::StructUtils::GetStructCrc32(FConstStructView::Make(Params));
    	FSharedStruct SharedFragment = EntityManager.GetOrCreateSharedFragmentByHash<FMassSimulationLODSharedFragment>(ParamsHash, Params);
    	BuildContext.AddSharedFragment(SharedFragment);
    
    	// Variable ticking from simulation LOD
    	if(bEnableVariableTicking)
    	{
    		BuildContext.AddFragment<FMassSimulationVariableTickFragment>();
    		BuildContext.AddChunkFragment<FMassSimulationVariableTickChunkFragment>();
    
    		FConstSharedStruct VariableTickParamsFragment = EntityManager.GetOrCreateConstSharedFragment(VariableTickParams);
    		BuildContext.AddConstSharedFragment(VariableTickParamsFragment);
    
    		uint32 VariableTickParamsHash = UE::StructUtils::GetStructCrc32(FConstStructView::Make(VariableTickParams));
    		FSharedStruct VariableTickSharedFragment = EntityManager.GetOrCreateSharedFragmentByHash<FMassSimulationVariableTickSharedFragment>(VariableTickParamsHash, VariableTickParams);
    		BuildContext.AddSharedFragment(VariableTickSharedFragment);
    	}
    }