Exemplo n.º 1
0
    // Handle events when mouse point enter or leaves the SceneView
    void ProcessMouseEnterLeaveSceneview()
    {
        // If mouse enters SceneView window, show visualizer
        if (Event.current.type == EventType.MouseEnterWindow)
        {
            MAST_Placement_Visualizer.SetVisualizerVisibility(true);
        }

        // If mouse leaves SceneView window
        else if (Event.current.type == EventType.MouseLeaveWindow)
        {
            // Hide visualizer
            MAST_Placement_Visualizer.SetVisualizerVisibility(false);

            // Stop any drawing
            if (drawing)
            {
                drawing = false;
            }

            // Stop any painting
            if (painting)
            {
                MAST_Placement_PaintArea.DeletePaintArea();
                painting = false;
            }

            // Stop any erasing
            if (erasing)
            {
                erasing = false;
            }
        }
    }
Exemplo n.º 2
0
    // ---------------------------------------------------------------------------
    // Clean-up
    // ---------------------------------------------------------------------------
    private void CleanUpInterface()
    {
        //Debug.Log("Cleaning Up Interface");

        // Remove SceneView delegate
        #if UNITY_2019_1_OR_NEWER
        SceneView.duringSceneGui -= this.OnScene;
        #else
        SceneView.onSceneGUIDelegate -= this.OnScene;
        #endif

        // Delete placement grid
        MAST_Grid_Manager.DestroyGrid();

        // Deselect palette item and delete visualizer
        MAST_Palette.selectedItemIndex = -1;
        MAST_Placement_Visualizer.RemoveVisualizer();

        // Deselect draw tool and change placement mode to none
        MAST_Settings.gui.toolbar.selectedDrawToolIndex = -1;
        MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.None);

        // Cancel any drawing or painting
        drawing  = false;
        painting = false;
        erasing  = false;
        MAST_Placement_PaintArea.DeletePaintArea();
    }
Exemplo n.º 3
0
    public static GameObject PlacePrefabInScene()
    {
        // Make sure target parent is referenced
        ReferenceTargetParent();

        if (MAST_Placement_Visualizer.visualizerOnGrid && MAST_Placement_Visualizer.GetGameObject() != null &&
            MAST_Placement_Visualizer.GetGameObject() != null)
        {
            // If drawing continuous and an object was just drawn here, exit without drawing
            if (MAST_Placement_Interface.placementMode == MAST_Placement_Interface.PlacementMode.DrawContinuous)
            {
                if (lastPosition == MAST_Placement_Visualizer.GetGameObject().transform.position)
                {
                    return(null);
                }
            }

            // If GameObject already exists here, exit without placing the block
            if (GameObjectAlreadyHere())
            {
                return(null);
            }

            // Instantiate the prefab
            GameObject newPrefab = (GameObject)PrefabUtility.InstantiatePrefab(MAST_Palette.GetSelectedPrefab());

            // Correct GameObject transform and name "to remove (clone)"
            newPrefab.transform.rotation   = MAST_Placement_Visualizer.GetGameObject().transform.rotation;
            newPrefab.transform.localScale = MAST_Placement_Visualizer.GetGameObject().transform.localScale;
            newPrefab.transform.position   = MAST_Placement_Visualizer.GetGameObject().transform.position;
            newPrefab.name = MAST_Palette.GetSelectedPrefab().name;

            // Make new prefab child of the target parent
            newPrefab.transform.parent = targetParent.transform;

            // Add MAST script to GameObject if it was missing it
            if (!newPrefab.GetComponent <MAST_Prefab_Component>())
            {
                newPrefab.AddComponent <MAST_Prefab_Component>();
            }

            // Randomize the seed after placement
            if (MAST_Settings.gui.toolbar.selectedDrawToolIndex == 3)
            {
                MAST_Placement_Randomizer.GenerateNewRandomSeed();
            }

            // Save this GameObject position
            lastPosition = MAST_Placement_Visualizer.GetGameObject().transform.position;

            // Make this an Undo point, just after placing the prefab
            Undo.RegisterCreatedObjectUndo(newPrefab, "Placed new Prefab");

            // Return with newly created GameObject
            return(newPrefab);
        }

        return(null);
    }
    // Change visualizer to eraser
    public static void ChangePrefabToEraser()
    {
        // Remove any existing visualizer
        MAST_Placement_Visualizer.RemoveVisualizer();

        // Create a new visualizer with eraser
        MAST_Placement_Visualizer.CreateVisualizer(MAST_Asset_Loader.GetEraserPrefab());
    }
    // Complete paint area
    public static void CompletePaintArea()
    {
        // Get current mouse position on grid
        Vector3 paintAreaEnd = MAST_Placement_Helper.GetPositionOnGridClosestToMousePointer();

        paintAreaEnd.y = MAST_Settings.gui.grid.gridHeight *
                         MAST_Settings.gui.grid.xzUnitSize + MAST_Const.grid.yOffsetToAvoidTearing;

        // If selected Prefab can be scaled
        if (MAST_Placement_Helper.GetScalable())
        {
            // Place the prefab
            GameObject placedPrefab = MAST_Placement_Place.PlacePrefabInScene();

            // Move prefab centerpoint between both paint areas
            placedPrefab.transform.position = (paintAreaStart + paintAreaEnd) / 2;

            // Scale prefab X and Z to match paint area
            Vector3 scale = new Vector3(
                Mathf.Abs(paintAreaStart.x - paintAreaEnd.x) + 1f,
                1,
                Mathf.Abs(paintAreaStart.z - paintAreaEnd.z) + 1f);

            placedPrefab.transform.localScale = scale;
        }

        // If selected Prefab cannot be scaled
        else
        {
            // Get base of rows and columns "lowest value"
            float xBase = paintAreaStart.x < paintAreaEnd.x ? paintAreaStart.x : paintAreaEnd.x;
            float zBase = paintAreaStart.z < paintAreaEnd.z ? paintAreaStart.z : paintAreaEnd.z;

            // Get count of rows and columns in paint area
            int xCount = (int)(Mathf.Abs(paintAreaStart.x - paintAreaEnd.x) / MAST_Settings.gui.grid.xzUnitSize);
            int zCount = (int)(Mathf.Abs(paintAreaStart.z - paintAreaEnd.z) / MAST_Settings.gui.grid.xzUnitSize);

            // Loop through each grid space in the area
            for (int x = 0; x <= xCount; x++)
            {
                for (int z = 0; z <= zCount; z++)
                {
                    // Set visualizer position
                    MAST_Placement_Visualizer.GetGameObject().transform.position =
                        new Vector3(xBase + (x * MAST_Settings.gui.grid.xzUnitSize),
                                    MAST_Settings.gui.grid.gridHeight * MAST_Settings.gui.grid.xzUnitSize,
                                    zBase + (z * MAST_Settings.gui.grid.xzUnitSize));

                    // Add Prefab to scene
                    MAST_Placement_Place.PlacePrefabInScene();
                }
            }
        }

        // Delete painting area
        DeletePaintArea();
    }
    // Change visualizer prefab when a new item is selected in the palette menu
    public static void ChangeSelectedPrefab()
    {
        // Get reference to the MAST script attached to the GameObject
        MAST_Placement_Helper.mastScript =
            MAST_Palette.GetSelectedPrefab().GetComponent <MAST_Prefab_Component>();

        // Remove any existing visualizer
        MAST_Placement_Visualizer.RemoveVisualizer();

        // Create a new visualizer
        MAST_Placement_Visualizer.CreateVisualizer(MAST_Palette.GetSelectedPrefab());
    }
    // Check if a GameObject already exists here and if neither can be placed inside others
    private static bool GameObjectAlreadyHere()
    {
        // Get array containing all Colliders within 1.5f square box at placement position
        Collider[] colliders =
            Physics.OverlapBox(
                MAST_Placement_Visualizer.GetGameObject().transform.position,
                new Vector3(0.75f, 0.75f, 0.75f));

        // Loop through each GameObject inside or colliding with this OverlapBox
        foreach (Collider collider in colliders)
        {
            // If the nearby GameObject has a parent
            if (collider.gameObject.transform.parent != null)
            {
                // Get Parent GameObject for the GameObject containing this Collider
                GameObject nearObject = collider.gameObject.transform.parent.gameObject;

                // If near GameObject is not the visualizer itself
                if (nearObject.name != "MAST_Visualizer")
                {
                    // If the placed GameObject shares the same Position as the near GameObject
                    if (nearObject.transform.position ==
                        MAST_Placement_Visualizer.GetGameObject().transform.position)
                    {
                        //TODO:  Add rotation check as well

                        // Get the MAST Component script of the near GameObject
                        MAST_Prefab_Component nearComponent = nearObject.GetComponent <MAST_Prefab_Component>();

                        // If neither the GameObject nor placed GameObject can be placed inside others
                        if (!MAST_Placement_Helper.GetPlaceInsideOthers() && !nearComponent.placeInsideOthers)
                        {
                            // Return true "Not safe to place"
                            return(true);
                        }
                    }
                }
            }
        }
        // Return false "Safe to place"
        return(false);
    }
    // Rotate the visualizer or whatever object is selected
    public static GameObject RotateObject()
    {
        GameObject gameObject = GetObjectToManipulate(MAST_Placement_Visualizer.GetGameObject());

        if (gameObject != null)
        {
            // Make this an Undo point, just before rotating the existing object
            if (allowUndoRegistering)
            {
                Undo.RegisterCompleteObjectUndo(gameObject.transform, "Rotated GameObject");
                allowUndoRegistering = false;
            }


            // TODO:  Add code to see if local space rotation allows this rotation
            //        This is different from world space


            // OnScene Change Target Axis Icon Button
            switch (rotateAxis)
            {
            case Axis.X:
                gameObject.transform.Rotate(MAST_Placement_Helper.GetRotationFactor().x, 0f, 0f, Space.World);
                break;

            case Axis.Y:
                gameObject.transform.Rotate(0f, MAST_Placement_Helper.GetRotationFactor().y, 0f, Space.World);
                break;

            case Axis.Z:
                gameObject.transform.Rotate(0f, 0f, MAST_Placement_Helper.GetRotationFactor().z, Space.World);
                break;
            }

            // Remember this rotation for future prefab placement
            currentRotation = gameObject.transform.rotation;
        }

        // Return rotated GameObject
        return(gameObject);
    }
Exemplo n.º 9
0
    // ---------------------------------------------------------------------------
    // Clean-up
    // ---------------------------------------------------------------------------
    private void CleanUpInterface()
    {
        //Debug.Log("Cleaning Up Interface");

        // Delete placement grid
        MAST_Grid_Manager.DestroyGrid();

        // Deselect palette item and delete visualizer
        MAST_Palette.selectedItemIndex = -1;
        MAST_Placement_Visualizer.RemoveVisualizer();

        // Deselect draw tool and change placement mode to none
        MAST_Settings.gui.toolbar.selectedDrawToolIndex = -1;
        MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.None);

        // Cancel any drawing or painting
        drawing  = false;
        painting = false;
        erasing  = false;
        MAST_Placement_PaintArea.DeletePaintArea();
    }
    public static void ErasePrefab()
    {
        // Get array containing all Colliders within eraser
        Collider[] colliders =
            Physics.OverlapBox(
                MAST_Placement_Visualizer.GetGameObject().transform.position +
                new Vector3(0f, 0.35f, 0f), new Vector3(0.4f, 0.4f, 0.4f));

        // Loop through each GameObject inside or colliding with this OverlapBox
        foreach (Collider collider in colliders)
        {
            // Use try/catch, incase this collider's GameObject is already destroyed
            try
            {
                // If the nearby GameObject has a parent
                if (collider.gameObject.transform.parent != null)
                {
                    // Get Parent GameObject for the GameObject containing this Collider
                    GameObject objectToDelete = GetPrefabParent(collider.gameObject.transform).gameObject;

                    // If a GameObject placed with MAST was found
                    if (objectToDelete != null)
                    {
                        // If near GameObject is not the visualizer itself
                        if (objectToDelete.name != "MAST_Visualizer" &&
                            objectToDelete.name != MAST_Const.grid.defaultName &&
                            objectToDelete.name != MAST_Const.grid.defaultParentName)
                        {
                            // Erase it, but allow an undo
                            Undo.DestroyObjectImmediate(objectToDelete);
                        }
                    }
                }
            }
            catch
            {
                // Do nothing since this collider's GameObject was already destroyed
            }
        }
    }
    // Start paint area
    public static void StartPaintArea()
    {
        if (MAST_Placement_Visualizer.GetGameObject() != null)
        {
            // Set painting area to true
            paintingArea = true;

            // Record paint area start location
            paintAreaStart   = MAST_Placement_Visualizer.GetGameObject().transform.position;
            paintAreaStart.y = MAST_Settings.gui.grid.gridHeight *
                               MAST_Settings.gui.grid.xzUnitSize + MAST_Const.grid.yOffsetToAvoidTearing;

            // Create new Paint Area Visualizer
            paintAreaVisualizer = GameObject.CreatePrimitive(PrimitiveType.Plane);
            paintAreaVisualizer.transform.position = new Vector3(0f, 0f, 0f);
            paintAreaVisualizer.name = "MAST_Paint_Area_Visualizer";

            // Configure Paint Area Visualizer MeshRenderer
            MeshRenderer paintAreaMeshRenderer = paintAreaVisualizer.GetComponent <MeshRenderer>();
            paintAreaMeshRenderer.lightProbeUsage      = UnityEngine.Rendering.LightProbeUsage.Off;
            paintAreaMeshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
            paintAreaMeshRenderer.shadowCastingMode    = UnityEngine.Rendering.ShadowCastingMode.Off;
            paintAreaMeshRenderer.receiveShadows       = false;

            // Configure Paint Area Visualizer Material
            if (paintAreaMaterial == null)
            {
                paintAreaMaterial = MAST_Asset_Loader.GetPaintAreaMaterial();
            }
            paintAreaMeshRenderer.material = paintAreaMaterial;

            // Hide the Paint Area Visualizer in the hierarchy
            paintAreaVisualizer.hideFlags = HideFlags.HideInHierarchy;

            // Update the paint area
            UpdatePaintArea();
        }
    }
