From 65bff60f171be20732f505a4004a2467993e95c0 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 4 Aug 2025 01:32:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E8=87=B4=E5=AF=B9=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E7=94=B1=E4=BA=8E=E5=B0=84=E7=BA=BF=E6=98=AF=E5=8D=95?= =?UTF-8?q?=E5=90=91=E7=9A=84=EF=BC=8C=E6=89=80=E4=BB=A5=E8=BF=98=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=94=AF=E6=8C=81=E5=90=91=E5=8F=8D=E6=96=B9=E5=90=91?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Gizmo/Gizmo3DMove.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/example/Gizmo/Gizmo3DMove.cpp b/example/Gizmo/Gizmo3DMove.cpp index 4c0c98b8..f7d2a109 100644 --- a/example/Gizmo/Gizmo3DMove.cpp +++ b/example/Gizmo/Gizmo3DMove.cpp @@ -63,6 +63,7 @@ namespace int PickAXIS=-1; //拾取轴 float PickDist=0; //拾取辆距离轴心的距离 + float PickCenterPPU=0; //拾取时的中心点相对屏幕空间象素的缩放比 Matrix4f PickL2W; //拾取时的变换矩阵 Vector3f PickCenter; //拾取时的中心位置 @@ -271,8 +272,13 @@ namespace PickCenter, // 线段起点 end); // 线段终点 - CurDist=glm::length(p_ls-PickCenter); //计算线段上的点与原点的距离 - CurTranslate->SetOffset(axis_vector*(CurDist-PickDist)); //设置偏移量 + const float cur_ppu=cc->GetPixelPerUnit(p_ls); //求线段上的点相对屏幕空间象素的缩放比 + + CurDist=glm::length(p_ls-PickCenter); + + const float offset=(CurDist/PickCenterPPU)-(PickDist/PickCenterPPU); //计算线段上的点与原点的距离 + + CurTranslate->SetOffset(axis_vector*offset); //设置偏移量 std::cout<<"PickDist: "<SetMouseRay(&MouseRay,mouse_coord); - Matrix4f l2w=GetLocalToWorldMatrix(); - Vector3f center=TransformPosition(l2w,Vector3f(0,0,0)); + const CameraInfo *ci=cc->GetCameraInfo(); + const Matrix4f l2w=GetLocalToWorldMatrix(); + const Vector3f center=TransformPosition(l2w,Vector3f(0,0,0)); + const float center_ppu=cc->GetPixelPerUnit(center); //求原点坐标相对屏幕空间象素的缩放比 Vector3f axis_vector; Vector3f start; Vector3f end; Vector3f p_ray,p_ls; float dist; float pixel_per_unit; - float center_ppu; MaterialInstance *mi; - center_ppu=cc->GetPixelPerUnit(center); //求原点坐标相对屏幕空间象素的缩放比 - CurAXIS=-1; for(int i=0;i<3;i++) @@ -314,7 +319,7 @@ namespace axis_vector=GetAxisVector(AXIS(i)); //取得轴向量 start =TransformPosition(l2w,axis_vector*center_ppu* GIZMO_CENTER_SPHERE_RADIUS); //将轴的起点转换到世界坐标 - end =TransformPosition(l2w,axis_vector*center_ppu*(GIZMO_CENTER_SPHERE_RADIUS+GIZMO_CONE_LENGTH+GIZMO_CYLINDER_HALF_LENGTH)); + end =TransformPosition(l2w,axis_vector*std::abs(ci->zfar-ci->znear)); //求射线与线段的最近点 MouseRay.ClosestPoint( p_ray, //射线上的点 @@ -331,7 +336,9 @@ namespace mi=pick_mi; CurAXIS=i; - CurDist=glm::length(p_ls-start); //计算线段上的点与原点的距离 + CurDist=glm::length(p_ls-center)/center_ppu; //计算线段上的点与原点的距离 + + PickCenterPPU=center_ppu; } else {