#include 'ProceduralMeshComponent.h' #include 'RandomTerrain.h' #include 'Components/StaticMeshComponent.h' #include 'Engine/StaticMesh.h' #include 'Materials/MaterialInterface.h'

// Sets default values ARandomTerrain::ARandomTerrain() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true;

// Create the Runtime Mesh Component 
TerrainSizeX = 5000.f;
TerrainSizeY = 5000.f;
TerrainStep = 125.f;
HeightScale = 500.f;
Roughness = 0.8f;
TerrainMaterial = nullptr;
AutoGenerate = true;

// Create the Procedural Mesh Component
ProceduralMeshComponent = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("ProceduralMeshComponent"));

}

// Called when the game starts or when spawned void ARandomTerrain::BeginPlay() { Super::BeginPlay();

if (AutoGenerate)
{
	GenerateTerrain();
	GenerateMesh();
}

}

// Called every frame void ARandomTerrain::Tick(float DeltaTime) { Super::Tick(DeltaTime);

}

void ARandomTerrain::GenerateTerrain() { // Clear arrays Vertices.Empty(); Triangles.Empty(); UVs.Empty(); Normals.Empty();

// Define terrain area
int32 NumVerticesX = FMath::RoundToInt(TerrainSizeX / TerrainStep) + 1;
int32 NumVerticesY = FMath::RoundToInt(TerrainSizeY / TerrainStep) + 1;

// Generate terrain points
for (int32 j = 0; j < NumVerticesY; j++)
{
	for (int32 i = 0; i < NumVerticesX; i++)
	{
		float X = i * TerrainStep;
		float Y = j * TerrainStep;
		float Z = FMath::PerlinNoise2D(FVector2D(X, Y) * Roughness) * HeightScale;

		FVector Vertex(X, Y, Z);
		Vertices.Add(Vertex);
	}
}

// Generate triangles
for (int32 j = 0; j < NumVerticesY - 1; j++)
{
	for (int32 i = 0; i < NumVerticesX - 1; i++)
	{
		int32 V1 = i + j * NumVerticesX;
		int32 V2 = i + (j + 1) * NumVerticesX;
		int32 V3 = i + 1 + j * NumVerticesX;
		int32 V4 = i + 1 + (j + 1) * NumVerticesX;

		AddTriangle(V1, V2, V3);
		AddTriangle(V2, V4, V3);
	}
}

}

void ARandomTerrain::AddTriangle(int32 V1, int32 V2, int32 V3) { Triangles.Add(V1); Triangles.Add(V2); Triangles.Add(V3);

FVector Normal = FVector::CrossProduct(Vertices[V3] - Vertices[V1], Vertices[V2] - Vertices[V1]).GetSafeNormal();
for (int32 i = 0; i < 3; i++)
{
	Normals.Add(Normal);
}

UVs.Add(FVector2D(Vertices[V1].X / TerrainSizeX, Vertices[V1].Y / TerrainSizeY));
UVs.Add(FVector2D(Vertices[V2].X / TerrainSizeX, Vertices[V2].Y / TerrainSizeY));
UVs.Add(FVector2D(Vertices[V3].X / TerrainSizeX, Vertices[V3].Y / TerrainSizeY));

}

void ARandomTerrain::SubDivide(int32 V1, int32 V2, int32 V3, int32 Depth) { if (Depth == 0) { AddTriangle(V1, V2, V3); return; }

int32 V12 = Vertices.Num();
int32 V23 = V12 + 1;
int32 V31 = V12 + 2;

FVector P12 = (Vertices[V1] + Vertices[V2]) * 0.5f;
FVector P23 = (Vertices[V2] + Vertices[V3]) * 0.5f;
FVector P31 = (Vertices[V3] + Vertices[V1]) * 0.5f;

Vertices.Add(P12);
Vertices.Add(P23);
Vertices.Add(P31);

SubDivide(V1, V12, V31, Depth - 1);
SubDivide(V2, V23, V12, Depth - 1);
SubDivide(V3, V31, V23, Depth - 1);
SubDivide(V12, V23, V31, Depth - 1);

}

void ARandomTerrain::GenerateMesh() { TArray VerticesCopy = Vertices; // 由于UProceduralMeshComponent会修改顶点数组,所以需要拷贝一份 ProceduralMeshComponent->CreateMeshSection(0, VerticesCopy, Triangles, Normals, UVs, TArray(), TArray(), true); ProceduralMeshComponent->SetMaterial(0, TerrainMaterial); ProceduralMeshComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform); ProceduralMeshComponent->SetRelativeLocation(FVector(0.f, 0.f, 0.f)); ProceduralMeshComponent->SetRelativeRotation(FRotator(0.f, 0.f, 0.f)); ProceduralMeshComponent->SetRelativeScale3D(FVector(1.f, 1.f, 1.f)); }

Procedural Random Terrain Generation in Unreal Engine

原文地址: https://www.cveoy.top/t/topic/nayJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录