Exemplo n.º 12
0
    // Handle object placement
    private void ObjectPlacement()
    {
        // Get mouse events for object placement when in the Scene View
        Event currentEvent = Event.current;

        // Change position of visualizer
        MAST_Placement_Visualizer.UpdateVisualizerPosition();

        switch (MAST_Settings.gui.toolbar.selectedDrawToolIndex)
        {
        // Draw single tool
        case 0:
        // Randomizer tool
        case 3:
            // If left mouse button was clicked
            if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
            {
                // Keep mouseclick from selecting other objects
                GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
                Event.current.Use();

                // Place selected prefab on grid
                MAST_Placement_Place.PlacePrefabInScene();
            }
            break;

        // Draw continuous tool
        case 1:
            // If not already drawing and the left mouse button pressed
            if (!drawing)
            {
                if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
                {
                    // Start drawing
                    drawing = true;

                    // Keep mouseclick from selecting other objects
                    GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
                    Event.current.Use();
                }
            }

            // If drawing and left mouse button not released
            if (drawing)
            {
                // Place selected prefab on grid
                MAST_Placement_Place.PlacePrefabInScene();

                // If left mouse button released
                if (currentEvent.type == EventType.MouseUp && currentEvent.button == 0)
                {
                    drawing = false;
                }
            }
            break;

        // Paint area tool
        case 2:
            // If not already painting and the left mouse button pressed
            if (!painting)
            {
                if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
                {
                    // Start drawing
                    painting = true;

                    // Start paint area at current mouse location
                    MAST_Placement_PaintArea.StartPaintArea();

                    // Keep mouseclick from selecting other objects
                    GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
                    Event.current.Use();
                }
            }

            // If drawing and left mouse button not released
            if (painting)
            {
                // Update the paint area as the mouse moves
                MAST_Placement_PaintArea.UpdatePaintArea();

                // If left mouse button released
                if (currentEvent.type == EventType.MouseUp && currentEvent.button == 0)
                {
                    MAST_Placement_PaintArea.CompletePaintArea();
                    painting = false;
                }
            }
            break;

        // Erase tool
        case 4:
            // If not already erasing and the left mouse button pressed
            if (!erasing)
            {
                if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
                {
                    // Start drawing
                    erasing = true;

                    // Keep mouseclick from selecting other objects
                    GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
                    Event.current.Use();
                }
            }

            // If erasing and left mouse button not released
            if (erasing)
            {
                // Place selected prefab on grid
                MAST_Placement_Interface.ErasePrefab();

                // If left mouse button released
                if (currentEvent.type == EventType.MouseUp && currentEvent.button == 0)
                {
                    erasing = false;
                }
            }
            break;
        }
    }
// ---------------------------------------------------------------------------
    #region Change Placement Mode
// ---------------------------------------------------------------------------
    public static void ChangePlacementMode(PlacementMode newPlacementMode)
    {
        // Get new selected Draw Tool
        placementMode = newPlacementMode;

        // Remove any previous visualizer
        MAST_Placement_Visualizer.RemoveVisualizer();

        // --------------------------------
        // Create Visualizer
        // --------------------------------

        // If changed tool to Nothing or Eraser
        if (placementMode == PlacementMode.None || placementMode == PlacementMode.Erase)
        {
            // Deselect any item in the palette
            MAST_Palette.selectedItemIndex = -1;

            // If changed tool to Eraser
            if (placementMode == PlacementMode.Erase)
            {
                // Create eraser visualizer
                ChangePrefabToEraser();
            }

            // If changed tool to Nothing, remove the visualizer
            else
            {
                MAST_Placement_Visualizer.RemoveVisualizer();
            }
        }

        // If changed tool to Draw Single, Draw Continuous, Paint Area, or Randomizer
        else
        {
            // If a palette item is selected
            if (MAST_Palette.selectedItemIndex != -1)
            {
                // Create visualizer from selected item in the palette
                ChangeSelectedPrefab();

                // If changed tool to Randomizer
                if (placementMode == PlacementMode.Randomize)
                {
                    // Make a new random seed
                    MAST_Placement_Randomizer.GenerateNewRandomSeed();
                }
            }
        }

        // If Draw Continuous nor Paint Area tools are selected
        if (placementMode != PlacementMode.DrawContinuous &&
            placementMode != PlacementMode.PaintArea)
        {
            // Delete last saved position
            MAST_Placement_Place.lastPosition = Vector3.positiveInfinity;

            // Remove any paint area visualization
            MAST_Placement_PaintArea.DeletePaintArea();
        }
    }