Example #1
0
        public void DrawBrushTypeField()
        {
            GUILayout.BeginHorizontal();
            PrimitiveBrushType[] selectedTypes = BrushTargets.Select(item => item.BrushType).ToArray();

            if (overridenBrushType.HasValue)
            {
                selectedTypes = new PrimitiveBrushType[] { overridenBrushType.Value };
            }

            PrimitiveBrushType?newType = SabreGUILayout.EnumPopupMixed("Brush Type", selectedTypes);

            if (newType.HasValue)
            {
                overridenBrushType = newType;

                if (newType.Value == PrimitiveBrushType.Prism)
                {
                    GUILayout.Label("Sides", SabreGUILayout.GetForeStyle(), GUILayout.Width(30));
                    EditorGUILayout.PropertyField(prismSideCountProp, new GUIContent(""));
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                }
                else if (newType.Value == PrimitiveBrushType.Cylinder)
                {
                    GUILayout.Label("Sides", SabreGUILayout.GetForeStyle(), GUILayout.Width(30));
                    EditorGUILayout.PropertyField(cylinderSideCountProp, new GUIContent(""));
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                }

                else if (newType.Value == PrimitiveBrushType.Sphere)
                {
                    GUILayout.Label("Sides", SabreGUILayout.GetForeStyle(), GUILayout.Width(30));
                    EditorGUILayout.PropertyField(sphereSideCountProp, new GUIContent(""));
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                }
            }

            if (GUILayout.Button("Reset Polygons"))
            {
                Undo.RecordObjects(targets, "Reset Polygons");
                foreach (var thisBrush in targets)
                {
                    if (overridenBrushType.HasValue)
                    {
                        ((PrimitiveBrush)thisBrush).BrushType = overridenBrushType.Value;
                    }
                    ((PrimitiveBrush)thisBrush).ResetPolygons();
                    ((PrimitiveBrush)thisBrush).Invalidate(true);
                }

                overridenBrushType = null;
            }
            GUILayout.EndHorizontal();

            if (GUILayout.Button("Shell"))
            {
                List <GameObject> newSelection = new List <GameObject>();
                foreach (var thisBrush in targets)
                {
                    GameObject newObject = ((PrimitiveBrush)thisBrush).Duplicate();
                    Polygon[]  polygons  = newObject.GetComponent <PrimitiveBrush>().GetPolygons();
                    VertexUtility.DisplacePolygons(polygons, -CurrentSettings.PositionSnapDistance);
                    Bounds newBounds = newObject.GetComponent <PrimitiveBrush>().GetBounds();
                    // Verify the new geometry
                    if (GeometryHelper.IsBrushConvex(polygons) &&
                        newBounds.GetSmallestExtent() > 0)
                    {
                        Undo.RegisterCreatedObjectUndo(newObject, "Shell");
                        newSelection.Add(newObject);
                    }
                    else
                    {
                        // Produced a concave brush, delete it and pretend nothing happened
                        GameObject.DestroyImmediate(newObject);
                        Debug.LogWarning("Could not shell " + thisBrush.name + " as shelled geometry would not be valid. Try lowering Pos Snapping and attempt Shell again.");
                    }
                }

                if (newSelection.Count > 0)
                {
                    Selection.objects = newSelection.ToArray();
                }
            }
        }