antoinefortin
11/14/2019 - 3:16 AM

ProceduralMeshCircular.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "BasicProceduralMesh.h"
#define T_PI 6.28318530718

// Sets default values
ABasicProceduralMesh::ABasicProceduralMesh()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	myProceduralMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
	RootComponent = myProceduralMesh;
	// New in UE 4.17, multi-threaded PhysX cooking.
	myProceduralMesh->bUseAsyncCooking = true;



}

// Called when the game starts or when spawned
void ABasicProceduralMesh::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABasicProceduralMesh::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// This is called when actor is spawned (at runtime or when you drop it into the world in editor)
void ABasicProceduralMesh::PostActorCreated()
{
	Super::PostActorCreated();
	// CreerUnTriangle();
	creerTriangle();
}

void ABasicProceduralMesh::PostLoad()
{
	Super::PostLoad();
	// CreerUnTriangle();
	creerTriangle();
}

void ABasicProceduralMesh::creerCercle()
{
	TArray<FVector> vertices;	
	//vertices.Add(FVector(x,y,z));
	int stepAroundCircle = 150;
	int radius = 100;
	float angle = T_PI /stepAroundCircle;

	// Process vertex to Polar Coordinate --> Cartesian in main loop buffer
	for (int i = 0; i < stepAroundCircle; i++)
	{
		vertices.Add(FVector(0, 0, 0));
		vertices.Add(FVector(0, radius * cos(angle * (i)), radius * sin(angle * (i))));
		vertices.Add(FVector(0, radius * cos(angle * (i + 1)), radius * sin(angle * (i + 1))));
 		
	}
	
	TArray<int32> Triangles;
	
	for (int i = 0; i < stepAroundCircle * 3; i++)
	{
		Triangles.Add(i);
	}
	

	
	/* Copy pasted */
	TArray<FVector> normals;
	normals.Add(FVector(1, 0, 0));
	normals.Add(FVector(1, 0, 0));
	normals.Add(FVector(1, 0, 0));

	TArray<FVector2D> UV0;
	UV0.Add(FVector2D(0, 0));
	UV0.Add(FVector2D(10, 0));
	UV0.Add(FVector2D(0, 10));

	TArray<FProcMeshTangent> tangents;
	tangents.Add(FProcMeshTangent(0, 1, 0));
	tangents.Add(FProcMeshTangent(0, 1, 0));
	tangents.Add(FProcMeshTangent(0, 1, 0));

	TArray<FLinearColor> vertexColors;
	vertexColors.Add(FLinearColor(0, 0, 0, 1.0));
	vertexColors.Add(FLinearColor(0.5, 0.5, 0.75, 1.0));
	vertexColors.Add(FLinearColor(0, 0, 0, 1.0));
	/* Copy pasted */


	myProceduralMesh->CreateMeshSection_LinearColor(0, vertices, Triangles, normals, UV0, vertexColors, tangents, true);

	// Enable collision data
	myProceduralMesh->ContainsPhysicsTriMeshData(true);
}