# How to draw debug mesh in a component visualizer?

**URL:** <https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** unreal-engine, module, editor, question\
**Created:** [October 29, 2016, 7:20pm UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836 "2016-10-29T19:20:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IAmCurrahee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IAmCurrahee](https://forums.unrealengine.com/u/IAmCurrahee)\
**Post date:** [October 29, 2016, 7:20pm UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/1 "2016-10-29T19:20:47Z")

</div>

I currently have a simple component which follows a spline. I’m looking to extend the editor to preview a given mesh which is following the spline in the editor. I currently already have my editor module and component visualizer set up. I’m able to draw points and lines, but I’m wondering what’s the easiest way to draw a given mesh.

Edit:  
My current setup is based upon [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/Component_Visualizers#Laser_Cannon_Class_Example).  
I’m doing all of the drawing in the DrawVisualization function.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [October 29, 2016, 11:05pm UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/2 "2016-10-29T23:05:15Z")

</div>

What is your current set up, what do you use to draw? and why you want to draw mesh?

---

<div class="post-metadata">

**Author:** ![IAmCurrahee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IAmCurrahee](https://forums.unrealengine.com/u/IAmCurrahee)\
**Post date:** [October 30, 2016, 12:39am UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/3 "2016-10-30T00:39:58Z")

</div>

I updated my question with some more detail on my setup. I want to draw a mesh because I want to be able to visualize a mesh moving along a spline in the editor. Let me know if you need any more info!

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [October 30, 2016, 2:53am UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/4 "2016-10-30T02:53:42Z")

</div>

Ah ok, you know preview in blueprint is a world same as game world and actor behaves like in level editor (without simulation), you could detect that actor is preview world

> **[UWorld::IsPreviewWorld](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/IsPreviewWorld/)**
>
> Returns true if this world is a preview game world (editor or game)

and if it you could spawn mesh component to display mesh (you could integrate it with the things that makes work in game actully).

I checked check the visuializer thing (i actully didn’t know about PDI it seems interesting) and it seems it has vertex drawing:

> **[FPrimitiveDrawInterface::DrawMesh](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/FPrimitiveDrawInterface/DrawMesh/)**
>
> Draw a mesh element. This should only be called through the DrawMesh function.

But i also find PDI shape drawing utilities here allowing to make simple meshes, they seems ot exist outside of UE4 convention standards, thats why you don’t see them in API refrence:

[https://github.com/EpicGames/UnrealEngine/blob/e528f9f7fa161504dd629c3b390deac93650e43a/Engine/Source/Runtime/Engine/Private/PrimitiveDrawingUtils.cpp](https://github.com/EpicGames/UnrealEngine/blob/e528f9f7fa161504dd629c3b390deac93650e43a/Engine/Source/Runtime/Engine/Private/PrimitiveDrawingUtils.cpp)

I tried to search UStaticMesh drawing but could not find anything useful, if anything it it links to mesh components.

---

<div class="post-metadata">

**Author:** ![IAmCurrahee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IAmCurrahee](https://forums.unrealengine.com/u/IAmCurrahee)\
**Post date:** [October 30, 2016, 9:13am UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/5 "2016-10-30T09:13:46Z")

</div>

Thanks for the help! I’m still new to programming in Unreal so I have a few more questions.

For the draw mesh with PDI, is there a way to get the FMeshBatch data from a UStaticMesh/UStaticMeshComponent? I saw this method earlier and it seemed promising as it doesn’t require the creation of any new actors/components.

Also, if I were to spawn a new actor with a mesh component is there a way to have not be serialized/permanent? Do you know of any examples of code doing such a thing?

---

<div class="post-metadata">

**Author:** ![Dealman](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dealman](https://forums.unrealengine.com/u/Dealman)\
**Post date:** [December 4, 2023, 3:33pm UTC](https://forums.unrealengine.com/t/how-to-draw-debug-mesh-in-a-component-visualizer/374836/6 "2023-12-04T15:33:39Z")

</div>

While this post is very old, I’ll post an answer here as I had a gander at this as well and came across this thread during my journey. Do note that this answer is for UE 5.3, older versions may not work the same.

First of all, you need to grab the vertices and indices that make up the mesh - this assumes you’ve got a valid reference to a `UStaticMesh`, but you can also get the `UStaticMesh` from a `UStaticMeshComponent`.

You can then fetch the data like this;

```cpp
// Preferrably this is event-driven, and not done at runtime
// Reset the arrays if you change/update the mesh
Vertices.Reset(); // TArray<FVector>
Indices.Reset(); // TArray<int32>

// Get the mesh description, here you can specify a specific LOD
UStaticMeshDescription* MeshDescription = MyStaticMesh->GetStaticMeshDescription(0);
const FVertexArray& VertexArray = MeshDescription->Vertices();
const FTriangleArray& TriangleArray = MeshDescription->Triangles();

// Store all the vertex positions (local space)
for (FVertexID VertexID : VertexArray.GetElementIDs())
{
	FVector VertexLocation = MeshDescription->GetVertexPosition(VertexID);
	Vertices.Add(VertexLocation);
}

// Iterate through all the triangles to get the indices
for (FTriangleID TriangleID : TriangleArray.GetElementIDs())
{
	// Get the triangle vertex instances, these are the indices - weird naming
	// there should always be 3 vertices per triangle
	TArray<FVertexInstanceID> VertexInstanceIds;
	MeshDescription->GetTriangleVertexInstances(TriangleID, VertexInstanceIds);
	for (const FVertexInstanceID VertexInstanceID : VertexInstanceIds)
	{
		FVertexID VertexID = MeshDescription->GetVertexInstanceVertex(VertexInstanceID);
		Indices.Add(static_cast<int32>(VertexID));
	}
}

```

In order to draw the actual mesh, you need to include `"SceneManagement.h"`, this contains _a lot_ of various drawing methods. Specifically you want to use this;

```cpp
void DrawDebugMesh(const UWorld* InWorld, TArray<FVector> const& Verts, TArray<int32> const& Indices, FColor const& Color, bool bPersistent=false, float LifeTime=-1.f, uint8 DepthPriority = 0);

```

Do note that this requires a `UWorld` pointer and not a `FPrimitiveDrawingInterface`. If you’re working with component visualizers like me - this is easy enough to get from the `DrawVisualization` method.

Furthermore, do note that you must specify a `LifeTime` value that is greater than `0.0f`. I believe they expect you to use a DeltaTime, but component visualizers do not provide this as far as I’m aware.

For example, this is how I draw mine;

```cpp
UWorld* World = MyComponent->GetWorld();
TArray<FVector> ComponentSpaceVertices;
for (auto Vertex : MyComponent->Vertices)
{
	ComponentSpaceVertices.Add(MyComponent.GetLocation() + Vertex);
}
DrawDebugMesh(World, ComponentSpaceVertices, MyComponent->Indices, FColor::Green, false, 0.16f, SDPG_Foreground);

```

This is all pseudocode and not exactly how I work with mine, but it should be enough to convey the idea of how it works.

Also do note, that this drawn mesh _is not_ wireframed as I thought it would be. Which honestly is quite weird, or at least that there is no option to draw the mesh exclusively with lines.

That said, you now have the ability to get the data you need from the mesh description in order to draw lines if you want a wireframed mesh. You can also get edges similarly to how you get triangles.
