Unreal 4 Custom Terrain Node

A Brief Introduction

This week I started work on creating the custom nodes I need in Unreal Engine 4 to create my procedural vegetation tool.
UE4’s documentation is well none for being lacking however when it comes to creating custom blueprint nodes it is exceptionally more difficult to find useful information on the subject and I spent many hours banging my head against a brick wall getting nowhere.

My goal for this week was somehow getting access to the data contained within the Landscape class, as this is not possible to do with blueprints I had to look into custom node creation, which in Unreal engine is called a Blueprint Function Library.

Writing a custom node in C++

You can create a Blueprint Function Library by right clicking in the content browser, selecting “New C++ Class…” and selecting “Blueprint Function Library”.

When Writing a custom node you must put UFUNCTION() before your function, in this you can define how the blueprint editor will see your node, name it and define various keywords for searching. Blueprint functions must all be public and static.

Pins

What I have gathered so far is that pins are assigned as the parameters in the function signature; Pointers and Variables are inputs, References and the return type are outputs (if not void). I want to take in the Landscape object, an int to use as the level of detail for exporting the mesh and to return an array of vectors for the vertices, the function parameters are as follows:
ALandscape * landscape, int sampleLod, TArray & points

The documentation shows that the parent class of a landscape actor(ALandscape) is ALandscapeProxy which has the functionality to export the mesh data to an FRawMesh struct so I added an include for “Developer/RawMesh/Public/RawMesh.h”. The RawMesh struct is not available in Blueprints so I use the points referance, I set it to the vertex array in the raw mesh.

Problems

An important point that is missing from the documentation is that you have to manually add modules to the .build.cs file for the linker to actually link the various libraries that the RawMesh and Landscape classes are in. This is done by adding the module names to this line in the build file:
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "RawMesh", "Landscape" });
I assume that the module names are the same as the header files that you include like they were in my case however I do not know as the documentation surrounding the build system is very complicated.

Finished Code

Here’s my finished code:

// in Header
UFUNCTION(BlueprintPure, DisplayName = "Get Landscape Stuff", Category = "Landscape Helpers")
static void GetMesh(ALandscape * landscape, int sampleLod, TArray & points);

// in Cpp
void UMyFunctionLibrary::GetMesh(ALandscape * landscape, int sampleLod, TArray & points)
{
// Raw mesh structure we'll export to
FRawMesh rawMesh;
// Export the landscape to the Raw Mesh using the supplied LOD
// Higher number means lower detail, less points (faster to demo with cubes)
landscape->ExportToRawMesh(sampleLod, rawMesh);
// Finally put the vertex positions in the raw mesh to the points array referance
points = rawMesh.VertexPositions;
}

It is important to note that the Editor must be restarted after compiling a custom node for it to show up in the blueprint editor, this can be time consuming and the docs do not mention it but if you are unable to fund your nodes or they are not showing the right pins, try restarting the editor.

Here’s my node in the blueprint editor:
Node

To check that I was getting the correct data I spawned a cube at each point in the array, This is very slow when using the lower LOD value, this is because the vertices are per face, in theory meaning that all vertices are duplicated 8 times (not including on the edges of the terrain, however here’s the node setup:

terrainLevelBP.PNG

This gave the following render, a successful result.

CubesOnLandscape

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s