Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "AChunk.h"
- #include "RuntimeMeshComponent.h"
- #include "SimplexNoiseBPLibrary.h"
- #include "UMapGeneration.h"
- // Sets default values
- AAChunk::AAChunk() : x(0), y(0), z(0), isGenerate(false), index(-1), maxIndex(0), tickNumber(0) {
- // 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;
- RuntimeMesh = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMesh"));
- }
- AAChunk::AAChunk(int32 pX, int32 pY) : z(0), isGenerate(false), index(-1), maxIndex(0), tickNumber(0), isCalculated(false), verticesIndex(0), trianglesIndex(0), uvsIndex(0), normalsIndex(0) {
- // 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;
- RuntimeMesh = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMesh"));
- x = pX;
- y = pY;
- }
- // Called when the game starts or when spawned
- void AAChunk::BeginPlay()
- {
- Super::BeginPlay();
- maxIndex = pow(numberBlockOnLine, 2.0)*numberBlockOnZ;
- listBlocks.SetNum(maxIndex);
- }
- // Called every frame
- void AAChunk::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (!isGenerate) {
- tickNumber = (tickNumber+1)%3;
- if (tickNumber == 0) {
- // First part of generation
- int32 loopI(0);
- while (loopI < 5001) {
- index++;
- if (index >= maxIndex) {
- isGenerate = true;
- index = -1;
- break;
- }
- else {
- int32 tempCount = numberBlockOnZ * numberBlockOnLine;
- int32 tempX = ((index - (index%tempCount)) / tempCount) * blockSize + (x*numberBlockOnLine*blockSize);
- int32 tempY = (((index - (index%numberBlockOnZ)) / numberBlockOnZ)% numberBlockOnLine) * blockSize + (y*numberBlockOnLine*blockSize);
- int32 tempZ = (index%numberBlockOnZ)*blockSize + z * numberBlockOnZ*blockSize;
- FBlockStructure tempStructure(tempX, tempY, tempZ, getBlockIdByPosition(FVector(tempX, tempY, tempZ)));
- listBlocks[index] = tempStructure;
- }
- loopI++;
- }
- }
- }else {
- if (index < (maxIndex - 1)) {
- tickNumber = (tickNumber + 1) % 3;
- if (tickNumber == 0) {
- if (!isCalculated) {
- int32 loopI(0);
- while (loopI < 5001) {
- index++;
- if (index >= maxIndex) {
- isCalculated = true;
- vertices.SetNum(verticesIndex);
- triangles.SetNum(trianglesIndex);
- uvs.SetNum(uvsIndex);
- normals.SetNum(normalsIndex);
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("verticesIndex : " + FString::FromInt(verticesIndex)));
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("trianglesIndex : " + FString::FromInt(trianglesIndex)));
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("uvsIndex : " + FString::FromInt(uvsIndex)));
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("normalsIndex : " + FString::FromInt(normalsIndex)));
- verticesIndex = 0;
- trianglesIndex = 0;
- uvsIndex = 0;
- normalsIndex = 0;
- index = -1;
- break;
- }else {
- getListsSize(listBlocks[index]);
- }
- loopI++;
- }
- }
- else {
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("OK c'est bon ! : "));
- int32 loopI(0);
- while (loopI < 5001) {
- index++;
- if (index >= maxIndex) {
- loadMap();
- tickNumber = 0;
- break;
- }
- else {
- if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Index : " + FString::FromInt(index)));
- reloadBlock(EBlockAction::PLACE, listBlocks[index]);
- }
- loopI++;
- }
- }
- }
- }
- }
- }
- void AAChunk::getListsSize(FBlockStructure const& pBlock) {
- if (blockIsHere(FVector(pBlock.x, pBlock.y, pBlock.z)) && pBlock.id != 1) {
- for (EBlockFace face : getBlocksArround(getBlockLocation(pBlock), true)) {
- verticesIndex += 4;
- trianglesIndex += 2;
- uvsIndex += 4;
- normalsIndex += 4;
- }
- }
- }
- void AAChunk::loadMap() {
- TArray<FRuntimeMeshTangent> Tangents;
- TArray<FColor> VertexColors;
- VertexColors.Init(FColor(0.0f, 0.0f, 0.0f, 1.0f), vertices.Num());
- Tangents.Init(FRuntimeMeshTangent(1.0f, 0.0f, 0.0f), vertices.Num());
- /* if (GEngine)
- GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("A : " + FString::FromInt(vertices.Num()) + " " + FString::FromInt(triangles.Num()))); */
- RuntimeMesh->CreateMeshSection(0, vertices, triangles, normals, uvs, VertexColors, Tangents, true);
- RuntimeMesh->SetSectionMaterial(0, material);
- }
- void AAChunk::reloadBlock(EBlockAction pAction, FBlockStructure const& pBlock) {
- if (blockIsHere(FVector(pBlock.x, pBlock.y, pBlock.z)) && pBlock.id != 1) {
- if (pAction == EBlockAction::PLACE) {
- for (EBlockFace face : getBlocksArround(getBlockLocation(pBlock), true)) {
- TArray<FVector> listPoints;
- listPoints.SetNum(4);
- TArray<FVector> listTriangles;
- listPoints.SetNum(2);
- TArray<FVector> listNormals;
- listNormals.SetNum(4);
- TArray<FVector2D> listUvs;
- listUvs.SetNum(4);
- getPointsFace(face, pBlock, listPoints, listTriangles, listNormals, listUvs);
- for (FVector triangle : listTriangles) {
- triangles[trianglesIndex++] = verticesIndex + 1 + triangle.X;
- triangles[trianglesIndex++] = verticesIndex + 1 + triangle.Y;
- triangles[trianglesIndex++] = verticesIndex + 1 + triangle.Z;
- }
- for (FVector vertice : listPoints) {
- vertices[verticesIndex++] = vertice;
- // vertexColors.Add(getVertexFromPosition(getBlockLocation(pBlock)));
- }
- for (FVector normal : listNormals) {
- normals[normalsIndex++] = normal;
- }
- for (FVector2D uv : listUvs) {
- uvs[uvsIndex++] = uv;
- }
- }
- }
- }
- }
- int32 AAChunk::getIndiceAtLocation(FVector locTemp) const {
- int32 chunkSize(numberBlockOnLine*blockSize);
- locTemp.X = ((int32) locTemp.X) - ((int32) locTemp.X)%blockSize;
- locTemp.Y = ((int32) locTemp.Y) - ((int32)locTemp.Y) %blockSize;
- locTemp.Z = ((int32) locTemp.Z) - ((int32)locTemp.Z) %blockSize;
- if (blockIsHere(locTemp)) {
- return (locTemp.X - chunkSize * x) / blockSize * numberBlockOnLine * numberBlockOnZ + (locTemp.Y - chunkSize * y) / blockSize * numberBlockOnZ + (locTemp.Z - numberBlockOnZ * blockSize * z) / blockSize;
- }
- return 0;
- }
- bool AAChunk::blockIsHere(FVector const& locTemp) const {
- int32 tempX = locTemp.X;
- int32 tempY = locTemp.Y;
- int32 tempZ = locTemp.Z;
- int32 chunkSize(numberBlockOnLine*blockSize);
- return (tempX%blockSize == 0 && tempX >= x * chunkSize && tempX < (x + 1)*chunkSize && tempY%blockSize == 0 && tempY >= y * chunkSize && tempY < (y + 1)*chunkSize && tempZ%blockSize == 0 && tempZ >= z * (numberBlockOnZ*blockSize) && tempZ < (z + 1)*(blockSize*numberBlockOnZ));
- }
- TArray<EBlockFace> AAChunk::getBlocksArround(FVector const& locTemp, bool const blockArroundIsAir) const {
- TArray<EBlockFace> finalArrayFaces;
- if (blockIsHere(locTemp)) {
- TArray<FVector> arrayBlocks;
- arrayBlocks.SetNum(6);
- arrayBlocks[0] = FVector(-1, 0, 0);
- arrayBlocks[1] = FVector(1, 0, 0);
- arrayBlocks[2] = FVector(0, -1, 0);
- arrayBlocks[3] = FVector(0, 1, 0);
- arrayBlocks[4] = FVector(0, 0, -1);
- arrayBlocks[5] = FVector(0, 0, 1);
- TArray<EBlockFace> arrayFaces;
- arrayFaces.SetNum(6);
- arrayFaces[0] = EBlockFace::LEFT;
- arrayFaces[1] = EBlockFace::RIGHT;
- arrayFaces[2] = EBlockFace::BEHIND;
- arrayFaces[3] = EBlockFace::FRONT;
- arrayFaces[4] = EBlockFace::BOTTOM;
- arrayFaces[5] = EBlockFace::TOP;
- FVector tempLoc(0, 0, 0);
- int32 arrayIndex(0);
- for (FVector& vector : arrayBlocks) {
- tempLoc.X = vector.X * blockSize + locTemp.X;
- tempLoc.Y = vector.Y * blockSize + locTemp.Y;
- tempLoc.Z = vector.Z * blockSize + locTemp.Z;
- if (blockArroundIsAir == (listBlocks[getIndiceAtLocation(tempLoc)].id == 1) || !blockIsHere(tempLoc)) {
- finalArrayFaces.Add(arrayFaces[arrayIndex]);
- }
- arrayIndex++;
- }
- }
- return finalArrayFaces;
- }
- void AAChunk::getPointsFace(EBlockFace const face, FBlockStructure const& block, TArray<FVector>& returnPoints, TArray<FVector>& returnTriangles, TArray<FVector>& returnNormals, TArray<FVector2D>& returnUvs) {
- FVector tempLoc(block.x, block.y, block.z);
- TArray<FVector> arrayPoints;
- arrayPoints.SetNum(8);
- arrayPoints[0] = FVector(0, 0, 0);
- arrayPoints[1] = FVector(1, 0, 0);
- arrayPoints[2] = FVector(0, 0, 1);
- arrayPoints[3] = FVector(1, 0, 1);
- arrayPoints[4] = FVector(0, 1, 0);
- arrayPoints[5] = FVector(1, 1, 0);
- arrayPoints[6] = FVector(0, 1, 1);
- arrayPoints[7] = FVector(1, 1, 1);
- int32 blockId = block.id - 1;
- float xColor = blockId % numberTexturePerLine;
- float yColor = (blockId - blockId % numberTexturePerLine)/numberTexturePerLine;
- if (xColor < numberTexturePerLine && yColor < numberTexturePerLine) {
- returnUvs[0] = FVector2D(xColor / numberTexturePerLine, yColor/ numberTexturePerLine);
- returnUvs[1] = FVector2D((xColor+1) / numberTexturePerLine, yColor / numberTexturePerLine);
- returnUvs[2] = FVector2D(xColor / numberTexturePerLine, (yColor+1) / numberTexturePerLine);
- returnUvs[3] = FVector2D((xColor+1) / numberTexturePerLine, (yColor+1) / numberTexturePerLine);
- }
- else {
- returnUvs[0] = FVector2D(0.0f, 0.0f);
- returnUvs[1] = FVector2D(0.0f, 0.0f);
- returnUvs[2] = FVector2D(0.0f, 0.0f);
- returnUvs[3] = FVector2D(0.0f, 0.0f);
- }
- TArray<int32> arrayTempPoints;
- arrayTempPoints.SetNum(4);
- switch (face) {
- case EBlockFace::LEFT:
- arrayTempPoints[0] = 0;
- arrayTempPoints[1] = 2;
- arrayTempPoints[2] = 4;
- arrayTempPoints[3] = 6;
- returnNormals[0] = FVector(-1, 0, 0);
- returnNormals[1] = FVector(-1, 0, 0);
- returnNormals[2] = FVector(-1, 0, 0);
- returnNormals[3] = FVector(-1, 0, 0);
- break;
- case EBlockFace::RIGHT:
- arrayTempPoints[0] = 1;
- arrayTempPoints[1] = 3;
- arrayTempPoints[2] = 5;
- arrayTempPoints[3] = 7;
- returnNormals[0] = FVector(1, 0, 0);
- returnNormals[1] = FVector(1, 0, 0);
- returnNormals[2] = FVector(1, 0, 0);
- returnNormals[3] = FVector(1, 0, 0);
- break;
- case EBlockFace::FRONT:
- arrayTempPoints[0] = 4;
- arrayTempPoints[1] = 5;
- arrayTempPoints[2] = 6;
- arrayTempPoints[3] = 7;
- returnNormals[0] = FVector(0, 1, 0);
- returnNormals[1] = FVector(0, 1, 0);
- returnNormals[2] = FVector(0, 1, 0);
- returnNormals[3] = FVector(0, 1, 0);
- break;
- case EBlockFace::BEHIND:
- arrayTempPoints[0] = 0;
- arrayTempPoints[1] = 1;
- arrayTempPoints[2] = 2;
- arrayTempPoints[3] = 3;
- returnNormals[0] = FVector(0, -1, 0);
- returnNormals[1] = FVector(0, -1, 0);
- returnNormals[2] = FVector(0, -1, 0);
- returnNormals[3] = FVector(0, -1, 0);
- break;
- case EBlockFace::TOP:
- arrayTempPoints[0] = 2;
- arrayTempPoints[1] = 3;
- arrayTempPoints[2] = 6;
- arrayTempPoints[3] = 7;
- returnNormals[0] = FVector(0, 0, 1);
- returnNormals[1] = FVector(0, 0, 1);
- returnNormals[2] = FVector(0, 0, 1);
- returnNormals[3] = FVector(0, 0, 1);
- break;
- case EBlockFace::BOTTOM:
- arrayTempPoints[0] = 0;
- arrayTempPoints[1] = 1;
- arrayTempPoints[2] = 4;
- arrayTempPoints[3] = 5;
- returnNormals[0] = FVector(0, 0, -1);
- returnNormals[1] = FVector(0, 0, -1);
- returnNormals[2] = FVector(0, 0, -1);
- returnNormals[3] = FVector(0, 0, -1);
- break;
- /* case EBlockFace::TOP:
- arrayTempPoints.Insert(2, 0);
- arrayTempPoints.Insert(3, 1);
- arrayTempPoints.Insert(6, 2);
- arrayTempPoints.Insert(7, 3);
- returnNormals.Insert(FVector(0, 0, 1), 0);
- returnNormals.Insert(FVector(0, 0, 1), 1);
- returnNormals.Insert(FVector(0, 0, 1), 2);
- returnNormals.Insert(FVector(0, 0, 1), 3);
- break;
- case EBlockFace::BOTTOM:
- arrayTempPoints.Insert(0, 0);
- arrayTempPoints.Insert(1, 1);
- arrayTempPoints.Insert(4, 2);
- arrayTempPoints.Insert(5, 3);
- returnNormals.Insert(FVector(0, 0, -1), 0);
- returnNormals.Insert(FVector(0, 0, -1), 1);
- returnNormals.Insert(FVector(0, 0, -1), 2);
- returnNormals.Insert(FVector(0, 0, -1), 3);
- break; */
- }
- int32 countPoints(0);
- for (int32 pointNumber : arrayTempPoints) {
- arrayPoints[pointNumber] *= blockSize;
- arrayPoints[pointNumber] += tempLoc;
- returnPoints[countPoints++] = arrayPoints[pointNumber];
- }
- TArray<FVector> arrayTempTriangles;
- if (face == EBlockFace::LEFT || face == EBlockFace::BEHIND || face == EBlockFace::TOP) {
- returnTriangles[0] = FVector(2, 1, 0);
- returnTriangles[1] = FVector(1, 2, 3);
- }
- else {
- returnTriangles[0] = FVector(0, 1, 2);
- returnTriangles[1] = FVector(3, 2, 1);
- }
- }
- FVector AAChunk::getBlockLocation(FBlockStructure const& tempBlock) const {
- return FVector(tempBlock.x, tempBlock.y, tempBlock.z);
- }
- int32 AAChunk::breakBlock(FVector const& loc) {
- FVector tempLoc(loc.X - ((int32) loc.X%blockSize), loc.Y - ((int32)loc.Y%blockSize), loc.Z - ((int32)loc.Z%blockSize));
- if (x < 0) {
- tempLoc += FVector(-blockSize, 0, 0);
- }
- if (y < 0) {
- tempLoc += FVector(0, -blockSize, 0);
- }
- int32 tempIndice = getIndiceAtLocation(tempLoc);
- listBlocks[tempIndice] = FBlockStructure(listBlocks[tempIndice].x, listBlocks[tempIndice].y, listBlocks[tempIndice].z, 1);
- vertices.Empty();
- triangles.Empty();
- uvs.Empty();
- normals.Empty();
- index = -1;
- verticesIndex = 0;
- trianglesIndex = 0;
- uvsIndex = 0;
- normalsIndex = 0;
- return tempIndice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement