Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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 j; int i = 0; // used in the triangles&stuff generator loop
  15.  
  16. AVoxine::AVoxine()
  17. {
  18. USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
  19. RootComponent = SphereComponent;
  20.  
  21. UProceduralMeshComponent* mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
  22.  
  23. /**
  24. * Create/replace a section for this procedural mesh component.
  25. * @param SectionIndex Index of the section to create or replace.
  26. * @param Vertices Vertex buffer of all vertex positions to use for this mesh section.
  27. * @param Triangles Index buffer indicating which vertices make up each triangle. Length must be a multiple of 3.
  28. * @param Normals Optional array of normal vectors for each vertex. If supplied, must be same length as Vertices array.
  29. * @param UV0 Optional array of texture co-ordinates for each vertex. If supplied, must be same length as Vertices array.
  30. * @param VertexColors Optional array of colors for each vertex. If supplied, must be same length as Vertices array.
  31. * @param Tangents Optional array of tangent vector for each vertex. If supplied, must be same length as Vertices array.
  32. * @param bCreateCollision Indicates whether collision should be created for this section. This adds significant cost.
  33. */
  34. //UFUNCTION(BlueprintCallable, Category = "Components|ProceduralMesh", meta = (AutoCreateRefTerm = "Normals,UV0,VertexColors,Tangents"))
  35. // void CreateMeshSection(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals,
  36. // const TArray<FVector2D>& UV0, const TArray<FColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision);
  37.  
  38. for (int x = 0; x <= 16 ; x++)
  39. {
  40. for (int y = 0; y <= 16; y++)
  41. {
  42. for (int z = 0; z <= 256; z++)
  43. {
  44.  
  45. MakeACube(x, y, z);
  46.  
  47. // this loop generates triangles, normals, uv, colors and tangents
  48. for (int j = i*36; j <= ((i * 36) + 35); j++)
  49. {
  50. Triangles.Add(j);
  51. normals.Add(FVector(1, 0, 0));
  52. UV0.Add(FVector2D(0, 0));
  53. vertexColors.Add(FColor(100, 100, 100, 0));
  54. tangents.Add(FProcMeshTangent(1, 1, 1));
  55. }
  56.  
  57. i++; // to allocate next 36-ple of triangles
  58. }
  59. }
  60. }
  61.  
  62. // mesh generation
  63. mesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
  64.  
  65. // With default options
  66. //mesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), false);
  67.  
  68. // instantiate the generated mesh
  69. mesh->AttachTo(RootComponent);
  70. }
  71.  
  72.  
  73. // Called when the game starts or when spawned
  74. void AVoxine::BeginPlay()
  75. {
  76. Super::BeginPlay();
  77.  
  78. }
  79.  
  80. // Called every frame
  81. void AVoxine::Tick(float DeltaTime)
  82. {
  83. Super::Tick(DeltaTime);
  84.  
  85. }
  86.  
  87. void AVoxine::MakeACube(int gridPosX, int gridPosY, int gridPosZ)
  88. {
  89. FVector vertex1 = FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength);
  90. FVector vertex2 = FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength);
  91. FVector vertex3 = FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength);
  92. FVector vertex4 = FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength);
  93. FVector vertex5 = FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength);
  94. FVector vertex6 = FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength);
  95. FVector vertex7 = FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength);
  96. FVector vertex8 = FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength);
  97.  
  98. // front face
  99. vertices.Add(vertex1);
  100. vertices.Add(vertex2);
  101. vertices.Add(vertex3);
  102.  
  103. vertices.Add(vertex3);
  104. vertices.Add(vertex2);
  105. vertices.Add(vertex4);
  106.  
  107. // back face
  108. vertices.Add(vertex5);
  109. vertices.Add(vertex6);
  110. vertices.Add(vertex7);
  111.  
  112. vertices.Add(vertex6);
  113. vertices.Add(vertex8);
  114. vertices.Add(vertex7);
  115.  
  116. // top face
  117. vertices.Add(vertex6);
  118. vertices.Add(vertex4);
  119. vertices.Add(vertex8);
  120.  
  121. vertices.Add(vertex3);
  122. vertices.Add(vertex4);
  123. vertices.Add(vertex6);
  124.  
  125. // bottom face
  126. vertices.Add(vertex5);
  127. vertices.Add(vertex7);
  128. vertices.Add(vertex2);
  129.  
  130. vertices.Add(vertex1);
  131. vertices.Add(vertex5);
  132. vertices.Add(vertex2);
  133.  
  134. // right face
  135. vertices.Add(vertex2);
  136. vertices.Add(vertex7);
  137. vertices.Add(vertex4);
  138.  
  139. vertices.Add(vertex7);
  140. vertices.Add(vertex8);
  141. vertices.Add(vertex4);
  142.  
  143. // left face
  144. vertices.Add(vertex1);
  145. vertices.Add(vertex3);
  146. vertices.Add(vertex5);
  147.  
  148. vertices.Add(vertex5);
  149. vertices.Add(vertex3);
  150. vertices.Add(vertex6);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement