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 j; int i = 0; // used in the triangles&stuff generator loop
- 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 <= 16 ; x++)
- {
- for (int y = 0; y <= 16; y++)
- {
- for (int z = 0; z <= 256; z++)
- {
- MakeACube(x, y, z);
- // this loop generates triangles, normals, uv, colors and tangents
- for (int j = i*36; j <= ((i * 36) + 35); j++)
- {
- Triangles.Add(j);
- normals.Add(FVector(1, 0, 0));
- UV0.Add(FVector2D(0, 0));
- vertexColors.Add(FColor(100, 100, 100, 0));
- tangents.Add(FProcMeshTangent(1, 1, 1));
- }
- i++; // to allocate next 36-ple of triangles
- }
- }
- }
- // mesh generation
- mesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
- // With default options
- //mesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), false);
- // 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)
- {
- FVector vertex1 = FVector(gridPosX*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength);
- FVector vertex2 = FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength);
- FVector vertex3 = FVector(gridPosX*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength);
- FVector vertex4 = FVector(gridPosX*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength);
- FVector vertex5 = FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, gridPosZ*edgeLength);
- FVector vertex6 = FVector((gridPosX + 1)*edgeLength, gridPosY*edgeLength, (gridPosZ + 1)*edgeLength);
- FVector vertex7 = FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, gridPosZ*edgeLength);
- FVector vertex8 = FVector((gridPosX + 1)*edgeLength, (gridPosY + 1)*edgeLength, (gridPosZ + 1)*edgeLength);
- // front face
- vertices.Add(vertex1);
- vertices.Add(vertex2);
- vertices.Add(vertex3);
- vertices.Add(vertex3);
- vertices.Add(vertex2);
- vertices.Add(vertex4);
- // back face
- vertices.Add(vertex5);
- vertices.Add(vertex6);
- vertices.Add(vertex7);
- vertices.Add(vertex6);
- vertices.Add(vertex8);
- vertices.Add(vertex7);
- // top face
- vertices.Add(vertex6);
- vertices.Add(vertex4);
- vertices.Add(vertex8);
- vertices.Add(vertex3);
- vertices.Add(vertex4);
- vertices.Add(vertex6);
- // bottom face
- vertices.Add(vertex5);
- vertices.Add(vertex7);
- vertices.Add(vertex2);
- vertices.Add(vertex1);
- vertices.Add(vertex5);
- vertices.Add(vertex2);
- // right face
- vertices.Add(vertex2);
- vertices.Add(vertex7);
- vertices.Add(vertex4);
- vertices.Add(vertex7);
- vertices.Add(vertex8);
- vertices.Add(vertex4);
- // left face
- vertices.Add(vertex1);
- vertices.Add(vertex3);
- vertices.Add(vertex5);
- vertices.Add(vertex5);
- vertices.Add(vertex3);
- vertices.Add(vertex6);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement