Generate Procedural Mesh

Hi all,
A quick note to let you know that I have updated a little bit more the Wiki example
I am experimenting (very slowly) to add to it what others have posted to this thread: for now, VertexPosition, VertexColor and U, V.
I hope this will help new users!

If you want to propose update(s), you can do it directly via pull-requests (https://.com//UE4ProceduralMesh)

I am doing all this with UE4.5, but if anyone is interested it would be easy to keep a separate branch for 4.4 or earlier.

Let me know!

Just a quick heads up, Scheidecker updated BrickGame and his engine side culling mod to 4.5 a few days back - for anyone making use of the latter (and if you’re doing anything remotely similar to Minecraft-style voxels, you should be), now’s a good time to update.

[=mordentral;171619]
It should compile just fine as FColoredMaterialRenderProxy has FMaterialRenderProxy as its public parent class and it is perfectly fine to use it like that, you must have overzealous compiler settings enabled. It doesn’t hurt anything to cast to it, but it shouldn’t be strictly necessary going from child to parent pointers as far as I am aware.
[/]

The error I got was this:

1>D:\Source\UE4ProceduralMesh-master\Source\ProceduralMesh\GeneratedMeshComponent.cpp(195): error C2440: ‘=’ : cannot convert from ‘FColoredMaterialRenderProxy **’ to ‘FMaterialRenderProxy *’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Has anybody had any success passing the data to use as the polyline to the new object? There is the Lathe function but I want to create something custom. I am a beginner in UDK and I believe that I can’t create my own constructor and pass the object in there. I then tried to add a member which does this:


void
AGameGeneratedActor::GenerateGeometry()
{
	generatedMesh = NewNamedObject<UGeneratedMeshComponent>(this, TEXT("ProceduralMesh"));

	// Contains the points describing the polyline we are going to rotate
	TArray<FVector> points;

	points.Add(FVector(20, 5, 0));
	points.Add(FVector(15, 6, 0));
	points.Add(FVector(12, 7, 0));
	points.Add(FVector(11, 8, 0));
	points.Add(FVector(8, 7, 0));
	points.Add(FVector(7, 6, 0));
	points.Add(FVector(4, 5, 0));
	points.Add(FVector(3, 4, 0));
	points.Add(FVector(2, 3, 0));
	points.Add(FVector(1, 4, 0));


	TArray<FGeneratedMeshTriangle> triangles;
	Lathe(points, triangles, 128);
	generatedMesh->SetGeneratedMeshTriangles(triangles);
	RootComponent = generatedMesh;
}


If I run that then I don’t get any mesh out. However if I keep the code in the constructor:


	TSubobjectPtr<UGeneratedMeshComponent> mesh = PCIP.CreateDefaultSubobject<UGeneratedMeshComponent>(this, TEXT("GeneratedMesh"));

	// Contains the points describing the polyline we are going to rotate
	TArray<FVector> points;

	points.Add(FVector(20, 5, 0));
	points.Add(FVector(15, 6, 0));
	points.Add(FVector(12, 7, 0));
	points.Add(FVector(11, 8, 0));
	points.Add(FVector(8, 7, 0));
	points.Add(FVector(7, 6, 0));
	points.Add(FVector(4, 5, 0));
	points.Add(FVector(3, 4, 0));
	points.Add(FVector(2, 3, 0));
	points.Add(FVector(1, 4, 0));


	TArray<FGeneratedMeshTriangle> triangles;
	Lathe(points, triangles, 128);
	mesh->SetGeneratedMeshTriangles(triangles);

	RootComponent = mesh;


Then I do get my procedural mesh. Presumably I am doing something basic, and incorrect, in the construction?

Edit:

Found the answer here.

Adding:



generatedMesh->RegisterComponent();


And everything bursts into life.

Nice, I will add an example of this to my demo on , and then back into the wiki.

[=Taces;171081]
In fact I had 2048 Threads (Two per chunk, one for Terrain Calculation and one for the Generation of the triangles) - Shame on me :smiley:
I’ll give the batching and the Chunks as components a try and reply later if it improved performance any further.

[/]

I now managed to get the batching and the Chunks as components to work, but as far as I can see there haven’t been any more performance gains.
However, it doesn’t hurt and at least the component thing is likely to improve the performance when I later implement multiplayer and create the Chunks for each player on the server.

[=Taces;176141]
I now managed to get the batching and the Chunks as components to work, but as far as I can see there haven’t been any more performance gains.
However, it doesn’t hurt and at least the component thing is likely to improve the performance when I later implement multiplayer and create the Chunks for each player on the server.
[/]

You might not notice an FPS or manual count difference but i’m sure if you actually profiled it there would be a noticeable difference just from the batching (if not the components), just be sure that ticking is disabled on the components though.

Also I (yesterday) got full chunk compression and replication working across the network and it is a bit of a pain. The RPC functions appear to quit out if the packet send is over 1024 bytes so you have to check and split the packets up if the compressed chunk is large enough, I had assumed that the RPC implementation would split these already prior to sending but I guess not.

Is there any way to do this in the editor yet? I’m looking into making my own isometric terrain system, wondering if its possible to create meshes in the editor yet.

[=cj31387;176991]
Is there any way to do this in the editor yet? I’m looking into making my own isometric terrain system, wondering if its possible to create meshes in the editor yet.
[/]

Besides BSP or terrain? I’m not sure I understand what you are looking for? A blueprint system for making meshes?

[=mordentral;176384]
You might not notice an FPS or manual count difference but i’m sure if you actually profiled it there would be a noticeable difference just from the batching (if not the components), just be sure that ticking is disabled on the components though.

Also I (yesterday) got full chunk compression and replication working across the network and it is a bit of a pain. The RPC functions appear to quit out if the packet send is over 1024 bytes so you have to check and split the packets up if the compressed chunk is large enough, I had assumed that the RPC implementation would split these already prior to sending but I guess not.
[/]

My approach for multiplayer is to only send the changed blocks from the server to the clients, since a seed always generates the same terrain it isn’t necessary to send information to the client he already knows. However, I didn’t implement it yet but I don’t see a reason why this wouldn’t work.

[=cj31387;176991]
Is there any way to do this in the editor yet? I’m looking into making my own isometric terrain system, wondering if its possible to create meshes in the editor yet.
[/]

I think he want’s to spawn a mesh in the editor and form/deform it there, just like what you can in e.g. Blender. If so it should somehow be possible but pretty complicated to accomplish.

[=Taces;178265]
My approach for multiplayer is to only send the changed blocks from the server to the clients, since a seed always generates the same terrain it isn’t necessary to send information to the client he already knows. However, I didn’t implement it yet but I don’t see a reason why this wouldn’t work.

I think he want’s to spawn a mesh in the editor and form/deform it there, just like what you can in e.g. Blender. If so it should somehow be possible but pretty complicated to accomplish.
[/]

yeah I want to know if its possible to create meshes in the editor, so i can make my own super low poly old school terrain system, and make my own terrain brushes that move a vertex up or down. Like this… ba60951fd03e83bf41a04213668aa75761eb50c1.jpeg

The reason I’m asking is because I’ve read that dynamic collision doesn’t work yet in the editor.

[=cj31387;178518]
yeah I want to know if its possible to create meshes in the editor, so i can make my own super low poly old school terrain system, and make my own terrain brushes that move a vertex up or down. Like this…
[/]

If you want to do something like this then you do not even necessarily need to create custom meshes on the fly, you can just use a few custom meshes that you combine with blueprint.

Firstly, thanks to the guys developing this; it’s really helping me in my project. I also want to state that I am a complete novice, so the way I have implemented this is probably not the best.

[=cj31387;178518]
yeah I want to know if its possible to create meshes in the editor, so i can make my own super low poly old school terrain system, and make my own terrain brushes that move a vertex up or down. Like this…

The reason I’m asking is because I’ve read that dynamic collision doesn’t work yet in the editor.
[/]

I am doing the same thing and I hope my explanation is answering what you are asking. At first, I was just using one GeneratedMeshComponent to make the top layer. It contained a dynamic 2D array of FVector’s that contained the vertex locations for it and just looped through it making 2 triangles for each tile in the construction. I am currently reimplementing this using a Tile struct to allow each tile to be an individual block with 8 vertices each rather than a “plane” (think RollerCoater Tycoon type terrain, where each tile can be raised separately).

To start this project, I had used the Top-Down shooter project originally, and in there I picked up the code for cursor interactions: -


	APlayerController* Controller = GetPlayerController();
	if (Controller != NULL)
	{
		FHitResult Hit;
		Controller->GetHitResultUnderCursor(ECC_Visibility, false, Hit);
		if (Hit.bBlockingHit)
		{
			// We hit something
			if (CursorHL != NULL)
			{
				CursorHL->UpdateLocation(Hit.ImpactPoint);
			}
		}
	}


The Hit.ImpactPoint gives you an FVector of where in the world the cursor hit an object. In my code above, I am using another class, CursorHL, that contains a cursor object which highlights the relevant tile using another GeneratedMeshComponent as that allowed me to modify it to “follow” slopes. The function UpdateLocation takes those world co-ords and “snaps” them to the grid before moving the cursor. So far, I have only implemented the terrain and haven’t had any issues; it has always recognised the “collision” of the cursor with the GeneratedMeshComponent in the editor. I believe you can specify which objects you want to check the cursor has hit, which is going to be needed later when I start adding other things on top of the terrain and I only want to highlight the terrain.

To modify the terrain in real time was a bit clumsy and would appreciate feedback on a better way of doing it. In my example, you press the LMB to raise a point or RMB to lower the point and this would then update the vextex FVector in the array. I then go around the vertex redrawing each tile in turn by creating another 2 triangles with the updated vertex location, using some logic to work out which way the diagonal would go as that would affect the way the slope looked and worked. The relevant triangles in the Triangles array are updated with these new triangles. Finally, after the updating the Triangles array for each tile as needed, the way I found to update the mesh was to use the code: -


MapChunkMesh->SetGeneratedMeshTriangles(Triangles);

where MapChunkMesh is my GeneratedMeshComponent.

Of course, when you have a large map, you don’t want to redraw the whole thing each time, so I broke it down into “chunks”. This also seemed to help performance and at the moment, I am finding 32 x 32 to be a good spot and I don’t think my maps will go above 1024 x 1024 at the moment. Once I’ve settled on a chunk size, I can probably get rid of the dynamic part of the 2D array within each chunk. I still have a dynamic 2D array of these chunks, each of which is a GeneratedMeshComponent, to allow for custom map sizes.

I hope that goes some way to answer your question, assuming I understood it correctly!

Has anybody got a separate low poly collision mesh to work with the procedural mesh? I see that the code that generates the collision meshes but I can’t see where it gets that cooked data from. Ideally I’d like a low poly collision format for my procedural mesh.

Collision creation is here:


	if(bPhysicsStateCreated)
	{
		DestroyPhysicsState();
		UpdateBodySetup();
		CreatePhysicsState();

		// Works in Packaged build only since UE4.5:
		ModelBodySetup->InvalidatePhysicsData();
		ModelBodySetup->CreatePhysicsMeshes();
	}


I can’t see how the collision mesh is bound to ProceduralMeshTris. If I could find that then I could have two forms of:


bool UProceduralMeshComponent::SetProceduralMeshTriangles(const TArray<FProceduralMeshTriangle>& Triangles)

like


bool UProceduralMeshComponent::SetProceduralMeshTriangles(const TArray<FProceduralMeshTriangle>& Triangles,const TArray<FProceduralMeshTriangle>& CollisionTriangles=NULL)

and when the CreatePhysicsMeshes() call is called, however it gets it’s geometry, I need to route to CollisionTriangles rather than Triangles.

The component gives out its collision in the function below, just reference a different vertex buffer when passing the information back to the CollisionData variable.


bool UGeneratedMeshComponent::GetPhysicsTriMeshData(struct FTriMeshCollisionData* CollisionData, bool InUseAllTriData)
{
	FTriIndices Triangle;
 
	for(int32 i=0;i<GeneratedMeshTris.Num();i++) {
		const FGeneratedMeshTriangle& tri = GeneratedMeshTris*;
 
		Triangle.v0 = CollisionData->Vertices.Add(tri.Vertex0);
		Triangle.v1 = CollisionData->Vertices.Add(tri.Vertex1);
		Triangle.v2 = CollisionData->Vertices.Add(tri.Vertex2);
 
		CollisionData->Indices.Add(Triangle);
		CollisionData->MaterialIndices.Add(i);
	}
 
	CollisionData->bFlipNormals = true;
 
	return true;
}

[=dmacesic;9185]
Here’s the example. A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

I haven’t been able to embed images (I’m a wiki noob and short on time), but there are links. Here’s what the blueprint portion looks like, really standard and simple:

[/]

Hi,

Thanks a lot for the lovely tutorial. I have one concern though, I am currently generating 4 triangles in construction script. This works way better than adjusting the position of a triangular static mesh, but performance is slow, I mean when I am changing the parameters that determine the vertex , it takes time to change , and framerate drops for few seconds until the change is done. If i generate normal mesh components ,like the other meshes you can see below , it works instantly.

Is there any way to improve the performance? Have you faced such issues when calling from construction script?

I’ve updated the source to UDK4.6 and added a pull request.

Thanks, I’ve merged it!

By the way, it’s UE4, not UK :slight_smile:

Hi guys. I’ve read through the thread, but did not find the answer for dynamic changes in the mesh. For example I have a generated land as an actor with chunks as procedural generated components.

What is the best way of changing the mesh for separate components? Should it be destroyed and then generated a new one, or there is a way to change triangles and collisions for the existing one?

I’ve tried once to delete components and generate new once on their place, but it did not work smoothly. My character simply lagging, when I try to walk on the component which is recreating itself.

You can simply call “SetGeneratedMeshTriangles” on your mesh instance and pass the new triangles to it. SetGeneratedMEshTriangles will then copy over the new triangles, update the collision mesh and mark the render state dirty, wich causes the engine to redraw the mesh.

Hope that helps :slight_smile: