UE4 C++ Procedural Terrain Generation with Perlin Noise
// Fill out your copyright notice in the Description page of Project Settings. #include "ProceduralMeshComponent.h" #include "RandomTarrain.h"
// Sets default values ARandomTarrain::ARandomTarrain() { // 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
ProceduralMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("ProceduralMesh"));
RootComponent = ProceduralMesh;
}
// Called when the game starts or when spawned void ARandomTarrain::BeginPlay() { Super::BeginPlay();
if (AutoGenerate)
{
GenerateTerrain();
}
}
// Called every frame void ARandomTarrain::Tick(float DeltaTime) { Super::Tick(DeltaTime);
}
void ARandomTarrain::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);
}
}
// Generate mesh
GenerateMesh();
}
void ARandomTarrain::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();
Normals.Add(Normal);
Normals.Add(Normal);
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 ARandomTarrain::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 ARandomTarrain::GenerateMesh()
{
// Create the procedural mesh
ProceduralMesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, Normals, UVs, TArray
原文地址: https://www.cveoy.top/t/topic/nayx 著作权归作者所有。请勿转载和采集!