Hey there,
I’m trying to implement infinite procedural generation in my game, and by following some tutorials I finally made a chunck using Perlin Noise (with the parameters set by me), but now I’m wondering how I can make it infinite and the guy I followed made a tutorial about this only using Blueprints, but not in C++ as he usually does and said he won’t make it, here is the link:
so I’m wondering if any of you can help me transfer what he is doing in Blueprints to my C++ code because I’m really new and still learning Unreal Engine.
This is the code I used for the single chunk actor class:
Triangles.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Triangles.h"
#include "ProceduralMeshComponent.h"
// Sets default values
ATriangles::ATriangles()
{
// 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>("ProceduralMesh");
ProceduralMesh->SetupAttachment(GetRootComponent());
}
// Called when the game starts or when spawned
void ATriangles::BeginPlay()
{
Super::BeginPlay();
//start
createVertices();
createTriangles();
ProceduralMesh->CreateMeshSection(0, Vertices, Triangles, TArray<FVector>(), UV0, TArray<FColor>(), TArray<FProcMeshTangent>(), true);
ProceduralMesh->SetMaterial(0, Material);
}
// Called every frame
void ATriangles::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ATriangles::createVertices()
{
for (int x = 0; x <= Xsize; x++) {
for (int y = 0; y <= Ysize; y++) {
Vertices.Add(FVector(x * scale, y * scale, 0 ));
UV0.Add(FVector2D(x * UVscale, y * UVscale));
//Debug
//DrawDebugSphere(GetWorld(), FVector(x * scale, y * scale, 0), 25.0f, 16, FColor::Red, true, -1.0f, 0U, 0.0f);
}
}
}
void ATriangles::createTriangles()
{
int vertex = 0;
for (int x = 0; x < Xsize; x++) {
for (int y = 0; y < Ysize; y++) {
//Bottom Triangle
Triangles.Add(vertex);
Triangles.Add(vertex + 1);
Triangles.Add(vertex + Ysize + 1);
//Upper Triangle
Triangles.Add(vertex + 1);
Triangles.Add(vertex + Ysize + 2);
Triangles.Add(vertex + Ysize + 1);
//Increment
vertex++;
}
vertex++;
}
}
Triangles.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Triangles.generated.h"
class UProceduralMeshComponent;
class UMaterialInterface;
UCLASS()
class PROJECTAI_API ATriangles : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATriangles();
UPROPERTY(EditAnywhere, Meta = (ClampMin = 0))
int Xsize = 0;
UPROPERTY(EditAnywhere, Meta = (ClampMin = 0))
int Ysize = 0;
UPROPERTY(EditAnywhere, Meta = (ClampMin = 0.000001))
float scale = 100;
UPROPERTY(EditAnywhere, Meta = (ClampMin = 0.000001))
float UVscale = 1;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere)
UMaterialInterface *Material;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UProceduralMeshComponent *ProceduralMesh;
TArray<FVector> Vertices;
TArray<int> Triangles;
TArray<FVector2D> UV0;
void createVertices();
void createTriangles();
};
Thanks!