bool CalcMouseWorldPos(Point mouse, Camera camera, out Vector3 mouseWorldPos) { Ray ray = camera.Unproject(new Vector2(mouse.X, mouse.Y), Device.Viewport); float d; if (Ray.Intersects(ray, new Plane(new Vector4(0, 0, 1, 0)), out d)) { mouseWorldPos = ray.Position + ray.Direction * d; return true; } mouseWorldPos = Vector3.Zero; return false; }
bool CalcMouseWorldPosHeightmaped(Point mouse, Camera camera, out Vector3 mouseWorldPos) { mouseWorldPos = Vector3.Zero; if (mouse.X < 0 || mouse.Y < 0) return false; Ray ray = camera.Unproject(new Vector2(mouse.X, mouse.Y), Device.Viewport); float d; if (terrainGrid.Intersects(ray, out d)) { mouseWorldPos = ray.Position + ray.Direction * d; return true; } return false; /*DataRectangle r = depthData.LockRectangle(new Rectangle(mouse, new Size(1, 1)), LockFlags.None); float depth = r.Data.Read<float>(); depthData.UnlockRectangle(); if (depth == 0) return CalcMouseWorldPos(mouse, camera, out mouseWorldPos); mouseWorldPos = Vector3.Unproject(new Vector3(mouse.X, mouse.Y, depth), Device.Viewport.X, Device.Viewport.Y, Device.Viewport.Width, Device.Viewport.Height, Device.Viewport.MinZ, Device.Viewport.MaxZ, Scene.Camera.View * Scene.Camera.Projection); return true;*/ /*Ray ray = camera.Unproject(new Vector2(mouse.X, mouse.Y), Device.Viewport); mouseWorldPos = ray.Position + ray.Direction * depth; return true;*/ }