一、应用需求
在数字孪生应用桌面中要实现Mesh的中和自由编辑,需要通过LineTraceSingleByObjectType随时选中场景中的任意动态Mesh.
二、问题
在PIE模式下通过可以通过LineTraceSingleByObjectType,鼠标可以拾取场景中任意绘制的Mesh,然而在独立程序中所有新绘制的对象都无法拾取,LineTraceSingleByObjectType返回空
三、分析
1、检查Mesh的碰撞设置(CollisionChannel),如下:
DynamicMeshComponent = CreateDefaultSubobject(TEXT("DynamicMeshComponent"));
if (DynamicMeshComponent)
{
DynamicMeshComponent->SetupAttachment(RootComponent);
//避免产生阴影
DynamicMeshComponent->SetCastShadow(false);
//避免在绘制时WorldRay投射到Mesh自己身上
DynamicMeshComponent->SetCollisionProfileName(UCollisionProfile::BlockAll_ProfileName);
DynamicMeshComponent->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
DynamicMeshComponent->SetMobility(EComponentMobility::Type::Movable);
}
2、检查LineTraceSingleByObjectType调用参数
FCollisionObjectQueryParams QueryParams(FCollisionObjectQueryParams::AllDynamicObjects);
FHitResult Result;
bool bHitWorld = GWorld->LineTraceSingleByObjectType(Result, RayStart, RayEnd, QueryParams);
if (bHitWorld)
{
WorldHitPoint = Result.ImpactPoint;
HitDistance = Result.Distance;
3、查看运行日志,发现问题,日志如下:
LogPhysics: Error: Attempting to build physics data for /Game/Default/DefaultMap.DefaultMap:PersistentLevel.Polygon_2147482358.DynamicMeshComponent.BodySetup_2147482357 at runtime, but runtime cooking is disabled (see the RuntimePhysXCooking plugin).
4、由此判断是因为程序插件配置问题,最终发现是RuntimePhysXCooking没有加载导致的,在运行程序的*.Build.cs中添加插件配置
PrivateDependencyModuleNames.AddRange(
new string[]
{
"Core",
"CoreUObject",
"Engine",
"InputCore",
"RuntimePhysXCooking"
});
5、重新编译,运行,一切正常!原先不能选中的Mesh就可以鼠标拾取了。