How can I access the triangles of a static mesh?

I’m trying to access the triangles of a static mesh through C++. Is there a way to get them?

Cheers

I can tell you how to access the vertices, but not the triangles

what do you want to do?

Rama

There’s two things I’m trying to achieve:

  1. Calculate the normal component of a triangle;

  2. I want to deform the mesh;

Cheers

The answer is going to depend on how and why you want to access them. If you want to write a tool to deform a mesh and save those changes permanently, you want to modify the FRawMesh. That is the source data from which UE4 creates the vertex and index streams used to render at runtime. For examples of this, see UStaticMesh::GetVertexColorData and UStaticMesh::SetVertexColorData. Note that after modifying the mesh in the editor you should call PostEditChange on the mesh. That will rebuild the renderable data and make sure any static mesh instances are updated to reflect your change.

If you wish to deform the mesh at runtime, you can access the renderable data directly.E.g. MyStaticMesh->RenderData->LODResources[0].VertexBuffer.GetXXX(). In order to access the triangles you need to look at the individual sections. Each section references a material and a range of the index buffer. That range in the index buffer specifies the indices of each vertex. FEdModeFoliage::GetStaticMeshVertexColorForHit is a simple example of this.

Note that if you want to update the mesh dynamically at runtime, it will be slow. Static meshes are optimized for the vertices to not move. Recreating a static mesh’s resources at runtime will generally hitch. You probably want to find a slightly different method to handle this. E.g. dynamically updating per-vertex displacements (or a displacement map) that you read in your material and apply via World Position Offset or Displacement.

Additionally, in shipping builds we free the CPU accessible vertex and index buffers to save memory. If you need to access this data at runtime you will have to make a small code modification in FStaticMeshLODResources::Serialize to set bNeedsCPUAccess to always be true.

Hi everybody! Hi @fdjveiga @Rama @nick_p

I would like to achieve what @fdjveiga was looking for. But I guess it’s impossible via blueprints?
Sorry for answer digging, but information about mesh deformation are inexistant… :confused:

I want to increase number of vertices and change there position