示例#1
0
        internal static CSGBrush CreateBrush(Transform parent, string brushName, ControlMesh controlMesh, Shape shape)
        {
            var gameObject = OperationsUtility.CreateGameObject(parent, brushName, false);

            if (!gameObject)
            {
                return(null);
            }
            var brush = gameObject.AddComponent <CSGBrush>();

            if (!brush)
            {
                return(null);
            }
            brush.ControlMesh = controlMesh;
            brush.Shape       = shape;
            if (brush.ControlMesh != null)
            {
                brush.ControlMesh.SetDirty();
            }
            if (brush.Shape != null)
            {
                ShapeUtility.EnsureInitialized(brush.Shape);
            }
            return(brush);
        }
示例#2
0
        protected bool GenerateBrushObjects(int brushObjectCount, bool inGridSpace = true)
        {
            Undo.IncrementCurrentGroup();
            undoGroupIndex = Undo.GetCurrentGroup();

            var lastUsedModel = SelectionUtility.LastUsedModel;

            if (!ModelTraits.IsModelEditable(lastUsedModel))
            {
                lastUsedModel = null;
            }
            var lastUsedModelTransform = !lastUsedModel ? null : lastUsedModel.transform;

            if (!lastUsedModelTransform ||
                !lastUsedModel.isActiveAndEnabled)
            {
                if (prevSelection != null && prevSelection.Length > 0)
                {
                    for (int i = 0; i < prevSelection.Length; i++)
                    {
                        UnityEngine.Object obj   = prevSelection[i];
                        CSGBrush           brush = obj as CSGBrush;
                        MonoBehaviour      mono  = obj as MonoBehaviour;
                        GameObject         go    = obj as GameObject;
                        if (!brush)
                        {
                            if (mono)
                            {
                                brush = mono.GetComponentInChildren <CSGBrush>();
                            }
                            if (go)
                            {
                                brush = go.GetComponentInChildren <CSGBrush>();
                            }
                        }

                        if (!brush)
                        {
                            continue;
                        }

                        if ((brush.gameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.NotEditable | HideFlags.DontSaveInBuild)) != 0)
                        {
                            continue;
                        }

                        if (brush.ChildData == null ||
                            brush.ChildData.ModelTransform == null)
                        {
                            continue;
                        }

                        var model = brush.ChildData.Model;
                        if (!model ||
                            !model.isActiveAndEnabled)
                        {
                            continue;
                        }

                        lastUsedModelTransform = brush.ChildData.ModelTransform;
                        break;
                    }
                }
            }

            if (generatedBrushes != null && generatedBrushes.Length > 0)
            {
                for (int i = generatedBrushes.Length - 1; i >= 0; i--)
                {
                    if (generatedBrushes[i])
                    {
                        continue;
                    }
                    ArrayUtility.RemoveAt(ref generatedBrushes, i);
                }
                for (int i = generatedGameObjects.Length - 1; i >= 0; i--)
                {
                    if (generatedGameObjects[i])
                    {
                        var brush = generatedGameObjects[i].GetComponentInChildren <CSGBrush>();
                        if (brush && ArrayUtility.Contains(generatedBrushes, brush))
                        {
                            continue;
                        }
                    }
                    ArrayUtility.RemoveAt(ref generatedGameObjects, i);
                }
            }

            if (generatedGameObjects == null ||
                generatedGameObjects.Length != brushObjectCount)
            {
                if (generatedBrushes != null && generatedBrushes.Length > 0)
                {
                    for (int i = 0; i < generatedBrushes.Length; i++)
                    {
                        InternalCSGModelManager.OnDestroyed(generatedBrushes[i]);
                        GameObject.DestroyImmediate(generatedBrushes[i]);
                    }
                }
                if (generatedGameObjects != null && generatedGameObjects.Length > 0)
                {
                    for (int i = 0; i < generatedGameObjects.Length; i++)
                    {
                        GameObject.DestroyImmediate(generatedGameObjects[i]);
                    }
                }

                if (parentGameObject != null)
                {
                    GameObject.DestroyImmediate(parentGameObject);
                }

                //DebugEditorWindow.PrintDebugInfo();

                if (lastUsedModelTransform == null)
                {
                    parentGameObject = OperationsUtility.CreateGameObject(lastUsedModelTransform, "Model", true);
                    InternalCSGModelManager.CreateCSGModel(parentGameObject);
                    parentModel = parentGameObject.GetComponent <CSGModel>();
                    Undo.RegisterCreatedObjectUndo(parentGameObject, "Created model");

                    if (brushObjectCount > 1)
                    {
                        operationGameObject = OperationsUtility.CreateGameObject(parentGameObject.transform, "Operation", true);
                        var transform = operationGameObject.transform;
                        SetBrushTransformation(transform);

                        var operation = operationGameObject.AddComponent <CSGOperation>();
                        if (CurrentCSGOperationType != invalidCSGOperationType)
                        {
                            operation.OperationType = CurrentCSGOperationType;
                        }
                        operation.HandleAsOne = true;
                        Undo.RegisterCreatedObjectUndo(operationGameObject, "Created operation");
                        parentTransform = operationGameObject.transform;
                    }
                    else
                    {
                        parentTransform = parentGameObject.transform;
                    }
                }
                else
                if (brushObjectCount > 1)
                {
                    parentModel      = lastUsedModelTransform.GetComponent <CSGModel>();
                    parentGameObject = OperationsUtility.CreateGameObject(lastUsedModelTransform, "Brushes", true);
                    var transform = parentGameObject.transform;
                    SetBrushTransformation(transform);

                    operationGameObject = parentGameObject;
                    var operation = operationGameObject.AddComponent <CSGOperation>();
                    if (CurrentCSGOperationType != invalidCSGOperationType)
                    {
                        operation.OperationType = CurrentCSGOperationType;
                    }
                    operation.HandleAsOne = true;
                    parentTransform       = operationGameObject.transform;
                    Undo.RegisterCreatedObjectUndo(parentGameObject, "Created brush");
                }
                else
                {
                    parentGameObject    = null;
                    operationGameObject = null;
                    parentTransform     = lastUsedModelTransform;
                    parentModel         = lastUsedModelTransform.GetComponent <CSGModel>();
                }


                generatedGameObjects = new GameObject[brushObjectCount];
                generatedBrushes     = new CSGBrush[brushObjectCount];
                for (int p = 0; p < brushObjectCount; p++)
                {
                    string name;
                    if (brushObjectCount == 1)
                    {
                        name = "Brush";
                    }
                    else
                    {
                        name = "Brush (" + p + ")";
                    }
                    var gameObject = OperationsUtility.CreateGameObject(parentTransform, name, false);
                    gameObject.SetActive(false);

                    var brushComponent = gameObject.AddComponent <CSGBrush>();

                    if (operationGameObject == null)
                    {
                        if (CurrentCSGOperationType != invalidCSGOperationType)
                        {
                            brushComponent.OperationType = CurrentCSGOperationType;
                        }
                        operationGameObject = gameObject;
                        var transform = gameObject.transform;
                        SetBrushTransformation(transform);
                    }

                    generatedBrushes[p]             = brushComponent;
                    generatedBrushes[p].ControlMesh = new ControlMesh();
                    generatedBrushes[p].Shape       = new Shape();
                    Undo.RegisterCreatedObjectUndo(gameObject, "Created brush");
                    generatedGameObjects[p] = gameObject;
                }
                //InternalCSGModelManager.Refresh(forceHierarchyUpdate: true);
                // brushes not registered at this point!??


                //DebugEditorWindow.PrintDebugInfo();
                //Selection.objects = generatedGameObjects;
            }
            else
            {
                UpdateBrushPosition();
            }

            return(generatedBrushes != null && generatedBrushes.Length > 0 && generatedBrushes.Length == brushObjectCount);
        }
示例#3
0
        public static bool HandleSceneKeyUp(IEditMode tool, bool checkForTextEditing = true)
        {
            if (EditorGUIUtility.editingTextField && checkForTextEditing)
            {
                return(false);
            }

            if (Keys.MakeSelectedPassThroughKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.SetPassThroughOnSelected(); return(true);
            }
            if (Keys.MakeSelectedAdditiveKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Additive); return(true);
            }
            if (Keys.MakeSelectedSubtractiveKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Subtractive); return(true);
            }
            if (Keys.MakeSelectedIntersectingKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                OperationsUtility.ModifyOperationsOnSelected(CSGOperationType.Intersecting); return(true);
            }

            if (Keys.ToggleSelectedObjectVisibilityKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.ToggleSelectedObjectVisibility(); return(true);
            }
            if (Keys.QuickHideSelectedObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.HideSelectedObjects(); return(true);
            }
            if (Keys.QuickHideUnselectedObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.HideUnselectedObjects(); return(true);
            }
            if (Keys.UnHideAllObjectsKey.IsKeyPressed() && tool.UsesUnitySelection)
            {
                SelectionUtility.UnHideAll(); return(true);
            }
            if (Keys.CancelActionKey.IsKeyPressed())
            {
                SelectionUtility.DeselectAll(); return(true);
            }

            if (Keys.HalfGridSizeKey.IsKeyPressed())
            {
                GridUtility.HalfGridSize(); return(true);
            }
            if (Keys.DoubleGridSizeKey.IsKeyPressed())
            {
                GridUtility.DoubleGridSize(); return(true);
            }
            if (Keys.ToggleShowGridKey.IsKeyPressed())
            {
                GridUtility.ToggleShowGrid(); return(true);
            }
            if (Keys.ToggleSnappingKey.IsKeyPressed())
            {
                GridUtility.ToggleSnapToGrid(); return(true);
            }
            return(false);
        }
