Agrarsense
InstancedActor.h
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
6#pragma once
7
8#include "CoreMinimal.h"
9#include "GameFramework/Actor.h"
10#include "Engine/EngineTypes.h"
11#include "Engine/StaticMesh.h"
12#include "Components/StaticMeshComponent.h"
13#include "Internationalization/Text.h"
14
18
19#include "InstancedActor.generated.h"
20
36UCLASS()
37class AGRARSENSE_API AInstancedActor : public AActor, public IInteractable, public IDestroyable, public IActorInformation
38{
39 GENERATED_BODY()
40
41public:
42
43 AInstancedActor(const FObjectInitializer& ObjectInitializer);
44
45 virtual void BeginPlay() override;
46
47 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
48
53 UFUNCTION(BlueprintCallable)
54 bool AddToInstancedRendering();
55
60 UFUNCTION(BlueprintCallable)
61 void RemoveFromInstancedRendering(bool EnableStaticMeshComponent = true);
62
66 UFUNCTION(BlueprintCallable)
67 void UpdateTransformPosition();
68
73 void InstanceAdded(int32 CompIndex, int32 InstanceNum);
74
79 void UpdateIndex(int32 UpdatedIndex);
80
85 UFUNCTION(BlueprintCallable)
86 void SetTypeAndModel(const FString& Type, const FString& Model)
87 {
88 TypeID = Type;
89 ModelID = Model;
90 }
91
92 /*
93 * Set whether InstancedRenderer should update this InstancedActor transform automatically.
94 * If this InstancedActor is not added to InstancedRenderer, this will just return.
95 */
96 UFUNCTION(BlueprintCallable)
97 void SetUpdateTransformAutomatically(bool UpdateAutomatically);
98
99 /*
100 * Get whether this actor is a tree.
101 */
102 UFUNCTION(BlueprintCallable)
103 bool IsTreeActor() const;
104
109 void UpdatePreviousTransform(const FTransform& NewTransform)
110 {
111 PreviousTransform = NewTransform;
112 }
113
118 UFUNCTION(BlueprintPure)
119 bool IsAddedToInstancedRendering() const
120 {
121 return AddedToInstancedRenderer;
122 }
123
124 UFUNCTION(BlueprintPure)
125 UStaticMeshComponent* GetStaticMeshComponent() const
126 {
127 return StaticMeshComponent;
128 }
129
130 UFUNCTION(BlueprintPure)
131 int GetInstanceStartCullDistance() const
132 {
133 return InstanceStartCullDistanceInCm;
134 }
135
136 UFUNCTION(BlueprintPure)
137 int GetInstanceEndCullDistance() const
138 {
139 return InstanceEndCullDistanceInCm;
140 }
141
142 UFUNCTION(BlueprintPure)
143 bool UpdateTransformAutomatically() const
144 {
145 return ShouldUpdateTransformAutomatically;
146 }
147
148 UFUNCTION(BlueprintCallable)
149 FString GetTypeIDString() const
150 {
151 return TypeID;
152 }
153
154 UFUNCTION(BlueprintCallable)
155 FString GetModelIDString() const
156 {
157 return ModelID;
158 }
159
161 {
162 return ComponentIndex;
163 }
164
166 {
167 return InstanceNumber;
168 }
169
171 {
172 return AlternativeMaterial;
173 }
174
176 {
177 return CustomDepthSetup;
178 }
179
181 {
182 return AllowWorldPositionOffsetDisable;
183 }
184
185 FTransform GetPreviousTransform() const
186 {
187 return PreviousTransform;
188 }
189
190 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
191 UStaticMeshComponent* StaticMeshComponent = nullptr;
192
193 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
194 bool AllowWorldPositionOffsetDisable = false;
195
196 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
197 bool AddToInstancedRenderer = true;
198
199 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
200 int InstanceStartCullDistanceInCm = 45000;
201
202 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
203 int InstanceEndCullDistanceInCm = 46500;
204
205 // Should this actor transform be automatically updated (more expensive)
206 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
207 bool ShouldUpdateTransformAutomatically = false;
208
209 // If we have multiple actors with same mesh but with different material(s), this should be set to true!
210 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
211 bool AlternativeMaterial = false;
212
213 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
214 FText InteractableName;
215
216 virtual FText GetInteractableName_Implementation() const override
217 {
218 return InteractableName;
219 }
220
221 virtual FTransform GetInteractableWorldTransform_Implementation() const override
222 {
223 return GetActorTransform();
224 }
225
226 virtual FString GetActorID_Implementation() const override
227 {
228 return ActorID;
229 }
230
231 virtual FString GetActorName_Implementation() const override
232 {
233 return ActorName;
234 }
235
236 virtual void SetActorName_Implementation(const FString& NewActorName) override
237 {
238 ActorName = NewActorName;
239 }
240
241 virtual void SetActorIDAndName_Implementation(const FString& NewActorName, const FString& NewID) override
242 {
243 ActorName = NewActorName;
244 ActorID = NewID;
245 SetAndValidateActorIDAndName(ActorName, ActorID, TWeakObjectPtr<AActor>(this));
246 }
247
248private:
249
250 UPROPERTY(VisibleAnywhere)
251 FString ActorName;
252
253 UPROPERTY(VisibleAnywhere)
254 FString ActorID;
255
256 FTransform PreviousTransform;
257
258 int32 ComponentIndex = 9999999;
259
260 int32 InstanceNumber = 9999999;
261
262 bool AddedToInstancedRenderer = false;
263
264 FString TypeID = "none";
265
266 FString ModelID = "none";
267
268 bool CustomDepthSetup = false;
269
270};
int GetInstanceIndex() const
virtual void SetActorIDAndName_Implementation(const FString &NewActorName, const FString &NewID) override
FTransform GetPreviousTransform() const
bool HasAlternativeMaterial() const
int GetComponentIndex() const
virtual void SetActorName_Implementation(const FString &NewActorName) override
virtual FString GetActorID_Implementation() const override
virtual FTransform GetInteractableWorldTransform_Implementation() const override
void UpdatePreviousTransform(const FTransform &NewTransform)
bool IsCustomDepthSetup() const
bool GetAllowWorldPositionOffsetDisable() const
virtual FString GetActorName_Implementation() const override