UE | Slice Procedural Mesh 实现动态切割模型
Procedural Mesh 程序化网格体
Create Mesh Section 生成模型网格体
SectionIndex
:创建或者替换的面的id,默认为0Vertices
: 三角形的顶点,排列顺序会影响Triangles
数组中的索引Triangles
:组成三角形的顶点索引,一般情况下为顺时针Normals
: 顶点的法线信息,与第二个参数顺序一致,数组大小一致(可不填)UV0 UV1 UV2
:顶点的UV信息,与第二个参数顺序一致,数组大小一致(可不填)VertexColors
:顶点的颜色信息(可不填)Tangents
:顶点的切线信息(可不填)bCreateCollision
:是否创建碰撞信息(注意:此处的碰撞是复杂碰撞信息)
Copy ProceduralMesh From StaticMeshComponent 复制模型数据
SliceProceduralMesh 切割程序化模型
Plane Position
:世界坐标系下该平面的中心位置Plane Normal
:世界坐标系下该平面的发现Create Other Half
: 是否将切下来的那一块模型独立出来Cap Option
: 切面状态No Cap
: 切面不缝合,模型中空Create New Section for Cap
: 切面缝合,使用自定义的材质Use Last Section for Cap
: 切面缝合,使用与模型一样的材质
Cap Material
: 如果Cap Option是Create New Section for Cap时自定义的材质
代码实现
b Allow CPU Access
开启bAllowCPUAccess
,才能动态切割模型
AActor* actor = SpawnSkirtingLine->Create();
if (actor == nullptr) {
return;
}
UStaticMeshComponent* component = Cast<UStaticMeshComponent>(actor->GetRootComponent());
if (!component->GetStaticMesh()->bAllowCPUAccess) {
PrintStr("SkirtingLine not bAllowCPUAccess");
return;
}
Procedural Mesh Component
新建程序化模型组件copyComponent
,通过Copy ProceduralMesh From StaticMeshComponent
函数复制Actor
中StaticMesh
数据到程序化模型ProceduralMesh
中
UProceduralMeshComponent* copyComponent = NewObject<UProceduralMeshComponent>(actor, *("copy" + FString::FromInt(index++)));
copyComponent->SetupAttachment(component);
copyComponent->RegisterComponent();
copyComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
UKismetProceduralMeshLibrary::CopyProceduralMeshFromStaticMeshComponent(component, 0, copyComponent, true);
copyComponent->SetWorldLocation((x + z) / 2);
copyComponent->SetWorldRotation(normal.Rotation() + FRotator(0, -90, 0));
Slice Procedural Mesh
切割模型
FVector sliceStart = y; //裁剪开始点
UProceduralMeshComponent* cutComponent = nullptr;
EProcMeshSliceCapOption capOption = EProcMeshSliceCapOption::UseLastSectionForCap; //Cap Option
FVector sliceNormal = y - z; //裁剪平面的法向量
UKismetProceduralMeshLibrary::SliceProceduralMesh(copyComponent, sliceStart, sliceNormal, false, cutComponent, capOption, nullptr);
标签:模型,Cap,Mesh,copyComponent,UE,程序化,Procedural
From: https://www.cnblogs.com/Dreammoon/p/18394289