|
|
|
@ -202,67 +202,56 @@ public partial class Viewport3D |
|
|
|
|
private void BtnDirection_OnClick(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
var directionName = ((Button)sender).Name.ToString(); |
|
|
|
|
var directionValue = (int)TbCustomizeRevolve.Value; |
|
|
|
|
var directionValue = (int)TbCustomizeRevolve.Value; // 旋转角度(单位:度) |
|
|
|
|
|
|
|
|
|
if (directionValue == 0) return; |
|
|
|
|
|
|
|
|
|
// 获取当前相机和模型中心 |
|
|
|
|
var camera = Viewport3Dx.Camera as HelixToolkit.Wpf.SharpDX.PerspectiveCamera; |
|
|
|
|
if (camera == null) return; |
|
|
|
|
|
|
|
|
|
var modelCenter = ViewportManager.CenterVector; |
|
|
|
|
var modelCenter = ViewportManager.CenterVector; // 模型中心 |
|
|
|
|
var currentPosition = camera.Position; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将相机位置转换为极坐标 |
|
|
|
|
var dx = currentPosition.X - modelCenter.X; |
|
|
|
|
var dy = currentPosition.Y - modelCenter.Y; |
|
|
|
|
var dz = currentPosition.Z - modelCenter.Z; |
|
|
|
|
|
|
|
|
|
var radius = Math.Sqrt(dx * dx + dy * dy + dz * dz); |
|
|
|
|
var azimuth = Math.Atan2(dz, dx) * 180.0 / Math.PI; |
|
|
|
|
var elevation = Math.Atan2(dy, Math.Sqrt(dx * dx + dz * dz)) * 180.0 / Math.PI; |
|
|
|
|
|
|
|
|
|
var radius = Math.Sqrt(dx * dx + dy * dy + dz * dz); // 相机与模型中心的距离 |
|
|
|
|
var azimuth = Math.Atan2(dz, dx) * 180.0 / Math.PI; // 方位角(水平平面上的角度) |
|
|
|
|
var elevation = Math.Atan2(dy, Math.Sqrt(dx * dx + dz * dz)) * 180.0 / Math.PI; // 仰角(垂直方向的角度) |
|
|
|
|
|
|
|
|
|
switch (directionName) |
|
|
|
|
{ |
|
|
|
|
case "BtnTop": |
|
|
|
|
//上 |
|
|
|
|
elevation += directionValue; |
|
|
|
|
break; |
|
|
|
|
case "BtnBottom": |
|
|
|
|
//下 |
|
|
|
|
elevation -= directionValue; |
|
|
|
|
break; |
|
|
|
|
case "BtnLeft": |
|
|
|
|
//左 |
|
|
|
|
azimuth -= directionValue; |
|
|
|
|
break; |
|
|
|
|
case "BtnRight": |
|
|
|
|
//右 |
|
|
|
|
azimuth += directionValue; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (elevation >= 90) |
|
|
|
|
{ |
|
|
|
|
elevation = 89.9f; |
|
|
|
|
azimuth += 180.0f; |
|
|
|
|
} |
|
|
|
|
else if (elevation <= -90) |
|
|
|
|
{ |
|
|
|
|
elevation = -89.9f; |
|
|
|
|
azimuth += 180.0f; |
|
|
|
|
} |
|
|
|
|
azimuth = (azimuth + 360) % 360; |
|
|
|
|
|
|
|
|
|
// 限制仰角范围在 -89.9 到 89.9 度,避免超出范围导致位置错误 |
|
|
|
|
elevation = Math.Clamp(elevation, -89.9, 89.9); |
|
|
|
|
azimuth = (azimuth + 360) % 360; // 方位角在 0 到 360 范围内循环 |
|
|
|
|
|
|
|
|
|
// 将极坐标转换回直角坐标 |
|
|
|
|
var newX = modelCenter.X + radius * Math.Cos(elevation * Math.PI / 180.0) * Math.Cos(azimuth * Math.PI / 180.0); |
|
|
|
|
var newY = modelCenter.Y + radius * Math.Sin(elevation * Math.PI / 180.0); |
|
|
|
|
var newZ = modelCenter.Z + radius * Math.Cos(elevation * Math.PI / 180.0) * Math.Sin(azimuth * Math.PI / 180.0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新相机位置和视角 |
|
|
|
|
camera.Position = new Point3D(newX, newY, newZ); |
|
|
|
|
|
|
|
|
|
camera.LookDirection = new Vector3D(modelCenter.X - newX, modelCenter.Y - newY, modelCenter.Z - newZ); |
|
|
|
|
|
|
|
|
|
var rightDirection = Vector3D.CrossProduct(camera.LookDirection, new Vector3D(0, 1, 0)); // 计算右方向 |
|
|
|
|
|
|
|
|
|
if (rightDirection.Length < 0.027) |
|
|
|
|
{ |
|
|
|
|
rightDirection = Vector3D.CrossProduct(camera.LookDirection, new Vector3D(-1, 0, 0)); // 计算右方向 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
camera.UpDirection = Vector3D.CrossProduct(camera.LookDirection, rightDirection); |
|
|
|
|
// camera.UpDirection = new Vector3D(0, 1, 0); // 保持 Y 轴为上方向 |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|