CARLA
 
载入中...
搜索中...
未找到
StaticMeshFactory.cpp
浏览该文件的文档.
1// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "StaticMeshFactory.h"
9#include "Engine/StaticMeshActor.h"
11
12TArray<FActorDefinition> AStaticMeshFactory::GetDefinitions()
13{
15 auto StaticMeshDefinition = ABFL::MakeGenericDefinition(
16 TEXT("static"),
17 TEXT("prop"),
18 TEXT("mesh"));
19 StaticMeshDefinition.Class = AStaticMeshActor::StaticClass();
20 StaticMeshDefinition.Variations.Emplace(FActorVariation{
21 TEXT("mesh_path"),
23 {""}, false});
24 StaticMeshDefinition.Variations.Emplace(FActorVariation{
25 TEXT("mass"),
27 {""}, false});
28 StaticMeshDefinition.Variations.Emplace(FActorVariation{
29 TEXT("scale"),
31 {"1.0f"}, false});
32 return { StaticMeshDefinition };
33}
34
36 const FTransform &SpawnAtTransform,
37 const FActorDescription &ActorDescription)
38{
40 auto *World = GetWorld();
41 if (World == nullptr)
42 {
43 UE_LOG(LogCarla, Error, TEXT
44 ("AStaticMeshFactory: cannot spawn mesh into an empty world."));
45 return {};
46 }
47
48 float Scale = ABFL::RetrieveActorAttributeToFloat("scale", ActorDescription.Variations, 1.0f);
49 FTransform ScaledTransform(SpawnAtTransform);
50 ScaledTransform.SetScale3D(FVector(Scale));
51
52 FActorSpawnParameters SpawnParameters;
53 SpawnParameters.SpawnCollisionHandlingOverride =
54 ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
55 auto *StaticMeshActor = World->SpawnActor<AStaticMeshActor>(
56 ActorDescription.Class, ScaledTransform, SpawnParameters);
57
58 auto *StaticMeshComponent = Cast<UStaticMeshComponent>(
59 StaticMeshActor->GetRootComponent());
60
61 if (StaticMeshComponent)
62 {
63 if (ActorDescription.Variations.Contains("mesh_path"))
64 {
65 FString MeshPath = ABFL::ActorAttributeToString(
66 ActorDescription.Variations["mesh_path"], "");
67
68 UStaticMesh *Mesh = LoadObject<UStaticMesh>(nullptr, *MeshPath);
69 StaticMeshComponent->SetMobility(EComponentMobility::Movable);
70 if (!StaticMeshComponent->SetStaticMesh(Mesh))
71 UE_LOG(LogCarla, Warning, TEXT("Failed to set the mesh"));
72 StaticMeshComponent->SetMobility(EComponentMobility::Static);
73
74 if (ActorDescription.Variations.Contains("mass"))
75 {
76 float Mass = ABFL::RetrieveActorAttributeToFloat("mass", ActorDescription.Variations, 0.0f);
77 if (Mass > 0)
78 {
79 StaticMeshComponent->SetMobility(EComponentMobility::Movable);
80 StaticMeshComponent->SetSimulatePhysics(true);
81 StaticMeshComponent->SetCollisionProfileName("PhysicsActor");
82 StaticMeshComponent->SetMassOverrideInKg("", Mass);
83 }
84 }
85 }
86 }
87 return FActorSpawnResult(StaticMeshActor);
88}
TArray< FActorDefinition > GetDefinitions() final
Retrieve the definitions of the static mesh actor
FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription) final
Spawn an actor based on ActorDescription and Transform.
A description of a Carla Actor with all its variation.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
TSubclassOf< AActor > Class
Class of the actor to be spawned.
Result of an actor spawn function.