# Generate Procedural Mesh

**URL:** <https://forums.unrealengine.com/t/generate-procedural-mesh/1393>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 30, 2014, 5:34am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393 "2014-03-30T05:34:58Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![Mars007](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mars007](https://forums.unrealengine.com/u/Mars007)\
**Post date:** [October 1, 2014, 9:43am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/84 "2014-10-01T09:43:25Z")

</div>

As a proof of concept, i´ve created a procedural city (screenshot attached).  
You can choose how many roads, buildings, parks and so on you want, and my class spawns them in the editor.

As far as i can see, FGeneratedMeshVertexFactory is not compatible with static lighting, because it crashes when i hit the Rebuild-button.

 ![Crash.PNG](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/f/27fdec2e3642ef9e8b4ad8abd5a4512e0c5c9f8e.png)

Is there a trick, to use static lighting never the less?

---

<div class="post-metadata">

**Author:** ![mordentral](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mordentral](https://forums.unrealengine.com/u/mordentral)\
**Post date:** [October 1, 2014, 6:08pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/85 "2014-10-01T18:08:06Z")

</div>

[=Mars007;156068]  
As a proof of concept, i´ve created a procedural city (screenshot attached).  
You can choose how many roads, buildings, parks and so on you want, and my class spawns them in the editor.

As far as i can see, FGeneratedMeshVertexFactory is not compatible with static lighting, because it crashes when i hit the Rebuild-button.

 ![Crash.PNG](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/f/27fdec2e3642ef9e8b4ad8abd5a4512e0c5c9f8e.png)

Is there a trick, to use static lighting never the less?  
[/]

Yeah as it shows in the screenshot itself it is an ASSERT that throws if the process is trying to initialize the vertex factory inside the rendering thread. You can remove the assert and handle that part of the code getting called cleanly instead.

I ran into that awhile ago and changed it so it worked with what I was doing.

```auto

	/** Initialization */
	void Init(const FGeneratedMeshVertexBuffer* VertexBuffer)
	{
		**check(!IsInRenderingThread());**
 
		ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
			InitGeneratedMeshVertexFactory,
			FGeneratedMeshVertexFactory*,VertexFactory,this,
			const FGeneratedMeshVertexBuffer*,VertexBuffer,VertexBuffer,
		{
			// Initialize the vertex factory's stream components.
			DataType NewData;
			NewData.PositionComponent = STRUCTMEMBER_VERTEXSTREAMCOMPONENT(VertexBuffer,FDynamicMeshVertex,Position,VET_Float3);
			NewData.TextureCoordinates.Add(
				FVertexStreamComponent(VertexBuffer,STRUCT_OFFSET(FDynamicMeshVertex,TextureCoordinate),sizeof(FDynamicMeshVertex),VET_Float2)
				);
			NewData.TangentBasisComponents[0] = STRUCTMEMBER_VERTEXSTREAMCOMPONENT(VertexBuffer,FDynamicMeshVertex,TangentX,VET_PackedNormal);
			NewData.TangentBasisComponents[1] = STRUCTMEMBER_VERTEXSTREAMCOMPONENT(VertexBuffer,FDynamicMeshVertex,TangentZ,VET_PackedNormal);
			NewData.ColorComponent = STRUCTMEMBER_VERTEXSTREAMCOMPONENT(VertexBuffer, FDynamicMeshVertex, Color, VET_Color);
			VertexFactory->SetData(NewData);
		});
	}

```

---

<div class="post-metadata">

**Author:** ![SRombauts](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SRombauts](https://forums.unrealengine.com/u/SRombauts)\
**Post date:** [October 1, 2014, 7:55pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/86 "2014-10-01T19:55:11Z")

</div>

Golding (Epic) stated previously that dynamic meshes are not compatible with static lighting:

[=JamesG;9597]  
Right now this geometry is slightly slower than a regular static mesh, and doesn’t support things like precomputed lighting.  
[/]

[=mordentral;156360]  
You can remove the assert and handle that part of the code getting called cleanly instead.  
[/]

So how do you do that? I mean, after removing the assert on line 44, what do you change on the code to make it support precomputed lighting?

Cheers!

---

<div class="post-metadata">

**Author:** ![Mars007](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mars007](https://forums.unrealengine.com/u/Mars007)\
**Post date:** [October 1, 2014, 9:15pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/87 "2014-10-01T21:15:18Z")

</div>

This was an easy fix 🙂  
I thought this check prevents the engine from doing something bad.  
But as i removed the IsInRenderingThread check, i got back static lighting for static meshes.  
Dynamic meshes have to use dynamic lighting, as stated above.

---

<div class="post-metadata">

**Author:** ![mordentral](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mordentral](https://forums.unrealengine.com/u/mordentral)\
**Post date:** [October 1, 2014, 10:02pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/88 "2014-10-01T22:02:52Z")

</div>

[=;156409]  
Golding (Epic) stated previously that dynamic meshes are not compatible with static lighting:

So how do you do that? I mean, after removing the assert on line 44, what do you change on the code to make it support precomputed lighting?

Cheers!  
[/]

The reason it doesn’t work normally is because the example loads after the game has loaded so any baked lighting will obviously be incorrect, also if it changes then the baked lighting will also be wrong. If you have a mesh that is generated and never changes and is generated in engine prior to the baking process then it works just fine.

---

<div class="post-metadata">

**Author:** ![Sgt\_Ludwig](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sgt\_Ludwig](https://forums.unrealengine.com/u/Sgt_Ludwig)\
**Post date:** [October 10, 2014, 9:22pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/89 "2014-10-10T21:22:51Z")

</div>

Hey guys! I’ve been trying to get this to work because it is critical for the game I’d like to start building. Basically I’m recreating a Tychoon-style terrain system and a procedural mesh with collision is the core component.

In Unity cranking out a procedural mesh was a piece of cake. So far in Unreal it’s been impossible. I’ve tried tinkering with as much stuff as I can but nothing seems to work. I keep getting the following error:

```auto

Error	2	error C2664: 'FVertexBufferRHIRef RHICreateVertexBuffer(uint32,uint32,FRHIResourceCreateInfo &)' : cannot convert argument 3 from 'EBufferUsageFlags' to 'FRHIResourceCreateInfo &'	C:\Users\...]\Source\ProMesh43\GeneratedMeshComponent.cpp	19	1	ProMesh43

```

I’ve tried fiddling with things, but I either get linking errors or errors that all kinds of behind the scenes functions take different arguments. I’ve tried this with 4.3, 4.4 and the 4.5 preview.

I realize this isn’t exactly plug and play, but has anyone else had these problems? (I’m new to C++ and don’t have the skills to get elbow deep in UE4)

---

<div class="post-metadata">

**Author:** ![Sgt\_Ludwig](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sgt\_Ludwig](https://forums.unrealengine.com/u/Sgt_Ludwig)\
**Post date:** [October 10, 2014, 9:58pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/90 "2014-10-10T21:58:29Z")

</div>

Nevermind… I forgot to add RHI, RenderCore and ShadeCore modules to my build file. Derp.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [October 11, 2014, 5:41am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/91 "2014-10-11T05:41:20Z")

</div>

[=;150736]  
Hey guys!

Thanks to Ori Cohen we got PhysX Cooking in runtime:

- https://.com/EpicGames/UnrealEngine/commit/167a797ef8ed7062432f6449f97c4a408b3ea73b
- https://.com/EpicGames/UnrealEngine/commit/7a01d572a98c9959e6ee48d9643c991751e59e72

This is still in the master though, but you should be able to integrate it into your code without effort.

Cheers,

[/]

Thanks for sharing those links !

I’ve been hoping and wishing for this since 4.0 !

Fully functional dynamic meshes at runtime, runtime collision changes, yaaaay!

---

<div class="post-metadata">

**Author:** ![SRombauts](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SRombauts](https://forums.unrealengine.com/u/SRombauts)\
**Post date:** [October 13, 2014, 8:58pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/92 "2014-10-13T20:58:48Z")

</div>

Hi, thank you all for the demo code of Procedural Mesh Generation !

To help people get the code working quickly, I’ve created a simple “ProceduralMesh” project featuring the demo code shown on the Wiki; its [here on (UE4ProceduralMesh)](https://.com//UE4ProceduralMesh/)

Cheers

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [October 14, 2014, 7:10am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/94 "2014-10-14T07:10:20Z")

</div>

[=;163155]

To help people get the code working quickly, I’ve created a simple “ProceduralMesh” project featuring the demo code shown on the Wiki; its [here on (UE4ProceduralMesh)](https://.com//UE4ProceduralMesh/)  
[/]

Cool! Hope we can use that one to improve the code!

---

<div class="post-metadata">

**Author:** ![Kinsmen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kinsmen](https://forums.unrealengine.com/u/Kinsmen)\
**Post date:** [October 14, 2014, 5:40pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/95 "2014-10-14T17:40:38Z")

</div>

This is great!

---

<div class="post-metadata">

**Author:** ![Kinsmen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kinsmen](https://forums.unrealengine.com/u/Kinsmen)\
**Post date:** [October 14, 2014, 5:42pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/96 "2014-10-14T17:42:28Z")

</div>

Hello, quick question. In my blueprint… If I call the below function at every tick, will this animate any changes I had made to the mesh? I mean, is this the right way to do it?

[]

void AGameGeneratedMesh::UpdateMesh(TArray\<FAtom\> theUniverse)  
{

```
TArray&lt;FCustomMeshTriangle&gt; triangles;

AGameGeneratedMesh::Cubing(triangles, theUniverse);

mesh-&gt;SetCustomMeshTriangles(triangles);
RootComponent = mesh;

```

}  
[/]

---

<div class="post-metadata">

**Author:** ![Taces](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Taces](https://forums.unrealengine.com/u/Taces)\
**Post date:** [October 15, 2014, 9:09pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/97 "2014-10-15T21:09:27Z")

</div>

Hey,  
first a big thanks for the Procedural Mesh Tutorial and the code! As far as I can see we are using FDynamicMeshVertex inside the constructor of the scene proxy, therefore it should be possible to set the color of the vertices, which is done by “const FColor VertexColor(255, 255, 255);” in the wiki code, as VertexColor is later assigned to the vertices. But no matter what values i choose for VertexColor, my triangles stay black. Possibly I get the concept of vertex colors wrong, but shouldn’t the whole triangle be like VertexColor if I’m assigning it to every vertice of the triangle?  
Alternatively I’ll try to use a Texture for my mesh tomorrow, but as I only want the triangles/quads to have a single color it would be a bit “overpowered” to use a 1px_1px Texture (or 2_2 whatever, simply that 1 pixel on the texture will represent one color) I think and in addition it would be much more flexible to be able to use FColor or anything alike I can simply adjust in code. (I haven’t got it working to get a MID on the mesh either).

Thanks in advance,  
Taces

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_52a8cf78](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_52a8cf78](https://forums.unrealengine.com/u/anonymous_user_52a8cf78)\
**Post date:** [October 16, 2014, 6:52am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/98 "2014-10-16T06:52:08Z")

</div>

I have managed to generate a mesh using the tutorial, but I cannot get collision with it to work. The mesh is static and a big part of the level, which the ship will fly around in. But the ship doesn’t collide with the mesh, it just flys through it. It does collide with other walls I have placed using the editor. I have enabled collision blocking for everything for the mesh. The ship has physics simulation enabled so I believe this may be the reason it doesn’t collide, as on this [pagedocs.unrealengine.com/latest/INT/Engine/Physics/Collision/index.html](http://pagedocs.unrealengine.com/latest/INT/Engine/Physics/Collision/index.html) it says complex collision is never used when simulating physics, and I interpret that as it will never collide with the triangles of the generated mesh. So I guess I have to somehow generate a set of simplified collision primitives for the generated mesh. How would I go about doing this? Is the functionality of the editor for generating the k-DOP collision primitives available in the API somewhere?

Thanks!

---

<div class="post-metadata">

**Author:** ![Taces](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Taces](https://forums.unrealengine.com/u/Taces)\
**Post date:** [October 16, 2014, 8:55pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/99 "2014-10-16T20:55:30Z")

</div>

I now tried to assign a simple material to my mesh, but somehow it isn’t rendered. If I check the size of Component-\>Materials inside the FPrimitiveSceneProxy\* UGeneratedMeshComponent::CreateSceneProxy() function, it is 0, although I added the Material to the array before. This is the constructor of my Actor:

```auto

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

	TArray<FGeneratedMeshTriangle> points;

	FGeneratedMeshTriangle tri;

	tri.Vertex0.Position = FVector(0, 0, 0);
	tri.Vertex1.Position = FVector(100, 0, 100);
	tri.Vertex2.Position = FVector(0, 0, 100);

	//points.Add(tri);

	tri.Vertex0.Position = FVector(100, 0, 100);
	tri.Vertex1.Position = FVector(0, 0, 0);
	tri.Vertex2.Position = FVector(100, 0, 0);

	points.Add(tri);

	mesh->SetGeneratedMeshTriangles(points);

	static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/TestMaterial.TestMaterial'"));
	UMaterial* Material = MatFinder.Object;
	UMaterialInstanceDynamic* DynMat1 = UMaterialInstanceDynamic::Create(Material, this);
	DynMat1->SetVectorParameterValue("Color", FLinearColor(1.f, 0.f, 0.f));
        UE_LOG(LogTemp, Warning, TEXT("Terrain Before: %d"), mesh->Materials.Num());
	mesh->SetMaterial(0, DynMat1);
	if (DynMat1 == NULL)
	{
		UE_LOG(LogTemp, Warning, TEXT(" DMI NULL"));
	}
	else 
	{
		UE_LOG(LogTemp, Warning, TEXT(" DMI NOT NULL"));
	}
	UE_LOG(LogTemp, Warning, TEXT("Terrain: %d"), mesh->Materials.Num());
	RootComponent = mesh;

```

Here the log says:

[]  
LogTemp:Warning: Terrain Before: 0  
LogTemp:Warning: DMI NOT NULL  
LogTemp:Warning: Terrain: 1  
[/]

And in the scene proxy:

```auto

UE_LOG(LogTemp, Warning, TEXT("GMC: %d"), Component->Materials.Num());
			Material = Component->GetMaterial(0);
			

			if (Material == NULL)
			{
				UE_LOG(LogTemp, Warning, TEXT("NULL"));
				Material = UMaterial::GetDefaultMaterial(MD_Surface);
			}
			else UE_LOG(LogTemp, Warning, TEXT("NOT NULL"));

```

and it says:

[]

LogTemp:Warning: Ret: 0  
LogTemp:Warning: Ret: 0  
LogTemp:Warning: GMC: 0  
LogTemp:Warning: NULL  
LogTemp:Warning: Ret: 0  
[/]

“Ret: 0” comes from here:

```auto

int32 UGeneratedMeshComponent::GetNumMaterials() const
{
	UE_LOG(LogTemp, Warning, TEXT("Ret: %d"), Materials.Num());
	return 1;
}

```

If I change this to “return Materials.Num()” the triangle isn’t rendered at all, since it then always returns 0…

Sorry for the much code, but do you have any idea how to fix this issue?

Thanks,  
Taces

---

<div class="post-metadata">

**Author:** ![Sgt\_Ludwig](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sgt\_Ludwig](https://forums.unrealengine.com/u/Sgt_Ludwig)\
**Post date:** [October 17, 2014, 2:11am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/100 "2014-10-17T02:11:04Z")

</div>

Working with UE4.5

I’m working on getting this running with UE4.5…

I am getting the following errors:

```auto

Error	2	error C2660: 'UMeshComponent::GetMaterialRelevance' : function does not take 0 arguments	C:\...]\GeneratedMeshComponent.cpp	118	1	MGTycoon
Error	3	error C2660: 'CreatePrimitiveUniformBufferImmediate' : function does not take 4 arguments	C:\...]\GeneratedMeshComponent.cpp	209	1	MGTycoon

```

It seems like the signatures changed for these back-end functions?

Just stabbing around I got it to compile with:

```auto

118| , MaterialRelevance(Component->GetMaterialRelevance(ERHIFeatureLevel::Num))
...|
209| BatchElement.PrimitiveUniformBuffer = CreatePrimitiveUniformBufferImmediate(GetLocalToWorld(), GetBounds(), GetLocalBounds(), true, true);

```

Since I don’t know what this does exactly, I was wondering if anybody knows what I should actually be doing to solve this problem.

---

<div class="post-metadata">

**Author:** ![mordentral](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mordentral](https://forums.unrealengine.com/u/mordentral)\
**Post date:** [October 17, 2014, 1:00pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/101 "2014-10-17T13:00:10Z")

</div>

[=Sgt\_Ludwig;165019]  
I’m working on getting this running with UE4.5…

I am getting the following errors:

```auto

Error	2	error C2660: 'UMeshComponent::GetMaterialRelevance' : function does not take 0 arguments	C:\...]\GeneratedMeshComponent.cpp	118	1	MGTycoon
Error	3	error C2660: 'CreatePrimitiveUniformBufferImmediate' : function does not take 4 arguments	C:\...]\GeneratedMeshComponent.cpp	209	1	MGTycoon

```

It seems like the signatures changed for these back-end functions?

[/]

For the material relevance I pulled from the custom mesh they package in with releases for this:

```auto

Component->GetMaterialRelevance(GetScene()->GetFeatureLevel())

```

For the final argument of ‘CreatePrimitiveUniformBufferImmediate’ I get sporadic crashes if I don’t True it.

---

<div class="post-metadata">

**Author:** ![Taces](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Taces](https://forums.unrealengine.com/u/Taces)\
**Post date:** [October 17, 2014, 2:59pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/102 "2014-10-17T14:59:41Z")

</div>

If anybody else has the same issue as I did (Material is not applied), I figured out how to do it: Instead of applying the material inside the constructor of my actor, I had to apply it in “PostInitializeComponents” - this way everything works fine! Although I don’t really understand why it doesn’t work in the constructor, it would be great if somebody could explan that? I’d suppose that the components are recreated after the constructor of my actor, but I dont’ know why this should happen.

---

<div class="post-metadata">

**Author:** ![Sgt\_Ludwig](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sgt\_Ludwig](https://forums.unrealengine.com/u/Sgt_Ludwig)\
**Post date:** [October 17, 2014, 3:02pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/103 "2014-10-17T15:02:37Z")

</div>

Thanks mordentral!

---

<div class="post-metadata">

**Author:** ![Ryvar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ryvar](https://forums.unrealengine.com/u/Ryvar)\
**Post date:** [October 22, 2014, 12:37am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/104 "2014-10-22T00:37:49Z")

</div>

Apologies for the late reply, Taces, but I think [my posts in this thread](https://forums.unrealengine.com/showthread.php?7764-Passing-custom-information-to-material-from-procedural-mesh) might clear up a few things for you re: VertexColor.

Hope that helps!

[Previous page](https://forums.unrealengine.com/t/generate-procedural-mesh/1393.md?page=4)

[Next page](https://forums.unrealengine.com/t/generate-procedural-mesh/1393.md?page=6)
