Procedural ocean mesh

Procedural ocean mesh
This commit is contained in:
Kevin Poretti 2021-04-20 22:59:17 -04:00
parent f6ecbc44e5
commit 0e47a235a3
8 changed files with 89 additions and 13 deletions

BIN
Content/Core/BP_TestGameMode.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Environment/BP_ProcOcean.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Environment/BP_TestOcean.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Maps/ShipTest.umap (Stored with Git LFS)

Binary file not shown.

BIN
Content/Maps/ShipTest_BuiltData.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Ships/BP_TestCube.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -3,13 +3,17 @@
#include "Ocean.h" #include "Ocean.h"
#include "DrawDebugHelpers.h"
// Sets default values // Sets default values
AOcean::AOcean() AOcean::AOcean()
{ {
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. // 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; PrimaryActorTick.bCanEverTick = true;
OceanMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OceanMesh")); XSize = YSize = 10.0f;
OceanMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("OceanMesh"));
OceanMesh->SetupAttachment(RootComponent); OceanMesh->SetupAttachment(RootComponent);
OceanMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); OceanMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
@ -29,7 +33,8 @@ float AOcean::GetOceanHeight()
void AOcean::BeginPlay() void AOcean::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
GenerateMesh();
} }
// Called every frame // Called every frame
@ -37,5 +42,44 @@ void AOcean::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
for (auto Vertex : Vertices)
{
DrawDebugSphere(GetWorld(), Vertex, 2.0f, 6, FColor::Red, false, 0.0f, -1, 1.0);
}
}
void AOcean::GenerateMesh()
{
Vertices = TArray<FVector>();
Vertices.SetNum((XSize + 1) * (YSize + 1));
UV0 = TArray<FVector2D>();
UV0.SetNum(Vertices.Num());
Tangents = TArray<FProcMeshTangent>();
Tangents.SetNum(Vertices.Num());
FProcMeshTangent Tangent;
for(uint32 i = 0, y = 0; y <= YSize; y++)
{
for(uint32 x = 0; x <= XSize; x++, i++)
{
Vertices[i] = FVector(x * 100.0f, y * 100.0f, 0.0f);
UV0[i] = FVector2D((float)x/XSize, (float)y/YSize);
Tangents[i] = Tangent;
}
}
Triangles = TArray<int32>();
Triangles.SetNum(XSize * YSize * 6);
for(uint32 ti = 0, vi = 0, y = 0; y < YSize; y++, vi++)
{
for(uint32 x = 0; x < XSize; x++, ti += 6, vi++)
{
Triangles[ti] = vi;
Triangles[ti + 3] = Triangles[ti + 2] = vi + 1;
Triangles[ti + 4] = Triangles[ti + 1] = vi + XSize + 1;
Triangles[ti + 5] = vi + XSize + 2;
}
}
OceanMesh->CreateMeshSection(1, Vertices, Triangles, Normals, UV0, VertexColors, Tangents, false);
} }

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "Ocean.generated.h" #include "Ocean.generated.h"
UCLASS() UCLASS()
@ -18,14 +19,42 @@ public:
float GetOceanHeight(); float GetOceanHeight();
protected: protected:
UPROPERTY(EditDefaultsOnly, Category="Components") UPROPERTY(BlueprintReadOnly, Category="Components")
UStaticMeshComponent* OceanMesh; UProceduralMeshComponent* OceanMesh;
// XSize and YSize are the number of tiles in the mesh
UPROPERTY(EditDefaultsOnly, Category="Properties")
uint32 XSize;
UPROPERTY(EditDefaultsOnly, Category="Properties")
uint32 YSize;
UPROPERTY()
TArray<FVector> Vertices;
UPROPERTY()
TArray<int32> Triangles;
UPROPERTY()
TArray<FVector> Normals;
UPROPERTY()
TArray<FVector2D> UV0;
UPROPERTY()
TArray<FColor> VertexColors;
UPROPERTY()
TArray<FProcMeshTangent> Tangents;
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
private:
void GenerateMesh();
}; };