CARLA
 
载入中...
搜索中...
未找到
StaticMeshCollection.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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 "Carla.h"
9
10#include "Components/InstancedStaticMeshComponent.h"
11#include "Engine/StaticMesh.h"
12
13/*
14 * AStaticMeshCollection 的构造函数,初始化静态网格集合类的实例。
15 * 该构造函数会禁用 Actor 的 Tick(每帧更新),并设置根组件为一个默认的场景组件。
16 * 根组件的移动性被设置为静态,表示该组件在游戏运行时不会移动或变化。
17 */
19 const FObjectInitializer &ObjectInitializer) :
20 Super(ObjectInitializer) {
21 PrimaryActorTick.bCanEverTick = false;
22 RootComponent =
23 ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComponent"));
24 RootComponent->SetMobility(EComponentMobility::Static);
25}
26
27/*
28 * PushBackInstantiator 函数用于向静态网格集合中添加一个新的实例化静态网格组件。
29 * 该函数创建一个新的 UInstancedStaticMeshComponent,并将其附加到根组件上。
30 * 然后设置其网格为传入的 Mesh,注册组件并将其添加到 MeshInstantiators 列表中。
31 */
33 auto Instantiator = NewObject<UInstancedStaticMeshComponent>(this);
34 check(Instantiator != nullptr);
35 Instantiator->SetMobility(EComponentMobility::Static);
36 Instantiator->SetupAttachment(RootComponent);
37 Instantiator->SetStaticMesh(Mesh);
38 Instantiator->RegisterComponent();
39 MeshInstantiators.Add(Instantiator);
40}
41
42/*
43 * SetStaticMesh 函数用于根据索引 i 设置静态网格实例化组件的网格。
44 * 如果给定的索引有效且对应的实例化组件存在,则更新该组件的静态网格为传入的 Mesh。
45 */
46void AStaticMeshCollection::SetStaticMesh(uint32 i, UStaticMesh *Mesh) {
47 if ((GetNumberOfInstantiators() > i) && (MeshInstantiators[i] != nullptr)) {
48 MeshInstantiators[i]->SetStaticMesh(Mesh);
49 }
50}
51
52/*
53 * AddInstance 函数用于向指定索引的静态网格实例化组件添加一个实例。
54 * 根据传入的索引 i 和变换信息 Transform,将新的实例添加到对应的实例化组件中。
55 * 如果索引有效且对应的实例化组件存在,则调用 AddInstance 方法进行添加。
56 */
57void AStaticMeshCollection::AddInstance(uint32 i, const FTransform &Transform) {
58 if ((GetNumberOfInstantiators() > i) && (MeshInstantiators[i] != nullptr)) {
59 MeshInstantiators[i]->AddInstance(Transform);
60 }
61}
62
63/*
64 * ClearInstances 函数用于清除所有静态网格实例化组件中的实例。
65 * 遍历所有实例化组件,如果组件不为空,则调用 ClearInstances 方法来移除所有实例。
66 */
68 for (auto *Instantiator: MeshInstantiators) {
69 if (Instantiator != nullptr) {
70 Instantiator->ClearInstances();
71 }
72 }
73}
74
75/*
76 * ClearInstantiators 函数用于清除所有静态网格实例化组件(Instantiators)。
77 * 首先调用 ClearInstances() 清除所有实例,然后清空 MeshInstantiators 数组。
78 */
TArray< UInstancedStaticMeshComponent * > MeshInstantiators
void PushBackInstantiator(UStaticMesh *Mesh)
uint32 GetNumberOfInstantiators() const
void ClearInstantiators()
同时清除实例。
AStaticMeshCollection(const FObjectInitializer &ObjectInitializer)
void AddInstance(uint32 i, const FTransform &Transform)
void SetStaticMesh(uint32 i, UStaticMesh *Mesh)