Advertisement
Guest User

Untitled

a guest
May 1st, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. #include "Psyrain.h"
  2. #include "Voxine.h"
  3. #include "ProceduralMeshComponent.h"
  4.  
  5.  
  6. float edgeLength = 100.0; // cube edge length
  7. TArray<FVector> vertices; // contains one cube vertices
  8. TArray<int32> Triangles; // store triangles which composes one cube
  9. TArray<FVector> normals; // store normal vectors of one cube vertices
  10. TArray<FVector2D> UV0; // store texture co-ordinates of one cube vertices
  11. TArray<FColor> vertexColors; // store colors of one cube vertices
  12. TArray<FProcMeshTangent> tangents; // store tangents of one cube vertices
  13.  
  14. int i = 0; // integer to instantiate mesh sections
  15.  
  16.  
  17. AVoxine::AVoxine()
  18. {
  19. USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
  20. RootComponent = SphereComponent;
  21.  
  22. UProceduralMeshComponent* mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
  23.  
  24. /**
  25. * Create/replace a section for this procedural mesh component.
  26. * @param SectionIndex Index of the section to create or replace.
  27. * @param Vertices Vertex buffer of all vertex positions to use for this mesh section.
  28. * @param Triangles Index buffer indicating which vertices make up each triangle. Length must be a multiple of 3.
  29. * @param Normals Optional array of normal vectors for each vertex. If supplied, must be same length as Vertices array.
  30. * @param UV0 Optional array of texture co-ordinates for each vertex. If supplied, must be same length as Vertices array.
  31. * @param VertexColors Optional array of colors for each vertex. If supplied, must be same length as Vertices array.
  32. * @param Tangents Optional array of tangent vector for each vertex. If supplied, must be same length as Vertices array.
  33. * @param bCreateCollision Indicates whether collision should be created for this section. This adds significant cost.
  34. */
  35. //UFUNCTION(BlueprintCallable, Category = "Components|ProceduralMesh", meta = (AutoCreateRefTerm = "Normals,UV0,VertexColors,Tangents"))
  36. // void CreateMeshSection(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals,
  37. // const TArray<FVector2D>& UV0, const TArray<FColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision);
  38.  
  39. for (int x = 0; x <= 4; x++)
  40. {
  41. for (int y = 0; y <= 4; y++)
  42. {
  43. for (int z = 0; z <= 4; z++)
  44. {
  45.  
  46. MakeACube(x, y, z);
  47.  
  48. // mesh generation
  49. mesh->CreateMeshSection(i, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
  50.  
  51. // With default options
  52. //mesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), false);
  53.  
  54. // empty everything to be ready to generate another cube
  55. vertices.Empty();
  56. Triangles.Empty();
  57.  
  58. i++; // next section
  59. }
  60. }
  61. }
  62.  
  63. // instantiate the generated mesh
  64. mesh->AttachTo(RootComponent);
  65. }
  66.  
  67.  
  68. // Called when the game starts or when spawned
  69. void AVoxine::BeginPlay()
  70. {
  71. Super::BeginPlay();
  72.  
  73. }
  74.  
  75. // Called every frame
  76. void AVoxine::Tick(float DeltaTime)
  77. {
  78. Super::Tick(DeltaTime);
  79.  
  80. }
  81.  
  82. void AVoxine::MakeACube(int gridPosX, int gridPosY, int gridPosZ)
  83. {
  84. // front face
  85. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  86. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  87. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  88.  
  89. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  90. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  91. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  92.  
  93. // back face
  94. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  95. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  96. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  97.  
  98. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  99. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  100. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  101.  
  102. // top face
  103. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  104. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  105. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  106.  
  107. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  108. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  109. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  110.  
  111. // bottom face
  112. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  113. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  114. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  115.  
  116. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  117. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  118. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  119.  
  120. // right face
  121. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  122. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  123. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  124.  
  125. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
  126. vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, edgeLength));
  127. vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
  128.  
  129. // left face
  130. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  131. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  132. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  133.  
  134. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
  135. vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  136. vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
  137.  
  138. // this loop generates triangles, normals, uv, colors and tangents
  139. for (int j = 0; j < 36; j++)
  140. {
  141. Triangles.Add(j);
  142. normals.Add(FVector(1, 0, 0)); // most of these values are absolutely meaningless
  143. UV0.Add(FVector2D(0, 0)); // I set them just to have to deal with a similar amount of values
  144. vertexColors.Add(FColor(100, 100, 100, 0)); // than what it'll be like when I'll reach the point
  145. tangents.Add(FProcMeshTangent(1, 1, 1)); // to need to set meaningful ones
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement