admin 发表于 2023-9-15 21:53:11

游戏中 屏幕坐标 与 世界坐标的转换






郁金香灬外挂技术
       
        https://www.yjxsoft.com/
       
        本教程视频1920*1080分辩率下观看最佳
        VS2017+win10 64位 环境
        郁金香老师 QQ -> 150330575 >>phone 139 9636 2600
        欢迎大家参加 郁金香灬技术 游戏安全与外挂的研究学习。
       
        兴趣是我们最好的老师
        成长需要过程与循序渐进        
        兴趣+坚持+时间+优秀的教程会帮助你快速成功
   


学习目标:
   
       
        下载Epic Games启动程序
        https://www.epicgames.com/site/zh-CN/home
        https://www.unrealengine.com/zh-CN/download
       
       
        可以参考UE4/UE5引擎里的 坐标转换源代码,可以通用大部分游戏
       
        Vector.h //包含了 FVector
       
        屏幕坐标转世界坐标
        FSceneView::DeprojectScreenToWorld(const FVector2D& ScreenPos, const FIntRect& ViewRect, const FMatrix& InvViewMatrix, const FMatrix& InvProjectionMatrix, FVector& out_WorldOrigin, FVector& out_WorldDirection)
       
        世界坐标转屏幕坐标
   通过 FSceneView::ProjectWorldToScreen 函数可以计算出,和上面屏幕转世界坐标一样, ViewMatrix 和ProjectionMatrix已经算出,ViewProjectionMatrix直接两者相乘即可得出。

        /** Transforms a point from world-space to the view's screen-space. */
        FVector4 FSceneView::WorldToScreen(const FVector& WorldPoint) const;

        /** Transforms a point from the view's screen-space to world-space. */
        FVector FSceneView::ScreenToWorld(const FVector4& ScreenPoint) const;
       
       
FVector4 FSceneView::WorldToScreen(const FVector& WorldPoint) const
{
        return ViewMatrices.GetViewProjectionMatrix().TransformFVector4(FVector4(WorldPoint,1));
}

FVector FSceneView::ScreenToWorld(const FVector4& ScreenPoint) const
{
        return ViewMatrices.GetInvViewProjectionMatrix().TransformFVector4(ScreenPoint);
}
       
        //世界坐标转屏幕坐标
bool FSceneView::ProjectWorldToScreen(const FVector& WorldPosition, const FIntRect& ViewRect, const FMatrix& ViewProjectionMatrix, FVector2D& out_ScreenPos)
{
        FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(WorldPosition, 1.f));
        if ( Result.W > 0.0f )
        {
                // the result of this will be x and y coords in -1..1 projection space
                const float RHW = 1.0f / Result.W;
                FPlane PosInScreenSpace = FPlane(Result.X * RHW, Result.Y * RHW, Result.Z * RHW, Result.W);

                // Move from projection space to normalized 0..1 UI space
                const float NormalizedX = ( PosInScreenSpace.X / 2.f ) + 0.5f;
                const float NormalizedY = 1.f - ( PosInScreenSpace.Y / 2.f ) - 0.5f;

                FVector2D RayStartViewRectSpace(
                        ( NormalizedX * (float)ViewRect.Width() ),
                        ( NormalizedY * (float)ViewRect.Height() )
                        );

                out_ScreenPos = RayStartViewRectSpace + FVector2D(static_cast<float>(ViewRect.Min.X), static_cast<float>(ViewRect.Min.Y));

                return true;
        }

        return false;
}


页: [1]
查看完整版本: 游戏中 屏幕坐标 与 世界坐标的转换