示例#4
0
        public void DragPerform(SceneView sceneView)
        {
            try
            {
                InternalCSGModelManager.skipCheckForChanges = true;
                if (visualDragGameObject == null)
                {
                    CreateVisualObjects(sceneView != null);
                }

                if (sceneView && haveNoParent && !containsModel)
                {
                    var model = SelectionUtility.LastUsedModel;
                    if (!model)
                    {
                        model = OperationsUtility.CreateModelInstanceInScene(selectModel: false);
                        InternalCSGModelManager.EnsureInitialized(model);
                        InternalCSGModelManager.CheckTransformChanged();
                        InternalCSGModelManager.OnHierarchyModified();
                    }
                    var parent = model.transform;

                    int counter = 0;
                    foreach (var obj in visualDragGameObject)
                    {
                        if (!obj)
                        {
                            continue;
                        }
                        if (obj.activeSelf)
                        {
                            obj.transform.SetParent(parent, false);
                            obj.transform.SetSiblingIndex(hoverSiblingIndex + counter);
                            counter++;
                        }
                    }
                }

                if (visualDragGameObject != null)
                {
                    var selection = new List <GameObject>();
                    for (int i = visualDragGameObject.Count - 1; i >= 0; i--)
                    {
                        if (!visualDragGameObject[i])
                        {
                            continue;
                        }
                        if (visualDragGameObject[i].activeSelf)
                        {
                            Undo.RegisterCreatedObjectUndo(visualDragGameObject[i], "Instantiated prefab");
                            selection.Add(visualDragGameObject[i]);
                        }
                        else
                        {
                            GameObject.DestroyImmediate(visualDragGameObject[i]);
                        }
                    }
                    visualDragGameObject = null;

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

                if (sceneView)
                {
                    for (int i = 0; i < SceneView.sceneViews.Count; i++)
                    {
                        var sceneview = SceneView.sceneViews[i] as SceneView;
                        if (!sceneview)
                        {
                            continue;
                        }

                        if (sceneview.camera.pixelRect.Contains(Event.current.mousePosition))
                        {
                            sceneview.Focus();
                        }
                    }
                }
                visualDragGameObject = null;

                InternalCSGModelManager.CheckForChanges(forceHierarchyUpdate: true);
            }
            finally
            {
                InternalCSGModelManager.skipCheckForChanges = false;
                RealtimeCSG.CSGGrid.ForcedGridCenter        = prevForcedGridCenter;
                RealtimeCSG.CSGGrid.ForcedGridRotation      = prevForcedGridRotation;
                RealtimeCSG.CSGGrid.ForceGrid = false;
            }
        }
示例#5
0
        protected void GenerateBrushObjects(int brushObjectCount, bool inGridSpace = true)
        {
            Undo.IncrementCurrentGroup();
            undoGroupIndex = Undo.GetCurrentGroup();

            var lastUsedModel          = SelectionUtility.LastUsedModel;
            var lastUsedModelTransform = !lastUsedModel ? null : lastUsedModel.transform;

            if (!lastUsedModelTransform ||
                !lastUsedModel.isActiveAndEnabled)
            {
                if (prevSelection != null && prevSelection.Length > 0)
                {
                    for (int i = 0; i < prevSelection.Length; i++)
                    {
                        UnityEngine.Object obj   = prevSelection[i];
                        CSGBrush           brush = obj as CSGBrush;
                        MonoBehaviour      mono  = obj as MonoBehaviour;
                        GameObject         go    = obj as GameObject;
                        if (!brush)
                        {
                            if (mono)
                            {
                                brush = mono.GetComponentInChildren <CSGBrush>();
                            }
                            if (go)
                            {
                                brush = go.GetComponentInChildren <CSGBrush>();
                            }
                        }

                        if (!brush)
                        {
                            continue;
                        }

                        if ((brush.gameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.NotEditable | HideFlags.DontSaveInBuild)) != 0)
                        {
                            continue;
                        }

                        var brush_cache = InternalCSGModelManager.GetBrushCache(brush);
                        if (brush_cache == null ||
                            brush_cache.childData == null ||
                            brush_cache.childData.ModelTransform == null)
                        {
                            continue;
                        }

                        var model = brush_cache.childData.Model;
                        if (!model ||
                            !model.isActiveAndEnabled)
                        {
                            continue;
                        }

                        lastUsedModelTransform = brush_cache.childData.ModelTransform;
                        break;
                    }
                }
            }

            if (generatedGameObjects == null ||
                generatedGameObjects.Length != brushObjectCount)
            {
                if (generatedBrushes != null && generatedBrushes.Length > 0)
                {
                    for (int i = 0; i < generatedBrushes.Length; i++)
                    {
                        InternalCSGModelManager.OnDestroyed(generatedBrushes[i]);
                        GameObject.DestroyImmediate(generatedBrushes[i]);
                    }
                }
                if (generatedGameObjects != null && generatedGameObjects.Length > 0)
                {
                    for (int i = 0; i < generatedGameObjects.Length; i++)
                    {
                        GameObject.DestroyImmediate(generatedGameObjects[i]);
                    }
                }
                if (parentGameObject != null)
                {
                    GameObject.DestroyImmediate(parentGameObject);
                }

#if DEMO
                if (CSGBindings.BrushesAvailable() < brushObjectCount)
                {
                    Debug.Log("Demo brush limit hit (" + CSGBindings.BrushesAvailable() + " available, " + brushObjectCount + " required), for the ability to create more brushes please purchase Realtime-CSG");
                    return;
                }
#endif
                if (lastUsedModelTransform == null)
                {
                    parentGameObject = OperationsUtility.CreateGameObject(lastUsedModelTransform, "Model", true);
                    InternalCSGModelManager.CreateCSGModel(parentGameObject);
                    parentModel = parentGameObject.GetComponent <CSGModel>();
                    Undo.RegisterCreatedObjectUndo(parentGameObject, "Created model");

                    if (brushObjectCount > 1)
                    {
                        operationGameObject = OperationsUtility.CreateGameObject(parentGameObject.transform, "Operation", true);
                        var transform = operationGameObject.transform;
                        SetBrushTransformation(transform);

                        var operation = operationGameObject.AddComponent <CSGOperation>();
                        if (CurrentCSGOperationType != invalidCSGOperationType)
                        {
                            operation.OperationType = CurrentCSGOperationType;
                        }
                        operation.HandleAsOne = true;
                        Undo.RegisterCreatedObjectUndo(operationGameObject, "Created operation");
                        parentTransform = operationGameObject.transform;
                    }
                    else
                    {
                        parentTransform = parentGameObject.transform;
                    }
                }
                else
                if (brushObjectCount > 1)
                {
                    parentModel      = lastUsedModelTransform.GetComponent <CSGModel>();
                    parentGameObject = OperationsUtility.CreateGameObject(lastUsedModelTransform, "Brushes", true);
                    var transform = parentGameObject.transform;
                    SetBrushTransformation(transform);

                    operationGameObject = parentGameObject;
                    var operation = operationGameObject.AddComponent <CSGOperation>();
                    if (CurrentCSGOperationType != invalidCSGOperationType)
                    {
                        operation.OperationType = CurrentCSGOperationType;
                    }
                    operation.HandleAsOne = true;
                    parentTransform       = operationGameObject.transform;
                    Undo.RegisterCreatedObjectUndo(parentGameObject, "Created brush");
                }
                else
                {
                    parentGameObject    = null;
                    operationGameObject = null;
                    parentTransform     = lastUsedModelTransform;
                    parentModel         = lastUsedModelTransform.GetComponent <CSGModel>();
                }


                generatedGameObjects = new GameObject[brushObjectCount];
                generatedBrushes     = new CSGBrush[brushObjectCount];
                for (int p = 0; p < brushObjectCount; p++)
                {
                    string name;
                    if (brushObjectCount == 1)
                    {
                        name = "Brush";
                    }
                    else
                    {
                        name = "Brush (" + p + ")";
                    }
                    var gameObject = OperationsUtility.CreateGameObject(parentTransform, name, false);
                    gameObject.SetActive(false);

                    var brushComponent = gameObject.AddComponent <CSGBrush>();

                    if (operationGameObject == null)
                    {
                        if (CurrentCSGOperationType != invalidCSGOperationType)
                        {
                            brushComponent.OperationType = CurrentCSGOperationType;
                        }
                        operationGameObject = gameObject;
                        var transform = gameObject.transform;
                        SetBrushTransformation(transform);
                    }

                    generatedBrushes[p]             = brushComponent;
                    generatedBrushes[p].ControlMesh = new ControlMesh();
                    generatedBrushes[p].Shape       = new Shape();
                    Undo.RegisterCreatedObjectUndo(gameObject, "Created brush");
                    generatedGameObjects[p] = gameObject;
                }
                //Selection.objects = generatedGameObjects;
            }
            else
            {
                UpdateBrushPosition();
            }
        }