Procedural Models - jgoffeney/Cesium4Unreal GitHub Wiki

Back

References

Raster Mesh Generation

project.build.cs

For my project build file I enabled C++ 17 and to the project PublicDependencyModuleNames I added my GDAL plugin, the Cesium plugin and the ProceduralMeshComponent module.

// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class GdalPlugin : ModuleRules
{
	public GdalPlugin(ReadOnlyTargetRules Target) : base(Target)
	{
		//Previous
		//PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

		PCHUsage = PCHUsageMode.NoSharedPCHs;
		PrivatePCHHeaderFile = "GdalPlugin.h";
		CppStandard = CppStandardVersion.Cpp17;

		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", 
			"LibGDAL", "CesiumRuntime", "ProceduralMeshComponent" });

		PrivateDependencyModuleNames.AddRange(new string[] { });

		PrivateIncludePaths.AddRange(
			new string[] {
				"GdalPlugin",
				// ... add other private include paths required here ...
			}
		);
		
	}
}

Generating Mesh

GDAL

The raster array data is accessed via the GDAL plugin using standard raster reading methods.

KismetProceduralMeshLibrary

Unreal contains functions for automatically generating mesh components for a regular grid as CreateGridMeshWelded which takes as inputs the grid dimensions and the Unreal spacing between vertices and generates the vertices, triangles and UVs. The vertices are of type FVector and after the function only the x and y coordinates are computed. To add elevation each vertex needs to be updated with its value of interest, such as from a raster array. Note: the source raster data should be projected with square cell sizes.

TArray<FVector> vertices;
TArray<int32> triangles;
TArray<FVector2D> uvs;

UKismetProceduralMeshLibrary::CreateGridMeshWelded(width, height, triangles, vertices, uvs, 10.0f);

The next part is to generate the vertex normals and tangents. The function CalculateTangentsForMesh uses the previously computed triangles, uvs and vertices with updated elevation.

TArray<FVector> normals;
TArray<FProcMeshTangent> tangents;

UKismetProceduralMeshLibrary::CalculateTangentsForMesh(vertices, triangles, uvs, normals, tangents);

The CreateMeshSection method of UProceduralMeshComponent sets the components to generate the mesh itself.

UProceduralMeshComponent* rasterMesh = CreateDefaultSubobject<UProceduralMeshComponent>("CustomMesh");
rasterMesh->CreateMeshSection(0, vertices, triangles, normals, uvs, TArray<FColor>(), tangents, true);
⚠️ **GitHub.com Fallback** ⚠️