Hi, I am converting a Procedural Mesh into a Static Mesh, and then assigning it a material using the Set Material node.
This works well in editor mode, but when I make a packaged build the material does not show up. It instead shows the light grey / dark grey checkerboard pattern.
Here is my code for converting the Procedural Mesh into a Static Mesh
UStaticMesh* UProceduralHelperFunctionsBPLibrary::ConvertToStaticMesh(UProceduralMeshComponent* ProceduralMesh, UMaterialInterface* Material)
{
if (!ProceduralMesh)
return nullptr;
FMeshDescription MeshDescription = BuildMeshDescription(ProceduralMesh);
// If we got some valid data.
if (MeshDescription.Polygons().Num() > 0)
{
// Create StaticMesh object
UStaticMesh* StaticMesh = NewObject<UStaticMesh>(ProceduralMesh/*Package, MeshName, RF_Public | RF_Standalone*/);
StaticMesh->InitResources();
// Enable CPU access for the static mesh
StaticMesh->bAllowCPUAccess = true;
StaticMesh->SetLightingGuid();
// Add source to new StaticMesh
auto Desc = StaticMesh->CreateStaticMeshDescription();
// Assuming you have access to the MeshDescription object
FStaticMeshAttributes StaticMeshAttributes(MeshDescription);
auto VertexInstanceColors = StaticMeshAttributes.GetVertexInstanceColors();
// Set vertex colors explicitly
for (const FVertexInstanceID VertexInstanceID : MeshDescription.VertexInstances().GetElementIDs())
{
FVector4f VertexColor(0.0f, 0.0f, 0.0f, 0.0f); // Set the initial vertex color to grey (0, 0, 0, 0)
VertexInstanceColors[VertexInstanceID] = VertexColor;
}
Desc->SetMeshDescription(MeshDescription);
// buildSimpleCol = false, cause it creates box collision based on mesh bounds or whatever :(
StaticMesh->BuildFromStaticMeshDescriptions({ Desc }, false);
// Create collision data for the static mesh
StaticMesh->CreateBodySetup();
UBodySetup* NewBodySetup = StaticMesh->GetBodySetup();
NewBodySetup->BodySetupGuid = FGuid::NewGuid();
NewBodySetup->bGenerateMirroredCollision = false;
NewBodySetup->bDoubleSidedGeometry = true;
NewBodySetup->CollisionTraceFlag = CTF_UseComplexAsSimple;
NewBodySetup->DefaultInstance.SetCollisionProfileName(UCollisionProfile::BlockAll_ProfileName);
// Copy convex elements from ProceduralMesh's BodySetup
NewBodySetup->AggGeom = ProceduralMesh->ProcMeshBodySetup->AggGeom;
// Create physics meshes
NewBodySetup->CreatePhysicsMeshes();
return StaticMesh;
}
return nullptr;
}
And here is how I use it in Blueprints:
Does anyone know how I can fix this issue?
Thanks