转自: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(, , ));
vertices.Add(FVector(, , ));
vertices.Add(FVector(, , )); TArray<int32> Triangles;
Triangles.Add();
Triangles.Add();
Triangles.Add(); TArray<FVector> normals;
normals.Add(FVector(, , ));
normals.Add(FVector(, , ));
normals.Add(FVector(, , )); TArray<FVector2D> UV0;
UV0.Add(FVector2D(, ));
UV0.Add(FVector2D(, ));
UV0.Add(FVector2D(, )); TArray<FProcMeshTangent> tangents;
tangents.Add(FProcMeshTangent(, , ));
tangents.Add(FProcMeshTangent(, , ));
tangents.Add(FProcMeshTangent(, , )); 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(, 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.

最新文章

  1. (转)dubbo框架基本分析
  2. C#集合类型大盘点
  3. C# 隐式类型转换
  4. UESTC 764 失落的圣诞节 --RMQ/线段树
  5. Android 获取本机WIFI及3G网络IP
  6. &quot;NO 3D support is available from the host&quot;
  7. JavaScript向select下拉框中加入和删除元素
  8. tomcat基础应用
  9. 迈向angularjs2系列(6):路由机制
  10. 解决thymeleaf layout布局不生效
  11. Linux 容器 vs 虚拟机 —— 谁更胜一筹
  12. iOS 类似朋友圈的图片浏览器SDPhotoBrowser
  13. 一个简单的安卓+Servlet图片上传例子
  14. 51单片机GPIO口模拟串口通信
  15. UVA225-Golygons(dfs)
  16. 关于Python ,requests的小技巧
  17. Angular2学习笔记
  18. EF 数据迁移
  19. Node.js HTTP Server对象及GET、POST请求
  20. exception http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

热门文章

  1. BM算法【实数模板】
  2. 四.python基础数据类型
  3. 2019.11.30 Mysql查询知识
  4. WinDbg常用命令系列---||(系统状态)
  5. 通过redash query results 数据源实现跨数据库的查询
  6. 洛谷 P2563 [AHOI2001]质数和分解 题解
  7. 洛谷 P2085 最小函数值
  8. 机器学习---逻辑回归(一)(Machine Learning Logistic Regression I)
  9. PE安装器说明by双心
  10. Ecms7.5版CK编辑器保留word格式如何修改