From 0e47a235a36472344ea4f013d34e542e9956d25e Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Apr 2021 22:59:17 -0400 Subject: [PATCH] Procedural ocean mesh Procedural ocean mesh --- Content/Core/BP_TestGameMode.uasset | 2 +- Content/Environment/BP_ProcOcean.uasset | 3 ++ Content/Environment/BP_TestOcean.uasset | 4 +-- Content/Maps/ShipTest.umap | 4 +-- Content/Maps/ShipTest_BuiltData.uasset | 2 +- Content/Ships/BP_TestCube.uasset | 4 +-- Source/SeaOfCrooks/Private/Ocean.cpp | 48 +++++++++++++++++++++++-- Source/SeaOfCrooks/Public/Ocean.h | 35 ++++++++++++++++-- 8 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 Content/Environment/BP_ProcOcean.uasset diff --git a/Content/Core/BP_TestGameMode.uasset b/Content/Core/BP_TestGameMode.uasset index 6bed780..39e1ffc 100644 --- a/Content/Core/BP_TestGameMode.uasset +++ b/Content/Core/BP_TestGameMode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c71f5f5d1fa24eaf8ec36ed307b2f2e06a29dc8aeb650493c90422705cf66b79 +oid sha256:89d29c122be67bc8eb5f7f93e8e4f24c0d0b46bb2cf962a3cd4936a7c6db714d size 18601 diff --git a/Content/Environment/BP_ProcOcean.uasset b/Content/Environment/BP_ProcOcean.uasset new file mode 100644 index 0000000..bd62385 --- /dev/null +++ b/Content/Environment/BP_ProcOcean.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:df191faa29f44c3b11438fab742d1bf79c4db940d569437e196e83e94178be44 +size 19731 diff --git a/Content/Environment/BP_TestOcean.uasset b/Content/Environment/BP_TestOcean.uasset index c7f3ef3..67bc9a5 100644 --- a/Content/Environment/BP_TestOcean.uasset +++ b/Content/Environment/BP_TestOcean.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:18945ec5b74b3f515aef8b31d293d3be44295fcd512448da1ad47f27d9534e84 -size 107579 +oid sha256:da59a4a58ac5ffa7b74f7f543bd32288f1d0f0eb088f2fe5405d73ee438aa3ae +size 1932 diff --git a/Content/Maps/ShipTest.umap b/Content/Maps/ShipTest.umap index 51e7e33..de12c53 100644 --- a/Content/Maps/ShipTest.umap +++ b/Content/Maps/ShipTest.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:85d6792a05ba0143f47208deda648c88d6674e2d778a7ca16c1e2d52ee8b211a -size 634533 +oid sha256:5df392b65d37c515491283999f9d1277b03ce25617243d135174375e06de745f +size 633471 diff --git a/Content/Maps/ShipTest_BuiltData.uasset b/Content/Maps/ShipTest_BuiltData.uasset index 3c89043..4ece473 100644 --- a/Content/Maps/ShipTest_BuiltData.uasset +++ b/Content/Maps/ShipTest_BuiltData.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a69cf4d0f9fe5232228a1797b5b762e3ecc507289569096ff04468b17067cd46 +oid sha256:2065d941ad2619b4e42f49c948cbbb047531d2a22ef11201cdf698c3ce4ddef7 size 1328744 diff --git a/Content/Ships/BP_TestCube.uasset b/Content/Ships/BP_TestCube.uasset index bf2b0cf..a0b60b4 100644 --- a/Content/Ships/BP_TestCube.uasset +++ b/Content/Ships/BP_TestCube.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:60f97c2b57e374f356a34d953f5e90a14ddc224af764c952fccacf92093027d9 -size 115733 +oid sha256:168f94c82a3d2d2cde8ec649f8408141bd3743a296ceba45054860cc321c0caa +size 108879 diff --git a/Source/SeaOfCrooks/Private/Ocean.cpp b/Source/SeaOfCrooks/Private/Ocean.cpp index 5fe0455..7ab583e 100644 --- a/Source/SeaOfCrooks/Private/Ocean.cpp +++ b/Source/SeaOfCrooks/Private/Ocean.cpp @@ -3,13 +3,17 @@ #include "Ocean.h" +#include "DrawDebugHelpers.h" + // Sets default values AOcean::AOcean() { // 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; - OceanMesh = CreateDefaultSubobject(TEXT("OceanMesh")); + XSize = YSize = 10.0f; + + OceanMesh = CreateDefaultSubobject(TEXT("OceanMesh")); OceanMesh->SetupAttachment(RootComponent); OceanMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); @@ -29,7 +33,8 @@ float AOcean::GetOceanHeight() void AOcean::BeginPlay() { Super::BeginPlay(); - + + GenerateMesh(); } // Called every frame @@ -37,5 +42,44 @@ void AOcean::Tick(float 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(); + Vertices.SetNum((XSize + 1) * (YSize + 1)); + UV0 = TArray(); + UV0.SetNum(Vertices.Num()); + Tangents = TArray(); + 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(); + 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); } diff --git a/Source/SeaOfCrooks/Public/Ocean.h b/Source/SeaOfCrooks/Public/Ocean.h index 2361fc6..186a73d 100644 --- a/Source/SeaOfCrooks/Public/Ocean.h +++ b/Source/SeaOfCrooks/Public/Ocean.h @@ -4,6 +4,7 @@ #include "CoreMinimal.h" #include "GameFramework/Actor.h" +#include "ProceduralMeshComponent.h" #include "Ocean.generated.h" UCLASS() @@ -18,14 +19,42 @@ public: float GetOceanHeight(); protected: - UPROPERTY(EditDefaultsOnly, Category="Components") - UStaticMeshComponent* OceanMesh; + UPROPERTY(BlueprintReadOnly, Category="Components") + 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 Vertices; + + UPROPERTY() + TArray Triangles; + + UPROPERTY() + TArray Normals; + + UPROPERTY() + TArray UV0; + + UPROPERTY() + TArray VertexColors; + + UPROPERTY() + TArray Tangents; // Called when the game starts or when spawned virtual void BeginPlay() override; - + public: // Called every frame virtual void Tick(float DeltaTime) override; +private: + void GenerateMesh(); + };