智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...
随笔 - 991, 文章 - 0, 评论 - 27, 阅读 - 341万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Procedural Mesh Component in C++:Getting Started

Posted on   Bill Yuan  阅读(1336)  评论(0编辑  收藏  举报

转自:https://wiki.unrealengine.com/Procedural_Mesh_Component_in_C++:Getting_Started

I create a simple triangle using the UProceduralMeshComponent API, from there extending it should be easy. Once the class is compiled you can just drag it into your scene.

To use this component, include the paths in the build.cs file of your project:

MyProject.Build.cs:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ProceduralMeshComponent" });

and in your .uproject file (MyProject.uproject) in case you work on a project:

    "AdditionalDependencies": [..., ..., "ProceduralMeshComponent"]

After 4.17, plugins can now depend on other plugins, so in case you are working on a plugin instead of a project, you will have to add this to your .uplugin file:

复制代码
"Modules": [
      {
        ....
      }
    ],
    "Plugins": [                       // <--
      {                                // <--
        "Name": "ProceduralMeshComponent",    // <--
        "Enabled": true                // <--
      }                                // <--
    ]
复制代码

To fix errors with Visual Studio IntelliSense you need to right-click MyProject.uproject and re-generate Visual Studio project files. In Visual Studio 2017, open "Solution Explorer" and open the "Game" folder, right-click on the first line, which should be the root of your solution, select: "Rescan Solution".


I've created an Actor class.

Add the header to your MyActor.h file above the "MyActor.generated.h" include which has to be the last include.

#include "ProceduralMeshComponent.h"
#include "MyActor.generated.h"

In the header file, the following is added to support assigning a material.

private:
    UPROPERTY(VisibleAnywhere)
    UProceduralMeshComponent * mesh;

In my cpp file I have added the following to the constructor:

MyActor.cpp

复制代码
// Creating a standard root object.
AMyActor::AMyActor()
{
    mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
    RootComponent = mesh;
        // New in UE 4.17, multi-threaded PhysX cooking.
        mesh->bUseAsyncCooking = true;
}


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

// This is called when actor is already in level and map is opened
void AMyActor::PostLoad()
{
    Super::PostLoad();
    CreateTriangle();
}

void AMyActor::CreateTriangle()
{
    TArray<FVector> vertices;
    vertices.Add(FVector(0, 0, 0));
    vertices.Add(FVector(0, 100, 0));
    vertices.Add(FVector(0, 0, 100));

    TArray<int32> Triangles;
    Triangles.Add(0);
    Triangles.Add(1);
    Triangles.Add(2);

    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.75, 0.75, 0.75, 1.0));
    vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
    vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));

    mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, normals, UV0, vertexColors, tangents, true);
        
        // Enable collision data
    mesh->ContainsPhysicsTriMeshData(true);
}
复制代码

The documentation for CreateMeshSection and CreateMeshSection_LinearColor functions is this:

复制代码
/**
    *    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.
    */

// '''Don't use this function'''. It is deprecated. Use LinearColor version.
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);

// In this one you can send FLinearColor instead of FColor for the Vertex Colors.
void CreateMeshSection_LinearColor(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals, const TArray<FVector2D>& UV0, const TArray<FLinearColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision)

// Updates a section of this procedural mesh component. This is faster than CreateMeshSection, but does not let you change topology. Collision info is also updated.
void UpdateMeshSection_LinearColor(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<FVector>& Normals, const TArray<FVector2D>& UV0, const TArray<FLinearColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents);
复制代码

If you have a <YourGameName>GameModeBase.cpp, make sure to add a reference to the header of the class where you added the above code, that way you will see it in your Editor in "C++ Classes" Content Browser and will be able to drag it to your scene.

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2017-11-07 俞敏洪:我创业24年感悟的3条CEO守则
2017-11-07 做开发十年,我总结出了这些开发经验
2017-11-07 UE4如何检测目标在锥形视野内
2017-11-07 UE4 几个好用的插件和Wiki教程
点击右上角即可分享
微信分享提示