# 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:** 10

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 21, 2015, 3:19pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/188 "2015-02-21T15:19:24Z")

</div>

Are you also having this lighting issue with point and spot lights?

---

<div class="post-metadata">

**Author:** ![Cultrarius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cultrarius](https://forums.unrealengine.com/u/Cultrarius)\
**Post date:** [February 21, 2015, 3:29pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/189 "2015-02-21T15:29:22Z")

</div>

[=;226358]  
Are you also having this lighting issue with point and spot lights?  
[/]

Yes, I have generally the problem that my generated meshes bleed light. Say I have a generated wall and attach a light to it, then it looks fine - one side is illuminated by the light and the other side is dark. However, if I put a mesh of some sort on the dark side of the wall then the light affects it as if the wall was not there…

So, what I do is that I never use point lights, only spot lights and Directional lights. It does not really look as good as I would hope, but I also do not have the time nor the skill to find a solution. Maybe it has to do with the generated lightmap or maybe my geometry has holes or whatever - the engine somehow does not like it ^^

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 21, 2015, 4:14pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/190 "2015-02-21T16:14:02Z")

</div>

I’m having a similar issue. You can see the light bleeding into the cabinet as the camera is pulled back.

https://.com/watch?v=MnJWbElI114

Edit: adjusting the shadow bias and shadow filter sharpen for the point light seemed to fix my issue. Now I just need to figure out how to bake out lightmaps for a procedural mesh.

---

<div class="post-metadata">

**Author:** ![Fred\_FS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fred\_FS](https://forums.unrealengine.com/u/Fred_FS)\
**Post date:** [February 21, 2015, 7:07pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/191 "2015-02-21T19:07:21Z")

</div>

Playing around with the shadow parameters does not work for me (the edges get smaller, but they do not vanish).

---

<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:** [February 22, 2015, 5:51pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/192 "2015-02-22T17:51:04Z")

</div>

I don’t have any edge bleeding with mine, but I am also getting the normal in the shader.

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 23, 2015, 1:04pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/193 "2015-02-23T13:04:38Z")

</div>

[=mordentral;226948]  
I don’t have any edge bleeding with mine, but I am also getting the normal in the shader.  
[/]

Do you mean that you are using a normal mapped material? Are you still having lighting issues without getting the normal?

---

<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:** [February 23, 2015, 1:36pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/194 "2015-02-23T13:36:24Z")

</div>

I have the Tagent Space Normal box unchecked for the material and get the normal in the shader. Also it’s been months since I actually generated tangents for the mesh so I don’t know if I ever had light bleeding or not. However any mix of double sided material/single sided/disconnecting the normal I try right now can’t reproduce the issues that you guys are having.

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 24, 2015, 3:09pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/195 "2015-02-24T15:09:56Z")

</div>

Can you post an image of your material? Not having to calculate tangents would save me a ton of headache.

---

<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:** [February 24, 2015, 4:57pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/196 "2015-02-24T16:57:16Z")

</div>

![derivednormal.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/1/5/15d669fcd818bcdfebc0610ada1f3b63ceb8f4aa.jpeg)&stc=1&d=1419127040

Output of the cross goes to the normal input for the material, to use normal maps from textures you have to mask out the B channel for them and add the RG channels to the cross output. This of course is all being done on my end because I am triplaner texturing a landscape mesh, you might want to stick with calculated tangents since you seem to be generating static interior geometry and won’t have as much raw data to pass back and forth to the GPU as I do.

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 24, 2015, 6:38pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/197 "2015-02-24T18:38:10Z")

</div>

I have a couple of meshes that may benefit from your technique. Is there an extreme performance cost of calculating 2 derivatives every frame vs calculating the tangents once at build time?

---

<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:** [February 24, 2015, 11:51pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/198 "2015-02-24T23:51:17Z")

</div>

It will be slightly slower of course in the shader but if you are sending lots of data to the gpu over and over or want to reduce vertice count (indices can share verts then as the normal isn’t stored per vert) then it is worth it.

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 25, 2015, 2:13am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/199 "2015-02-25T02:13:54Z")

</div>

What are you using as the inputs to ddx and ddy? Since I’m looking at fixing the lighting on my meshes anyways, I was noticing that flipping the green channel on my normal maps gives me the correct lighting on my generated meshes. I’m using [this](http://www.terathon.com/code/tangent.html)algorithm, to calculate my tangents.

---

<div class="post-metadata">

**Author:** ![TaurusI76](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TaurusI76](https://forums.unrealengine.com/u/TaurusI76)\
**Post date:** [February 25, 2015, 4:57am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/200 "2015-02-25T04:57:29Z")

</div>

[=JohnnyBeans78;224072]  
I am stumped. I cannot get this working in 4.7. As I have said the collision mesh exists but it does not render. I am going to start fresh and do the tutorial step by step to see if I can get it working then add in the other stuff and see where it breaks (U.V.'s, vertex colors, smooth meshing, …). I am digging the VR features of 4.7 too much to stay in 4.6 any longer. It is time to bite the bullet.  
[/]

I’ve had the exact same problem since the update from 4.6.1 to 4.7. I’d be super glad if somebody had a solution…

---

<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:** [February 25, 2015, 1:29pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/201 "2015-02-25T13:29:30Z")

</div>

[=;228742]  
What are you using as the inputs to ddx and ddy? Since I’m looking at fixing the lighting on my meshes anyways, I was noticing that flipping the green channel on my normal maps gives me the correct lighting on my generated meshes. I’m using [this](http://www.terathon.com/code/tangent.html)algorithm, to calculate my tangents.  
[/]

My apologies I forgot to SS that part, it uses the Absolute World Position as the input to both.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_498fc5c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_498fc5c21](https://forums.unrealengine.com/u/anonymous_user_498fc5c21)\
**Post date:** [February 25, 2015, 1:58pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/204 "2015-02-25T13:58:33Z")

</div>

Hi!! Sorry for my bad english ) (its not my native lang).

I was edit the code and include smoothings groups support for triangles.

```auto

USTRUCT(BlueprintType)
struct ProceduralMeshTriangle
{
    GENERATED_USTRUCT_BODY()

        UPROPERTY(EditAnywhere, Category = Triangle)
        ProceduralMeshVertex Vertex0;

    UPROPERTY(EditAnywhere, Category = Triangle)
        ProceduralMeshVertex Vertex1;

    UPROPERTY(EditAnywhere, Category = Triangle)
        ProceduralMeshVertex Vertex2;

  ** *UPROPERTY(EditAnywhere, Category = Triangle)
        int32 SmoothingsGroups;***
};

```

and in to sceneproxy class

```auto

      **TArray<FVector> PositionEq;
        TArray<int32> SmoothingGroupEq;**

        // Add each triangle to the vertex/index buffer
        for (int TriIdx = 0; TriIdx < Component->ProceduralMeshTris.Num(); TriIdx++)
        {
            FProceduralMeshTriangle& Tri = Component->ProceduralMeshTris[TriIdx];

            const FVector Edge01 = (Tri.Vertex1.Position - Tri.Vertex0.Position);
            const FVector Edge02 = (Tri.Vertex2.Position - Tri.Vertex0.Position);

            const FVector TangentX = Edge01.SafeNormal();
            const FVector TangentZ = (Edge02 ^ Edge01).SafeNormal();
            const FVector TangentY = (TangentX ^ TangentZ).SafeNormal();

            int32 VIndex;
            ***int32 SG; //SmoothigGrup Buffer

            SG = Tri.SmoothingsGroups;***

            FDynamicMeshVertex Vert0;
            Vert0.Position = Tri.Vertex0.Position;
            Vert0.Color = Tri.Vertex0.Color;
            Vert0.SetTangents(TangentX, TangentY, TangentZ);
            Vert0.TextureCoordinate.Set(Tri.Vertex0.U, Tri.Vertex0.V);
            ***if (!PositionEq.Contains(Vert0.Position))
            {
                SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                PositionEq.Add(Vert0.Position);
                VIndex = VertexBuffer.Vertices.Add(Vert0);
                IndexBuffer.Indices.Add(VIndex);
            } else 
                {
                if ((SmoothingGroupEq[PositionEq.Find(Vert0.Position)] == SG))
                    {
                        IndexBuffer.Indices.Add(PositionEq.Find(Vert0.Position));
                    } else    
                        {
                            SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                            PositionEq.Add(Vert0.Position);
                            VIndex = VertexBuffer.Vertices.Add(Vert0);
                            IndexBuffer.Indices.Add(VIndex);
                        }
                }***

            FDynamicMeshVertex Vert1;
            Vert1.Position = Tri.Vertex1.Position;
            Vert1.Color = Tri.Vertex1.Color;
            Vert1.SetTangents(TangentX, TangentY, TangentZ);
            Vert1.TextureCoordinate.Set(Tri.Vertex1.U, Tri.Vertex1.V);
            ***if (!PositionEq.Contains(Vert1.Position))
            {
                SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                PositionEq.Add(Vert1.Position);
                VIndex = VertexBuffer.Vertices.Add(Vert1);
                IndexBuffer.Indices.Add(VIndex);
            } else
                {
                    if ((SmoothingGroupEq[PositionEq.Find(Vert1.Position)] == SG))
                    {
                    IndexBuffer.Indices.Add(PositionEq.Find(Vert1.Position));
                    } else
                        {
                        SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                        PositionEq.Add(Vert1.Position);
                        VIndex = VertexBuffer.Vertices.Add(Vert1);
                        IndexBuffer.Indices.Add(VIndex);
                        }
                }***
            FDynamicMeshVertex Vert2;
            Vert2.Position = Tri.Vertex2.Position;
            Vert2.Color = Tri.Vertex2.Color;
            Vert2.SetTangents(TangentX, TangentY, TangentZ);
            Vert2.TextureCoordinate.Set(Tri.Vertex2.U, Tri.Vertex2.V);
            ***if (!PositionEq.Contains(Vert2.Position))
            {
                SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                PositionEq.Add(Vert2.Position);
                VIndex = VertexBuffer.Vertices.Add(Vert2);
                IndexBuffer.Indices.Add(VIndex);
            } else
                {
                    if ((SmoothingGroupEq[PositionEq.Find(Vert2.Position)] == SG))
                    {
                        IndexBuffer.Indices.Add(PositionEq.Find(Vert2.Position));
                    } else
                        {
                            SmoothingGroupEq.Add(Tri.SmoothingsGroups);
                            PositionEq.Add(Vert2.Position);
                            VIndex = VertexBuffer.Vertices.Add(Vert2);
                            IndexBuffer.Indices.Add(VIndex);
                        }
                }***
        }

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_498fc5c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_498fc5c21](https://forums.unrealengine.com/u/anonymous_user_498fc5c21)\
**Post date:** [February 25, 2015, 2:20pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/205 "2015-02-25T14:20:35Z")

</div>

And another news: I use procedural mesh in BP and make the procedural box, but whith tesselation. U can build box with x,y,z dimensions and t polygons in each side of box.  
[https://drive.google.com/file/d/0ByXahhyru0l-VURLWWUxaW9mZWs/view?usp=sharing](https://drive.google.com/file/d/0ByXahhyru0l-VURLWWUxaW9mZWs/view?usp=sharing)

But this thing have some problems.

1. This BP based on construction script and in viewport work fine. But when i press the play button my mesh disappear.
2. This mesh contain vertex color. But vertex paint not work. I was founded in staticmeshcomponent.h struct Fpaintedvertex and thinking about including this code to the procedural mesh. But im a begginer…
3. Next step what i want to make - add material id to the fproceduraltriangles.

One week ago i was maked a BP wich build procedural house from static mesh clusters. This BP have input parameters: dimensions, number of floors, number of window and doors for each wall. And i can use vertex paint and diffrent materials. And it work fine. BUT. When i build a new house for example 5-9 floors and 10x15 meters dimensions - this BP contain 1000 static meshes. FPS down form 120 to 30. When I add a whole building of the same number of polygons from the 3d max - its ok whith FPS (120).

Thats why i need procedural mesh - generate one mesh for one building or one floor.

---

<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:** [February 25, 2015, 2:42pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/206 "2015-02-25T14:42:25Z")

</div>

[=TaurusI76;228819]  
I’ve had the exact same problem since the update from 4.6.1 to 4.7. I’d be super glad if somebody had a solution…  
[/]

Guys make sure that you actually implement

```auto

virtual void GetDynamicMeshElements(const TArray<const FSceneView*>& Views, const FSceneViewFamily& ViewFamily, uint32 VisibilityMap, FMeshElementCollector& Collector) const override

```

and

```auto

virtual void DrawStaticElements(FStaticPrimitiveDrawInterface* PDI)

```

The DrawDynamicElements() function has been on the way out for a couple of patches and they finally enforced it in this one and it doesn’t get called at all (that is why it isn’t rendering for you).

This is actually the very first major update where I didn’t have to change a single line of code to compile my project 😛

---

<div class="post-metadata">

**Author:** ![Fred\_FS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fred\_FS](https://forums.unrealengine.com/u/Fred_FS)\
**Post date:** [February 26, 2015, 12:07am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/207 "2015-02-26T00:07:42Z")

</div>

[=mordentral;228303]

![derivednormal.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/1/5/15d669fcd818bcdfebc0610ada1f3b63ceb8f4aa.jpeg)&stc=1&d=1419127040

Output of the cross goes to the normal input for the material, to use normal maps from textures you have to mask out the B channel for them and add the RG channels to the cross output. This of course is all being done on my end because I am triplaner texturing a landscape mesh, you might want to stick with calculated tangents since you seem to be generating static interior geometry and won’t have as much raw data to pass back and forth to the GPU as I do.  
[/]

Thank you for posting this. But unfortunately it does not fix my lighting problems. Currently I have no idea what I could try and it looks really awkwared if ue4 changes the shading mode for higher distances and therefore getting a bright lighed landscape in the deep of the night ;).

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [February 26, 2015, 1:42am UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/208 "2015-02-26T01:42:54Z")

</div>

Has anyone had any luck generating a procedural mesh as a static mesh? My procedural mesh actor is a child of AStaticMeshActor, and I’ve tried:

```auto

	GenerateCube(triangles, cubeSize, cubeSize, cubeSize, FVector(0.f, 0.f, 0.f), false, false, false);
	mesh->SetProceduralMeshTriangles(triangles);
	StaticMeshComponent->SetStaticMesh(Cast<UStaticMesh>(mesh));

```

with no luck. Ideally I’d like to be able to bake lighting on the mesh which isn’t possible as a procedural mesh. Is this even possible?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_498fc5c21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_498fc5c21](https://forums.unrealengine.com/u/anonymous_user_498fc5c21)\
**Post date:** [February 26, 2015, 12:45pm UTC](https://forums.unrealengine.com/t/generate-procedural-mesh/1393/209 "2015-02-26T12:45:32Z")

</div>

[=;229704]  
Has anyone had any luck generating a procedural mesh as a static mesh? My procedural mesh actor is a child of AStaticMeshActor, and I’ve tried:

```auto

	GenerateCube(triangles, cubeSize, cubeSize, cubeSize, FVector(0.f, 0.f, 0.f), false, false, false);
	mesh->SetProceduralMeshTriangles(triangles);
	StaticMeshComponent->SetStaticMesh(Cast<UStaticMesh>(mesh));

```

with no luck. Ideally I’d like to be able to bake lighting on the mesh which isn’t possible as a procedural mesh. Is this even possible?  
[/]

Yesterday i was tried too. But unreal crash whith this code. I’d like to be able to bake light and vertex paint. I see that the proceduralmeshcomponent and the staticmeshcomponent is a child of one class - meshcomponent. May be its possible to make proceduralmesh with the staticmesh opportunities. But i dont know how. Now i working with this idea. If u find how pleas send a message.

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

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