Hi!
I’m using Unreal 5.2.1 and creating a Procedural Mesh for the first time.
I have this class:
UCLASS()
class MYPROJECT_API UPStickProceduralMeshComponent : public UProceduralMeshComponent
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Category = "Line Component")
double Radius{ 100.0f };
UPROPERTY(EditAnywhere, Category = "Line Component")
uint32 Side {
2
};
UPROPERTY(EditAnywhere, Category = "Line Component")
float Thickness{
1.0f
};
UPROPERTY(EditAnywhere, Category = "Line Component")
FLinearColor Color {
1.0f, 1.0f, 1.0f, 1.0
};
UPROPERTY(EditAnywhere, Category = "Line Component")
UMaterial* Material{
nullptr
};
UPROPERTY(EditAnywhere, Category = "Line Component")
float Intensity{
100.0f
};
UPStickProceduralMeshComponent(const FObjectInitializer& ObjectIn);
protected:
UPROPERTY()
UMaterialInstanceDynamic* MaterialInstanceDynamic{ nullptr };
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void PostLoad() override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
void BuildMesh();
};
With this BuildMesh
code:
void UPStickProceduralMeshComponent::BuildMesh()
{
TArray<FVector> Vertices;
TArray<FLinearColor> Colors;
TArray<FVector> Normals;
TArray<int32> Triangles;
FVector Point, DirVert, DirHorz; // Direction Up, Out
FVector V0, V1, V2, V3;
FVector LastV0, LastV1, LastV2, LastV3;
int32 I0, I1, I2, I3;
int32 LastI0, LastI1, LastI2, LastI3;
float SinTheta, CosTheta, Theta;
const float UnitSize = 0.05f; // Half size of unit 0.1f
for (uint32 i = 0; i <= Side; ++i)
{
Theta = FMath::DegreesToRadians(i * 360.0f / Side);
FMath::SinCos<float>(&SinTheta, &CosTheta, Theta);
Point = FVector(SinTheta * Radius, CosTheta * Radius, 0.0f);
DirVert = FVector(0.0f, 0.0f, 1.0f);
DirHorz = FVector(SinTheta, CosTheta, 0.0f);
V0 = Point + DirHorz * UnitSize;
V1 = Point + DirHorz * -UnitSize;
V2 = Point + DirVert * UnitSize;
V3 = Point + DirVert * -UnitSize;
I0 = Vertices.Add(V0);
I1 = Vertices.Add(V1);
I2 = Vertices.Add(V2);
I3 = Vertices.Add(V3);
Normals.Add(DirHorz);
Normals.Add(-DirHorz);
Normals.Add(DirVert);
Normals.Add(-DirVert);
Colors.Add(Color);
Colors.Add(Color);
Colors.Add(Color);
Colors.Add(Color);
if (i > 0) // Generate triangles except first index
{
Triangles.Add(LastI0); Triangles.Add(I0), Triangles.Add(I1);
Triangles.Add(I1), Triangles.Add(LastI1), Triangles.Add(LastI0);
Triangles.Add(LastI2); Triangles.Add(I2), Triangles.Add(I3);
Triangles.Add(I3), Triangles.Add(LastI3), Triangles.Add(LastI2);
}
LastV0 = V0;
LastV1 = V1;
LastV2 = V2;
LastV3 = V3;
LastI0 = I0;
LastI1 = I1;
LastI2 = I2;
LastI3 = I3;
}
CreateMeshSection_LinearColor(0, Vertices, Triangles, Normals, TArray<FVector2D>(), Colors, TArray<FProcMeshTangent>(), false);
if (MaterialInstanceDynamic == nullptr)
{
MaterialInstanceDynamic = UMaterialInstanceDynamic::Create(Material, this);
}
MaterialInstanceDynamic->SetScalarParameterValue("Thickness", Thickness);
MaterialInstanceDynamic->SetScalarParameterValue("Intensity", Intensity);
SetMaterial(0, MaterialInstanceDynamic);
}
I added the component to an AActor
class:
UCLASS()
class MYPROJECT_API APStickActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APStickActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UPStickProceduralMeshComponent* LineComponent{ nullptr };
};
Code:
APStickActor::APStickActor()
{
PrimaryActorTick.bCanEverTick = true;
LineComponent = CreateDefaultSubobject<UPStickProceduralMeshComponent>(TEXT("LineComponent"));
RootComponent = LineComponent;
}
// Called when the game starts or when spawned
void APStickActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APStickActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
To use it I have created a blueprint class that inherits from it, but when I check the view port for this actor I see that the component has been created along the Y axis and I can’t rotate it:
How can I create it along X axis?
Or maybe, I have to rotate it.
Thanks!