Advertisement
Guest User

Untitled

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