从UE5的坐标转换函数出发,手把手带你复现一个简易的3D拾取Demo(C++/蓝图)
从UE5坐标转换到3D拾取实战开发全流程解析在虚幻引擎5的交互式应用开发中3D拾取功能是最基础也最核心的交互手段之一。无论是点击放置物体、角色选择还是UI交互都离不开屏幕坐标到世界坐标的转换。本文将以一个完整的点击生成物体Demo为例系统讲解从理论到实践的完整实现路径。1. 环境准备与项目设置首先创建一个全新的UE5 C项目选择Blank模板确保已安装最新版本的Visual Studio和对应的虚幻引擎版本。在项目设置中启用以下关键模块// YourProjectName.Build.cs PublicDependencyModuleNames.AddRange(new string[] { Core, CoreUObject, Engine, InputCore, HeadMountedDisplay, NavigationSystem, AIModule });创建两个主要蓝图类BP_InteractivePlayerController继承自PlayerControllerBP_SpawnableActor继承自Actor带静态网格组件在项目设置中配置默认Player Controller为我们的BP_InteractivePlayerController。2. 核心坐标转换原理剖析UE5中的坐标转换链条可以表示为屏幕坐标 → NDC坐标 → 裁剪空间坐标 → 世界坐标关键函数DeprojectScreenPositionToWorld的内部工作流程屏幕坐标归一化const float NormalizedX (PixelX - ViewRect.Min.X) / ViewRect.Width(); const float NormalizedY (PixelY - ViewRect.Min.Y) / ViewRect.Height();NDC空间转换const float ScreenSpaceX (NormalizedX - 0.5f) * 2.0f; const float ScreenSpaceY ((1.0f - NormalizedY) - 0.5f) * 2.0f;逆矩阵变换FMatrix const InvViewProjMatrix ProjectionData.ComputeViewProjectionMatrix().InverseFast(); FVector4 WorldSpacePos InvViewProjMatrix.TransformFVector4(ClipSpacePos);重要参数对比参数类型取值范围转换目的屏幕坐标(0,0)到(分辨率宽,高)原始输入数据NDC坐标[-1,1]范围标准化设备坐标系裁剪空间坐标包含深度值(Z)投影前统一空间3. 完整拾取系统实现3.1 鼠标点击事件处理在PlayerController中设置输入绑定// 在SetupInputComponent中 InputComponent-BindAction(LeftMouseClick, IE_Pressed, this, ABP_InteractivePlayerController::HandleMouseClick);点击事件处理函数void ABP_InteractivePlayerController::HandleMouseClick() { float MouseX, MouseY; if (GetMousePosition(MouseX, MouseY)) { FVector WorldLocation, WorldDirection; if (DeprojectScreenPositionToWorld( MouseX, MouseY, WorldLocation, WorldDirection)) { // 执行射线检测 PerformLineTrace(WorldLocation, WorldDirection); } } }3.2 射线检测与物体生成实现精确的射线检测void ABP_InteractivePlayerController::PerformLineTrace(FVector Start, FVector Direction) { FHitResult HitResult; FCollisionQueryParams TraceParams(FName(TEXT(InteractTrace)), true); if (GetWorld()-LineTraceSingleByChannel( HitResult, Start, Start (Direction * 10000), // 10米检测距离 ECC_Visibility, TraceParams)) { SpawnActorAtHitLocation(HitResult); } else { // 未命中时在地面生成 FVector GroundPos CalculateGroundPosition(Start, Direction); SpawnActorAtLocation(GroundPos); } }物体生成逻辑void ABP_InteractivePlayerController::SpawnActorAtLocation(FVector Location) { FActorSpawnParameters SpawnParams; SpawnParams.SpawnCollisionHandlingOverride ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn; GetWorld()-SpawnActorABP_SpawnableActor( SpawnableClass, Location FVector(0,0,50), // 略微抬高 FRotator::ZeroRotator, SpawnParams); }4. 高级功能扩展4.1 拖拽交互实现在PlayerController中添加拖拽状态跟踪// 成员变量 bool bIsDragging false; AActor* DraggedActor nullptr; FVector DragOffset; // 输入绑定扩展 InputComponent-BindAction(LeftMouseClick, IE_Released, this, ABP_InteractivePlayerController::HandleMouseRelease);拖拽更新逻辑void ABP_InteractivePlayerController::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (bIsDragging DraggedActor) { float MouseX, MouseY; GetMousePosition(MouseX, MouseY); FVector WorldLocation, WorldDirection; DeprojectScreenPositionToWorld(MouseX, MouseY, WorldLocation, WorldDirection); // 计算新的位置保持原始高度 FVector NewPos CalculateDragPosition(WorldLocation, WorldDirection); DraggedActor-SetActorLocation(NewPos DragOffset); } }4.2 多物体选择与管理实现选择高亮效果// 在选择时调用 void HighlightActor(AActor* Actor) { if (CurrentSelected) { CurrentSelected-GetMesh()-SetRenderCustomDepth(false); } Actor-GetMesh()-SetRenderCustomDepth(true); Actor-GetMesh()-SetCustomDepthStencilValue(252); CurrentSelected Actor; }物体管理数据结构TArrayABP_SpawnableActor* SpawnedActors; // 生成时记录 SpawnedActors.Add(NewActor); // 清除所有物体 for (auto Actor : SpawnedActors) { Actor-Destroy(); } SpawnedActors.Empty();5. 性能优化与调试技巧5.1 射线检测优化策略使用对象查询过滤器FCollisionObjectQueryParams ObjectParams; ObjectParams.AddObjectTypesToQuery(ECC_WorldStatic); ObjectParams.AddObjectTypesToQuery(ECC_PhysicsBody); GetWorld()-LineTraceSingleByObjectType( HitResult, Start, End, ObjectParams, TraceParams);异步射线检测示例// 在.h文件中 FTraceHandle TraceHandle; FCollisionQueryParams AsyncTraceParams; // 在.cpp中 TraceHandle GetWorld()-AsyncLineTraceByChannel( Start, End, ECC_Visibility, AsyncTraceParams, FCollisionResponseParams::DefaultResponseParam, TraceDelegate);5.2 调试可视化工具绘制调试射线DrawDebugLine( GetWorld(), Start, End, FColor::Green, false, // 不持久化 2.0f, // 持续时间 0, // 深度优先级 2.0f); // 线宽显示坐标信息GEngine-AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(World Pos: X%.2f, Y%.2f, Z%.2f), HitResult.Location.X, HitResult.Location.Y, HitResult.Location.Z));在开发这类交互系统时最常遇到的坑是忽略坐标系的转换顺序。记得在一次项目中我花了整整一天时间调试一个拾取偏移问题最后发现是在NDC坐标转换时漏掉了Y轴的反转步骤。这种基础但关键的技术点往往需要亲手实现几次才能真正掌握。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2583006.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!