Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.94 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "VoxelActor.h"
  5. #include "BIGDIG.h"
  6.  
  7. #define ECC_LandScape ECC_GameTraceChannel1
  8.  
  9.  
  10. const int32 bTriangles[] = { 2, 1, 0, 0, 3, 2 };
  11. const FVector2D bUVs[] = { FVector2D(0.000000, 0.000000), FVector2D(0.000000, 1.000000), FVector2D(1.000000, 1.000000), FVector2D(1.000000, 0.000000) };
  12. const FVector bNormals0[] = { FVector(0, 0, 1), FVector(0, 0, 1), FVector(0, 0, 1), FVector(0, 0, 1) };
  13. const FVector bNormals1[] = { FVector(0, 0, -1), FVector(0, 0, -1), FVector(0, 0, -1), FVector(0, 0, -1) };
  14. const FVector bNormals2[] = { FVector(0, 1, 0), FVector(0, 1, 0), FVector(0, 1, 0), FVector(0, 1, 0) };
  15. const FVector bNormals3[] = { FVector(0, -1, 0), FVector(0, -1, 0), FVector(0, -1, 0), FVector(0, -1, 0) };
  16. const FVector bNormals4[] = { FVector(1, 0, 0), FVector(1, 0, 0), FVector(1, 0, 0), FVector(1, 0, 0) };
  17. const FVector bNormals5[] = { FVector(-1, 0, 0), FVector(-1, 0, 0), FVector(-1, 0, 0), FVector(-1, 0, 0) };
  18. const FVector bMask[] = { FVector(0.000000, 0.000000, 1.000000), FVector(0.000000, 0.000000, -1.000000), FVector(0.000000, 1.000000, 0.000000), FVector(0.000000, -1.000000, 0.000000), FVector(1.000000, 0.000000, 0.000000), FVector(-1.000000, 0.000000, 0.000000) };
  19.  
  20.  
  21.  
  22.  
  23. AVoxelActor::AVoxelActor()
  24. {
  25.  
  26.     PrimaryActorTick.bCanEverTick = true;
  27.  
  28. }
  29.  
  30.  
  31. void AVoxelActor::BeginPlay()
  32. {
  33.     Super::BeginPlay();
  34.  
  35.  
  36. }
  37.  
  38.  
  39. void AVoxelActor::Tick(float DeltaTime)
  40. {
  41.  
  42.  
  43. }
  44.  
  45.  
  46. void AVoxelActor::OnConstruction(const FTransform & Transform)
  47. {
  48.     chunkZElements = 80;
  49.     chunkTotalElements = chunkLineElements * chunkLineElements * chunkZElements;
  50.     chunkLineElementsP2 = chunkLineElements * chunkLineElements;
  51.     voxelSizeHalf = voxelSize / 2;
  52.  
  53.     FString string = "Voxel_" + FString::FromInt(chunkXindex) + "_" + FString::FromInt(chunkYindex);
  54.     FName name = FName(*string);
  55.     proceduralComponent = NewObject<UProceduralMeshComponent>(this, name);
  56.     proceduralComponent->RegisterComponent();
  57.    
  58.     RootComponent = proceduralComponent;
  59.     RootComponent->SetWorldTransform(Transform); // set position of root component (bubble actor)
  60.  
  61.     proceduralComponent->SetCollisionResponseToChannel(ECC_LandScape, ECollisionResponse::ECR_Block);
  62.  
  63.     Super::OnConstruction(Transform);
  64.  
  65.     GenerateChunk();
  66.     UpdateMesh();
  67. }
  68.  
  69.  
  70. void AVoxelActor::GenerateChunk()
  71. {
  72.     chunkFields.SetNumUninitialized(chunkTotalElements);
  73.     TArray <int32> noise = calculateNoise();
  74.  
  75.  
  76.     for (int32 x = 0; x < chunkLineElements; x++)
  77.     {
  78.         for (int32 y = 0; y < chunkLineElements; y++)
  79.         {
  80.             for (int32 z = 0; z < chunkZElements; z++)
  81.             {
  82.  
  83.                 int32 index = x + (y * chunkLineElements) + (z * chunkLineElementsP2);
  84.  
  85.                
  86.  
  87.  
  88.                 //chunkFields[index] = (z == 30 + noise[x + y * chunkLineElements]) ? 2 : 0;
  89.                 chunkFields[index] = (z < 30 + noise[x + y * chunkLineElements]) ? 1 : 0;
  90.  
  91.  
  92.  
  93.             }
  94.         }
  95.     }
  96.  
  97. }
  98.  
  99. void AVoxelActor::UpdateMesh()
  100. {
  101.     TArray<FMeshSection> meshSections;
  102.     meshSections.SetNum(Materials.Num());
  103.     int32 el_num = 0;
  104.  
  105.     for (int32 x = 0; x < chunkLineElements; x++)
  106.     {
  107.         for (int32 y = 0; y < chunkLineElements; y++)
  108.         {
  109.             for (int32 z = 0; z < chunkZElements; z++)
  110.             {
  111.                 int32 index = x + (chunkLineElements * y) + (chunkLineElementsP2 * z);
  112.                 int32 meshIndex = chunkFields[index];
  113.  
  114.                 if (meshIndex > 0)
  115.                 {
  116.                     meshIndex--;
  117.  
  118.                    
  119.                     TArray<FVector> &Vertices = meshSections[meshIndex].Vertices;
  120.                     TArray<int32> &Triangles = meshSections[meshIndex].Triangles;
  121.                     TArray<FVector> &Normals = meshSections[meshIndex].Normals;
  122.                     TArray<FVector2D> &UVs = meshSections[meshIndex].UVs;
  123.                     TArray<FProcMeshTangent> &Tangents = meshSections[meshIndex].Tangents;
  124.                     TArray<FColor> &VertexColors = meshSections[meshIndex].VertexColors;
  125.                     int32 elementID = meshSections[meshIndex].elementID;
  126.  
  127.                     //add Faces, vertex, UVS and normals
  128.                     int triangle_num = 0;
  129.                     for (int i = 0; i < 6; i++)
  130.                     {
  131.                         int newIndex = index + bMask[i].X + (bMask[i].Y * chunkLineElements) + (bMask[i].Z * chunkLineElementsP2);
  132.  
  133.                         bool flag = false;
  134.                         if (meshIndex >= 20) flag = true;
  135.                         else if ((x + bMask[i].X < chunkLineElements) && (x + bMask[i].X >= 0) && (y + bMask[i].Y < chunkLineElements) && (y + bMask[i].Y >= 0))
  136.                         {
  137.                             if (newIndex < chunkFields.Num() && newIndex >= 0)
  138.                                 if (chunkFields[newIndex] < 1) flag = true;
  139.                         }
  140.  
  141.                         else flag = true;
  142.  
  143.                         if(flag)
  144.                         {
  145.                             Triangles.Add(bTriangles[0] + triangle_num + elementID);
  146.                             Triangles.Add(bTriangles[1] + triangle_num + elementID);
  147.                             Triangles.Add(bTriangles[2] + triangle_num + elementID);
  148.                             Triangles.Add(bTriangles[3] + triangle_num + elementID);
  149.                             Triangles.Add(bTriangles[4] + triangle_num + elementID);
  150.                             Triangles.Add(bTriangles[5] + triangle_num + elementID);
  151.                             triangle_num += 4; //add 4 vertex to next triangle
  152.  
  153.                             switch (i)                     
  154.                             {
  155.                             case 0: {
  156.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  157.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  158.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  159.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  160.  
  161.                                 Normals.Append(bNormals0, ARRAY_COUNT(bNormals0));
  162.                                 break;
  163.                             }
  164.                             case 1: {
  165.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  166.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  167.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  168.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  169.  
  170.                                 Normals.Append(bNormals1, ARRAY_COUNT(bNormals1));
  171.                                 break;
  172.                             }
  173.                             case 2: {
  174.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  175.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  176.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  177.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  178.  
  179.                                 Normals.Append(bNormals2, ARRAY_COUNT(bNormals2));
  180.                                 break;
  181.                             }
  182.                             case 3: {
  183.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  184.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  185.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  186.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  187.  
  188.                                 Normals.Append(bNormals3, ARRAY_COUNT(bNormals3));
  189.                                 break;
  190.                             }
  191.                             case 4: {
  192.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  193.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  194.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  195.                                 Vertices.Add(FVector(voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  196.  
  197.                                 Normals.Append(bNormals4, ARRAY_COUNT(bNormals4));
  198.                                 break;
  199.                             }
  200.                             case 5: {
  201.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  202.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  203.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), -voxelSizeHalf + (z * voxelSize)));
  204.                                 Vertices.Add(FVector(-voxelSizeHalf + (x * voxelSize), -voxelSizeHalf + (y * voxelSize), voxelSizeHalf + (z * voxelSize)));
  205.  
  206.                                 Normals.Append(bNormals5, ARRAY_COUNT(bNormals5));
  207.                                 break;
  208.                             }
  209.                             }
  210.  
  211.                                     UVs.Append(bUVs, ARRAY_COUNT(bUVs));
  212.                                     FColor color = FColor(255, 255, 255, i);
  213.                                     VertexColors.Add(color); VertexColors.Add(color); VertexColors.Add(color); VertexColors.Add(color);
  214.  
  215.                                    
  216.                             }
  217.  
  218.                         }  
  219.                         el_num += triangle_num;
  220.                         meshSections[meshIndex].elementID += triangle_num;
  221.                        
  222.                     }
  223.                
  224.             }
  225.         }
  226.     }
  227.     proceduralComponent->ClearAllMeshSections();
  228.     for (int i = 0; i < meshSections.Num(); i++)
  229.     {
  230.         if (meshSections[i].Vertices.Num() > 0)
  231.         proceduralComponent->CreateMeshSection(i, meshSections[i].Vertices, meshSections[i].Triangles, meshSections[i].Normals, meshSections[i].UVs, meshSections[i].VertexColors, meshSections[i].Tangents, true);
  232.         //last property is collission turn of for testing high loads(if now characters are needed)
  233.     }
  234.  
  235.  
  236.  
  237.     int s = 0;
  238.     while (s < Materials.Num())
  239.     {
  240.         proceduralComponent->SetMaterial(s, Materials[s]);
  241.         s++;
  242.  
  243.     }
  244.  
  245. }
  246.  
  247.  
  248. TArray<int32> AVoxelActor::calculateNoise_Implementation()
  249. {
  250.     TArray<int32> aa;
  251.     aa.SetNum(chunkLineElementsP2);
  252.     return aa;
  253. }
  254.  
  255. void AVoxelActor::setVoxel(FVector localPos, int32 value)
  256. {
  257.     int32 x = localPos.X / voxelSize;
  258.     int32 y = localPos.Y / voxelSize;
  259.     int32 z = localPos.Z / voxelSize;
  260.  
  261.     int32 index = x + (y * chunkLineElements) + (z * chunkLineElementsP2);
  262.  
  263.     chunkFields[index] = value;
  264.     UpdateMesh();
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement