Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.03 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "AChunk.h"
  4. #include "RuntimeMeshComponent.h"
  5. #include "SimplexNoiseBPLibrary.h"
  6. #include "UMapGeneration.h"
  7.  
  8. // Sets default values
  9. AAChunk::AAChunk() : x(0), y(0), z(0), isGenerate(false), index(-1), maxIndex(0), tickNumber(0) {
  10. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  11. PrimaryActorTick.bCanEverTick = true;
  12. RuntimeMesh = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMesh"));
  13. }
  14.  
  15. AAChunk::AAChunk(int32 pX, int32 pY) : z(0), isGenerate(false), index(-1), maxIndex(0), tickNumber(0) {
  16. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  17. PrimaryActorTick.bCanEverTick = true;
  18. RuntimeMesh = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMesh"));
  19.  
  20. x = pX;
  21. y = pY;
  22. }
  23.  
  24. // Called when the game starts or when spawned
  25. void AAChunk::BeginPlay()
  26. {
  27. Super::BeginPlay();
  28.  
  29. maxIndex = pow(numberBlockOnLine, 2.0)*numberBlockOnZ;
  30. listBlocks.SetNum(maxIndex);
  31.  
  32. isCalculated = false;
  33. verticesIndex = 0;
  34. trianglesIndex = 0;
  35. uvsIndex = 0;
  36. normalsIndex = 0;
  37. vertexColorsIndex = 0;
  38.  
  39. /* TArray<FVector> Vertices;
  40. TArray<FVector> Normals;
  41. TArray<FRuntimeMeshTangent> Tangents;
  42. TArray<FColor> VertexColors;
  43. TArray<FVector2D> TextureCoordinates;
  44. TArray<int32> Triangles;
  45.  
  46. Vertices.Add(FVector(0, 100, 100));
  47. Vertices.Add(FVector(100, 100, 100));
  48. Vertices.Add(FVector(100, 0, 100));
  49. Vertices.Add(FVector(0, 0, 100));
  50.  
  51. Vertices.Add(FVector(0, 0, 100));
  52. Vertices.Add(FVector(100, 0, 100));
  53. Vertices.Add(FVector(0, 0, 0));
  54. Vertices.Add(FVector(100, 0, 0));
  55.  
  56. Vertices.Add(FVector(100,0,0));
  57. Vertices.Add(FVector(100, 0, 100));
  58. Vertices.Add(FVector(100,100,100));
  59. Vertices.Add(FVector(100,100,0));
  60.  
  61. Vertices.Add(FVector(0, 0, 0));
  62. Vertices.Add(FVector(0, 0, 100));
  63. Vertices.Add(FVector(0, 100, 0));
  64. Vertices.Add(FVector(0, 100, 100));
  65.  
  66. Triangles.Add(0);
  67. Triangles.Add(1);
  68. Triangles.Add(2);
  69. Triangles.Add(0);
  70. Triangles.Add(2);
  71. Triangles.Add(3);
  72.  
  73. Triangles.Add(4);
  74. Triangles.Add(5);
  75. Triangles.Add(6);
  76. Triangles.Add(6);
  77. Triangles.Add(5);
  78. Triangles.Add(7);
  79.  
  80. Triangles.Add(8);
  81. Triangles.Add(9);
  82. Triangles.Add(10);
  83. Triangles.Add(11);
  84. Triangles.Add(8);
  85. Triangles.Add(10);
  86.  
  87. Triangles.Add(14);
  88. Triangles.Add(13);
  89. Triangles.Add(12);
  90. Triangles.Add(15);
  91. Triangles.Add(13);
  92. Triangles.Add(14);
  93.  
  94. RuntimeMesh->CreateMeshSection(0, Vertices, Triangles, Normals, TextureCoordinates, VertexColors, Tangents); */
  95. }
  96.  
  97. // Called every frame
  98. void AAChunk::Tick(float DeltaTime)
  99. {
  100. Super::Tick(DeltaTime);
  101.  
  102. if (!isCalculated) {
  103. isCalculated = true;
  104. int32 loopI(0);
  105. while (loopI < maxIndex) {
  106.  
  107.  
  108. loopI++;
  109. }
  110.  
  111. }
  112. if (!isGenerate) {
  113. tickNumber = (tickNumber+1)%3;
  114. if (tickNumber == 0) {
  115. // First part of generation
  116. int32 loopI(0);
  117. while (loopI < 5001) {
  118. index++;
  119. if (index >= maxIndex) {
  120. isGenerate = true;
  121. index = -1;
  122. break;
  123. }
  124. else {
  125.  
  126. /* int32 tempCount = numberBlockOnZ*numberBlockOnLine;
  127. int32 tempX = ((index-(index%tempCount))/tempCount) * blockSize + (x*numberBlockOnLine*blockSize);
  128. int32 tempY = ((index-(index%numberBlockOnZ))/numberBlockOnZ) * blockSize + (y*numberBlockOnLine*blockSize);
  129. int32 tempZ = (index%numberBlockOnZ)*blockSize + z*numberBlockOnZ*blockSize; */
  130.  
  131. int32 tempCount = numberBlockOnZ * numberBlockOnLine;
  132. int32 tempX = ((index - (index%tempCount)) / tempCount) * blockSize + (x*numberBlockOnLine*blockSize);
  133. int32 tempY = (((index - (index%numberBlockOnZ)) / numberBlockOnZ)% numberBlockOnLine) * blockSize + (y*numberBlockOnLine*blockSize);
  134. int32 tempZ = (index%numberBlockOnZ)*blockSize + z * numberBlockOnZ*blockSize;
  135.  
  136. /* int32 tempCount = maxIndex / numberBlockOnLine;
  137. int32 tempX = (index / tempCount)*blockSize + (x*(numberBlockOnLine*blockSize));
  138. int32 tempY = ((index%tempCount) / numberBlockOnLine)*blockSize + (y*numberBlockOnLine*blockSize);
  139. int32 tempZ = ((index%tempCount) % numberBlockOnLine)*blockSize + z; */
  140.  
  141. FBlockStructure tempStructure(tempX, tempY, tempZ, getBlockIdByPosition(FVector(tempX, tempY, tempZ)));
  142. listBlocks[index] = tempStructure;
  143. }
  144. loopI++;
  145. }
  146. }
  147.  
  148. }else {
  149. if (index < (maxIndex - 1)) {
  150. tickNumber = (tickNumber + 1) % 3;
  151. if (tickNumber == 0) {
  152. int32 loopI(0);
  153. while (loopI < 5001) {
  154. index++;
  155. if (index >= maxIndex) {
  156. loadMap();
  157. tickNumber = 0;
  158. break;
  159. }
  160. else {
  161. reloadBlock(EBlockAction::PLACE, listBlocks[index]);
  162. }
  163. loopI++;
  164. }
  165. }
  166. }
  167. }
  168. }
  169.  
  170. void AAChunk::loadMap() {
  171. TArray<FRuntimeMeshTangent> Tangents;
  172. TArray<FColor> VertexColors;
  173.  
  174. VertexColors.Init(FColor(0.0f, 0.0f, 0.0f, 1.0f), vertices.Num());
  175. Tangents.Init(FRuntimeMeshTangent(1.0f, 0.0f, 0.0f), vertices.Num());
  176.  
  177. /* if (GEngine)
  178. GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("A : " + FString::FromInt(vertices.Num()) + " " + FString::FromInt(triangles.Num()))); */
  179.  
  180. RuntimeMesh->CreateMeshSection(0, vertices, triangles, normals, uvs, VertexColors, Tangents, true);
  181. RuntimeMesh->SetSectionMaterial(0, material);
  182. }
  183.  
  184. void AAChunk::reloadBlock(EBlockAction pAction, FBlockStructure const& pBlock) {
  185. if (blockIsHere(FVector(pBlock.x, pBlock.y, pBlock.z)) && pBlock.id != 1) {
  186. if (pAction == EBlockAction::PLACE) {
  187. for (EBlockFace face : getBlocksArround(getBlockLocation(pBlock), true)) {
  188. TArray<FVector> listPoints;
  189. TArray<FVector> listTriangles;
  190. TArray<FVector> listNormals;
  191. TArray<FVector2D> listUvs;
  192.  
  193. getPointsFace(face, pBlock, listPoints, listTriangles, listNormals, listUvs);
  194. for (FVector triangle : listTriangles) {
  195. triangles.Add(vertices.Num() + triangle.X);
  196. triangles.Add(vertices.Num() + triangle.Y);
  197. triangles.Add(vertices.Num() + triangle.Z);
  198. }
  199.  
  200. for (FVector vertice : listPoints) {
  201. vertices.Add(vertice);
  202. // vertexColors.Add(getVertexFromPosition(getBlockLocation(pBlock)));
  203. }
  204.  
  205. for (FVector normal : listNormals) {
  206. normals.Add(normal);
  207. }
  208.  
  209. for (FVector2D uv : listUvs) {
  210. uvs.Add(uv);
  211. }
  212. }
  213. }
  214. }
  215. }
  216.  
  217. int32 AAChunk::getIndiceAtLocation(FVector locTemp) const {
  218. int32 chunkSize(numberBlockOnLine*blockSize);
  219. locTemp.X = ((int32) locTemp.X) - ((int32) locTemp.X)%blockSize;
  220. locTemp.Y = ((int32) locTemp.Y) - ((int32)locTemp.Y) %blockSize;
  221. locTemp.Z = ((int32) locTemp.Z) - ((int32)locTemp.Z) %blockSize;
  222. if (blockIsHere(locTemp)) {
  223. // return locTemp.Z / numberBlockOnZ + (locTemp.Y - chunkSize * y) / (blockSize*numberBlockOnZ) + (locTemp.X - chunkSize * x) / (blockSize*numberBlockOnZ*numberBlockOnLine);
  224. return (locTemp.X - chunkSize * x) / blockSize * numberBlockOnLine * numberBlockOnZ + (locTemp.Y - chunkSize * y) / blockSize * numberBlockOnZ + (locTemp.Z - numberBlockOnZ * blockSize * z) / blockSize;
  225.  
  226. // return (locTemp.X - chunkSize * x) / blockSize * pow(numberBlockOnLine, 2.0) + (locTemp.Y - chunkSize * y) / blockSize * numberBlockOnLine + locTemp.Z / blockSize;
  227. }
  228. return 0;
  229. }
  230.  
  231. bool AAChunk::blockIsHere(FVector const& locTemp) const {
  232. int32 tempX = locTemp.X;
  233. int32 tempY = locTemp.Y;
  234. int32 tempZ = locTemp.Z;
  235. int32 chunkSize(numberBlockOnLine*blockSize);
  236. 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));
  237.  
  238. // 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)));
  239. }
  240.  
  241. TArray<EBlockFace> AAChunk::getBlocksArround(FVector const& locTemp, bool const blockArroundIsAir) const {
  242. TArray<EBlockFace> finalArrayFaces;
  243. if (blockIsHere(locTemp)) {
  244. TArray<FVector> arrayBlocks;
  245. arrayBlocks.Add(FVector(-1, 0, 0));
  246. arrayBlocks.Add(FVector(1, 0, 0));
  247. arrayBlocks.Add(FVector(0, -1, 0));
  248. arrayBlocks.Add(FVector(0, 1, 0));
  249. arrayBlocks.Add(FVector(0, 0, -1));
  250. arrayBlocks.Add(FVector(0, 0, 1));
  251.  
  252. TArray<EBlockFace> arrayFaces;
  253. arrayFaces.Add(EBlockFace::LEFT);
  254. arrayFaces.Add(EBlockFace::RIGHT);
  255. arrayFaces.Add(EBlockFace::BEHIND);
  256. arrayFaces.Add(EBlockFace::FRONT);
  257. arrayFaces.Add(EBlockFace::BOTTOM);
  258. arrayFaces.Add(EBlockFace::TOP);
  259.  
  260. FVector tempLoc(0, 0, 0);
  261.  
  262. int32 arrayIndex(0);
  263. for (FVector& vector : arrayBlocks) {
  264. tempLoc.X = vector.X * blockSize + locTemp.X;
  265. tempLoc.Y = vector.Y * blockSize + locTemp.Y;
  266. tempLoc.Z = vector.Z * blockSize + locTemp.Z;
  267.  
  268. if (blockArroundIsAir == (listBlocks[getIndiceAtLocation(tempLoc)].id == 1) || !blockIsHere(tempLoc)) {
  269. finalArrayFaces.Add(arrayFaces[arrayIndex]);
  270. }
  271. arrayIndex++;
  272. }
  273. }
  274. return finalArrayFaces;
  275. }
  276.  
  277. void AAChunk::getPointsFace(EBlockFace const face, FBlockStructure const& block, TArray<FVector>& returnPoints, TArray<FVector>& returnTriangles, TArray<FVector>& returnNormals, TArray<FVector2D>& returnUvs) {
  278. FVector tempLoc(block.x, block.y, block.z);
  279. TArray<FVector> arrayPoints;
  280. arrayPoints.Add(FVector(0, 0, 0));
  281. arrayPoints.Add(FVector(1, 0, 0));
  282. arrayPoints.Add(FVector(0, 0, 1));
  283. arrayPoints.Add(FVector(1, 0, 1));
  284. arrayPoints.Add(FVector(0, 1, 0));
  285. arrayPoints.Add(FVector(1, 1, 0));
  286. arrayPoints.Add(FVector(0, 1, 1));
  287. arrayPoints.Add(FVector(1, 1, 1));
  288.  
  289. int32 blockId = block.id - 1;
  290. float xColor = blockId % numberTexturePerLine;
  291. float yColor = (blockId - blockId % numberTexturePerLine)/numberTexturePerLine;
  292.  
  293. if (xColor < numberTexturePerLine && yColor < numberTexturePerLine) {
  294. returnUvs.Add(FVector2D(xColor / numberTexturePerLine, yColor/ numberTexturePerLine));
  295. returnUvs.Add(FVector2D((xColor+1) / numberTexturePerLine, yColor / numberTexturePerLine));
  296. returnUvs.Add(FVector2D(xColor / numberTexturePerLine, (yColor+1) / numberTexturePerLine));
  297. returnUvs.Add(FVector2D((xColor+1) / numberTexturePerLine, (yColor+1) / numberTexturePerLine));
  298. }
  299. else {
  300. returnUvs.Add(FVector2D(0.0f, 0.0f));
  301. returnUvs.Add(FVector2D(0.0f, 0.0f));
  302. returnUvs.Add(FVector2D(0.0f, 0.0f));
  303. returnUvs.Add(FVector2D(0.0f, 0.0f));
  304. }
  305.  
  306. /* switch (block.id) {
  307. case 1:
  308. returnUvs.Add(FVector2D(0.0f, 0.0f));
  309. returnUvs.Add(FVector2D(0.5f, 0.0f));
  310. returnUvs.Add(FVector2D(0.0f, 0.5f));
  311. returnUvs.Add(FVector2D(0.5f, 0.5f));
  312. break;
  313. case 2:
  314. returnUvs.Add(FVector2D(0.5f, 0.0f));
  315. returnUvs.Add(FVector2D(1.0f, 0.0f));
  316. returnUvs.Add(FVector2D(0.5f, 0.5f));
  317. returnUvs.Add(FVector2D(1.0f, 0.5f));
  318. break;
  319. case 3:
  320. returnUvs.Add(FVector2D(0.0f, 0.0f));
  321. returnUvs.Add(FVector2D(0.5f, 0.0f));
  322. returnUvs.Add(FVector2D(0.0f, 0.5f));
  323. returnUvs.Add(FVector2D(0.5f, 0.5f));
  324. break;
  325. } */
  326.  
  327. TArray<int32> arrayTempPoints;
  328. switch (face) {
  329. case EBlockFace::LEFT:
  330. arrayTempPoints.Add(0);
  331. arrayTempPoints.Add(2);
  332. arrayTempPoints.Add(4);
  333. arrayTempPoints.Add(6);
  334. returnNormals.Add(FVector(-1, 0, 0));
  335. returnNormals.Add(FVector(-1, 0, 0));
  336. returnNormals.Add(FVector(-1, 0, 0));
  337. returnNormals.Add(FVector(-1, 0, 0));
  338. break;
  339. case EBlockFace::RIGHT:
  340. arrayTempPoints.Add(1);
  341. arrayTempPoints.Add(3);
  342. arrayTempPoints.Add(5);
  343. arrayTempPoints.Add(7);
  344. returnNormals.Add(FVector(1, 0, 0));
  345. returnNormals.Add(FVector(1, 0, 0));
  346. returnNormals.Add(FVector(1, 0, 0));
  347. returnNormals.Add(FVector(1, 0, 0));
  348. break;
  349. case EBlockFace::FRONT:
  350. arrayTempPoints.Add(4);
  351. arrayTempPoints.Add(5);
  352. arrayTempPoints.Add(6);
  353. arrayTempPoints.Add(7);
  354. returnNormals.Add(FVector(0, 1, 0));
  355. returnNormals.Add(FVector(0, 1, 0));
  356. returnNormals.Add(FVector(0, 1, 0));
  357. returnNormals.Add(FVector(0, 1, 0));
  358. break;
  359. case EBlockFace::BEHIND:
  360. arrayTempPoints.Add(0);
  361. arrayTempPoints.Add(1);
  362. arrayTempPoints.Add(2);
  363. arrayTempPoints.Add(3);
  364. returnNormals.Add(FVector(0, -1, 0));
  365. returnNormals.Add(FVector(0, -1, 0));
  366. returnNormals.Add(FVector(0, -1, 0));
  367. returnNormals.Add(FVector(0, -1, 0));
  368. break;
  369. case EBlockFace::TOP:
  370. arrayTempPoints.Add(2);
  371. arrayTempPoints.Add(3);
  372. arrayTempPoints.Add(6);
  373. arrayTempPoints.Add(7);
  374. returnNormals.Add(FVector(0, 0, 1));
  375. returnNormals.Add(FVector(0, 0, 1));
  376. returnNormals.Add(FVector(0, 0, 1));
  377. returnNormals.Add(FVector(0, 0, 1));
  378. break;
  379. case EBlockFace::BOTTOM:
  380. arrayTempPoints.Add(0);
  381. arrayTempPoints.Add(1);
  382. arrayTempPoints.Add(4);
  383. arrayTempPoints.Add(5);
  384. returnNormals.Add(FVector(0, 0, -1));
  385. returnNormals.Add(FVector(0, 0, -1));
  386. returnNormals.Add(FVector(0, 0, -1));
  387. returnNormals.Add(FVector(0, 0, -1));
  388. break;
  389. }
  390.  
  391. for (int32 pointNumber : arrayTempPoints) {
  392. arrayPoints[pointNumber] *= blockSize;
  393. arrayPoints[pointNumber] += tempLoc;
  394. returnPoints.Add(arrayPoints[pointNumber]);
  395. }
  396.  
  397. TArray<FVector> arrayTempTriangles;
  398. if (face == EBlockFace::LEFT || face == EBlockFace::BEHIND || face == EBlockFace::TOP) {
  399. returnTriangles.Add(FVector(2, 1, 0));
  400. returnTriangles.Add(FVector(1, 2, 3));
  401. }
  402. else {
  403. returnTriangles.Add(FVector(0, 1, 2));
  404. returnTriangles.Add(FVector(3, 2, 1));
  405. }
  406. }
  407.  
  408. FVector AAChunk::getBlockLocation(FBlockStructure const& tempBlock) const {
  409. return FVector(tempBlock.x, tempBlock.y, tempBlock.z);
  410. }
  411.  
  412. int32 AAChunk::breakBlock(FVector const& loc) {
  413. FVector tempLoc(loc.X - ((int32) loc.X%blockSize), loc.Y - ((int32)loc.Y%blockSize), loc.Z - ((int32)loc.Z%blockSize));
  414.  
  415. if (x < 0) {
  416. tempLoc += FVector(-blockSize, 0, 0);
  417. }
  418. if (y < 0) {
  419. tempLoc += FVector(0, -blockSize, 0);
  420. }
  421.  
  422. int32 tempIndice = getIndiceAtLocation(tempLoc);
  423. listBlocks[tempIndice] = FBlockStructure(listBlocks[tempIndice].x, listBlocks[tempIndice].y, listBlocks[tempIndice].z, 1);
  424. vertices.Empty();
  425. triangles.Empty();
  426. uvs.Empty();
  427. normals.Empty();
  428. index = -1;
  429. return tempIndice;
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement