| 11 |
// |
// |
| 12 |
//------------------------------------------------------------------ |
//------------------------------------------------------------------ |
| 13 |
|
|
| 14 |
|
//------------------------------------------------------------------ |
| 15 |
|
// |
| 16 |
|
// using http://www.codeplex.com/3DTools |
| 17 |
|
// |
| 18 |
|
//------------------------------------------------------------------- |
| 19 |
|
|
| 20 |
|
|
| 21 |
using System; |
using System; |
| 22 |
using System.Windows; |
using System.Windows; |
| 23 |
using System.Windows.Input; |
using System.Windows.Input; |
| 25 |
using System.Windows.Media.Media3D; |
using System.Windows.Media.Media3D; |
| 26 |
using System.Windows.Controls; |
using System.Windows.Controls; |
| 27 |
using Petzold.Media3D; |
using Petzold.Media3D; |
| 28 |
|
using _3DTools; |
| 29 |
|
|
| 30 |
|
|
| 31 |
namespace Xenki.DefaultRenderer |
namespace Xenki.DefaultRenderer |
| 46 |
private PerspectiveCamera _camera; |
private PerspectiveCamera _camera; |
| 47 |
private Point3D _cameraLookPoint; |
private Point3D _cameraLookPoint; |
| 48 |
private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
| 49 |
ModelVisual3D clickedVisual; |
ModelVisual3D visul3d; |
| 50 |
|
|
| 51 |
|
//rotate and pan object clicked |
| 52 |
private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
| 53 |
|
private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
| 54 |
|
private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 55 |
|
Point3D _clickedCenterPosition; |
| 56 |
|
Transform3DGroup _clickedGroup; |
| 57 |
|
|
| 58 |
bool isTracking = false; |
bool isTracking = false; |
| 59 |
|
|
| 60 |
public CameraControl(PerspectiveCamera camera) |
public CameraControl(PerspectiveCamera camera) |
| 61 |
{ |
{ |
| 62 |
_camera = camera; |
_camera = camera; |
| 63 |
|
|
| 64 |
_rotationTransform.Rotation = _rotation; |
_rotationTransform.Rotation = _rotation; |
|
_transform = new Transform3DGroup(); |
|
| 65 |
|
|
| 66 |
|
_transform = new Transform3DGroup(); |
| 67 |
//_transform.Children.Add(_translate); |
//_transform.Children.Add(_translate); |
| 68 |
_transform.Children.Add(_rotationTransform); |
_transform.Children.Add(_rotationTransform); |
| 69 |
// _transform.Children.Add(_scale); |
|
| 70 |
|
_cameraLookDirection = _camera.LookDirection; |
| 71 |
|
|
| 72 |
} |
} |
| 73 |
|
|
| 80 |
get { return _transform; } |
get { return _transform; } |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
|
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
| 84 |
|
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] |
| 85 |
|
public static extern bool SetCursorPos(int X, int Y); |
| 86 |
|
|
| 87 |
|
|
| 88 |
#region Event Handling |
#region Event Handling |
| 89 |
|
|
| 90 |
/// <summary> |
/// <summary> |
| 102 |
_eventSource.MouseUp -= this.OnMouseUp; |
_eventSource.MouseUp -= this.OnMouseUp; |
| 103 |
_eventSource.MouseMove -= this.OnMouseMove; |
_eventSource.MouseMove -= this.OnMouseMove; |
| 104 |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
| 105 |
|
_eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
| 106 |
} |
} |
| 107 |
_eventSource = value; |
_eventSource = value; |
| 108 |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
| 109 |
_eventSource.MouseLeftButtonDown += new MouseButtonEventHandler(_eventSource_MouseLeftButtonDown); |
_eventSource.MouseLeftButtonDown += _eventSource_MouseLeftButtonDown; |
| 110 |
|
_eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; |
| 111 |
_eventSource.MouseUp += this.OnMouseUp; |
_eventSource.MouseUp += this.OnMouseUp; |
| 112 |
_eventSource.MouseMove += this.OnMouseMove; |
_eventSource.MouseMove += this.OnMouseMove; |
|
|
|
| 113 |
} |
} |
| 114 |
} |
} |
| 115 |
|
|
| 116 |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
|
| 117 |
|
void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 118 |
{ |
{ |
| 119 |
double scale = e.Delta / 1200.00; |
Mouse.Capture(_eventSource, CaptureMode.Element); |
| 120 |
double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; |
|
| 121 |
|
Point pclick = e.GetPosition(_eventSource); |
| 122 |
|
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 123 |
|
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
| 124 |
|
if (result3d == null) |
| 125 |
|
return; |
| 126 |
|
|
| 127 |
if (distance > 1024 || distance <= 0.5) |
visul3d = result3d.VisualHit as ModelVisual3D; |
| 128 |
|
if (visul3d == null) |
| 129 |
|
return; |
| 130 |
|
|
| 131 |
|
Transform3DGroup group = visul3d.Transform as Transform3DGroup; |
| 132 |
|
if (group != null) |
| 133 |
|
{ |
| 134 |
|
foreach (Transform3D tran in group.Children) |
| 135 |
|
{ |
| 136 |
|
if (tran is RotateTransform3D) |
| 137 |
{ |
{ |
| 138 |
if (touch == false) |
|
| 139 |
|
_clickedRotate = tran as RotateTransform3D; |
| 140 |
|
if (_clickedRotate != null) |
| 141 |
{ |
{ |
| 142 |
touch = true; |
_clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 143 |
|
_clickedRotate.Rotation = _clickedAxisAngleRotate; |
| 144 |
} |
} |
| 145 |
} |
} |
| 146 |
else |
else if (tran is TranslateTransform3D) |
| 147 |
{ |
_clickedTranslate = tran as TranslateTransform3D; |
|
touch = false; |
|
| 148 |
} |
} |
| 149 |
|
} |
| 150 |
|
else |
| 151 |
|
return; |
| 152 |
|
|
| 153 |
if (touch == true) |
//for ALT + MOUSE + ROTATE |
| 154 |
scale = -scale; |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
| 155 |
_camera.Position = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, _camera.Position.Y - scale * _cameraLookDirection.Y, _camera.Position.Z - scale * _cameraLookDirection.Z); |
|
| 156 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_rotationTransform.CenterX = _cameraLookPoint.X; |
| 157 |
|
_rotationTransform.CenterY = _cameraLookPoint.Y; |
| 158 |
|
_rotationTransform.CenterZ = _cameraLookPoint.Z; |
| 159 |
|
|
| 160 |
|
_cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 161 |
|
_camera.LookDirection = _cameraLookDirection; |
| 162 |
|
|
| 163 |
|
isTracking = true; |
| 164 |
|
_eventSource.CaptureMouse(); |
| 165 |
|
_eventSource.Focus(); |
| 166 |
|
e.Handled = true; |
| 167 |
} |
} |
| 168 |
|
|
| 169 |
|
Point originalMouseClickPos = new Point(); |
| 170 |
void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 171 |
{ |
{ |
| 172 |
Mouse.Capture(EventSource, CaptureMode.Element); |
Mouse.Capture(EventSource, CaptureMode.Element); |
| 173 |
|
|
| 174 |
Point pclick = e.GetPosition(EventSource); |
Point pclick = e.GetPosition(EventSource); |
| 175 |
|
originalMouseClickPos = _eventSource.PointToScreen(pclick); |
| 176 |
|
|
| 177 |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 178 |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
| 179 |
if (result3d == null) |
if (result3d == null) |
| 180 |
return; |
return; |
| 181 |
|
|
| 182 |
ModelVisual3D visul3d = result3d.VisualHit as ModelVisual3D; |
visul3d = result3d.VisualHit as ModelVisual3D; |
| 183 |
if (visul3d == null) |
if (visul3d == null) |
| 184 |
return; |
return; |
| 185 |
|
|
| 186 |
_clickedTranslate = visul3d.Transform as TranslateTransform3D; |
_clickedGroup = visul3d.Transform as Transform3DGroup; |
| 187 |
if (_clickedTranslate == null) |
if (_clickedGroup != null) |
| 188 |
return; |
{ |
| 189 |
|
|
| 190 |
|
foreach (Transform3D tran in _clickedGroup.Children) |
| 191 |
|
{ |
| 192 |
|
if (tran is RotateTransform3D) |
| 193 |
|
{ |
| 194 |
|
_clickedRotate = tran as RotateTransform3D; |
| 195 |
|
if (_clickedRotate != null) |
| 196 |
|
{ |
| 197 |
|
_clickedAxisAngleRotate = _clickedRotate.Rotation as AxisAngleRotation3D; |
| 198 |
|
if (_clickedAxisAngleRotate == null) |
| 199 |
|
_clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 200 |
|
|
| 201 |
clickedVisual = visul3d; |
_clickedRotate.Rotation = _clickedAxisAngleRotate; |
| 202 |
clickedVisual.Transform = _clickedTranslate; |
} |
| 203 |
|
} |
| 204 |
|
else if (tran is TranslateTransform3D) |
| 205 |
|
{ |
| 206 |
|
_clickedTranslate = tran as TranslateTransform3D; |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
|
else |
| 211 |
|
return; |
| 212 |
|
|
| 213 |
Petzold.Media3D.LineRange line; |
//for ALT + MOUSE + ROTATE |
| 214 |
Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
|
_cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); |
|
| 215 |
|
|
| 216 |
_rotationTransform.CenterX = _cameraLookPoint.X; |
_rotationTransform.CenterX = _cameraLookPoint.X; |
| 217 |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
| 218 |
_rotationTransform.CenterZ = _cameraLookPoint.Z; |
_rotationTransform.CenterZ = _cameraLookPoint.Z; |
| 219 |
|
|
| 220 |
|
//for rotate the object mouse clicked |
| 221 |
|
_clickedRotate.CenterX = _clickedCenterPosition.X = visul3d.Content.Bounds.Location.X + visul3d.Content.Bounds.SizeX / 2; |
| 222 |
|
_clickedRotate.CenterY = _clickedCenterPosition.Y = visul3d.Content.Bounds.Location.Y + visul3d.Content.Bounds.SizeY / 2; |
| 223 |
|
_clickedRotate.CenterZ = _clickedCenterPosition.Z = visul3d.Content.Bounds.Location.Z + visul3d.Content.Bounds.SizeZ / 2; |
| 224 |
|
|
| 225 |
_cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_cameraLookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 226 |
_camera.LookDirection = _cameraLookDirection; |
_camera.LookDirection = _cameraLookDirection; |
| 227 |
|
|
| 228 |
|
//TRACK BALL |
| 229 |
|
_previousPosition3D = ProjectToTrackball( |
| 230 |
|
EventSource.ActualWidth, |
| 231 |
|
EventSource.ActualHeight, |
| 232 |
|
_previousPosition2D); |
| 233 |
|
|
| 234 |
isTracking = true; |
isTracking = true; |
| 235 |
_eventSource.CaptureMouse(); |
_eventSource.CaptureMouse(); |
| 236 |
_eventSource.Focus(); |
_eventSource.Focus(); |
| 241 |
{ |
{ |
| 242 |
Mouse.Capture(EventSource, CaptureMode.None); |
Mouse.Capture(EventSource, CaptureMode.None); |
| 243 |
isTracking = false; |
isTracking = false; |
| 244 |
|
//set the mouse to the original position when clicked the object. |
| 245 |
|
SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); |
| 246 |
|
|
| 247 |
_eventSource.ReleaseMouseCapture(); |
_eventSource.ReleaseMouseCapture(); |
| 248 |
_eventSource.Cursor = Cursors.Arrow; |
_eventSource.Cursor = Cursors.Arrow; |
|
string mmm = mm; |
|
| 249 |
} |
} |
| 250 |
|
|
| 251 |
private void OnMouseMove(object sender, MouseEventArgs e) |
private void OnMouseMove(object sender, MouseEventArgs e) |
| 266 |
} |
} |
| 267 |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.Q)) |
| 268 |
{ |
{ |
| 269 |
|
if(isTracking) |
| 270 |
CameraPanUP(currentPosition); |
CameraPanUP(currentPosition); |
| 271 |
} |
} |
| 272 |
|
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 273 |
|
{ |
| 274 |
|
if (isTracking) |
| 275 |
|
{ |
| 276 |
|
_eventSource.Cursor = Cursors.None; |
| 277 |
|
CameraPanLeftRight(currentPosition); |
| 278 |
|
} |
| 279 |
|
} |
| 280 |
|
else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) |
| 281 |
|
&& (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) |
| 282 |
|
{ |
| 283 |
|
if (isTracking) |
| 284 |
|
{ |
| 285 |
|
_eventSource.Cursor = Cursors.None; |
| 286 |
|
|
| 287 |
|
RotateObject(currentPosition); |
| 288 |
|
} |
| 289 |
|
} |
| 290 |
else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) |
else if (e.LeftButton == MouseButtonState.Pressed && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) |
| 291 |
{ |
{ |
| 292 |
|
if (isTracking) |
| 293 |
|
{ |
| 294 |
|
_eventSource.Cursor = Cursors.None; |
| 295 |
PanObject(currentPosition); |
PanObject(currentPosition); |
| 296 |
|
|
| 297 |
|
} |
| 298 |
} |
} |
| 299 |
|
else if (e.LeftButton == MouseButtonState.Pressed) |
| 300 |
|
{ |
| 301 |
|
LookAround(currentPosition); |
| 302 |
|
} |
| 303 |
|
|
| 304 |
|
|
| 305 |
_previousPosition2D = currentPosition; |
_previousPosition2D = currentPosition; |
| 306 |
|
|
| 307 |
} |
} |
| 308 |
|
|
| 309 |
private void PanObject(Point currentPosition) |
private void LookAround(Point currentPosition) |
| 310 |
{ |
{ |
| 311 |
double scale = currentPosition.Y - _previousPosition2D.Y; |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
|
scale = scale / 5; |
|
| 312 |
|
|
| 313 |
|
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 314 |
|
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
| 315 |
|
|
| 316 |
LineRange range; |
// We negate the angle because we are rotating the camera. |
| 317 |
ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, currentPosition, out range); |
// Do not do this if you are rotating the scene instead. |
| 318 |
Point3D pointNew = range.PointFromZ(clickedVisual.Content.Bounds.X); |
Quaternion delta = new Quaternion(axis, -angle); |
| 319 |
//Vector3D v = pointNew- new Point3D(clickedVisual.Content.Bounds.X,clickedVisual.Content.Bounds.Y,clickedVisual.Content.Bounds.Z); |
|
| 320 |
|
// Get the current orientantion from the RotateTransform3D |
| 321 |
|
Transform3DGroup group = _camera.Transform as Transform3DGroup; |
| 322 |
|
if (group == null) |
| 323 |
|
return; |
| 324 |
|
|
| 325 |
Vector3D vectMouse = pointNew - _cameraLookPoint; |
foreach (Transform3D tran in group.Children) |
| 326 |
_clickedTranslate.OffsetX = _clickedTranslate.OffsetX + vectMouse.X; |
{ |
| 327 |
_clickedTranslate.OffsetY = _clickedTranslate.OffsetY + vectMouse.Y; |
RotateTransform3D rotate = tran as RotateTransform3D; |
| 328 |
_clickedTranslate.OffsetZ = _clickedTranslate.OffsetZ + vectMouse.Z; |
if (rotate == null) |
| 329 |
|
continue; |
| 330 |
|
RotateTransform3D rt = (RotateTransform3D)rotate; |
| 331 |
|
AxisAngleRotation3D r = (AxisAngleRotation3D)rt.Rotation; |
| 332 |
|
Quaternion q = new Quaternion(r.Axis, r.Angle); |
| 333 |
|
|
| 334 |
//_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
// Compose the delta with the previous orientation |
| 335 |
|
q *= delta; |
| 336 |
|
|
| 337 |
|
// Write the new orientation back to the Rotation3D |
| 338 |
|
r.Axis = q.Axis; |
| 339 |
|
r.Angle = q.Angle; |
| 340 |
} |
} |
| 341 |
|
|
| 342 |
#endregion Event Handling |
_previousPosition3D = currentPosition3D; |
| 343 |
|
} |
| 344 |
|
|
| 345 |
private void LookUpDown(Point currentPosition) |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 346 |
{ |
{ |
| 347 |
double scale = currentPosition.Y- _previousPosition2D.Y; |
double scale = -e.Delta / 1200.000; |
| 348 |
scale = scale/5; |
|
| 349 |
_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
AdjustCameraPosition(_camera, _cameraLookDirection, scale); |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
string mm = ""; |
private void RotateObject(Point currentPosition) |
|
bool touch = false; |
|
|
private void Focus(Point currentPosition) |
|
| 353 |
{ |
{ |
| 354 |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition); |
| 355 |
double scale = yDelta / 100; |
|
| 356 |
//Maybe this part need replaced by collision detection method |
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D); |
| 357 |
double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; |
double angle = Vector3D.AngleBetween(_previousPosition3D, currentPosition3D); |
| 358 |
if (distance > 1024 || distance <= 0.5) |
|
| 359 |
|
if (_clickedAxisAngleRotate.Angle >= 360 || _clickedAxisAngleRotate.Angle <= -360) |
| 360 |
|
_clickedAxisAngleRotate.Angle = 0; |
| 361 |
|
|
| 362 |
|
_clickedAxisAngleRotate.Axis = axis; |
| 363 |
|
_clickedAxisAngleRotate.Angle -= angle; |
| 364 |
|
|
| 365 |
|
_previousPosition3D = currentPosition3D; |
| 366 |
|
|
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
private Vector3D ProjectToTrackball(double width, double height, Point point) |
| 370 |
{ |
{ |
| 371 |
if (touch == false) |
double x = point.X / (width / 2); // Scale so bounds map to [0,0] - [2,2] |
| 372 |
touch = true; |
double y = point.Y / (height / 2); |
| 373 |
|
|
| 374 |
|
x = x - 1; // Translate 0,0 to the center |
| 375 |
|
y = 1 - y; // Flip so +Y is up instead of down |
| 376 |
|
|
| 377 |
|
double z2 = 1 - x * x - y * y; // z^2 = 1 - x^2 - y^2 |
| 378 |
|
double z = z2 > 0 ? Math.Sqrt(z2) : 0; |
| 379 |
|
|
| 380 |
|
Vector3D v = new Vector3D(x, z, y); |
| 381 |
|
v.Normalize(); |
| 382 |
|
return v; |
| 383 |
} |
} |
| 384 |
else |
|
| 385 |
|
#endregion Event Handling |
| 386 |
|
|
| 387 |
|
|
| 388 |
|
private void CameraPanLeftRight(Point currentPosition) |
| 389 |
{ |
{ |
| 390 |
touch = false; |
double scalex = currentPosition.X - _previousPosition2D.X; |
| 391 |
|
|
| 392 |
|
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 393 |
|
|
| 394 |
|
_camera.Position += Math.Abs(scalex) * vector3D; |
| 395 |
} |
} |
|
if (touch == true) |
|
|
scale = -scale; |
|
| 396 |
|
|
| 397 |
_camera.Position = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, _camera.Position.Y - scale * _cameraLookDirection.Y, _camera.Position.Z - scale * _cameraLookDirection.Z); |
private void PanObject(Point currentPosition) |
| 398 |
|
{ |
| 399 |
|
double scaley = currentPosition.Y - _previousPosition2D.Y; |
| 400 |
|
scaley = Math.Abs( scaley ); |
| 401 |
|
|
| 402 |
//_translate.OffsetX -= scale * _cameraLookDirection.X;// _camera.LookDirection.X; |
double scalex = currentPosition.X - _previousPosition2D.X; |
| 403 |
//_translate.OffsetY -= scale * _cameraLookDirection.Y;//_camera.LookDirection.Y; |
scalex = Math.Abs( scalex); |
|
//_translate.OffsetZ -= scale * _cameraLookDirection.Z;//_camera.LookDirection.Z; |
|
| 404 |
|
|
|
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
|
| 405 |
|
|
| 406 |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 407 |
double angletimes = axisAngle; |
|
| 408 |
|
_clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
| 409 |
|
_clickedTranslate.OffsetY += scalex * vector3D.Y;// _clickedTranslateOriginal.OffsetY + vector3D.Y; |
| 410 |
|
_clickedTranslate.OffsetZ += scaley * vector3D.Z;// _clickedTranslateOriginal.OffsetZ + vector3D.Z;// vectMouse.Z; |
| 411 |
|
|
| 412 |
|
visul3d.Transform.Transform(new Point3D(visul3d.Content.Bounds.Location.X + scalex * vector3D.X, visul3d.Content.Bounds.Location.Y + scalex * vector3D.Y, visul3d.Content.Bounds.Location.Z + scaley * vector3D.Z)); |
| 413 |
|
|
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
private Vector3D GetTranslationVector3D(DependencyObject modelHit, Point startPosition, Point endPosition) |
| 417 |
|
{ |
| 418 |
|
Vector3D translationVector3D = new Vector3D(); |
| 419 |
|
|
| 420 |
|
Viewport3DVisual viewport = null; |
| 421 |
|
bool success = false; |
| 422 |
|
|
| 423 |
|
Matrix3D matrix3D = MathUtils.TryTransformTo2DAncestor(modelHit, out viewport, out success); |
| 424 |
|
if (success && matrix3D.HasInverse) |
| 425 |
|
{ |
| 426 |
|
matrix3D.Invert(); |
| 427 |
|
Point3D startPoint3D = new Point3D(startPosition.X, startPosition.Y, 0); |
| 428 |
|
Point3D endPoint3D = new Point3D(endPosition.X, endPosition.Y, 0); |
| 429 |
|
Vector3D vector3D = endPoint3D - startPoint3D; |
| 430 |
|
translationVector3D = matrix3D.Transform(vector3D); |
| 431 |
|
} |
| 432 |
|
|
| 433 |
|
return translationVector3D; |
| 434 |
|
} |
| 435 |
|
|
| 436 |
|
|
| 437 |
|
|
| 438 |
|
private void LookUpDown(Point currentPosition) |
| 439 |
|
{ |
| 440 |
|
double scale = currentPosition.Y- _previousPosition2D.Y; |
| 441 |
|
scale = scale/5.000000; |
| 442 |
|
_camera.LookDirection = new Vector3D(_camera.LookDirection.X, _camera.LookDirection.Y, _camera.LookDirection.Z + scale); |
| 443 |
|
} |
| 444 |
|
|
| 445 |
if (angletimes > 360 || angletimes < -360) |
private void Focus(Point currentPosition) |
| 446 |
angletimes = 0; |
{ |
| 447 |
|
//zoom |
| 448 |
|
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 449 |
|
double scale = yDelta / 100.000000; |
| 450 |
|
AdjustCameraPosition(_camera,_cameraLookDirection,scale); |
| 451 |
|
|
| 452 |
if (angletimes + _rotation.Angle >= 360) |
//rotate the camera |
| 453 |
_rotation.Angle = 0; |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
| 454 |
_rotation.Axis = new Vector3D(0, 0, 1); |
_rotation.Axis = new Vector3D(0, 0, 1); |
| 455 |
_rotation.Angle += axisAngle; |
_rotation.Angle -= axisAngle; |
| 456 |
|
|
| 457 |
|
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 458 |
} |
} |
| 459 |
|
|
| 460 |
|
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double scale ) |
| 461 |
|
{ |
| 462 |
|
Point3D cameraPos = new Point3D(camera.Position.X - scale * lookDirection.X, |
| 463 |
|
camera.Position.Y - scale * lookDirection.Y, |
| 464 |
|
camera.Position.Z - scale * lookDirection.Z); |
| 465 |
|
|
| 466 |
|
//too fast |
| 467 |
|
if (Point3D.Subtract(cameraPos, camera.Position).Length > 1.000000000) |
| 468 |
|
cameraPos = new Point3D(camera.Position.X - scale / 10 * lookDirection.X, |
| 469 |
|
camera.Position.Y - scale / 10 * lookDirection.Y, |
| 470 |
|
camera.Position.Z - scale / 10 * lookDirection.Z); |
| 471 |
|
|
| 472 |
|
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
| 473 |
|
|
| 474 |
|
if (distance > 524 || distance <= 1.000000) |
| 475 |
|
{ |
| 476 |
|
} |
| 477 |
|
else |
| 478 |
|
camera.Position = cameraPos; |
| 479 |
|
} |
| 480 |
|
//get the clicked 3d position |
| 481 |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 482 |
{ |
{ |
| 483 |
if (result.VisualHit != null) |
if (result.VisualHit != null) |
| 484 |
{ |
{ |
| 485 |
|
RayHitTestResult r = result as RayHitTestResult; |
| 486 |
|
if (r != null) |
| 487 |
|
{ |
| 488 |
|
RayMeshGeometry3DHitTestResult t = r as RayMeshGeometry3DHitTestResult; |
| 489 |
|
_cameraLookPoint = t.PointHit; |
| 490 |
return HitTestResultBehavior.Stop; |
return HitTestResultBehavior.Stop; |
| 491 |
} |
} |
| 492 |
|
} |
| 493 |
return HitTestResultBehavior.Continue; |
return HitTestResultBehavior.Continue; |
| 494 |
} |
} |
| 495 |
|
|
| 497 |
{ |
{ |
| 498 |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 499 |
double xDelta = currentPosition.X - _previousPosition2D.X; |
double xDelta = currentPosition.X - _previousPosition2D.X; |
| 500 |
double scale = yDelta / 10; |
double scale = yDelta / 5.00; |
| 501 |
|
|
| 502 |
_camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); |
_camera.Position = new Point3D(_camera.Position.X, _camera.Position.Y, _camera.Position.Z + scale); |
| 503 |
} |
} |