Agrarsense
AgrarsenseConsoleCommands.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
10
11#include "Delegates/DelegateSignatureImpl.inl"
12#include "Framework/Commands/UIAction.h"
13#include "Engine/World.h"
14#include "Engine/Engine.h"
15#include "Engine/GameViewportClient.h"
16
17#if WITH_EDITOR
18#include "EngineUtils.h"
19#include "Engine/World.h"
20#include "Editor.h"
21#endif
22
24{
25 // Register custom console commands
26 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
27 TEXT("r.WorldRendering"),
28 TEXT("Toggle World rendering"),
29 FExecuteAction::CreateLambda([this]() { ToggleWorldRendering(); })
30 ));
31
32 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
33 TEXT("r.InstancedRendering"),
34 TEXT("Toggle Agrarsense Instanced rendering"),
35 FExecuteAction::CreateLambda([this]() { ToggleInstancedRendering(); })
36 ));
37
38#if WITH_EDITOR
39 // With following commands,
40 // you can hide/show all InstancedActors to improve Unreal Editor performance
41 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
42 TEXT("r.HideInstancedActors"),
43 TEXT("Hide all Agrarsense Instanced actors"),
44 FExecuteAction::CreateLambda([this]() { SetInstancedActorsVisibilityEditor(false); })
45 ));
46
47 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
48 TEXT("r.ShowInstancedActors"),
49 TEXT("Show all Agrarsense Instanced actors"),
50 FExecuteAction::CreateLambda([this]() { SetInstancedActorsVisibilityEditor(true); })
51 ));
52
53 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
54 TEXT("r.HideTrees"),
55 TEXT("Hide all Agrarsense Instanced Trees"),
56 FExecuteAction::CreateLambda([this]() { SetInstacedTreesVisibilityEditor(false); })
57 ));
58
59 ConsoleCommands.Add(MakeUnique<FAutoConsoleCommand>(
60 TEXT("r.ShowTrees"),
61 TEXT("Show all Agrarsense Instanced Trees"),
62 FExecuteAction::CreateLambda([this]() { SetInstacedTreesVisibilityEditor(true); })
63 ));
64#endif
65}
66
68{
69 // Unregister all added console commands
70 for (TUniquePtr<FAutoConsoleCommand>& Command : ConsoleCommands)
71 {
72 Command.Reset();
73 }
74}
75
77{
78 if (GEngine && GEngine->GameViewport)
79 {
80 GEngine->GameViewport->bDisableWorldRendering = !GEngine->GameViewport->bDisableWorldRendering;
81 }
82}
83
85{
86 if (GEngine && GEngine->GameViewport)
87 {
88 UWorld* World = GEngine->GameViewport->GetWorld();
89 if (World)
90 {
91 auto* InstanceRenderer = UAgrarsenseStatics::GetInstancedRenderer(World);
92 if (InstanceRenderer)
93 {
94 InstanceRenderer->SetInstancedRendering(!InstanceRenderer->IsRendering());
95 }
96 }
97 }
98}
99
101{
102#if WITH_EDITOR
103 UWorld* World = GEditor ? GEditor->GetEditorWorldContext().World() : nullptr;
104 if (!World)
105 {
106 return;
107 }
108
109 for (TActorIterator<AInstancedActor> ActorItr(World); ActorItr; ++ActorItr)
110 {
111 AInstancedActor* InstancedActor = *ActorItr;
112 if (InstancedActor)
113 {
114 UStaticMeshComponent* StaticMeshComponent = InstancedActor->GetStaticMeshComponent();
115 if (StaticMeshComponent)
116 {
117 StaticMeshComponent->SetVisibility(Visible);
118 }
119 }
120 }
121#endif
122}
123
125{
126#if WITH_EDITOR
127
128 TArray<FString> TreeNames = { "birch", "spruce", "pine", "alder", "willow" };
129
130 UWorld* World = GEditor ? GEditor->GetEditorWorldContext().World() : nullptr;
131 if (!World)
132 {
133 return;
134 }
135
136 for (TActorIterator<AInstancedActor> ActorItr(World); ActorItr; ++ActorItr)
137 {
138 AInstancedActor* InstancedActor = *ActorItr;
139 if (InstancedActor)
140 {
141 UStaticMeshComponent* StaticMeshComponent = InstancedActor->GetStaticMeshComponent();
142 if (StaticMeshComponent)
143 {
144 // This is not that fast but this is fine this usage..
145 bool found = false;
146 UStaticMesh* Mesh = StaticMeshComponent->GetStaticMesh();
147 if (Mesh)
148 {
149 FString MeshName = Mesh->GetName().ToLower();
150 for (size_t i = 0; i < TreeNames.Num(); i++)
151 {
152 if (MeshName.Contains(TreeNames[i]))
153 {
154 found = true;
155 break;
156 }
157 }
158 }
159
160 if (found)
161 {
162 StaticMeshComponent->SetVisibility(Visible);
163 }
164 }
165 }
166 }
167#endif
168}
UStaticMeshComponent * GetStaticMeshComponent() const
TArray< TUniquePtr< FAutoConsoleCommand > > ConsoleCommands
void SetInstancedActorsVisibilityEditor(bool Visible)
void SetInstacedTreesVisibilityEditor(bool Visible)
static AInstancedRenderer * GetInstancedRenderer(const UObject *WorldContextObject)