UCustomMeshComponent 只能支持简单的自定mesh,这种Mesh并不支持贴图材质。官方的API也只有简单的三个函数而已。
ACustomSphereActor 是我自定义的辅助Actor
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CustomMeshComponent.h"
#include "GameFramework/Actor.h"
#include "Materials/Material.h"
#include "CustomPanelActor.generated.h"
UCLASS()
class CUSTOMLINEPLUGINS_API ACustomPanelActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACustomPanelActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
UPROPERTY(VisibleAnywhere)
UCustomMeshComponent* MeshComponent;
UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "Custom")
UMaterial* Material;
UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "Custom")
FVector ReocrdPos;
public:
UFUNCTION(BlueprintCallable, Category = "Custom")
void Create();
UFUNCTION(BlueprintCallable, Category = "Custom")
void Update(float Len);
UFUNCTION(BlueprintCallable, Category = "Custom")
void CreateSmallSphere(FVector Location,FString Name,float Scale);
private:
int num=180;
TArray<FVector> Points;
TArray<FCustomMeshTriangle> Meshvertices;
};
#include "CustomPanelActor.h"
#include "CustomMeshPanelActor.h"
#include "CustomSphereActor.h"
// Sets default values
ACustomPanelActor::ACustomPanelActor()
{
// 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;
MeshComponent = CreateDefaultSubobject<UCustomMeshComponent>(TEXT("CustomMeshComponentZ"));
}
// Called when the game starts or when spawned
void ACustomPanelActor::BeginPlay()
{
Super::BeginPlay();
ReocrdPos=GetActorLocation();
Create();
}
// Called every frame
void ACustomPanelActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACustomPanelActor::Create()
{
float angle = 360.0f / num;
FVector dir = FVector::Zero();
FVector forward = FVector::ForwardVector;
Points.Add(ReocrdPos);
//CreateSmallSphere(ReocrdPos,"AA_"+FString::FromInt(0),0.2f);
for (int i = 1; i < num+1; i++)
{
FVector vec = FVector(0,0,angle * i);
FQuat rot=FQuat::MakeFromEuler(vec);
dir = rot * forward;
Points.Add(ReocrdPos+dir);
// CreateSmallSphere(ReocrdPos+dir,"AA_"+FString::FromInt(i),0.2f);
}
for (int i = 0; i < num; i++)
{
FCustomMeshTriangle temp;
temp.Vertex0 = Points[num-1==i?1:2+i];
temp.Vertex1 = Points[num-1==i?i:1+i];
temp.Vertex2 = Points[0];
Meshvertices.Add(temp);
}
MeshComponent->AddCustomMeshTriangles(Meshvertices);
if(Material!=NULL)
{
UMaterialInterface* InterfacePtr = reinterpret_cast<UMaterialInterface*>(Material);
MeshComponent->SetMaterial(0,InterfacePtr);
}
}
void ACustomPanelActor::Update(float Len)
{
float angle = 360.0f / num;
FVector dir = FVector::Zero();
FVector forward = FVector::ForwardVector;
Points[0]=ReocrdPos;
for (int i = 1; i < num+1; i++)
{
FVector vec = FVector(0,0,angle * i);
FQuat rot=FQuat::MakeFromEuler(vec);
dir = rot * forward*Len;
Points[i]=(ReocrdPos+dir);
}
for (int i = 0; i < num; i++)
{
FCustomMeshTriangle temp;
temp.Vertex0 = Points[num-1==i?1:2+i];
temp.Vertex1 = Points[num-1==i?i:1+i];
temp.Vertex2 = Points[0];
Meshvertices[i]=temp;
}
MeshComponent->SetCustomMeshTriangles(Meshvertices);
}
void ACustomPanelActor::CreateSmallSphere(FVector Location,FString Name,float Scale=0.3f)
{
Location=ReocrdPos+Location;
ACustomSphereActor* Temp=GetWorld()->SpawnActor<ACustomSphereActor>(ACustomSphereActor::StaticClass(),Location, FRotator(0.0f));
Temp->SetActorScale3D(FVector(Scale,Scale,Scale));
Temp->SetActorLabel(Name);
//#if WITH_EDITOR
//ACustomSphereActor* temp = GetWorld()->SpawnActor<ACustomSphereActor>(ACustomSphereActor::StaticClass(), Location, FRotator(0.f));
// ACustomSphereActor* Temp = GetWorld()->SpawnActor<ACustomSphereActor>(Location, FRotator(0.f));
//#endif
}