Documentation for the Unreal C++ Plugin
Loading...
Searching...
No Matches
HighlightComponent.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "Kismet/GameplayStatics.h"
5
6// Sets default values for this component's properties
8{
9 // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
10 // off to improve performance if you don't need them.
11 PrimaryComponentTick.bCanEverTick = false;
12
13 ColorMap.Add(EHighlightColor::None, FLinearColor(0.0f, 0.0f, 0.0f));
14 ColorMap.Add(EHighlightColor::Blue, FLinearColor(0.0f, 0.866382f, 1.0f));
15 ColorMap.Add(EHighlightColor::Green, FLinearColor(0.061116f, 1.0f, 0.0f));
16 ColorMap.Add(EHighlightColor::Yellow, FLinearColor(1.0f, 0.850555f, 0.0f));
17 ColorMap.Add(EHighlightColor::Red, FLinearColor(1.0f, 0.0f, 0.0f));
18 ColorMap.Add(EHighlightColor::White, FLinearColor(1.0f, 1.0f, 1.0f));
19 ColorMap.Add(EHighlightColor::Orange, FLinearColor(1.0f, 0.1f, 0.0f));
20}
21
22// Called when the game starts
24{
25 Super::BeginPlay();
26}
27
29{
31 {
32 CurrentHighlight = HighlightColor;
33 OnHighlightValueChanged.Broadcast();
34 auto Color = [this]()->FVector{return FVector(*ColorMap.Find(CurrentHighlight));};
38 else
40 }
41}
42
44{
45 if (!Activate)
46 {
47 if (PulseHighlightTimerHandle.IsValid())
48 {
49 HighlightTime = 0.0;
50 PulseDirection = 0.0;
51 PulseHighlightTimerHandle.Invalidate();
52 BlockPulsing = true;
54 }
56 }
57 else
58 {
59 HighlightTime = 1.0;
60 PulseDirection = -1.0;
63 }
64 OnHighlightChanged.Broadcast();
65}
66
67void UHighlightComponent::ManuallyActivatePulseHighlight(bool Activate, TArray<UMeshComponent*> TargetComponent)
68{
69 if (!Activate)
70 {
71 if (PulseHighlightTimerHandle.IsValid())
72 {
73 HighlightTime = 0.0;
74 PulseDirection = 0.0;
75 PulseHighlightTimerHandle.Invalidate();
76 BlockPulsing = true;
78 }
80 }
81 else
82 {
83 HighlightTime = 1.0;
84 PulseDirection = -1.0;
85 OwnerMeshComponents.Empty();
86 for (auto const Component : TargetComponent)
87 {
88 OwnerMeshComponents.Add(Component);
89 }
91 }
92 OnHighlightChanged.Broadcast();
93}
94
96 TArray<UMeshComponent*> TargetComponent, TArray<int> TargetMaterialIndexes)
97{
98 if (Activate)
99 AffectedMaterialIndexes = TargetMaterialIndexes;
100 else
102
103 ManuallyActivatePulseHighlight(Activate, TargetComponent);
104}
105
107{
108 auto Color = [this]()->FVector
109 {
110 return FVector(FMath::Lerp(*ColorMap.Find(EHighlightColor::None), *ColorMap.Find(EHighlightColor::Blue), HighlightTime));;
111 };
112
113 if (AffectedMaterialIndexes.Num())
115 else
117
119 if (HighlightTime > 1.0)
120 {
121 HighlightTime = 1.0;
122 PulseDirection = -1.0;
123 }
124 else if (HighlightTime < 0.0)
125 {
126 HighlightTime = 0.0;
127 PulseDirection = 1.0;
128 }
129 if (BlockPulsing)
130 {
131 BlockPulsing = false;
132 return;
133 }
134 GetWorld()->GetTimerManager().SetTimer( PulseHighlightTimerHandle,this, &UHighlightComponent::PerformPulseHighlight, 0.1);
135}
136
137void UHighlightComponent::ChangeHighlightMaterialColor(const TFunctionRef<FVector()> Color, TArray<TWeakObjectPtr<UMeshComponent>> TargetMeshes)
138{
139 for (TWeakObjectPtr<UMeshComponent> Mesh : TargetMeshes)
140 {
141 if (DeactivatedMeshes.Find(Cast<UStaticMeshComponent>(Mesh.Get())) != INDEX_NONE)
142 {
143 continue;
144 }
145
146 if (UGameplayStatics::GetPlatformName() == TEXT("Android") || AlwaysEmissiveHighlight)
147 {
148 if (Cast<UStaticMeshComponent>(Mesh.Get()))
149 {
150 Mesh.Get()->SetVectorParameterValueOnMaterials(TEXT("EmissiveColor"), Color());
151 }
152 else if (Cast<USkeletalMeshComponent>(Mesh.Get()))
153 {
154 Mesh->SetVectorParameterValueOnMaterials(TEXT("EmissiveColor"), Color());
155 }
156 }
157
158 else
159 {
161 Mesh->SetRenderCustomDepth(false);
162 else
163 Mesh->SetRenderCustomDepth(true);
164
165 Mesh->SetCustomDepthStencilValue((uint8)CurrentHighlight);
166 }
167 }
168}
169
170void UHighlightComponent::ChangeHighlightSpecificMaterialColor(const TFunctionRef<FVector()> Color,
171 TArray<TWeakObjectPtr<UMeshComponent>> TargetMeshes, TArray<int> MaterialIndexes)
172{
173 for (TWeakObjectPtr<UMeshComponent> Mesh : TargetMeshes)
174 {
175 if (DeactivatedMeshes.Find(Cast<UStaticMeshComponent>(Mesh.Get())) != INDEX_NONE)
176 {
177 continue;
178 }
179
180 if (UGameplayStatics::GetPlatformName() == TEXT("Android") || AlwaysEmissiveHighlight)
181 {
182 if (Cast<UStaticMeshComponent>(Mesh.Get()))
183 {
184 for (int Index : MaterialIndexes)
185 {
186 UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Mesh.Get()->GetMaterial(Index));
187 if (!DynamicMaterial)
188 {
189 DynamicMaterial = Mesh.Get()->CreateAndSetMaterialInstanceDynamic(Index);
190 }
191 DynamicMaterial->SetVectorParameterValue(TEXT("EmissiveColor"), Color());
192 }
193 }
194 else if (Cast<USkeletalMeshComponent>(Mesh.Get()))
195 {
196 for (int Index : MaterialIndexes)
197 {
198 UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Mesh.Get()->GetMaterial(Index));
199 if (!DynamicMaterial)
200 {
201 DynamicMaterial = Mesh.Get()->CreateAndSetMaterialInstanceDynamic(Index);
202 }
203 DynamicMaterial->SetVectorParameterValue(TEXT("EmissiveColor"), Color());
204 }
205 }
206 }
207 }
208}
209
211{
212 OwnerMeshComponents.Empty();
213 TArray<UStaticMeshComponent*> StaticMeshes;
214 GetOwner()->GetComponents<UStaticMeshComponent>(StaticMeshes);
215 TArray<USkeletalMeshComponent*> SkeletalMeshes;
216 GetOwner()->GetComponents<USkeletalMeshComponent>(SkeletalMeshes);
217 OwnerMeshComponents.Append(StaticMeshes);
218 OwnerMeshComponents.Append(SkeletalMeshes);
219}
220
EHighlightColor
UENUM()
TMap< EHighlightColor, FLinearColor > ColorMap
void ManuallyActivatePulseHighlightOnSpecificMaterials(bool Activate, TArray< UMeshComponent * > TargetComponent, TArray< int > TargetMaterialIndexes)
Manually activates or deactivates the pulse highlight effect on specific part of the object and speci...
TArray< TWeakObjectPtr< UMeshComponent > > OwnerMeshComponents
TArray< int > AffectedMaterialIndexes
FOnHighlightChanged OnHighlightChanged
UPROPERTY(BlueprintAssignable)
TArray< UStaticMeshComponent * > DeactivatedMeshes
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
bool AlwaysEmissiveHighlight
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
void PerformPulseHighlight()
UFUNCTION()
bool IsShowHighlight
UPROPERTY()
FTimerHandle PulseHighlightTimerHandle
UPROPERTY(BlueprintReadWrite)
virtual void BeginPlay() override
void ChangeHighlightMaterialColor(const TFunctionRef< FVector()> Color, TArray< TWeakObjectPtr< UMeshComponent > > TargetMeshes)
void ManuallyActivatePulseHighlight(bool Activate, TArray< UMeshComponent * > TargetComponent)
Manually activates or deactivates the pulse highlight effect on specific part of the object.
void ChangeHighlightSpecificMaterialColor(const TFunctionRef< FVector()> Color, TArray< TWeakObjectPtr< UMeshComponent > > TargetMeshes, TArray< int > MaterialIndexes)
EHighlightColor CurrentHighlight
UPROPERTY()
void UpdateHighlightValue(EHighlightColor HighlightColor)
Updates the highlight color of the object.
void ActivatePulseHighlight(bool Activate)
Activates or deactivates the pulse highlight effect.
FOnHighlightValueChanged OnHighlightValueChanged
UPROPERTY(BlueprintAssignable)