FindCollisionUV可以做碰撞或击中某物件上碰撞点对应的UV坐标,我们可以基于该UV坐标作为材质参数传入到该物件材质中做一些碰撞或击中材质效果。注意:碰撞或射线检测,必须开启复杂检测,该物件设置开启物理。
/**
* Try and find the UV for a collision impact. Note this ONLY works if 'Support UV From Hit Results' is enabled in Physics Settings.
*/
UFUNCTION(BlueprintPure, Category = "Collision")
static bool FindCollisionUV(const struct FHitResult& Hit, int32 UVChannel, FVector2D& UV);
bool UGameplayStatics::FindCollisionUV(const struct FHitResult& Hit, int32 UVChannel, FVector2D& UV)
{
bool bSuccess = false;
if (!UPhysicsSettings::Get()->bSupportUVFromHitResults)
{
FMessageLog("PIE").Warning(LOCTEXT("CollisionUVNoSupport", "Calling FindCollisionUV but 'Support UV From Hit Results' is not enabled in project settings. This is required for finding UV for collision results."));
}
else
{
UPrimitiveComponent* HitPrimComp = Hit.Component.Get();
if (HitPrimComp)
{
UBodySetup* BodySetup = HitPrimComp->GetBodySetup();
if (BodySetup)
{
const FVector LocalHitPos = HitPrimComp->GetComponentToWorld().InverseTransformPosition(Hit.Location);
bSuccess = BodySetup->CalcUVAtLocation(LocalHitPos, Hit.FaceIndex, UVChannel, UV);
}
}
}
return bSuccess;
}
基于官方第一人称例子工程,使用如下图: