The snippet of code below comes mainly from the procedural mesh generation wiki article, but I’ve attempted to assign a very simple default material to it, which instantly crashes the editor (when I recompile, and when I load the editor). No error message or information is presented.
The exact line it crashes at is 18, when the new instance is created.
The code I’ve been working with (entire class, based on default actor template):
// Fill out your copyright notice in the Description page of Project Settings.
#include "MeshTestGame.h"
#include "GeneratedCubeActor.h"
#include "ProceduralMeshComponent.h"
// Sets default values
AGeneratedCubeActor::AGeneratedCubeActor()
{
// 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;
// Find mat
static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/wood_mat.wood_mat'"));
UE_LOG(LogTemp, Warning, TEXT("Material pointer: 0x%X"), Material.Object); // Prints 0x43507580
auto matinstance = UMaterialInstanceDynamic::Create(Material.Object, this); // At this point the Editor crashes without any message or info.
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
UProceduralMeshComponent* mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
TArray<FVector> vertices;
vertices.Add(FVector(0, 0, 0));
vertices.Add(FVector(0, 100, 0));
vertices.Add(FVector(0, 100, 100));
vertices.Add(FVector(0, 0, 100));
TArray<int32> Triangles;
Triangles.Add(0);
Triangles.Add(1);
Triangles.Add(2);
Triangles.Add(0);
Triangles.Add(2);
Triangles.Add(3);
TArray<FVector> normals;
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
TArray<FVector2D> UV0;
UV0.Add(FVector2D(0, 0));
UV0.Add(FVector2D(0, 1));
UV0.Add(FVector2D(1, 1));
UV0.Add(FVector2D(1, 0));
TArray<FColor> vertexColors;
vertexColors.Add(FColor(100, 100, 100, 100));
vertexColors.Add(FColor(100, 100, 100, 100));
vertexColors.Add(FColor(100, 100, 100, 100));
vertexColors.Add(FColor(100, 100, 100, 100));
TArray<FProcMeshTangent> tangents;
tangents.Add(FProcMeshTangent(1, 1, 1));
tangents.Add(FProcMeshTangent(1, 1, 1));
tangents.Add(FProcMeshTangent(1, 1, 1));
tangents.Add(FProcMeshTangent(1, 1, 1));
mesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
mesh->CreateAndSetMaterialInstanceDynamicFromMaterial(0, matinstance);
mesh->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void AGeneratedCubeActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGeneratedCubeActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}