一.Custom Mesh
二.Procedural Mesh
public: AMyActor(); protected: virtual void BeginPlay() override; UPROPERTY(VisibleAnywhere, BlueprintReadWrite) UProceduralMeshComponent* CustomMesh; /* The vertices of the mesh */ TArray<FVector> Vertices; /* The triangles of the mesh */ TArray<int32> Triangles; /* Creates a triangle that connects the given vertices */ void AddTriangle(int32 V1, int32 V2, int32 V3); void GenerateCubeMesh(); public: //Called every frame virtual void Tick(float DeltaTime) override;
//Sets default values AMyActor::AMyActor() { // Set this actor to call Tick() every frame. You can turn this off to PrimaryActorTick.bCanEverTick = true; CustomMesh = CreateDefaultSubobject<UProceduralMeshComponent>("CustomMesh"); SetRootComponent(CustomMesh); CustomMesh->bUseAsyncCooking = true; } // Called when the game starts or when spawned void AMyActor::BeginPlay() { Super::BeginPlay(); GenerateCubeMesh(); } void AMyActor::AddTriangle(int32 V1, int32 V2, int32 V3) { Triangles.Add(V1); Triangles.Add(V2); Triangles.Add(V3); } void AMyActor::GenerateCubeMesh() { //6 sides on cube, 4 verts each(corners) //These are relative locations to the placed Actor in the world Vertices.Add(FVector(0, -100, 0)); Vertices.Add(FVector(0, -100, 100)); Vertices.Add(FVector(0, 100, 0)); Vertices.Add(FVector(0, 100, 100)); Vertices.Add(FVector(100, -100, 0)); Vertices.Add(FVector(100, -100, 100)); Vertices.Add(FVector(100, 100, 100)); Vertices.Add(FVector(100, 100, 0)); //Back face AddTriangle(0, 2, 3); AddTriangle(3, 1, 0); //Left face AddTriangle(0, 1, 4); AddTriangle(4, 1, 5); //Front face AddTriangle(4, 5, 7); AddTriangle(7, 5, 6); //Right face AddTriangle(7, 6, 3); AddTriangle(3, 2, 7); //Top face AddTriangle(1, 3, 5); AddTriangle(6, 5, 3); //bottom face AddTriangle(2, 0, 4); AddTrianlgle(4, 7, 2); TArray<FLinearColor> VertexColors; VertexColors.Add(FLinearColor(0.f, 0.f, 1.f)); VertexColors.Add(FLinearColor(1.f, 0.f, 0.f)); VertexColors.Add(FLinearColor(1.f, 0.f, 0.f)); VertexColors.Add(FLinearColor(0.f, 1.f, 0.f)); VertexColors.Add(FLinearColor(0.5f, 1.f, 0.5f)); VertexColors.Add(FLinearColor(0.f, 1.f, 0.f)); VertexColors.Add(FLinearColor(1.f, 1.f, 0.f)); VertexColors.Add(FLinearColor(0.f, 1.f, 1.f)); CustomMesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), VertexColors, TArray<FProcMeshTangent>(), true); }
三.
UActorComponent USceneComponent UPrimitiveComponent UMeshComponent