From 6daa024fbc293f096d94dc8a31404d433436a00e Mon Sep 17 00:00:00 2001 From: Jay Stevens Date: Sat, 15 Jun 2019 12:07:20 -0700 Subject: [PATCH] Started adding SplineRiverComponent --- README.md | 4 +- .../Private/Rivers/NamedIslandRivers.cpp | 3 ++ .../Private/Rivers/RiverSplineComponent.cpp | 41 ++++++++++--------- .../Public/Rivers/NamedIslandRivers.h | 7 +++- .../Public/Rivers/RiverSplineComponent.h | 14 ++++--- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 64a928e..397c5e8 100644 --- a/README.md +++ b/README.md @@ -38,14 +38,14 @@ If you only want to access everything through Blueprint, you can just clone this "LoadingPhase": "Default", "AdditionalDependencies": [ - "PolygonalMapGenerator" + "IslandGenerator" ] } ], "Plugins": [ { - "Name": "PolygonalMapGenerator", + "Name": "IslandGenerator", "Enabled": true } ] diff --git a/Source/IslandGenerator/Private/Rivers/NamedIslandRivers.cpp b/Source/IslandGenerator/Private/Rivers/NamedIslandRivers.cpp index 42237a5..34dce1a 100644 --- a/Source/IslandGenerator/Private/Rivers/NamedIslandRivers.cpp +++ b/Source/IslandGenerator/Private/Rivers/NamedIslandRivers.cpp @@ -7,7 +7,10 @@ UNamedIslandRivers::UNamedIslandRivers() { RiverErosionFactor = 0.1f; FlowScale = 10.0f; + MinWaterfallHeight = 250.0f; MinRiverWidth = 30.0f; + + RiverSplineMeshType = URiverSplineComponent::StaticClass(); } void UNamedIslandRivers::AssignSideFlow_Implementation(TArray& s_flow, TArray& Rivers, UTriangleDualMesh* Mesh, const TArray& t_downslope_s, const TArray& river_t, FRandomStream& RiverRNG) const diff --git a/Source/IslandGenerator/Private/Rivers/RiverSplineComponent.cpp b/Source/IslandGenerator/Private/Rivers/RiverSplineComponent.cpp index 13812e7..0dfb540 100644 --- a/Source/IslandGenerator/Private/Rivers/RiverSplineComponent.cpp +++ b/Source/IslandGenerator/Private/Rivers/RiverSplineComponent.cpp @@ -2,13 +2,14 @@ #include "RiverSplineComponent.h" #include "NamedIslandRivers.h" +#include "Particles/ParticleSystemComponent.h" +#include "Kismet/GameplayStatics.h" USplineMeshComponent* URiverSplineComponent::CreateRiver(UNamedRiver* River, int32 SplineIndex, AActor* Parent, const UNamedIslandRivers* Rivers) { // Store variables UStaticMesh* riverMesh = Rivers->RiverMesh; UMaterialInterface* riverMaterial = Rivers->RiverMaterial; - TMap textureMap = Rivers->RiverTextures; float pointScale = Rivers->FlowScale; float minWidth = Rivers->MinRiverWidth; @@ -22,15 +23,16 @@ USplineMeshComponent* URiverSplineComponent::CreateRiver(UNamedRiver* River, int riverSpline->SetStaticMesh(riverMesh); riverSpline->bCastDynamicShadow = false; riverSpline->SetCollisionEnabled(ECollisionEnabled::NoCollision); - + // Create the river material - CreateMaterial(riverSpline, riverMaterial, textureMap); - SetupMaterialParameters(riverSpline); + RiverDynamicMaterial = CreateMaterial(riverSpline, riverMaterial); // Set the river scale - FVector scaleAtFirstPoint = (minWidth * GetScaleAtSplinePoint(SplineIndex)) + (pointScale * River->RiverFlow[SplineIndex]); + FVector rawFirstScale = GetScaleAtSplinePoint(SplineIndex); + FVector rawSecondScale = GetScaleAtSplinePoint(SplineIndex + 1); + FVector scaleAtFirstPoint = (minWidth * rawFirstScale) + (pointScale * River->RiverFlow[SplineIndex]); riverSpline->SetStartScale(FVector2D(scaleAtFirstPoint.X, scaleAtFirstPoint.Y)); - FVector scaleAtSecondPoint = (minWidth * GetScaleAtSplinePoint(SplineIndex + 1)) + (pointScale * River->RiverFlow[SplineIndex + 1]); + FVector scaleAtSecondPoint = (minWidth * rawSecondScale) + (pointScale * River->RiverFlow[SplineIndex + 1]); riverSpline->SetEndScale(FVector2D(scaleAtSecondPoint.X, scaleAtSecondPoint.Y)); // Attach it to the parent island @@ -51,24 +53,25 @@ USplineMeshComponent* URiverSplineComponent::CreateRiver(UNamedRiver* River, int return riverSpline; } -void URiverSplineComponent::CreateTangentWaterfalls() +void URiverSplineComponent::CreateTangentWaterfalls(AActor* Parent, int32 SplineIndex, const UNamedIslandRivers* Rivers) { + if (Rivers == NULL || Rivers->WaterfallParticles == NULL) + { + return; + } -} - -UMaterialInstanceDynamic* URiverSplineComponent::CreateMaterial(USplineMeshComponent* SplineMesh, UMaterialInterface* RiverMaterial, TMap TextureMap) -{ - UMaterialInstanceDynamic* riverMat = SplineMesh->CreateDynamicMaterialInstance(0, RiverMaterial); - for (auto kvp : TextureMap) + FVector splineTangent = GetTangentAtSplinePoint(SplineIndex, ESplineCoordinateSpace::Local); + if (splineTangent.Z <= (-1.0f * Rivers->MinWaterfallHeight)) { - riverMat->SetTextureParameterValue(kvp.Key, kvp.Value); + UParticleSystemComponent* waterfall = UGameplayStatics::SpawnEmitterAttached(Rivers->WaterfallParticles, this, NAME_None, GetLocationAtSplinePoint(SplineIndex, ESplineCoordinateSpace::Local), GetRotationAtSplinePoint(SplineIndex, ESplineCoordinateSpace::Local), EAttachLocation::KeepRelativeOffset); + Waterfalls.Add(waterfall); } - return riverMat; } -void URiverSplineComponent::SetupMaterialParameters(USplineMeshComponent* SplineMesh) +UMaterialInstanceDynamic* URiverSplineComponent::CreateMaterial(USplineMeshComponent* SplineMesh, UMaterialInterface* RiverMaterial) { - + UMaterialInstanceDynamic* riverMat = SplineMesh->CreateDynamicMaterialInstance(0, RiverMaterial); + return riverMat; } TArray URiverSplineComponent::CreateRiverMeshes(UNamedRiver* River, AActor* Parent, const UNamedIslandRivers* Rivers) @@ -81,8 +84,8 @@ TArray URiverSplineComponent::CreateRiverMeshes(UNamedRiv for (int32 i = 0; i < GetNumberOfSplinePoints() - 1; i++) { meshes.Add(CreateRiver(River, i, Parent, Rivers)); - CreateTangentWaterfalls(); + CreateTangentWaterfalls(Parent, i, Rivers); } - UE_LOG(LogWorldGen, Log, TEXT("Created %d river meshes for %s."), meshes.Num(), *River->RiverName); + UE_LOG(LogWorldGen, Log, TEXT("Created %d river meshes for %s, with %d waterfalls."), meshes.Num(), *River->RiverName, Waterfalls.Num()); return meshes; } \ No newline at end of file diff --git a/Source/IslandGenerator/Public/Rivers/NamedIslandRivers.h b/Source/IslandGenerator/Public/Rivers/NamedIslandRivers.h index 3b41f17..d5d6b27 100644 --- a/Source/IslandGenerator/Public/Rivers/NamedIslandRivers.h +++ b/Source/IslandGenerator/Public/Rivers/NamedIslandRivers.h @@ -40,9 +40,14 @@ public: float RiverErosionFactor; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + TSubclassOf RiverSplineMeshType; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Materials") UMaterialInterface* RiverMaterial; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UParticleSystem* WaterfallParticles; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - TMap RiverTextures; + float MinWaterfallHeight; // How much width gets added for each additional unit of river "flow." UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) diff --git a/Source/IslandGenerator/Public/Rivers/RiverSplineComponent.h b/Source/IslandGenerator/Public/Rivers/RiverSplineComponent.h index 434e459..fe6f557 100644 --- a/Source/IslandGenerator/Public/Rivers/RiverSplineComponent.h +++ b/Source/IslandGenerator/Public/Rivers/RiverSplineComponent.h @@ -18,13 +18,17 @@ class ISLANDGENERATOR_API URiverSplineComponent : public USplineComponent { GENERATED_BODY() -private: - USplineMeshComponent* CreateRiver(class UNamedRiver* River, int32 SplineIndex, AActor* Parent, const class UNamedIslandRivers* Rivers); - void CreateTangentWaterfalls(); +protected: + UPROPERTY() + TArray Waterfalls; + UPROPERTY() + UMaterialInstanceDynamic* RiverDynamicMaterial; - UMaterialInstanceDynamic* CreateMaterial(USplineMeshComponent* SplineMesh, UMaterialInterface* RiverMaterial, TMap TextureMap); - void SetupMaterialParameters(USplineMeshComponent* SplineMesh); +protected: + virtual USplineMeshComponent* CreateRiver(class UNamedRiver* River, int32 SplineIndex, AActor* Parent, const class UNamedIslandRivers* Rivers); + void CreateTangentWaterfalls(AActor* Parent, int32 SplineIndex, const class UNamedIslandRivers* Rivers); + UMaterialInstanceDynamic* CreateMaterial(USplineMeshComponent* SplineMesh, UMaterialInterface* RiverMaterial); public: TArray CreateRiverMeshes(class UNamedRiver* River, AActor* Parent, const class UNamedIslandRivers* Rivers); }; -- 2.51.2