使用 C++ 编写一个可以在 Unreal Engine 中使用的类,它具有以下功能:

  1. 具有两个浮点数输入接口,分别是坐标 x 和坐标 y,可以根据输入的值定义一个二维区域。
  2. 根据定义的二维区域,生成与整数坐标值对应的点。
  3. 为生成的点添加随机的高度值,范围是 0~255。
  4. 根据调整后的点生成 mesh 内容。
#include "MyClass.h"
#include "RuntimeMeshComponent.h"

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

    // Create the Runtime Mesh Component
    MeshComponent = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMesh"));
}

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

    // Generate the mesh
    GenerateMesh();
}

// Generate the mesh
void AMyClass::GenerateMesh()
{
    // Define the 2D region based on X and Y inputs
    FVector2D RegionSize = FVector2D(XInput, YInput);

    // Calculate the number of vertices needed based on the region size
    int32 NumVertices = RegionSize.X * RegionSize.Y;

    // Create an array to hold the vertices
    TArray<FVector> Vertices;
    Vertices.SetNum(NumVertices);

    // Loop through each vertex and calculate its position
    for (int32 Y = 0; Y < RegionSize.Y; Y++)
    {
        for (int32 X = 0; X < RegionSize.X; X++)
        {
            // Calculate the position of the vertex
            FVector VertexPosition = FVector(X, Y, 0);

            // Add a random height value to the vertex
            VertexPosition.Z = FMath::RandRange(0, 255);

            // Add the vertex to the array
            Vertices[Y * RegionSize.X + X] = VertexPosition;
        }
    }

    // Create an array to hold the triangles
    TArray<int32> Triangles;
    Triangles.SetNum((RegionSize.X - 1) * (RegionSize.Y - 1) * 6);

    // Loop through each quad and create two triangles
    for (int32 Y = 0; Y < RegionSize.Y - 1; Y++)
    {
        for (int32 X = 0; X < RegionSize.X - 1; X++)
        {
            // Calculate the indices of the four vertices that make up the quad
            int32 TopLeftIndex = Y * RegionSize.X + X;
            int32 TopRightIndex = Y * RegionSize.X + X + 1;
            int32 BottomLeftIndex = (Y + 1) * RegionSize.X + X;
            int32 BottomRightIndex = (Y + 1) * RegionSize.X + X + 1;

            // Add the two triangles that make up the quad
            Triangles.Add(TopLeftIndex);
            Triangles.Add(BottomLeftIndex);
            Triangles.Add(TopRightIndex);
            Triangles.Add(TopRightIndex);
            Triangles.Add(BottomLeftIndex);
            Triangles.Add(BottomRightIndex);
        }
    }

    // Create the mesh section
    MeshComponent->CreateMeshSection(0, Vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), true);
}

该类使用 RuntimeMeshComponent 来生成网格,并将生成的网格添加到场景中。你可以在 Unreal Engine 中创建一个新的蓝图类,并使用这个类生成随机高度网格。

这个例子只是简单演示了如何使用 C++ 生成随机高度网格,你可以根据自己的需求进行修改和扩展。例如,你可以添加一些参数来控制网格的生成方式,例如高度范围、网格的密度等等。

希望这个例子能够帮助你学习如何使用 C++ 在 Unreal Engine 中生成自定义网格。

Unreal Engine C++ 类:生成随机高度网格

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

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