RapyutaSimulationPlugins
RRMeshActor.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 
18 
19 // RapyutaSimulationPlugins
20 
21 #include "Core/RRBaseActor.h"
22 
23 #include "Core/RRCoreUtils.h"
24 
26 
28 
29 #include "Core/RRUObjectUtils.h"
30 
32 
33 
34 
35 #include "RRMeshActor.generated.h"
36 
37 
38 
39 DECLARE_DELEGATE_OneParam(FOnMeshActorDeactivated, ARRMeshActor*);
40 
41 
42 
58 class RAPYUTASIMULATIONPLUGINS_API ARRMeshActor : public ARRBaseActor
59 
60 {
61 
62 public:
63 
70  ARRMeshActor();
71 
72 
73 
74 public:
75 
88  virtual bool Initialize() override;
89 
90 
91 
106  virtual bool HasInitialized(bool bIsLogged = false) const override;
107 
108 
109 
118  virtual void Reset() override;
119 
120  void DrawTransform();
121 
122 
123 
124 public:
125 
127 
128 
133  TArray<UMeshComponent*> MeshCompList;
134 
135 
136 
138 
139 
144  int32 CreatedMeshesNum = 0;
145 
146 
147 
149 
150 
155  int32 ToBeCreatedMeshesNum = 0;
156 
157 
158 
160 
161 
166  TObjectPtr<UMeshComponent> BaseMeshComp = nullptr;
167 
168 
169 
178  UMaterialInterface* GetBaseMeshMaterial(int32 InMaterialIndex = 0) const
179 
180  {
181 
182  return BaseMeshComp ? BaseMeshComp->GetMaterial(InMaterialIndex) : nullptr;
183 
184  }
185 
186 
187 
198  virtual void DeclareFullCreation(bool bInCreationResult);
199 
200 
201 
202 public:
203 
205 
206 
211  FIntVector CellIdx = FIntVector::ZeroValue;
212 
213 
214 
223  UMeshComponent* GetMeshComponent(int32 Index = 0) const;
224 
225 
226 
243  template<typename TMeshComp>
244 
245  TArray<TMeshComp*> CreateMeshComponentList(USceneComponent* InParentComp,
246 
247  const TArray<FString>& InMeshUniqueNameList,
248 
249  const TArray<FTransform>& InMeshRelTransf = TArray<FTransform>(),
250 
251  const TArray<FString>& InMaterialNameList = TArray<FString>())
252 
253  {
254 
255  // (Note) This method could be invoked multiple times
256 
257  TArray<TMeshComp*> addedMeshCompList;
258 
259  if (InMeshRelTransf.Num() > 0)
260 
261  {
262 
263  verify(InMeshRelTransf.Num() == InMeshUniqueNameList.Num());
264 
265  }
266 
267  if (InMaterialNameList.Num() > 0)
268 
269  {
270 
271  verify(InMaterialNameList.Num() == InMeshUniqueNameList.Num());
272 
273  }
274 
275 
276 
277  TMeshComp* meshComp = nullptr;
278 
279  for (auto i = 0; i < InMeshUniqueNameList.Num(); ++i)
280 
281  {
282 
283  const FString& meshUniqueName = InMeshUniqueNameList[i];
284 
285 
286 
287  // [ProcMeshComp] Verify path as absolute & existing
288 
289  if ((false == FPaths::IsRelative(meshUniqueName)) && (false == FPaths::FileExists(meshUniqueName)))
290 
291  {
292 
293  UE_LOG_WITH_INFO(LogTemp, Error, TEXT("Mesh invalid [%s] is non-existent"), *meshUniqueName);
294 
295  continue;
296 
297  }
298 
299 
300 
301  // [OBJECT MESH COMP] --
302 
303  //
304 
305  meshComp = URRUObjectUtils::CreateMeshComponent<TMeshComp>(
306 
307  this,
308 
309  meshUniqueName,
310 
311  FString::Printf(TEXT("%s_MeshComp_%u"), *ActorInfo->UniqueName, MeshCompList.Num()),
312 
313  InMeshRelTransf.IsValidIndex(i) ? InMeshRelTransf[i] : FTransform::Identity,
314 
315  ActorInfo->bIsStationary,
316 
317  ActorInfo->bIsPhysicsEnabled,
318 
319  ActorInfo->bIsCollisionEnabled,
320 
321  ActorInfo->bIsOverlapEventEnabled,
322 
323  InParentComp);
324 
325 
326 
327  if (meshComp)
328 
329  {
330 
331  // (Note) This must be the full path to the mesh file on disk
332 
333  if (meshComp->InitializeMesh(meshUniqueName))
334 
335  {
336 
337  addedMeshCompList.AddUnique(meshComp);
338 
339  }
340 
341  else
342 
343  {
344 
345  UE_LOG_WITH_INFO_NAMED(LogTemp, Error, TEXT("Failed initializing mesh comp[%s]"), *meshUniqueName);
346 
347  }
348 
349  }
350 
351  else
352 
353  {
354 
355  UE_LOG_WITH_INFO(LogTemp,
356 
357  Error,
358 
359  TEXT("[%s:%d] - Failed creating child Mesh Component [%s]!"),
360 
361  *ActorInfo->UniqueName,
362 
363  this,
364 
365  *meshUniqueName);
366 
367  }
368 
369 
370 
371  // [MeshCompList] <- [addedMeshCompList]
372 
373  MeshCompList.Append(addedMeshCompList);
374 
375  }
376 
377 
378 
379  // Change RootComponent -> BaseMeshComp
380 
381  if ((nullptr == BaseMeshComp) && (MeshCompList.Num() > 0))
382 
383  {
384 
385  BaseMeshComp = MeshCompList[0];
386 
387 
388 
389  if (BaseMeshComp->GetRelativeTransform().Equals(FTransform::Identity))
390 
391  {
392 
393  // Set as Root Component
394 
395  // Set the main mesh comp as the root
396 
397  // (Not clear why using the default scene component as the root just disrupts actor-children relative movement,
398 
399  // and thus also compromise the actor transform itself)!
400 
401  USceneComponent* oldRoot = RootComponent;
402 
403  SetRootComponent(BaseMeshComp);
404 
405  if (oldRoot)
406 
407  {
408 
409  oldRoot->DestroyComponent();
410 
411  }
412 
413  }
414 
415  }
416 
417 
418 
419  return addedMeshCompList;
420 
421  }
422 
423 
424 
435  virtual void OnBodyComponentMeshCreationDone(bool bInCreationResult, UObject* InMeshBodyComponent);
436 
437 
438 
447  void SetCustomDepthEnabled(bool bIsCustomDepthEnabled);
448 
449 
450 
461  void SetCustomDepthStencilValue(int32 InCustomDepthStencilValue);
462 
463 
464 
477  bool IsCustomDepthEnabled() const;
478 
479 
480 
489  TArray<int32> GetCustomDepthStencilValueList() const;
490 
491 
492 
494 
495  FOnMeshActorDeactivated OnDeactivated;
496 
497 
498 
509  FORCEINLINE virtual void SetActivated(bool bInIsActivated)
510 
511  {
512 
513 #if RAPYUTA_SIM_VISUAL_DEBUG
514 
515  // Visible/Invisibile
516 
517  SetActorHiddenInGame(!bInIsActivated);
518 
519 #endif
520 
521 
522 
523  // Then teleport itself to a camera-blind location if being deactivated,
524 
525  // so when it get activated back, it would not happen to appear at an unintended pose
526 
527  if (false == bInIsActivated)
528 
529  {
530 
531  CellIdx = FIntVector::NoneValue;
532 
533  SetActorLocation(FVector(0.f, 0.f, -5000.f));
534 
535  OnDeactivated.ExecuteIfBound(this);
536 
537  }
538 
539  else if (CellIdx == FIntVector::NoneValue)
540 
541  {
542 
543  CellIdx = FIntVector::ZeroValue;
544 
545  }
546 
547 
548 
549  // RenderCustomDepth (must be after [OnDeactivated], thus its current CustomDepthStencilValue could be stored)
550 
551  SetCustomDepthEnabled(bInIsActivated);
552 
553  }
554 
555 
556 
563  bool IsActivated() const
564 
565  {
566 
567  return (CellIdx != FIntVector::NoneValue);
568 
569  }
570 
571 
572 
573 protected:
574 
576 
577 
583 
584 
585 
587 
588 
593  uint8 bFullyCreated : 1;
594 
595 };
596 
RRProceduralMeshComponent.h
Procedural mesh components. this class is used to spawn robot and object from ROS 2 service.
RapyutaSimulationPlugins.h
Unreal Engine Mudule class.
ARRBaseActor::Initialize
virtual bool Initialize()
Set #GameMode #GameState #GameSingleton #PlayerController.
ARRMeshActor::SetActivated
virtual FORCEINLINE void SetActivated(bool bInIsActivated)
Activate/Deactivate mesh actor.
Definition: RRMeshActor.h:509
ARRMeshActor::MeshCompList
TArray< UMeshComponent * > MeshCompList
Body mesh component list.
Definition: RRMeshActor.h:133
ARRMeshActor::bFullyCreated
uint8 bFullyCreated
Whether all body meshes are fully created.
Definition: RRMeshActor.h:593
ARRMeshActor::OnDeactivated
FOnMeshActorDeactivated OnDeactivated
Delegate on mesh actor being deactivated.
Definition: RRMeshActor.h:495
ARRBaseActor
Base actor class for all Rapyuta Sim actors:
Definition: RRBaseActor.h:70
ARRMeshActor
Mesh actor.ARRBaseActor with list of #UMeshComponent.
Definition: RRMeshActor.h:58
RRUObjectUtils.h
UObject general utils.
ARRMeshActor::bLastMeshCreationResult
uint8 bLastMeshCreationResult
Last body mesh creation result.
Definition: RRMeshActor.h:582
ARRBaseActor::HasInitialized
virtual bool HasInitialized(bool bIsLogged=false) const
DECLARE_DELEGATE_OneParam
DECLARE_DELEGATE_OneParam(FOnMeshActorDeactivated, ARRMeshActor *)
RRStaticMeshComponent.h
static mesh component
ARRMeshActor::GetBaseMeshMaterial
UMaterialInterface * GetBaseMeshMaterial(int32 InMaterialIndex=0) const
Get BaseMeshComp's material.
Definition: RRMeshActor.h:178
ARRBaseActor::Reset
virtual void Reset()
Rest. Calls #ActorInfo::ClearMeshInfo.
ARRMeshActor::IsActivated
bool IsActivated() const
Whether mesh actor is activated.
Definition: RRMeshActor.h:563
RRBaseActor.h
Base actor class for all Rapyuta Sim actors.
ARRMeshActor::CreateMeshComponentList
TArray< TMeshComp * > CreateMeshComponentList(USceneComponent *InParentComp, const TArray< FString > &InMeshUniqueNameList, const TArray< FTransform > &InMeshRelTransf=TArray< FTransform >(), const TArray< FString > &InMaterialNameList=TArray< FString >())
Create mesh component list.
Definition: RRMeshActor.h:245
RRCoreUtils.h
Core utils.