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 return false;
79 }
80
82 if (InstanceRenderer)
83 {
84 return InstanceRenderer->AddActorToInstancedRendering(this);
85 }
86
87 return false;
88}
89
90void AInstancedActor::InstanceAdded(int32 CompIndex, int32 InstanceNum)
91{
92 // Hide StaticMeshComponent Visibility since InstancedRenderer
93 // is now resposible for rendering this Actor's mesh.
94 // Note. StaticMeshComponent is still resposible for collisions.
96 {
97 StaticMeshComponent->SetVisibility(false);
98 StaticMeshComponent->SetComponentTickEnabled(false);
99 }
100
101 ComponentIndex = CompIndex;
102 InstanceNumber = InstanceNum;
104}
105
106void AInstancedActor::UpdateIndex(int32 UpdatedIndex)
107{
108 if (UpdatedIndex > InstanceNumber)
109 {
110 return;
111 }
112
114}
115
117{
119 {
120 return;
121 }
122
123 AInstancedRenderer* InstanceRenderer = UAgrarsenseStatics::GetInstancedRenderer(GetWorld());
124 if (InstanceRenderer)
125 {
126 InstanceRenderer->UpdateInstanceTransform(ComponentIndex, InstanceNumber, GetActorTransform());
127 }
128}
129
131{
133 {
134 UStaticMesh* Mesh = StaticMeshComponent->GetStaticMesh();
135 if (Mesh)
136 {
137 FString Path = Mesh->GetPathName();
138 if (Path.Contains("Tree"))
139 {
140 return true;
141 }
142 }
143 }
144
145 return false;
146}
void UpdateTransformPosition()
void InstanceAdded(int32 CompIndex, int32 InstanceNum)
void UpdateIndex(int32 UpdatedIndex)
UStaticMeshComponent * StaticMeshComponent
FTransform PreviousTransform
AInstancedActor(const FObjectInitializer &ObjectInitializer)
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)
static AInstancedRenderer * GetInstancedRenderer(const UObject *WorldContextObject)