Programatic Dynamic Mesh Fracturing

I’m looking into Unreal’s Chaos Destruction tech, and while I am coming to understand the basics of the system with Geometry Collections, I’m not finding any resources on how to generate these assets dynamically at runtime. I’ve got Building objects that I’d like to explode into pieces once their health property has reached zero, but I don’t want to manually craft a unique GC for each building in each level. Is there any way, preferably using C++, that I could generate a GC on the fly based on the object’s mesh, spawn it where the original building’s about to despawn, and then create a random fracture based on that GC so it can immediately start falling apart?

3 Likes

Except for Gradient Space tutorials and a few others there is nothing regarding meshes like FDynamicMesh3 and components like
USimpleDynamicMeshComponent with C++. You will find a bit more on PMC tutorials, but not even those are in huge numbers. The only way is to do the tutorials that you find online the very few ones and try to get a very basic understanding how they work, after that you can head up to ghithub and try to disect how other people’s project work, they have code examples with these, you can search on ghithub for them. It’s going to take you lots of time and lots of effort to understand them in a more complex way.

I am looking for exactly the same.

I think I managed to properly implement a geometry collection component in c++. However, I can’t test my code yet, because I don’t know how to apply a fracture in C++. I am still searching.

Once I figured it out, I will share my code.

I’ve read that unreal’s chaos destruction is only accessible via editor commands at the moment, because they put less priority on programatic destruction, but they will come around eventually. This is only rumours.

Here is what I have in C++ so far. Be aware that this isn’t tested and might be wrong. (it does compile) I am grateful for any comment, especially on how to continue this code to actually apply some fracture to the geometry collection. And if someone can point out errors, that would be awesome, too.

// this runs inside 'OnConstruction' of an actor that has a static mesh component
const FSoftObjectPath SourcePath(StaticMeshComponent->GetStaticMesh());
TArray<TObjectPtr<UMaterialInterface>> SourceMaterials(StaticMesh->GetMaterials());
UGeometryCollection* RestCollection =
	NewObject<UGeometryCollection>(GetWorld(), UGeometryCollection::StaticClass());
RestCollection->SetFlags(RF_Transactional | RF_Public | RF_Standalone);
if(RestCollection->SizeSpecificData.IsEmpty())
{
	RestCollection->SizeSpecificData.Add(FGeometryCollectionSizeSpecificData());
}
GeometryCollection->SetRestCollection(RestCollection);
FTransform ComponentTransform(StaticMeshComponent->GetComponentTransform());
ComponentTransform.SetTranslation(ComponentTransform.GetTranslation() - GetTransform().GetTranslation());
RestCollection->GeometrySource.Add(
	{ SourcePath
	, ComponentTransform
	, SourceMaterials
	, true
	, false 
	});
FGeometryCollectionEngineConversion::AppendStaticMesh
	( StaticMeshComponent->GetStaticMesh()
	, SourceMaterials
	, ComponentTransform
	, RestCollection
	, false
	, true
	, false
	);
RestCollection->InitializeMaterials();
// TODO: check if necessary
//UFractureActionTool::AddSingleRootNodeIfRequired
GeometryCollection->MarkPackageDirty();
GeometryCollection->MarkRenderStateDirty();
//::GeometryCollection::GenerateTemporaryGuids
//	( FracturedGeometryCollection->GetGeometryCollection().Get()
//	, 0, true);
//FGeometryCollectionProximityUtility ProximityUtility(FracturedGeometryCollection->GetGeometryCollection().Get());
//ProximityUtility.UpdateProximity();

The code is largely copied from here:

Engine\Plugins\Experimental\ChaosEditor\Source\FractureEditor\Private\FractureToolGenerators.cpp

The parts that are commented towards the end, imply a dependency to the FractureEditor module, which I hope isn’t necessary.

I’m concerned even we can get it to work, will the performance be acceptable?
when I do fracture in editor, it takes quite some time to generate the pieces

I switched to the RealtimeMeshComponent Plugin (5 USD on MarketPlace, or for free via Github). I can’t tell yet wether it will work fine performance-wise, but it seems to be fine for my purposes.

Only problem with the plugin:

At the moment it is based on the ProceduralMeshComponent, and improves upon the perfomance thereof.

There is work planned to alternatively allow for a FDynamicMesh with the plugin.

Until that is available, however, all the tools (and functions) for fracturing are not available for the RealtimeMeshComponent plugin.
So I have to redo quite a bit of work, e.g. to fracture an asteroid.
Work that has been done for the dynamic mesh, already.

You think FDymanic mesh will be faster, that is just for unreal core geometry with other tools other than PMC that unreal has.