Exemple #1
0
        private static RaySceneQueryResult.Enumerator GetRaySceneQuery(double x, double y, out Ray ray)
        {
            float width  = Engine.Renderer.Camera.Viewport.ActualWidth;
            float height = Engine.Renderer.Camera.Viewport.ActualHeight;

            Ray mouseRay = Engine.Renderer.Camera.GetCameraToViewportRay((float)((x - 1) / width), (float)((y - 1) / height));

            RaySceneQuery mRaySceneQuery = Engine.Renderer.Scene.CreateRayQuery(mouseRay);

            mRaySceneQuery.QueryTypeMask =
                SceneManager.ENTITY_TYPE_MASK |
                SceneManager.STATICGEOMETRY_TYPE_MASK |
                SceneManager.USER_TYPE_MASK_LIMIT |
                SceneManager.FRUSTUM_TYPE_MASK |
                SceneManager.FX_TYPE_MASK |
                SceneManager.LIGHT_TYPE_MASK |
                SceneManager.WORLD_GEOMETRY_TYPE_MASK;
            mRaySceneQuery.SetSortByDistance(true, 16);

            RaySceneQueryResult result = mRaySceneQuery.Execute();

            RaySceneQueryResult.Enumerator itr = (RaySceneQueryResult.Enumerator)(result.GetEnumerator());

            ray = mouseRay;

            return(itr);
        }
Exemple #2
0
        private void _panel_MouseClick(object sender, MouseEventArgs e)
        {
            Point point = new Point(e.X, e.Y);

            Engine.QueueForRenderDispatcher(() =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    Ray ray;
                    RaySceneQueryResult.Enumerator itr = GetRaySceneQuery(point.X, point.Y, out ray);

                    if (itr != null)
                    {
                        while (itr.MoveNext())
                        {
                            RaySceneQueryResultEntry entry = itr.Current;
                            SceneNode parentNode           = entry.movable.ParentSceneNode;

                            PositionedInstance instance = Engine.SceneNodeStore.GetInstance(parentNode);

                            if (instance != null)
                            {
                                Engine.RunOnUIThread(() =>
                                {
                                    IoC.Get <IExplorer>().SelectInstance(instance);
                                });

                                ActiveScene.SelectedInstance = instance;

                                SetActiveSelectedNode(parentNode);
                                return;
                            }
                        }
                        SceneNode node;
                        if (ExistsSelectedNode(out node))
                        {
                            PositionedInstance oldInstance = Engine.SceneNodeStore.GetInstance(node);
                            if (oldInstance != null)
                            {
                                SizedInstance oldSized = oldInstance as SizedInstance;
                                if (oldSized != null)
                                {
                                    oldSized.IsSelected           = false;
                                    oldSized.IsBoundingBoxEnabled = false;
                                }
                            }

                            ActiveScene.SelectedInstance = null;

                            oldInstance.RemoveGizmoVisual();

                            ClearSelectedNode();
                        }
                    }
                }
            });
        }
Exemple #3
0
        private void _panel_MouseDown(object sender, MouseEventArgs e)
        {
            Point point = new Point(e.X, e.Y);

            Engine.QueueForRenderDispatcher(() =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    Ray ray;
                    RaySceneQueryResult.Enumerator itr = GetRaySceneQuery(point.X, point.Y, out ray);

                    if (itr != null)
                    {
                        if (itr.MoveNext())
                        {
                            RaySceneQueryResultEntry entry = itr.Current;
                            SceneNode parentNode           = entry.movable.ParentSceneNode;

                            TransformDragging dragging;
                            DirectionalTransformDragging directionalDragging;
                            if (IsTranslateClick(parentNode, out dragging))
                            {
                                _translateDragging = dragging;
                                IEnumerable <SceneNode> selected = GetSelectedNode();
                                if (selected != null)
                                {
                                    SceneNode node = selected.FirstOrDefault();
                                    if (node != null)
                                    {
                                        _translateDragDifference = ray.GetPoint(entry.distance) - node.Position;
                                    }
                                }
                                return;
                            }
                            else if (IsScaleClick(parentNode, out directionalDragging))
                            {
                                _scaleDragging = directionalDragging;
                                IEnumerable <SceneNode> selected = GetSelectedNode();
                                if (selected != null)
                                {
                                    SceneNode node = selected.FirstOrDefault();
                                    if (node != null)
                                    {
                                        _scaleDragDifference = ray.GetPoint(entry.distance) - node.Position;
                                    }
                                }
                                return;
                            }
                            else
                            {
                                _translateDragging = TransformDragging.NONE;
                            }
                        }
                    }
                }
            });
        }
        public bool Tick(int diffX, int diffY, string startX, string startY, int searchCounter)
        {
            if (mRoot != null && IsInitialized == true)
            {
                renderScene();
                foobar();
                if (mAnimationState != null)
                {
                    mAnimationState.AddTime(((float).01)); //todo - might not render last frame
                    if (mAnimationState.HasEnded)
                    {
                        return(false);
                    }
                }

                // Setup the scene query
                Vector3 camPos    = mCamera.RealPosition;
                Ray     cameraRay = new Ray(new Vector3(camPos.x, 5000.0f, camPos.z),
                                            Vector3.NEGATIVE_UNIT_Y);
                mRaySceneQuery.Ray = cameraRay;

                // Perform the scene query;
                RaySceneQueryResult            result = mRaySceneQuery.Execute();
                RaySceneQueryResult.Enumerator itr    = (RaySceneQueryResult.Enumerator)result.GetEnumerator();

                // Get the results, set the camera height
                if ((itr != null) && itr.MoveNext())
                {
                    if (itr.Current != null && itr.Current.worldFragment != null)
                    {
                        float terrainHeight = itr.Current.worldFragment.singleIntersection.y;

                        if ((terrainHeight + 120.0f) > camPos.y)
                        {// mCamera.SetPosition(camPos.x, terrainHeight + 150.0f, camPos.z);
                            cameraNode.Translate(cameraYawNode.Orientation *
                                                 cameraPitchNode.Orientation *
                                                 new Vector3(0, 10, 0),
                                                 SceneNode.TransformSpace.TS_LOCAL);
                        }
                    }
                }

                mRoot.RenderOneFrame();
            }
            return(true);
        }
        public void LeftClicked(float x, float y, string currentEntity)
        {
            // Save mouse position
            mouseX = x; mouseY = y;

            // Setup the ray scene query
            Ray mouseRay = mCamera.GetCameraToViewportRay(mouseX, mouseY);

            Ray newRay = new Ray(new Vector3(mouseRay.Origin.x, System.Math.Abs(mouseRay.Origin.y), mouseRay.Origin.z), mouseRay.Direction);

            mRaySceneQuery.Ray = newRay;

            // Execute query
            RaySceneQueryResult result = mRaySceneQuery.Execute();

            RaySceneQueryResult.Enumerator itr = (RaySceneQueryResult.Enumerator)result.GetEnumerator();

            // Get results, create a node/entity on the position
            if (itr != null && itr.MoveNext())
            {
                if (string.IsNullOrWhiteSpace(currentEntity))
                {
                    return;
                }

                if (itr.Current != null && itr.Current.worldFragment != null)
                {
                    Entity ent = mMgr.CreateEntity(currentEntity + mCount.ToString(), currentEntity);
                    mCurrentObject = mMgr.RootSceneNode.CreateChildSceneNode(currentEntity + "Node" + mCount.ToString(),
                                                                             itr.Current.worldFragment.singleIntersection);
                    mCount++;
                    mCurrentObject.AttachObject(ent);
                    mCurrentObject.SetScale(scale);
                }
            } //

            mLMouseDown = true;
            return;
        }
Exemple #6
0
        public static Vector3 GetBestLocationFromYDown(Vector3 vector, float fallbackY, float sizeY)
        {
            Ray ray = new Ray(vector + new Vector3(0, (float.MaxValue), 0), Vector3.NEGATIVE_UNIT_Y);

            RaySceneQuery mRaySceneQuery = Engine.Renderer.Scene.CreateRayQuery(ray);

            mRaySceneQuery.SetSortByDistance(true, 64);
            mRaySceneQuery.QueryMask     = (uint)QueryFlags.INSTANCE_ENTITY;
            mRaySceneQuery.QueryTypeMask =
                SceneManager.ENTITY_TYPE_MASK |
                SceneManager.STATICGEOMETRY_TYPE_MASK |
                SceneManager.USER_TYPE_MASK_LIMIT |
                SceneManager.FRUSTUM_TYPE_MASK |
                SceneManager.FX_TYPE_MASK |
                SceneManager.LIGHT_TYPE_MASK |
                SceneManager.WORLD_GEOMETRY_TYPE_MASK;

            RaySceneQueryResult result = mRaySceneQuery.Execute();

            RaySceneQueryResult.Enumerator itr = (RaySceneQueryResult.Enumerator)(result.GetEnumerator());

            Vector3 max = new Vector3(vector.x, fallbackY, vector.z);

            if (itr != null)
            {
                while (itr.MoveNext())
                {
                    RaySceneQueryResultEntry entry = itr.Current;
                    SceneNode parentNode           = entry.movable.ParentSceneNode;
                    Vector3   current = new Vector3(vector.x, parentNode.Position.y + sizeY, vector.z);
                    if (current.y > max.y)
                    {
                        max = current;
                    }
                }
            }
            return(max);
        }
Exemple #7
0
        private void _panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (_oldMousePosition == null)
            {
                _oldMousePosition = new Point(e.X, e.Y);
                return;
            }
            Point point = new Point(e.X, e.Y);

            Engine.QueueForRenderDispatcher(() =>
            {
                double deltaDirectionX = point.X - _oldMousePosition.X;
                double deltaDirectionY = point.Y - _oldMousePosition.Y;

                if (e.Button.HasFlag(MouseButtons.Right))
                {
                    ActiveScene.Context.ActiveWorld.Camera.pitch(new Radian((float)-deltaDirectionY / 200f).ValueDegrees);
                    ActiveScene.Context.ActiveWorld.Camera.yaw(new Radian((float)-deltaDirectionX / 200f).ValueDegrees);
                }

                IEnumerable <SceneNode> selected = GetSelectedNode();
                SceneNode selectedNode           = null;
                if (selected != null)
                {
                    selectedNode = selected.FirstOrDefault();
                }

                if (_translateDragging != TransformDragging.NONE && ActiveScene.ActiveGizmoType == Misc.Runtime.GizmoType.TRANSLATE)
                {
                    if (selectedNode != null)
                    {
                        Plane plane = new Plane();
                        if (_translateDragging == TransformDragging.X)
                        {
                            Vector3 normal = Vector3.UNIT_Y;
                            plane          = new Plane(normal, selectedNode.Position);
                        }
                        else if (_translateDragging == TransformDragging.Y)
                        {
                            Vector3 normal = (Engine.Renderer.Camera.Position - selectedNode.Position).NormalisedCopy;
                            normal.y       = 0;
                            plane          = new Plane(normal, selectedNode.Position);
                        }
                        else if (_translateDragging == TransformDragging.Z)
                        {
                            Vector3 normal = Vector3.UNIT_Y;
                            plane          = new Plane(normal, selectedNode.Position);
                        }

                        Vector3 translateVector;
                        if (GetRaySceneLocationWithPlane(point.X, point.Y, plane, out translateVector, selectedNode))
                        {
                            PositionedInstance instance = Engine.SceneNodeStore.GetInstance(selectedNode);

                            SizedInstance sized = instance as SizedInstance;

                            if (sized != null)
                            {
                                translateVector -= _translateDragDifference;

                                if (_translateDragging == TransformDragging.X)
                                {
                                    instance.position.x = (float)System.Math.Round(translateVector.x - (sized.size.x / 2));
                                    //PhysicsEngine.GetCollisionVectorResolvement(instance as PhysicsInstance);
                                }
                                else if (_translateDragging == TransformDragging.Y)
                                {
                                    instance.position.y = (float)System.Math.Round(translateVector.y - (sized.size.y / 2));
                                    //PhysicsEngine.GetCollisionVectorResolvement(instance as PhysicsInstance);
                                }
                                else if (_translateDragging == TransformDragging.Z)
                                {
                                    instance.position.z = (float)System.Math.Round(translateVector.z - (sized.size.z / 2));
                                    //PhysicsEngine.GetCollisionVectorResolvement(instance as PhysicsInstance);
                                }
                            }
                        }
                    }
                }
                else if (_scaleDragging != DirectionalTransformDragging.NONE && ActiveScene.ActiveGizmoType == Misc.Runtime.GizmoType.SCALE)
                {
                    if (selectedNode != null)
                    {
                        Plane plane = new Plane();
                        if (_scaleDragging == DirectionalTransformDragging.XA || _scaleDragging == DirectionalTransformDragging.XB)
                        {
                            Vector3 normal = Vector3.UNIT_Y;
                            plane          = new Plane(normal, selectedNode.Position);
                        }
                        else if (_scaleDragging == DirectionalTransformDragging.YA || _scaleDragging == DirectionalTransformDragging.YB)
                        {
                            Vector3 normal = (Engine.Renderer.Camera.Position - selectedNode.Position).NormalisedCopy;
                            normal.y       = 0;
                            plane          = new Plane(normal, selectedNode.Position);
                        }
                        else if (_scaleDragging == DirectionalTransformDragging.ZA || _scaleDragging == DirectionalTransformDragging.ZB)
                        {
                            Vector3 normal = Vector3.UNIT_Y;
                            plane          = new Plane(normal, selectedNode.Position);
                        }

                        Vector3 scaleVector;
                        if (GetRaySceneLocationWithPlane(point.X, point.Y, plane, out scaleVector, selectedNode))
                        {
                            PositionedInstance instance = Engine.SceneNodeStore.GetInstance(selectedNode);

                            SizedInstance sized = instance as SizedInstance;

                            if (sized != null)
                            {
                                int magicOffsetA = 2;

                                if (_scaleDragging == DirectionalTransformDragging.XA)
                                {
                                    sized.size.x = System.Math.Max(1, (float)System.Math.Round(scaleVector.x - (sized.position.x) - magicOffsetA));
                                }
                                else if (_scaleDragging == DirectionalTransformDragging.YA)
                                {
                                    sized.size.y = System.Math.Max(1, (float)System.Math.Round(scaleVector.y - (sized.position.y) - magicOffsetA));
                                }
                                else if (_scaleDragging == DirectionalTransformDragging.ZA)
                                {
                                    sized.size.z = System.Math.Max(1, (float)System.Math.Round(scaleVector.z - (sized.position.z) - magicOffsetA));
                                }
                                else if (_scaleDragging == DirectionalTransformDragging.XB)
                                {
                                    //TODO: fix
                                    float newX  = scaleVector.x + (sized.size.x / 2) + _scaleDragDifference.x; //wtf?
                                    float delta = (sized.position.x - newX);

                                    float rounded = (float)System.Math.Round(delta);

                                    sized.position.x -= rounded;
                                    sized.size.x     += rounded;
                                }
                                else if (_scaleDragging == DirectionalTransformDragging.YB)
                                {
                                }
                                else if (_scaleDragging == DirectionalTransformDragging.ZB)
                                {
                                }
                            }
                        }
                    }
                }

                Cursor cursor = null;
                if (_translateFreeDragging && _scaleDragging == DirectionalTransformDragging.NONE && _translateDragging == TransformDragging.NONE)
                {
                    if (selectedNode != null)
                    {
                        Plane plane = new Plane(Vector3.UNIT_Y, Engine.Renderer.Camera.Position.y - 16);

                        Vector3 vector;
                        if (GetRaySceneLocationWithPlane(point.X, point.Y, plane, out vector, selectedNode))
                        {
                            PositionedInstance instance = Engine.SceneNodeStore.GetInstance(selectedNode);

                            SizedInstance sized = instance as SizedInstance;

                            if (sized != null)
                            {
                                instance.position.x = (float)System.Math.Round(vector.x - (sized.size.x / 2));
                                instance.position.y = (float)System.Math.Round(vector.y - (sized.size.y / 2));
                                instance.position.z = (float)System.Math.Round(vector.z - (sized.size.z / 2));
                            }
                        }

                        cursor = Cursors.NoMove2D;
                    }
                    else
                    {
                        _translateFreeDragging = false;
                    }
                }

                Ray ray;
                RaySceneQueryResult.Enumerator itr = GetRaySceneQuery(point.X, point.Y, out ray);

                if (itr != null)
                {
                    bool nothing = true;
                    while (itr.MoveNext())
                    {
                        RaySceneQueryResultEntry entry = itr.Current;

                        if (entry.movable.QueryFlags != (uint)QueryFlags.IGNORE_ALL)
                        {
                            nothing = false;
                        }

                        if (entry.movable.QueryFlags == (uint)QueryFlags.INSTANCE_ENTITY)
                        {
                            SceneNode parentNode = entry.movable.ParentSceneNode;
                            if (selectedNode != null && selectedNode.Equals(parentNode) && e.Button.HasFlag(MouseButtons.Left) && _translateDragging == TransformDragging.NONE)
                            {
                                if (System.Math.Abs(deltaDirectionX) >= TRANSLATE_DRAG_THRESHOLD || System.Math.Abs(deltaDirectionY) >= TRANSLATE_DRAG_THRESHOLD)
                                {
                                    _translateFreeDragging = true;
                                }
                            }
                            if (!IsAlreadyHovered(parentNode))
                            {
                                IEnumerable <SceneNode> hovered = GetHoveredNodes();
                                if (hovered != null)
                                {
                                    foreach (SceneNode node in hovered)
                                    {
                                        if (!IsSelectedNode(node))
                                        {
                                            PositionedInstance oldInstance = Engine.SceneNodeStore.GetInstance(node);
                                            if (oldInstance != null)
                                            {
                                                SizedInstance oldSized = oldInstance as SizedInstance;
                                                if (oldSized != null)
                                                {
                                                    oldSized.IsBoundingBoxEnabled = false;
                                                }
                                            }
                                        }
                                    }
                                    ClearHovered();
                                }
                                AddToHoverNode(parentNode);
                            }
                            break;
                        }
                    }
                    if (nothing)
                    {
                        if (cursor == null)
                        {
                            cursor = Cursors.Default;
                        }
                        IEnumerable <SceneNode> hovered = GetHoveredNodes();
                        if (hovered != null)
                        {
                            foreach (SceneNode node in hovered)
                            {
                                if (!IsSelectedNode(node))
                                {
                                    PositionedInstance oldInstance = Engine.SceneNodeStore.GetInstance(node);
                                    if (oldInstance != null)
                                    {
                                        SizedInstance oldSized = oldInstance as SizedInstance;
                                        if (oldSized != null)
                                        {
                                            oldSized.IsBoundingBoxEnabled = false;
                                        }
                                    }
                                }
                            }
                            ClearHovered();
                        }
                    }
                    else
                    {
                        if (cursor == null)
                        {
                            cursor = Cursors.Hand;
                        }
                    }
                }
                if (cursor != null)
                {
                    System.Windows.Application.Current.Dispatcher.InvokeAsync(() => _panel.Cursor = cursor, DispatcherPriority.Background);
                }
                _oldMousePosition = point;
            });
        }
Exemple #8
0
        private static bool GetRaySceneLocationWithPlane(double x, double y, Plane plane, out Vector3 vector, SceneNode ignore)
        {
            float width  = Engine.Renderer.Camera.Viewport.ActualWidth;
            float height = Engine.Renderer.Camera.Viewport.ActualHeight;

            Ray mouseRay = Engine.Renderer.Camera.GetCameraToViewportRay((float)((x - 1) / width), (float)((y - 1) / height));

            Plane inverse = new Plane(-plane.normal, -plane.d);

            Pair <bool, float> d0 = mouseRay.Intersects(plane);
            Pair <bool, float> d1 = mouseRay.Intersects(inverse);

            RaySceneQuery mRaySceneQuery = Engine.Renderer.Scene.CreateRayQuery(mouseRay);

            mRaySceneQuery.SetSortByDistance(true, 16);
            mRaySceneQuery.QueryMask     = (uint)QueryFlags.INSTANCE_ENTITY;
            mRaySceneQuery.QueryTypeMask =
                SceneManager.ENTITY_TYPE_MASK |
                SceneManager.STATICGEOMETRY_TYPE_MASK |
                SceneManager.USER_TYPE_MASK_LIMIT |
                SceneManager.FRUSTUM_TYPE_MASK |
                SceneManager.FX_TYPE_MASK |
                SceneManager.LIGHT_TYPE_MASK |
                SceneManager.WORLD_GEOMETRY_TYPE_MASK;

            RaySceneQueryResult result = mRaySceneQuery.Execute();

            RaySceneQueryResult.Enumerator itr = (RaySceneQueryResult.Enumerator)(result.GetEnumerator());

            if (itr != null)
            {
                RaySceneQueryResultEntry entry = null;
                while (itr.MoveNext())
                {
                    entry = itr.Current;
                    if (entry != null && !entry.movable.ParentSceneNode.Equals(ignore))
                    {
                        vector = mouseRay.GetPoint(entry.distance);
                        return(true);
                    }
                }
                if (d0.first)
                {
                    Vector3 planePoint = mouseRay.GetPoint(d0.second);
                    if ((Engine.Renderer.Camera.Position - planePoint).Length < MAX_TRANSLATE_DRAG_DISTANCE)
                    {
                        vector = planePoint;
                        return(true);
                    }
                }
                if (d1.first)
                {
                    Vector3 planePoint = mouseRay.GetPoint(d1.second);
                    if ((Engine.Renderer.Camera.Position - planePoint).Length < MAX_TRANSLATE_DRAG_DISTANCE)
                    {
                        vector = planePoint;
                        return(true);
                    }
                    else
                    {
                        vector = Vector3.ZERO;
                        return(false);
                    }
                }
                else
                {
                    vector = Vector3.ZERO;
                    return(false);
                }
            }
            else
            {
                vector = Vector3.ZERO;
                return(false);
            }
        }