Ejemplo n.º 1
0
        public bool TransformFromInput(FRay ray, WSceneView view)
        {
            if (m_mode != FTransformMode.Translation)
            {
                WrapCursor();
            }

            // Store the cursor position in viewport coordinates.
            Vector2 screenDimensions = App.GetScreenGeometry();
            Vector2 cursorPos        = App.GetCursorPosition();
            Vector2 mouseCoords      = new Vector2(((2f * cursorPos.X) / screenDimensions.X) - 1f, (1f - ((2f * cursorPos.Y) / screenDimensions.Y))); //[-1,1] range

            bool shiftPressed = WInput.GetKey(Key.LeftShift) || WInput.GetKey(Key.RightShift);

            if (m_mode == FTransformMode.Translation)
            {
                // Create a Translation Plane
                Vector3 axisA, axisB;

                if (GetNumSelectedAxes() == 1)
                {
                    if (m_selectedAxes == FSelectedAxes.X)
                    {
                        axisB = Vector3.UnitX;
                    }
                    else if (m_selectedAxes == FSelectedAxes.Y)
                    {
                        axisB = Vector3.UnitY;
                    }
                    else
                    {
                        axisB = Vector3.UnitZ;
                    }

                    Vector3 dirToCamera = (m_position - view.GetCameraPos()).Normalized();
                    axisA = Vector3.Cross(axisB, dirToCamera);
                }
                else
                {
                    axisA = ContainsAxis(m_selectedAxes, FSelectedAxes.X) ? Vector3.UnitX : Vector3.UnitZ;
                    axisB = ContainsAxis(m_selectedAxes, FSelectedAxes.Y) ? Vector3.UnitY : Vector3.UnitZ;
                }

                Vector3 planeNormal = Vector3.Cross(axisA, axisB).Normalized();
                m_translationPlane = new FPlane(planeNormal, m_position);

                float intersectDist;
                if (m_translationPlane.RayIntersectsPlane(ray, out intersectDist))
                {
                    Vector3 hitPos     = ray.Origin + (ray.Direction * intersectDist);
                    Vector3 localDelta = Vector3.Transform(hitPos - m_position, m_rotation.Inverted().ToSinglePrecision());

                    // Calculate a new position
                    Vector3 newPos = m_position;
                    if (ContainsAxis(m_selectedAxes, FSelectedAxes.X))
                    {
                        newPos += Vector3.Transform(Vector3.UnitX, m_rotation.ToSinglePrecision()) * localDelta.X;
                    }
                    if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y))
                    {
                        newPos += Vector3.Transform(Vector3.UnitY, m_rotation.ToSinglePrecision()) * localDelta.Y;
                    }
                    if (ContainsAxis(m_selectedAxes, FSelectedAxes.Z))
                    {
                        newPos += Vector3.Transform(Vector3.UnitZ, m_rotation.ToSinglePrecision()) * localDelta.Z;
                    }

                    if (shiftPressed)
                    {
                        // Round to nearest 100 unit increment while shift is held down.
                        newPos.X = (float)Math.Round(newPos.X / 100f) * 100f;
                        newPos.Y = (float)Math.Round(newPos.Y / 100f) * 100f;
                        newPos.Z = (float)Math.Round(newPos.Z / 100f) * 100f;
                    }

                    // Check the new location to see if it's skyrocked off into the distance due to near-plane raytracing issues.
                    Vector3 newPosDirToCamera = (newPos - view.GetCameraPos()).Normalized();
                    float   dot = Math.Abs(Vector3.Dot(planeNormal, newPosDirToCamera));

                    //Console.WriteLine("hitPos: {0} localOffset: {1} newPos: {2}, dotResult: {3}", hitPos, localOffset, newPos, dot);
                    if (dot < 0.02f)
                    {
                        return(false);
                    }

                    // This is used to set the offset to the gizmo the mouse cursor is from the origin of the gizmo on the first frame
                    // that you click on the gizmo.
                    if (!m_hasSetMouseOffset)
                    {
                        m_translateOffset   = m_position - newPos;
                        m_deltaTranslation  = Vector3.Zero;
                        m_hasSetMouseOffset = true;
                        return(false);
                    }

                    // Apply Translation
                    m_deltaTranslation = Vector3.Transform(newPos - m_position + m_translateOffset, m_rotation.Inverted().ToSinglePrecision());

                    if (!ContainsAxis(m_selectedAxes, FSelectedAxes.X))
                    {
                        m_deltaTranslation.X = 0f;
                    }
                    if (!ContainsAxis(m_selectedAxes, FSelectedAxes.Y))
                    {
                        m_deltaTranslation.Y = 0f;
                    }
                    if (!ContainsAxis(m_selectedAxes, FSelectedAxes.Z))
                    {
                        m_deltaTranslation.Z = 0f;
                    }

                    m_totalTranslation += m_deltaTranslation;
                    m_position         += Vector3.Transform(m_deltaTranslation, m_rotation.ToSinglePrecision());

                    if (!m_hasTransformed && (m_deltaTranslation != Vector3.Zero))
                    {
                        m_hasTransformed = true;
                    }

                    return(m_hasTransformed);
                }
                else
                {
                    // Our raycast missed the plane
                    m_deltaTranslation = Vector3.Zero;
                    return(false);
                }
            }
            else if (m_mode == FTransformMode.Rotation)
            {
                Vector3d rotationAxis;
                if (m_selectedAxes == FSelectedAxes.X)
                {
                    rotationAxis = Vector3d.UnitX;
                }
                else if (m_selectedAxes == FSelectedAxes.Y)
                {
                    rotationAxis = Vector3d.UnitY;
                }
                else
                {
                    rotationAxis = Vector3d.UnitZ;
                }

                // Convert these from [0-1] to [-1, 1] to match our mouse coords.
                Vector2 lineOrigin = (view.UnprojectWorldToViewport(m_hitPoint) * 2) - Vector2.One;
                Vector2 lineEnd    = (view.UnprojectWorldToViewport(m_hitPoint + m_moveDir) * 2) - Vector2.One;

                lineOrigin.Y = -lineOrigin.Y;
                lineEnd.Y    = -lineEnd.Y;

                Vector2 lineDir   = (lineEnd - lineOrigin).Normalized();
                float   rotAmount = Vector2.Dot(lineDir, mouseCoords + m_wrapOffset - lineOrigin) * 180f;

                if (float.IsNaN(rotAmount))
                {
                    Console.WriteLine("rotAmountNaN!");
                    return(false);
                }

                if (!m_hasSetMouseOffset)
                {
                    m_rotateOffset      = -rotAmount;
                    m_deltaRotation     = Quaterniond.Identity;
                    m_hasSetMouseOffset = true;
                    return(false);
                }

                // Apply Rotation
                rotAmount += m_rotateOffset;
                if (shiftPressed)
                {
                    // Round to nearest 45 degree increment while shift is held down.
                    rotAmount = (float)Math.Round(rotAmount / 45f) * 45f;
                }
                Quaterniond oldRot = m_currentRotation;
                m_currentRotation = Quaterniond.FromAxisAngle(rotationAxis, WMath.DegreesToRadians(rotAmount));
                m_deltaRotation   = m_currentRotation * oldRot.Inverted();

                if (m_transformSpace == FTransformSpace.Local)
                {
                    m_rotation *= m_deltaRotation;
                }

                // Add to Total Rotation recorded for UI.
                if (m_selectedAxes == FSelectedAxes.X)
                {
                    m_totalRotation.X = rotAmount;
                }
                else if (m_selectedAxes == FSelectedAxes.Y)
                {
                    m_totalRotation.Y = rotAmount;
                }
                else
                {
                    m_totalRotation.Z = rotAmount;
                }

                if (!m_hasTransformed && rotAmount != 0f)
                {
                    m_hasTransformed = true;
                }

                return(m_hasTransformed);
            }
            else if (m_mode == FTransformMode.Scale)
            {
                // Create a line in screen space.
                // Convert these from [0-1] to [-1, 1] to match our mouse coords.
                Vector2 lineOrigin = (view.UnprojectWorldToViewport(m_position) * 2) - Vector2.One;
                lineOrigin.Y = -lineOrigin.Y;

                // Determine the appropriate world space directoin using the selected axes and then conver this for use with
                // screen-space controlls. This has to be done every frame because the axes can be flipped while the gizmo
                // is transforming, so we can't pre-calculate this.
                Vector3 dirX = Vector3.Transform(mFlipScaleX ? -Vector3.UnitX : Vector3.UnitX, m_rotation.ToSinglePrecision());
                Vector3 dirY = Vector3.Transform(mFlipScaleY ? -Vector3.UnitY : Vector3.UnitY, m_rotation.ToSinglePrecision());
                Vector3 dirZ = Vector3.Transform(mFlipScaleZ ? -Vector3.UnitZ : Vector3.UnitZ, m_rotation.ToSinglePrecision());
                Vector2 lineDir;

                // If there is only one axis, then the world space direction is the selected axis.
                if (GetNumSelectedAxes() == 1)
                {
                    Vector3 worldDir;
                    if (ContainsAxis(m_selectedAxes, FSelectedAxes.X))
                    {
                        worldDir = dirX;
                    }
                    if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y))
                    {
                        worldDir = dirY;
                    }
                    else
                    {
                        worldDir = dirZ;
                    }

                    Vector2 worldPoint = (view.UnprojectWorldToViewport(m_position + worldDir) * 2) - Vector2.One;
                    worldPoint.Y = -lineOrigin.Y;

                    lineDir = (worldPoint - lineOrigin).Normalized();
                }
                // If there's two axii selected, then convert both to screen space and average them out to get the line direction.
                else if (GetNumSelectedAxes() == 2)
                {
                    Vector3 axisA = ContainsAxis(m_selectedAxes, FSelectedAxes.X) ? dirX : dirY;
                    Vector3 axisB = ContainsAxis(m_selectedAxes, FSelectedAxes.Z) ? dirZ : dirY;

                    Vector2 screenA = (view.UnprojectWorldToViewport(m_position + axisA) * 2) - Vector2.One;
                    screenA.Y = -screenA.Y;
                    Vector2 screenB = (view.UnprojectWorldToViewport(m_position + axisB) * 2) - Vector2.One;
                    screenB.Y = -screenB.Y;

                    screenA = (screenA - lineOrigin).Normalized();
                    screenB = (screenB - lineOrigin).Normalized();
                    lineDir = ((screenA + screenB) / 2f).Normalized();
                }
                // There's three axis, just use up.
                else
                {
                    lineDir = Vector2.UnitY;
                }

                float scaleAmount = Vector2.Dot(lineDir, mouseCoords + m_wrapOffset - lineOrigin) * 5f;

                if (shiftPressed)
                {
                    // Round to nearest whole number scale while shift is held down.
                    scaleAmount = (float)Math.Round(scaleAmount);
                }

                // Set their initial offset if we haven't already
                if (!m_hasSetMouseOffset)
                {
                    m_scaleOffset       = -scaleAmount;
                    m_deltaScale        = Vector3.One;
                    m_hasSetMouseOffset = true;
                    return(false);
                }

                // Apply the scale
                scaleAmount = scaleAmount + m_scaleOffset + 1f;

                // A multiplier is applied to the scale amount if it's less than one to prevent it dropping into the negatives.
                // ???
                if (scaleAmount < 1f)
                {
                    scaleAmount = 1f / (-(scaleAmount - 1f) + 1f);
                }

                Vector3 oldScale = m_totalScale;
                m_totalScale = Vector3.One;
                if (ContainsAxis(m_selectedAxes, FSelectedAxes.X))
                {
                    m_totalScale.X = scaleAmount;
                }
                if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y))
                {
                    m_totalScale.Y = scaleAmount;
                }
                if (ContainsAxis(m_selectedAxes, FSelectedAxes.Z))
                {
                    m_totalScale.Z = scaleAmount;
                }

                m_deltaScale = new Vector3(m_totalScale.X / oldScale.X, m_totalScale.Y / oldScale.Y, m_totalScale.Z / oldScale.Z);

                if (!m_hasTransformed && (scaleAmount != 1f))
                {
                    m_hasTransformed = true;
                }

                return(m_hasTransformed);
            }

            return(false);
        }
Ejemplo n.º 2
0
        public bool CheckSelectedAxes(FRay ray)
        {
            // Convert the ray into local space so we can use axis-aligned checks, this solves the checking problem
            // when the gizmo is rotated due to being in Local mode.
            FRay localRay = new FRay();

            localRay.Direction = Vector3.Transform(ray.Direction, m_rotation.Inverted().ToSinglePrecision());
            localRay.Origin    = Vector3.Transform(ray.Origin - m_position, m_rotation.Inverted().ToSinglePrecision());

            //m_lineBatcher.DrawLine(localRay.Origin, localRay.Origin + (localRay.Direction * 10000), WLinearColor.White, 25, 5);
            List <AxisDistanceResult> results = new List <AxisDistanceResult>();

            if (m_mode == FTransformMode.Translation)
            {
                FAABox[] translationAABB = GetAABBBoundsForMode(FTransformMode.Translation);
                for (int i = 0; i < translationAABB.Length; i++)
                {
                    float intersectDist;
                    if (WMath.RayIntersectsAABB(localRay, translationAABB[i].Min, translationAABB[i].Max, out intersectDist))
                    {
                        results.Add(new AxisDistanceResult((FSelectedAxes)(i + 1), intersectDist));
                    }
                }
            }
            else if (m_mode == FTransformMode.Rotation)
            {
                // We'll use a combination of AABB and Distance checks to give us the quarter-circles we need.
                FAABox[] rotationAABB = GetAABBBoundsForMode(FTransformMode.Rotation);

                float screenScale = 0f;
                for (int i = 0; i < 3; i++)
                {
                    screenScale += m_scale[i];
                }
                screenScale /= 3f;

                for (int i = 0; i < rotationAABB.Length; i++)
                {
                    float intersectDist;
                    if (WMath.RayIntersectsAABB(localRay, rotationAABB[i].Min, rotationAABB[i].Max, out intersectDist))
                    {
                        Vector3 intersectPoint = localRay.Origin + (localRay.Direction * intersectDist);
                        // Convert this aabb check into a radius check so we clip it by the semi-circles
                        // that the rotation tool actually is.
                        if (intersectPoint.Length > 105f * screenScale)
                        {
                            continue;
                        }

                        if (i == 0 && !mShowRotX)
                        {
                            continue;
                        }
                        if (i == 1 && !mShowRotY)
                        {
                            continue;
                        }
                        if (i == 2 && !mShowRotZ)
                        {
                            continue;
                        }

                        results.Add(new AxisDistanceResult((FSelectedAxes)(i + 1), intersectDist));
                    }
                }
            }
            else if (m_mode == FTransformMode.Scale)
            {
                FAABox[] scaleAABB = GetAABBBoundsForMode(FTransformMode.Scale);
                for (int i = 0; i < scaleAABB.Length; i++)
                {
                    float intersectDist;
                    if (WMath.RayIntersectsAABB(localRay, scaleAABB[i].Min, scaleAABB[i].Max, out intersectDist))
                    {
                        // Special-case here to give the center scale point overriding priority. Because we intersected
                        // it, we can just override its distance to zero to make it clickable through the other bounding boxes.
                        if ((FSelectedAxes)i + 1 == FSelectedAxes.All)
                        {
                            intersectDist = 0f;
                        }

                        results.Add(new AxisDistanceResult((FSelectedAxes)(i + 1), intersectDist));
                    }
                }
            }

            if (results.Count == 0)
            {
                m_selectedAxes = FSelectedAxes.None;
                return(false);
            }

            // If we get an intersection, sort them by the closest intersection distance.
            results.Sort((x, y) => x.Distance.CompareTo(y.Distance));
            m_selectedAxes = results[0].Axis;

            // Store where the mouse hit on the first frame in world space. This means converting the ray back to worldspace.
            Vector3 localHitPoint = localRay.Origin + (localRay.Direction * results[0].Distance);

            m_hitPoint = Vector3.Transform(localHitPoint, m_rotation.ToSinglePrecision()) + m_position;
            return(true);
        }