Agrarsense
InstancedActor.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
6#include "InstancedActor.h"
7#include "InstancedRenderer.h"
9#include "Engine/World.h"
10
11#include "Kismet/GameplayStatics.h"
12#include "TimerManager.h"
13
14AInstancedActor::AInstancedActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
15{
16 StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
17 RootComponent = StaticMeshComponent;
18 PrimaryActorTick.bCanEverTick = false;
19 InteractableName = NSLOCTEXT("Agrarsense", "StaticInstancedActorInteractableName", "Prop");
20}
21
23{
24 Super::BeginPlay();
25
27 {
28 // Check if Custom Depth has been setup in Editor, if not Tagger.cpp needs to do this runtime.
29 CustomDepthSetup = (StaticMeshComponent->bRenderCustomDepth && StaticMeshComponent->CustomDepthStencilValue == 0) ? true : false;
30 }
31
32 PreviousTransform = GetActorTransform();
33
35}
36
37void AInstancedActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
38{
39 Super::EndPlay(EndPlayReason);
40
41 if (EndPlayReason != EEndPlayReason::Type::EndPlayInEditor)
42 {
44
46 {
47 StaticMeshComponent->UnregisterComponent();
48 StaticMeshComponent->DestroyComponent();
49 StaticMeshComponent = nullptr;
50 }
51 }
52}
53
54void AInstancedActor::RemoveFromInstancedRendering(bool EnableStaticMeshComponent)
55{
57 {
59 if (InstanceRenderer)
60 {
62 }
63 }
64
66
67 if (EnableStaticMeshComponent && StaticMeshComponent)
68 {
69 StaticMeshComponent->SetVisibility(true);
70 StaticMeshComponent->SetComponentTickEnabled(true);
71 }
72}
73
75{
77 {
78#if WITH_EDITOR
79 UE_LOG(LogTemp, Warning, TEXT("InstancedActor.cpp: Already added to instanced rendering."));
80#endif
81 return false;
82 }
83
85 {
86#if WITH_EDITOR
87 UE_LOG(LogTemp, Warning, TEXT("InstancedActor.cpp: %s Actor is not being renderered by UInstancedStaticMeshComponent. If you did not mean to do this, make sure AddToInstancedRenderer is set to true in blueprint."), *this->GetName());;
88#endif
89 return false;
90 }
91
93 if (InstanceRenderer)
94 {
95 return InstanceRenderer->AddActorToInstancedRendering(this);
96 }
97
98 return false;
99}
100
101void AInstancedActor::InstanceAdded(int32 CompIndex, int32 InstanceNum)
102{
103 // Hide StaticMeshComponent Visibility since InstancedRenderer
104 // is now resposible for rendering this Actor's mesh.
105 // Note. StaticMeshComponent is still resposible for collisions.
107 {
108 StaticMeshComponent->SetVisibility(false);
109 StaticMeshComponent->SetComponentTickEnabled(false);
110 }
111
112 ComponentIndex = CompIndex;
113 InstanceNumber = InstanceNum;
115}
116
117void AInstancedActor::UpdateIndex(int32 UpdatedIndex)
118{
119 if (UpdatedIndex > InstanceNumber)
120 {
121 return;
122 }
123
125}
126
128{
130 {
131 return;
132 }
133
134 AInstancedRenderer* InstanceRenderer = UAgrarsenseStatics::GetInstancedRenderer(GetWorld());
135 if (InstanceRenderer)
136 {
137 InstanceRenderer->UpdateInstanceTransform(ComponentIndex, InstanceNumber, GetActorTransform());
138 }
139}
140
142{
144 {
145 // No change, return
146 return;
147 }
148
149 ShouldUpdateTransformAutomatically = UpdateAutomatically;
150
151 AInstancedRenderer* InstanceRenderer = UAgrarsenseStatics::GetInstancedRenderer(GetWorld());
152 if (InstanceRenderer)
153 {
155 }
156}
157
159{
161 {
162 UStaticMesh* Mesh = StaticMeshComponent->GetStaticMesh();
163 if (Mesh)
164 {
165 FString Path = Mesh->GetPathName();
166 if (Path.Contains("Tree"))
167 {
168 return true;
169 }
170 }
171 }
172
173 return false;
174}
void UpdateTransformPosition()
void InstanceAdded(int32 CompIndex, int32 InstanceNum)
void UpdateIndex(int32 UpdatedIndex)
UStaticMeshComponent * StaticMeshComponent
FTransform PreviousTransform
AInstancedActor(const FObjectInitializer &ObjectInitializer)
void SetUpdateTransformAutomatically(bool UpdateAutomatically)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
bool AddedToInstancedRenderer
bool ShouldUpdateTransformAutomatically
void RemoveFromInstancedRendering(bool EnableStaticMeshComponent=true)
virtual void BeginPlay() override
bool AddToInstancedRendering()
bool IsTreeActor() const
bool AddActorToInstancedRendering(AInstancedActor *InstancedActor)
void RemoveInstance(int32 ComponentIndex, int32 InstanceNumber)
void UpdateInstanceTransform(int32 ComponentIndex, int32 InstanceNumber, const FTransform &NewTransform)
void AddOrRemoveActorFromAutomaticTransformUpdates(bool Add, AInstancedActor *InstancedActor)
static AInstancedRenderer * GetInstancedRenderer(const UObject *WorldContextObject)