Download

Mesh from UProceduralMeshComponent not showing in game

Im trying to use the ProceduralMeshComponent plugin to create meshes and for some reason the meshes don’t show when running the game.

I made a stripped down version of my code to try and just get a triangle to be drawn but I can’t seem to get it to work. Would appreciate any help figuring this out.

One oddity I noticed was that setting the visibility would cause a crash from within the render thread due to a null FPrimitiveSceneProxy pointer, not sure if that is related.



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "ProceduralMeshComponentTest.generated.h"

UCLASS()
class PROCEDURALMESHTEST_API AProceduralMeshComponentTest : public AActor
{
	GENERATED_BODY()
	
	UPROPERTY(VisibleAnywhere, Category = Materials)
	UProceduralMeshComponent *mesh;
public:	
	// Sets default values for this actor's properties
	AProceduralMeshComponentTest();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

};




// Fill out your copyright notice in the Description page of Project Settings.

#include "ProceduralMeshTest.h"
#include "ProceduralMeshComponentTest.h"


// Sets default values
AProceduralMeshComponentTest::AProceduralMeshComponentTest()
{
 	// 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;

}

// Called when the game starts or when spawned
void AProceduralMeshComponentTest::BeginPlay()
{
	Super::BeginPlay();
	mesh = NewObject<UProceduralMeshComponent>(this);
	RootComponent = mesh;
	TArray<FVector> Vertices;
	TArray<int32> Triangles;
	TArray<FVector> Normals;
	TArray<FVector2D> UVs;
	TArray<FProcMeshTangent> Tangents;
	TArray<FColor> VertexColors;
	Vertices.Add(FVector(100, 0, 0));
	Normals.Add(FVector(1, 0, 0));
	Vertices.Add(FVector(0, 100, 0));
	Normals.Add(FVector(0, 1, 0));
	Vertices.Add(FVector(0, 0, 100));
	Normals.Add(FVector(0, 0, 1));
	Triangles.Add(0);
	Triangles.Add(1);
	Triangles.Add(2);

	mesh->ClearAllMeshSections();
	mesh->CreateMeshSection(0, Vertices, Triangles, Normals, UVs, VertexColors, Tangents, false);
	mesh->SetMaterial(0, LoadObject<UMaterial>(NULL, TEXT("/Game/ProceduralTerrainGenerator/DoubleSidedPink.DoubleSidedPink")));
	//mesh->SetMeshSectionVisible(0, true); //This line causes a crash from within the render thread because a FPrimitiveSceneProxy* points to null
}

// Called every frame
void AProceduralMeshComponentTest::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}


I’ll start by saying I’m still pretty new to UE4 so not sure about creating components from the begin play function.

I will say however that I can get your example to work by simply moving…



mesh = NewObject<UProceduralMeshComponent>(this, TEXT("ProceduralMesh"));
RootComponent = mesh;


…to the constructor of the actor and then give the component a name and it should work fine!

Remember though that since you’re only adding one triangle, that you’ll only be able to see it from one side by default but it looks like you had it trying to load a double sided texture! Also if you want to be able to see it in the editor without playing the game move the mesh creation to the constructor. Be careful doing that with big meshes or lengthy generation processes.