Adding Models At Runtime - jgoffeney/Cesium4Unreal GitHub Wiki

Back

References

Description

When a procedural model is generated by an Actor it is not automatically added to the scene. After creating the model it needs to be attached to the actor's root component.

Header

With the header file add a USceneComponent to hold a pointer to the actors's root component and a pointer to the mesh. For multiple meshes use a TArray of pointers.

UCLASS()
class GLB2UNREAL_API AGlb2UnrealActor : public AActor
{
    GENERATED_BODY()
	
public:
    /// <summary>
    /// The root component
    /// </summary>
    USceneComponent* RootComp;

    /// <summary>
    /// The model object
    /// </summary>
    UProceduralMeshComponent* CurrentMesh;

Constructor

Within the constructor create a new subobject and set it as the actor's root component.

AGlb2UnrealActor::AGlb2UnrealActor()
{

    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
    RootComponent = RootComp;
}

BeginPlay

During initialization of the Actor create the model, set its attributes and attach the model to the actor.

// Create a new mesh object  
CurrentMesh = NewObject<UProceduralMeshComponent>(this, "MESH0");

CurrentMesh->bUseAsyncCooking = true;

/// Do stuff to generate the mesh attributes 

// Create the mesh section
CurrentMesh->CreateMeshSection(0, unrealPositionList, indicesList, normalsList, texCoordsList, 
    TArray<FColor>(), TArray<FProcMeshTangent>(), true);

// Attach the mesh to the actor
CurrentMesh->SetupAttachment(RootComp);
CurrentMesh->RegisterComponent();
⚠️ **GitHub.com Fallback** ⚠️