Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Psyrain.h"
- #include "Voxine.h"
- #include "ProceduralMeshComponent.h"
- float edgeLength = 100.0; // cube edge length
- TArray<FVector> vertices; // contains one cube vertices
- TArray<int32> Triangles; // store triangles which composes one cube
- TArray<FVector> normals; // store normal vectors of one cube vertices
- TArray<FVector2D> UV0; // store texture co-ordinates of one cube vertices
- TArray<FColor> vertexColors; // store colors of one cube vertices
- TArray<FProcMeshTangent> tangents; // store tangents of one cube vertices
- int i = 0; // integer to instantiate mesh sections
- AVoxine::AVoxine()
- {
- USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
- RootComponent = SphereComponent;
- UProceduralMeshComponent* mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
- /**
- * Create/replace a section for this procedural mesh component.
- * @param SectionIndex Index of the section to create or replace.
- * @param Vertices Vertex buffer of all vertex positions to use for this mesh section.
- * @param Triangles Index buffer indicating which vertices make up each triangle. Length must be a multiple of 3.
- * @param Normals Optional array of normal vectors for each vertex. If supplied, must be same length as Vertices array.
- * @param UV0 Optional array of texture co-ordinates for each vertex. If supplied, must be same length as Vertices array.
- * @param VertexColors Optional array of colors for each vertex. If supplied, must be same length as Vertices array.
- * @param Tangents Optional array of tangent vector for each vertex. If supplied, must be same length as Vertices array.
- * @param bCreateCollision Indicates whether collision should be created for this section. This adds significant cost.
- */
- //UFUNCTION(BlueprintCallable, Category = "Components|ProceduralMesh", meta = (AutoCreateRefTerm = "Normals,UV0,VertexColors,Tangents"))
- // void CreateMeshSection(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals,
- // const TArray<FVector2D>& UV0, const TArray<FColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision);
- for (int x = 0; x <= 4; x++)
- {
- for (int y = 0; y <= 4; y++)
- {
- for (int z = 0; z <= 4; z++)
- {
- MakeACube(x, y, z);
- // mesh generation
- mesh->CreateMeshSection(i, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
- // With default options
- //mesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), false);
- // empty everything to be ready to generate another cube
- vertices.Empty();
- Triangles.Empty();
- i++; // next section
- }
- }
- }
- // instantiate the generated mesh
- mesh->AttachTo(RootComponent);
- }
- // Called when the game starts or when spawned
- void AVoxine::BeginPlay()
- {
- Super::BeginPlay();
- }
- // Called every frame
- void AVoxine::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- }
- void AVoxine::MakeACube(int gridPosX, int gridPosY, int gridPosZ)
- {
- // front face
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- // back face
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- // top face
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- // bottom face
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- // right face
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength));
- // left face
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength));
- vertices.Add(FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- vertices.Add(FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength));
- // this loop generates triangles, normals, uv, colors and tangents
- for (int j = 0; j < 36; j++)
- {
- Triangles.Add(j);
- normals.Add(FVector(1, 0, 0)); // most of these values are absolutely meaningless
- UV0.Add(FVector2D(0, 0)); // I set them just to have to deal with a similar amount of values
- vertexColors.Add(FColor(100, 100, 100, 0)); // than what it'll be like when I'll reach the point
- tangents.Add(FProcMeshTangent(1, 1, 1)); // to need to set meaningful ones
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement