0%

UE4 ProceduralMeshComponent踩坑日记

UE4 使用ProceduralMeshComponent运行时显示动态物体的坑

主要使用的接口

1
2
3
4
5
6
7
8
9
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)

首先遇到的问题

  • 无法导入"ProceduralMeshComponent.h" 头文件, 明明我可以找到这个文件,但是却无法导入。

查阅官方文档获得答案,则是个实验性功能, 需要.build.cs 文件中添加对模块的依赖。

创建工程后的默认构建文件内容:

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

这里我的项目名字是ProceduralMeshDemo, 所以构建文件在VS中的路径 Games/Source/ProceduralMeshDemo/ProceduralMeshDemo.build.cs

在依赖中添加 "ProceduralMeshComponent", 最终内容为:

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

按照官方文档的说法,我需要再在 <ProjectName> 中添加插件配置, 官方文档较旧,我发现我没有那么做,插件依然可见可用了。我的UE4版本是4.24.3,所以,至少这个版本之后就可以不用这个步骤了。

接下来的问题

  • 成功创建了Mesh并展示出来了,但是VertexColor没有生效,模型没有颜色。

这个问题我没有找到官方的文档解决方式, 网上资料也了了少的可怜,只有少数朋友使用ProceduralMeshComponent来动态创建模型,但是并不对模型进行着色。最终在问答区找到了一个还比较贴切的问题,有大佬给了个含糊的回答。

Creating a material with "vertex colors" node plugged into the base color slot, > then using

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
mesh->SetMaterial(2, Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), NULL, *FString(UISC::florMTLoc + materials[0]))));  ```

Be sure to set the material index to match that of the element index of your procedural mesh. In my case it was indexs 1 and 2 (which is why it didn't show before because I used set material with index 0).


原答案地址: [How can i see vertex colors on procedural mesh?](https://answers.unrealengine.com/questions/395390/how-can-i-see-vertex-colors-on-procedural-mesh.html)

意思是要使用要给Vertex Clolors 作为基色的材质才行。

于是创建一个材质:

![][VertexColorMaterial]

贴一下构建平面的关键代码。
```cpp
const auto RowCount = 5;
const auto ColCount = 5;
//Construct Vertices
TArray<FVector> Vertices;
TArray<FColor> VertColors;
for (auto Row = 0; Row < RowCount; ++Row)
{
for (auto Col = 0; Col < ColCount; ++Col)
{
Vertices.Add(FVector(Row*10, Col*10, 10));
VertColors.Add(FColor(Row*40, Col*40, 100));
}
}

//Construct Triangles
TArray<int32> TriangleIndexes;
for (auto Row = 0; Row < RowCount-1; ++Row)
{
for (auto Col = 0; Col < ColCount-1; ++Col)
{
TriangleIndexes.Add(Row * ColCount + Col);
TriangleIndexes.Add(Row * ColCount + Col + 1);
TriangleIndexes.Add((Row + 1) * ColCount + Col);

TriangleIndexes.Add((Row + 1) * ColCount + Col);
TriangleIndexes.Add(Row * ColCount + Col + 1);
TriangleIndexes.Add((Row + 1 )* ColCount + Col + 1);
}
}

Mesh->CreateMeshSection(0, Vertices, TriangleIndexes, TArray<FVector>(), TArray<FVector2D>(), VertColors, TArray<FProcMeshTangent>(), false);
Mesh->SetMaterial(0, Material);

至此,终于初见效果了:

生活处处皆是坑

本以为是守得云开见月明, 明月就闪了一下。一段优化之后,莫名奇妙的出现了问题,Demo是在BeginPlay事件中创建的。莫名奇妙的Tick函数就不调了,九牛二虎没找到原因,只好重启了一次UE4Editor,莫名其妙就好了。

  • 优化过后在Tick中清理并创建后, 无法渲染出平面了。

一番折腾后发现,在CreateSection / UpdateSection 调用后,必须再次SetMaterial,即使材质从未改变。。。

有点心累, 先记到这里。