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) {
- // 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);
- isCalculated = false;
- verticesIndex = 0;
- trianglesIndex = 0;
- uvsIndex = 0;
- normalsIndex = 0;
- vertexColorsIndex = 0;
- /* TArray<FVector> Vertices;
- TArray<FVector> Normals;
- TArray<FRuntimeMeshTangent> Tangents;
- TArray<FColor> VertexColors;
- TArray<FVector2D> TextureCoordinates;
- TArray<int32> Triangles;
- Vertices.Add(FVector(0, 100, 100));
- Vertices.Add(FVector(100, 100, 100));
- Vertices.Add(FVector(100, 0, 100));
- Vertices.Add(FVector(0, 0, 100));
- Vertices.Add(FVector(0, 0, 100));
- Vertices.Add(FVector(100, 0, 100));
- Vertices.Add(FVector(0, 0, 0));
- Vertices.Add(FVector(100, 0, 0));
- Vertices.Add(FVector(100,0,0));
- Vertices.Add(FVector(100, 0, 100));
- Vertices.Add(FVector(100,100,100));
- Vertices.Add(FVector(100,100,0));
- Vertices.Add(FVector(0, 0, 0));
- Vertices.Add(FVector(0, 0, 100));
- Vertices.Add(FVector(0, 100, 0));
- Vertices.Add(FVector(0, 100, 100));
- Triangles.Add(0);
- Triangles.Add(1);
- Triangles.Add(2);
- Triangles.Add(0);
- Triangles.Add(2);
- Triangles.Add(3);
- Triangles.Add(4);
- Triangles.Add(5);
- Triangles.Add(6);
- Triangles.Add(6);
- Triangles.Add(5);
- Triangles.Add(7);
- Triangles.Add(8);
- Triangles.Add(9);
- Triangles.Add(10);
- Triangles.Add(11);
- Triangles.Add(8);
- Triangles.Add(10);
- Triangles.Add(14);
- Triangles.Add(13);
- Triangles.Add(12);
- Triangles.Add(15);
- Triangles.Add(13);
- Triangles.Add(14);
- RuntimeMesh->CreateMeshSection(0, Vertices, Triangles, Normals, TextureCoordinates, VertexColors, Tangents); */
- }
- // Called every frame
- void AAChunk::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (!isCalculated) {
- isCalculated = true;
- int32 loopI(0);
- while (loopI < maxIndex) {
- loopI++;
- }
- }
- 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) * blockSize + (y*numberBlockOnLine*blockSize);
- int32 tempZ = (index%numberBlockOnZ)*blockSize + z*numberBlockOnZ*blockSize; */
- 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;
- /* int32 tempCount = maxIndex / numberBlockOnLine;
- int32 tempX = (index / tempCount)*blockSize + (x*(numberBlockOnLine*blockSize));
- int32 tempY = ((index%tempCount) / numberBlockOnLine)*blockSize + (y*numberBlockOnLine*blockSize);
- int32 tempZ = ((index%tempCount) % numberBlockOnLine)*blockSize + z; */
- 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) {
- int32 loopI(0);
- while (loopI < 5001) {
- index++;
- if (index >= maxIndex) {
- loadMap();
- tickNumber = 0;
- break;
- }
- else {
- reloadBlock(EBlockAction::PLACE, listBlocks[index]);
- }
- loopI++;
- }
- }
- }
- }
- }
- 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;
- TArray<FVector> listTriangles;
- TArray<FVector> listNormals;
- TArray<FVector2D> listUvs;
- getPointsFace(face, pBlock, listPoints, listTriangles, listNormals, listUvs);
- for (FVector triangle : listTriangles) {
- triangles.Add(vertices.Num() + triangle.X);
- triangles.Add(vertices.Num() + triangle.Y);
- triangles.Add(vertices.Num() + triangle.Z);
- }
- for (FVector vertice : listPoints) {
- vertices.Add(vertice);
- // vertexColors.Add(getVertexFromPosition(getBlockLocation(pBlock)));
- }
- for (FVector normal : listNormals) {
- normals.Add(normal);
- }
- for (FVector2D uv : listUvs) {
- uvs.Add(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.Z / numberBlockOnZ + (locTemp.Y - chunkSize * y) / (blockSize*numberBlockOnZ) + (locTemp.X - chunkSize * x) / (blockSize*numberBlockOnZ*numberBlockOnLine);
- return (locTemp.X - chunkSize * x) / blockSize * numberBlockOnLine * numberBlockOnZ + (locTemp.Y - chunkSize * y) / blockSize * numberBlockOnZ + (locTemp.Z - numberBlockOnZ * blockSize * z) / blockSize;
- // return (locTemp.X - chunkSize * x) / blockSize * pow(numberBlockOnLine, 2.0) + (locTemp.Y - chunkSize * y) / blockSize * numberBlockOnLine + locTemp.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));
- // return ((tempX%blockSize == 0 && tempX >= x * chunkSize && tempX < (x*chunkSize + chunkSize)) && (tempY%blockSize == 0 && tempY >= y * chunkSize && tempY < (y*chunkSize + chunkSize)) && (tempZ%blockSize == 0 && tempZ >= z * chunkSize && tempZ < (z*chunkSize + chunkSize)));
- }
- TArray<EBlockFace> AAChunk::getBlocksArround(FVector const& locTemp, bool const blockArroundIsAir) const {
- TArray<EBlockFace> finalArrayFaces;
- if (blockIsHere(locTemp)) {
- TArray<FVector> arrayBlocks;
- arrayBlocks.Add(FVector(-1, 0, 0));
- arrayBlocks.Add(FVector(1, 0, 0));
- arrayBlocks.Add(FVector(0, -1, 0));
- arrayBlocks.Add(FVector(0, 1, 0));
- arrayBlocks.Add(FVector(0, 0, -1));
- arrayBlocks.Add(FVector(0, 0, 1));
- TArray<EBlockFace> arrayFaces;
- arrayFaces.Add(EBlockFace::LEFT);
- arrayFaces.Add(EBlockFace::RIGHT);
- arrayFaces.Add(EBlockFace::BEHIND);
- arrayFaces.Add(EBlockFace::FRONT);
- arrayFaces.Add(EBlockFace::BOTTOM);
- arrayFaces.Add(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.Add(FVector(0, 0, 0));
- arrayPoints.Add(FVector(1, 0, 0));
- arrayPoints.Add(FVector(0, 0, 1));
- arrayPoints.Add(FVector(1, 0, 1));
- arrayPoints.Add(FVector(0, 1, 0));
- arrayPoints.Add(FVector(1, 1, 0));
- arrayPoints.Add(FVector(0, 1, 1));
- arrayPoints.Add(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.Add(FVector2D(xColor / numberTexturePerLine, yColor/ numberTexturePerLine));
- returnUvs.Add(FVector2D((xColor+1) / numberTexturePerLine, yColor / numberTexturePerLine));
- returnUvs.Add(FVector2D(xColor / numberTexturePerLine, (yColor+1) / numberTexturePerLine));
- returnUvs.Add(FVector2D((xColor+1) / numberTexturePerLine, (yColor+1) / numberTexturePerLine));
- }
- else {
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- }
- /* switch (block.id) {
- case 1:
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- returnUvs.Add(FVector2D(0.5f, 0.0f));
- returnUvs.Add(FVector2D(0.0f, 0.5f));
- returnUvs.Add(FVector2D(0.5f, 0.5f));
- break;
- case 2:
- returnUvs.Add(FVector2D(0.5f, 0.0f));
- returnUvs.Add(FVector2D(1.0f, 0.0f));
- returnUvs.Add(FVector2D(0.5f, 0.5f));
- returnUvs.Add(FVector2D(1.0f, 0.5f));
- break;
- case 3:
- returnUvs.Add(FVector2D(0.0f, 0.0f));
- returnUvs.Add(FVector2D(0.5f, 0.0f));
- returnUvs.Add(FVector2D(0.0f, 0.5f));
- returnUvs.Add(FVector2D(0.5f, 0.5f));
- break;
- } */
- TArray<int32> arrayTempPoints;
- switch (face) {
- case EBlockFace::LEFT:
- arrayTempPoints.Add(0);
- arrayTempPoints.Add(2);
- arrayTempPoints.Add(4);
- arrayTempPoints.Add(6);
- returnNormals.Add(FVector(-1, 0, 0));
- returnNormals.Add(FVector(-1, 0, 0));
- returnNormals.Add(FVector(-1, 0, 0));
- returnNormals.Add(FVector(-1, 0, 0));
- break;
- case EBlockFace::RIGHT:
- arrayTempPoints.Add(1);
- arrayTempPoints.Add(3);
- arrayTempPoints.Add(5);
- arrayTempPoints.Add(7);
- returnNormals.Add(FVector(1, 0, 0));
- returnNormals.Add(FVector(1, 0, 0));
- returnNormals.Add(FVector(1, 0, 0));
- returnNormals.Add(FVector(1, 0, 0));
- break;
- case EBlockFace::FRONT:
- arrayTempPoints.Add(4);
- arrayTempPoints.Add(5);
- arrayTempPoints.Add(6);
- arrayTempPoints.Add(7);
- returnNormals.Add(FVector(0, 1, 0));
- returnNormals.Add(FVector(0, 1, 0));
- returnNormals.Add(FVector(0, 1, 0));
- returnNormals.Add(FVector(0, 1, 0));
- break;
- case EBlockFace::BEHIND:
- arrayTempPoints.Add(0);
- arrayTempPoints.Add(1);
- arrayTempPoints.Add(2);
- arrayTempPoints.Add(3);
- returnNormals.Add(FVector(0, -1, 0));
- returnNormals.Add(FVector(0, -1, 0));
- returnNormals.Add(FVector(0, -1, 0));
- returnNormals.Add(FVector(0, -1, 0));
- break;
- case EBlockFace::TOP:
- arrayTempPoints.Add(2);
- arrayTempPoints.Add(3);
- arrayTempPoints.Add(6);
- arrayTempPoints.Add(7);
- returnNormals.Add(FVector(0, 0, 1));
- returnNormals.Add(FVector(0, 0, 1));
- returnNormals.Add(FVector(0, 0, 1));
- returnNormals.Add(FVector(0, 0, 1));
- break;
- case EBlockFace::BOTTOM:
- arrayTempPoints.Add(0);
- arrayTempPoints.Add(1);
- arrayTempPoints.Add(4);
- arrayTempPoints.Add(5);
- returnNormals.Add(FVector(0, 0, -1));
- returnNormals.Add(FVector(0, 0, -1));
- returnNormals.Add(FVector(0, 0, -1));
- returnNormals.Add(FVector(0, 0, -1));
- break;
- }
- for (int32 pointNumber : arrayTempPoints) {
- arrayPoints[pointNumber] *= blockSize;
- arrayPoints[pointNumber] += tempLoc;
- returnPoints.Add(arrayPoints[pointNumber]);
- }
- TArray<FVector> arrayTempTriangles;
- if (face == EBlockFace::LEFT || face == EBlockFace::BEHIND || face == EBlockFace::TOP) {
- returnTriangles.Add(FVector(2, 1, 0));
- returnTriangles.Add(FVector(1, 2, 3));
- }
- else {
- returnTriangles.Add(FVector(0, 1, 2));
- returnTriangles.Add(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;
- return tempIndice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement