Esempio n. 1
0
 private void CreateHandleTriangleMesh(ref Mesh mesh, Vector3 scale)
 {
     if (Tool == Tool.Translate)
     {
         HandleMesh.CreateTriangleMesh(ref mesh, parentTransform, scale, viewOctant, mainCamera, coneMesh, handleBoxSize, capSize);
     }
     else if (Tool == Tool.Scale)
     {
         HandleMesh.CreateTriangleMesh(ref mesh, parentTransform, scale, viewOctant, mainCamera, cubeMesh, handleBoxSize, capSize);
     }
 }
Esempio n. 2
0
        private void CreateHandleLineMesh(ref Mesh mesh, Vector3 scale)
        {
            switch (Tool)
            {
            case Tool.Translate:
            case Tool.Scale:
                HandleMesh.CreatePositionLineMesh(ref mesh, parentTransform, scale, viewOctant, mainCamera, handleBoxSize);
                break;

            case Tool.Rotate:
                HandleMesh.CreateRotateMesh(ref mesh, 48, 1f);
                break;

            default:
                return;
            }
        }
Esempio n. 3
0
        private bool CheckHandleActivated(Vector2 mousePosition, out Axis plane)
        {
            plane = Axis.None;

            if (Tool == Tool.Translate || Tool == Tool.Scale)
            {
                float handleScreenSize = HandleUtility.GetHandleSize(parentTransform.position, mainCamera.transform.position, handleSize);

                // center
                Vector2 cen = mainCamera.WorldToScreenPoint(parentTransform.position);
                // up
                Vector2 up = mainCamera.WorldToScreenPoint((parentTransform.position + (parentTransform.up + parentTransform.up * capSize * 4f) * handleScreenSize));
                // right
                Vector2 right = mainCamera.WorldToScreenPoint((parentTransform.position + (parentTransform.right + parentTransform.right * capSize * 4f) * handleScreenSize));
                // forward
                Vector2 forward = mainCamera.WorldToScreenPoint((parentTransform.position + (parentTransform.forward + parentTransform.forward * capSize * 4f) * handleScreenSize));
                // First check if the plane boxes have been activated
                Vector2 p_right   = (cen + ((right - cen) * viewOctant.x) * handleBoxSize);
                Vector2 p_up      = (cen + ((up - cen) * viewOctant.y) * handleBoxSize);
                Vector2 p_forward = (cen + ((forward - cen) * viewOctant.z) * handleBoxSize);

                //x plane
                if (HandleUtility.PointInPolygon(new Vector2[] { cen, p_up, p_up, (p_up + p_forward) - cen,
                                                                 (p_up + p_forward) - cen, p_forward, p_forward, cen },
                                                 mousePosition))
                {
                    plane = Axis.Y | Axis.Z;
                }
                //y plane
                else if (HandleUtility.PointInPolygon(new Vector2[] { cen, p_right, p_right, (p_right + p_forward) - cen,
                                                                      (p_right + p_forward) - cen, p_forward, p_forward, cen },
                                                      mousePosition))
                {
                    plane = Axis.X | Axis.Z;
                }
                //z plane
                else if (HandleUtility.PointInPolygon(new Vector2[] { cen, p_up, p_up, (p_up + p_right) - cen,
                                                                      (p_up + p_right) - cen, p_right, p_right, cen },
                                                      mousePosition))
                {
                    plane = Axis.X | Axis.Y;
                }
                //x axis
                else if (HandleUtility.DistancePointLineSegment(mousePosition, cen, up) < handleInteractWidth)
                {
                    plane = Axis.Y;
                }
                //y axis
                else if (HandleUtility.DistancePointLineSegment(mousePosition, cen, right) < handleInteractWidth)
                {
                    plane = Axis.X;
                }
                //z axis
                else if (HandleUtility.DistancePointLineSegment(mousePosition, cen, forward) < handleInteractWidth)
                {
                    plane = Axis.Z;
                }
                else
                {
                    return(false);
                }

                return(true);
            }
            else
            {
                Vector3[][] vertices = HandleMesh.GetRotationVertices(16, 1f);

                float best = Mathf.Infinity;

                Vector2 cur, prev = Vector2.zero;
                plane = Axis.X;

                for (int i = 0; i < 3; i++)
                {
                    cur = mainCamera.WorldToScreenPoint(vertices[i][0]);

                    for (int n = 0; n < vertices[i].Length - 1; n++)
                    {
                        prev = cur;
                        cur  = mainCamera.WorldToScreenPoint(handleMatrix.MultiplyPoint3x4(vertices[i][n + 1]));

                        float dist = HandleUtility.DistancePointLineSegment(mousePosition, prev, cur);

                        if (dist < best && dist < handleInteractWidth)
                        {
                            Vector3 viewDir = (handleMatrix.MultiplyPoint3x4((vertices[i][n] + vertices[i][n + 1]) * .5f) - mainCamera.transform.position).normalized;
                            Vector3 nrm     = parentTransform.TransformDirection(vertices[i][n]).normalized;

                            if (Vector3.Dot(nrm, viewDir) > .5f)
                            {
                                continue;
                            }

                            best = dist;

                            switch (i)
                            {
                            case 0:             // Y
                                plane = Axis.Y; // Axis.X | Axis.Z;
                                break;

                            case 1:             // Z
                                plane = Axis.Z; // Axis.X | Axis.Y;
                                break;

                            case 2:             // X
                                plane = Axis.X; // Axis.Y | Axis.Z;
                                break;
                            }
                        }
                    }
                }

                if (best < handleInteractWidth + .1f)
                {
                    return(true);
                }
            }

            return(false);
        }