Skip to content
Snippets Groups Projects
Commit 9d797df4 authored by Yannis ROCHE's avatar Yannis ROCHE :x:
Browse files

je suis à bout

parent 8a6eda56
Branches
No related tags found
No related merge requests found
File added
File deleted
File deleted
No preview for this file type
File added
File deleted
// Fill out your copyright notice in the Description page of Project Settings.
#include "DiamondSquare.h"
#include "ProceduralMeshComponent.h"
#include "KismetProceduralMeshLibrary.h"
// Sets default values
ADiamondSquare::ADiamondSquare()
{
// 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;
ProceduralMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("ProceduralMesh"));
ProceduralMesh->SetupAttachment(GetRootComponent());
}
// Called when the game starts or when spawned
void ADiamondSquare::BeginPlay()
{
Super::BeginPlay();
}
void ADiamondSquare::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);
Vertices.Reset();
Triangles.Reset();
UV0.Reset();
CreateVertices();
CreateTriangles();
UKismetProceduralMeshLibrary::CalculateTangentsForMesh(Vertices, Triangles, UV0, Normals, Tangents);
ProceduralMesh->CreateMeshSection(0, Vertices, Triangles, Normals, UV0, TArray<FColor>(), TArray<FProcMeshTangent>(), false);
ProceduralMesh->SetMaterial(0, Material);
}
// Called every frame
void ADiamondSquare::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADiamondSquare::CreateVertices()
{
float centerX = (Scale * XSize) / 2.0f; // Milieu de la carte
float N = (Scale * XSize) * flatRatio; // Largeur de la zone plate
float M = (Scale * XSize) * slopeRatio; // Largeur de la transition
float k = 6.0f / M; // Contrle la pente de la sigmode
float actorX = GetActorLocation().X;
float actorY = GetActorLocation().Y;
float actorZ = GetActorLocation().Z;
for (int x = 0; x <= XSize; x++)
{
float distance = FMath::Abs(x * Scale - centerX);
float sigmoidFactor = 0.0f;
if (distance > N / 2.0f && distance < (N / 2.0f + M))
{
float x0 = N / 2.0f;
float x1 = N / 2.0f + M;
float xx = distance - N / 2.0f;
sigmoidFactor = xx / (x1 - x0);
}
else if (distance >= (N / 2.0f + M))
{
sigmoidFactor = 1.0f;
}
for (int y = 0; y <= YSize; y++)
{
// Gnration du bruit de Perlin
float noise = (FMath::PerlinNoise2D(FVector2D(x * NoiseScale + 0.1, y * NoiseScale + 0.1)) + 1.0f) * ZMultiplier;
// Modulation par la courbe de Gauss
float z = noise * sigmoidFactor;
Vertices.Add(FVector(x * Scale, y * Scale, z));
UV0.Add(FVector2D(x * UVScale, y * UVScale));
}
}
}
void ADiamondSquare::CreateTriangles()
{
int vertex = 0;
for (int x = 0; x < XSize; x++)
{
for (int i = 0; i < YSize; i++)
{
Triangles.Add(vertex); // Bottom left
Triangles.Add(vertex + 1); // Bottom right
Triangles.Add(vertex + YSize + 1); // Top left
Triangles.Add(vertex + 1); // Bottom right
Triangles.Add(vertex + YSize + 2); // Top right
Triangles.Add(vertex + YSize + 1); // Top left
vertex++;
}
vertex++;
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "DiamondSquare.generated.h"
class UProceduralMeshComponent;
class UMaterialInterface;
UCLASS()
class DRIVESAFE_API ADiamondSquare : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADiamondSquare();
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
int XSize;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
int YSize;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
float ZMultiplier = 1.0f;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
float NoiseScale = 1.0f;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0.000000001))
float Scale;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0.000000001))
float UVScale;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
float flatRatio = 0.1;
UPROPERTY(EditAnywhere, meta = (ClampMin = 0))
float slopeRatio = 0.1;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void OnConstruction(const FTransform& Transform) override;
UPROPERTY(EditAnywhere, Category = "Diamond Square")
UMaterialInterface* Material;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UProceduralMeshComponent* ProceduralMesh;
TArray<FVector> Vertices;
TArray<int> Triangles;
TArray<FVector2D> UV0;
TArray<FVector> Normals;
TArray<struct FProcMeshTangent> Tangents;
void CreateVertices();
void CreateTriangles();
};
...@@ -10,7 +10,7 @@ public class drivesafe : ModuleRules ...@@ -10,7 +10,7 @@ public class drivesafe : ModuleRules
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "LearningAgents", "LearningAgentsTraining", "LearningTraining" }); PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "LearningAgents", "LearningAgentsTraining", "LearningTraining" });
PrivateDependencyModuleNames.AddRange(new string[] { "LearningAgents", "LearningAgentsTraining", "LearningTraining" }); PrivateDependencyModuleNames.AddRange(new string[] { "LearningAgents", "LearningAgentsTraining", "LearningTraining", "ProceduralMeshComponent" });
// Uncomment if you are using Slate UI // Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment