Ejemplo n.º 1
0
        void OnToolbarGUI(int windowID)
        {
            GUI.enabled = (targetBrush != null);
            EditorGUILayout.BeginHorizontal();

            if (SabreGUILayout.Button("Clip"))
            {
                ApplyClipPlane(false);
            }

            if (SabreGUILayout.Button("Split"))
            {
                ApplyClipPlane(true);
            }
            if (SabreGUILayout.Button("Flip Plane"))
            {
                isFlipped = !isFlipped;
            }
            EditorGUILayout.EndHorizontal();

            GUI.enabled = (pointSelected > -1);
            if(SabreGUILayout.Button("Snap To Grid"))
            {
                float snapDistance = CurrentSettings.PositionSnapDistance;
                Vector3 newPosition = points[pointSelected];
                newPosition = targetBrush.transform.TransformPoint(newPosition);
                newPosition = MathHelper.RoundVector3(newPosition, snapDistance);
                newPosition = targetBrush.transform.InverseTransformPoint(newPosition);

                points[pointSelected] = newPosition;
            }

            displayPlane = new Plane(points[2], points[1], points[0]);
            if(isFlipped)
            {
                displayPlane = displayPlane.Flip();
            }

            planePosition = (points[0] + points[1] + points[2]) / 3f;
        }
Ejemplo n.º 2
0
        void ApplyClipPlane(bool keepBothSides)
        {
            if(targetBrush != null)
            {
                if(keepBothSides)
                {
                    Undo.RecordObject(targetBrush, "Split Brush");
                }
                else
                {
                    Undo.RecordObject(targetBrush, "Clipped Brush");
                }

                // Recalculate the clip plane from the world points, converting to local space for this transform
                Plane localClipPlane = new Plane(targetBrushTransform.InverseTransformPoint(points[2]),
                    targetBrushTransform.InverseTransformPoint(points[1]),
                    targetBrushTransform.InverseTransformPoint(points[0]));

                // If the user has specified to flip the plane, flip the plane we just calculated
                if(isFlipped)
                {
                    localClipPlane = localClipPlane.Flip();
                }

                // Clip the polygons against the plane
                List<Polygon> polygonsFront;
                List<Polygon> polygonsBack;

                if(PolygonFactory.SplitPolygonsByPlane(targetBrush.Polygons.ToList(), localClipPlane, false, out polygonsFront, out polygonsBack))
                {
                    // Update the brush with the new polygons
                    targetBrush.SetPolygons(polygonsFront.ToArray(), true);

                    // If they have decided to split instead of clip, create a second brush with the other side
                    if(keepBothSides)
                    {
                        GameObject newObject = targetBrush.Duplicate();

                        // Finally give the new brush the other set of polygons
                        newObject.GetComponent<PrimitiveBrush>().SetPolygons(polygonsBack.ToArray(), true);
                        newObject.transform.SetSiblingIndex(targetBrush.transform.GetSiblingIndex());
                        Undo.RegisterCreatedObjectUndo(newObject, "Split Brush");
                    }
                }
            }
        }