Exemplo n.º 1
0
 private void OnDrawPath(string msg, object extra = null)
 {
     MyGrid.Children.Clear();
     GridTool.Draw(MyGrid);
     DrawPath(MyGrid);
     MyCircle.Children.Clear();
     GridTool.Paint(MyCircle);
 }
Exemplo n.º 2
0
 private void MainWindow_Resize(object sender, EventArgs e)
 {
     MyGrid.Children.Clear();
     GridTool.Draw(MyGrid);
     DrawPath(MyGrid);
     MyCircle.Children.Clear();
     GridTool.Paint(MyCircle);
 }
Exemplo n.º 3
0
    static void ToggleGrid()
    {
        // Calls the OnToggle methods in both this class and the GridTool class.
        OnToggle();
        GridTool.OnToggle();

        // Repaints the scene view.
        SceneView.RepaintAll();
    }
Exemplo n.º 4
0
 private void OnDrawCircle(string msg, object extra = null)
 {
     MyCircle.Children.Clear();
     GridTool.DrawCircle(MyCircle);
 }
Exemplo n.º 5
0
    /// <summary>
    /// Updates the preview ghost.
    /// </summary>
    /// <param name="ray">Mouse position to world ray.</param>
    private void UpdatePreview(Ray ray)
    {
        // Repaints the sceneview.
        SceneView.RepaintAll();

        RaycastHit hit;

        // If the ray hits anything in the direction the mouse was pointing
        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            mousePosition = hit.point;

            // If the grid is enabled and snapToGrid is true
            if (GridTool.EnableGridTool && GridTool.SnapObjectToGrid && snapToGrid)
            {
                #region Grid Snapping
                // Finds the closest snap position.
                Vector3 snapPos = GridTool.SnapToGrid(mousePosition);
                // Creates a local variable and saves the mousePosition in it.
                Vector3 tempPos = mousePosition;
                Vector3 dir     = Vector3.zero;

                // Switches on snappingPlane
                switch (snappingPlane)
                {
                case SnapDirections.XY:
                    // Saves the correct coordinats in the tempPos variable.
                    tempPos.x = snapPos.x;
                    tempPos.y = snapPos.y;
                    // Sets the dir variable.
                    dir = Vector3.forward;
                    break;

                case SnapDirections.XZ:
                    // Saves the correct coordinats in the tempPos variable.
                    tempPos.x = snapPos.x;
                    tempPos.z = snapPos.z;
                    // Sets the dir variable.
                    dir = Vector3.up;
                    break;

                case SnapDirections.YZ:
                    // Saves the correct coordinats in the tempPos variable.
                    tempPos.y = snapPos.y;
                    tempPos.z = snapPos.z;
                    // Sets the dir variable.
                    dir = Vector3.right;
                    break;
                }

                // Casts a ray from the tempPos in the direction of dir and as long as GridTool.IncrementSize.
                if (Physics.Raycast(tempPos, dir, out hit, GridTool.IncrementSize))
                {
                    // If the ray hits something the calculate rotation method is called.
                    CalculateRotation(hit);

                    // Switch on snappingPlane
                    switch (snappingPlane)
                    {
                    case SnapDirections.XY:
                        // Sets the correct value for the z coordinate.
                        tempPos.z = hit.point.z;
                        break;

                    case SnapDirections.XZ:
                        // Sets the correct value for the y coordinate.
                        tempPos.y = hit.point.y;
                        break;

                    case SnapDirections.YZ:
                        // Sets the correct value for the x coordinate.
                        tempPos.x = hit.point.x;
                        break;
                    }
                }
                // If the above ray doesn't hit anything the ray is cast in the opposite direction.
                else if (Physics.Raycast(tempPos, -dir, out hit, GridTool.IncrementSize))
                {
                    // The same as above happen.
                    CalculateRotation(hit);

                    switch (snappingPlane)
                    {
                    case SnapDirections.XY:
                        tempPos.z = hit.point.z;
                        break;

                    case SnapDirections.XZ:
                        tempPos.y = hit.point.y;
                        break;

                    case SnapDirections.YZ:
                        tempPos.x = hit.point.x;
                        break;
                    }
                }

                // Sets the position of the preview to the tempPos.
                previewPosition = tempPos;
                #endregion
            }
            // If no snapping is required.
            else
            {
                // The position of the preview is set to mousePosition.
                previewPosition = mousePosition;

                // The rotation is calculated for the preview.
                CalculateRotation(hit);
            }
        }
        // If no surface is hit with the mouse.
        else
        {
            // The preview position is set to the mousePosition with the distance distanceToObject from the camera.
            previewPosition = ray.origin + (ray.direction * distanceToObject);

            // If the variable keepNativeRotation is true the preview will always have the same rotation.
            if (keepNativeRotation)
            {
                // Sets the preview rotatation to the native rotation of the object.
                previewRotation = selectedObject.transform.rotation;
            }
            // If the variable randomizeLocalYRotation is true the object will get a random rotation every time it is placed.
            else if (randomizeLocalYRotation)
            {
                previewRotation = selectedObject.transform.rotation * Quaternion.Euler(0, currentRandomRotation, 0);
            }
            // If none of the above is true the object will get the specified local y rotation. This is by default set to 0.
            else
            {
                previewRotation = selectedObject.transform.rotation * Quaternion.Euler(localRotation.x, localRotation.y, localRotation.z);
            }
        }
    }
Exemplo n.º 6
0
    static void OnBottomToolbarGUI(int windowID)
    {
        // Begins a horizontal control group.
        GUILayout.BeginHorizontal();

        // Sets the GUI skin.
        GUI.skin = guiSkin;

        // Starts a change check.
        EditorGUI.BeginChangeCheck();

        // Creates toggle buttons for each of the grid settings.
        GridTool.ShowGrid = CreateGUIToggle(new Rect(5, 5, 30, 30), GridTool.ShowGrid, toggleGridTexture, "Draw grid.");

        GridTool.XYGrid = CreateGUIToggle(new Rect(55, 5, 30, 30), GridTool.XYGrid, toggleXYGridTexture, "Draw grid on the XY-plane.");
        GridTool.XZGrid = CreateGUIToggle(new Rect(90, 5, 30, 30), GridTool.XZGrid, toggleXZGridTexture, "Draw grid on the XZ-plane.");
        GridTool.YZGrid = CreateGUIToggle(new Rect(125, 5, 30, 30), GridTool.YZGrid, toggleYZGridTexture, "Draw grid on the YZ-plane.");

        // Creates a line between the grid settings and the snap settings.
        CreateGUILine(new Rect(164, 0, 2, 40));

        // Creates toggle buttons for each of the snap settings.
        GridTool.SnapObjectToGrid          = CreateGUIToggle(new Rect(175, 5, 30, 30), GridTool.SnapObjectToGrid, toggleSnapObjectToGridTexture, "Snap to grid. When this is turned on objectes moved within the scene view will snap to the grid.");
        GridTool.SnapAxisOnly              = CreateGUIToggle(new Rect(210, 5, 30, 30), GridTool.SnapAxisOnly, toggleSnapAxisOnlyTexture, "Axis snapping. When this is turned on objectes moved within the scene view will only snap to the axis the object is being moved on.");
        GridTool.AllignAllSelectedWithGrid = CreateGUIToggle(new Rect(245, 5, 30, 30), GridTool.AllignAllSelectedWithGrid, toggleIndividualSnappingTexture, "Individual snapping. If multiple objects are moved at the same time each individual object will snap to the nearest snapping point.");

        // Creates a line between the snap settings and the grid position settings.
        CreateGUILine(new Rect(354, 0, 2, 40));

        // Creates a toggle button for a grid position setting.
        GridTool.UseSelectionPosition = CreateGUIToggle(new Rect(365, 5, 30, 30), GridTool.UseSelectionPosition, toggleUseSelectionPositionTexture, "Dynamic grid. When this is turned on the grid will change position based on the selected object.");

        // Creates a line between the grid position settings and other settings.
        CreateGUILine(new Rect(614, 0, 2, 40));

        // Creates a toggle button for the show start position setting.
        GridTool.ShowStartPosition = CreateGUIToggle(new Rect(625, 5, 30, 30), GridTool.ShowStartPosition, toggleShowStartPositionTexture, "Show start positions. When this is turned on objects moved within the scene view will leave a 'ghost' at their initial position. The 'ghost' is removed when the mouse button is released.");

        // If any of the above setting has been changed...
        if (EditorGUI.EndChangeCheck())
        {
            //... update the lines in the grid and the selection arrays.
            GridTool.UpdateGridLines();
            GridTool.UpdateSelectionArrays();
        }

        // Defines the x position of the dorpdown menu
        int snapSizeX = 285;

        GUI.Label(new Rect(snapSizeX, 5, 60, 20), "Cell size", EditorStyles.label);
        EditorGUI.BeginChangeCheck();

        GridTool.IncrementSize = EditorGUI.FloatField(new Rect(285, 20, 60, 15), Mathf.Abs(GridTool.IncrementSize));

        if (EditorGUI.EndChangeCheck())
        {
            GridTool.UpdateGridLines();
        }

        // Starts a change check
        EditorGUI.BeginChangeCheck();

        // Creates a vector 3 field in which the grid offset can be set.
        GridTool.GridOffset = EditorGUI.Vector3Field(new Rect(405, 3, 200, 35), "Grid offset", GridTool.GridOffset);

        // If the grid offset is changed...
        if (EditorGUI.EndChangeCheck())
        {
            //... update the lines in the grid.
            GridTool.UpdateGridLines();
        }

        // Ends the horizontal control group.
        GUILayout.EndHorizontal();
    }
Exemplo n.º 7
0
    /// <summary>
    /// Used to update the previews and the arrays used to draw them.
    /// </summary>
    private void UpdatePreviews()
    {
        // Sets the length of the arrays.
        groundedPositions = new Vector3[selectedObjects.Length];
        groundedRotations = new Quaternion[selectedObjects.Length];
        groundedMeshes    = new Mesh[selectedObjects.Length];
        foundGround       = new bool[selectedObjects.Length];

        for (int i = 0; i < selectedObjects.Length; i++)
        {
            // Default value. This remains false if no ground was found.
            foundGround[i] = false;

            RaycastHit hit;

            // Creates a raycast from the position of the selected object in the direction of groundDir.
            if (Physics.Raycast(selectedObjects[i].transform.position, groundDir, out hit, Mathf.Infinity))
            {
                // If the ray hits something that is not the current gameobject
                if (hit.collider != null && hit.collider.gameObject != selectedObjects[i])
                {
                    // Ground has been found
                    foundGround[i] = true;

                    // Saves the old rotation of the object
                    Quaternion oldRot = selectedObjects[i].transform.rotation;

                    // Sets the up vector of the object to the normal of the raycast hit.
                    selectedObjects[i].transform.up = hit.normal;

                    // Sets the values in these two arrays.
                    groundedRotations[i] = selectedObjects[i].transform.rotation;
                    groundedPositions[i] = hit.point;

                    // If the corectPositionsToGrid is true and the grid is enabled
                    if (corectPositionsToGrid && GridTool.EnableGridTool)
                    {
                        // The "On Grid" position is calculated and saved.
                        Vector3 onGrid = GridTool.SnapToGrid(groundedPositions[i]);

                        // Finds out if x, y or z should not be on grid
                        if (groundInDirection == DirectionEnum.Left || groundInDirection == DirectionEnum.Right)
                        {
                            onGrid.x = groundedPositions[i].x;
                        }
                        else if (groundInDirection == DirectionEnum.Up || groundInDirection == DirectionEnum.Down)
                        {
                            onGrid.y = groundedPositions[i].y;
                        }
                        else if (groundInDirection == DirectionEnum.Back || groundInDirection == DirectionEnum.Forward)
                        {
                            onGrid.z = groundedPositions[i].z;
                        }

                        // Sets the groundedPosition of the current object to the new onGrid vector.
                        groundedPositions[i] = onGrid;

                        // Creates a new raycast at the new position to find the rotation here.
                        if (Physics.Raycast(groundedPositions[i] - groundDir, groundDir, out hit, Mathf.Infinity))
                        {
                            if (hit.collider != null && hit.collider.gameObject != selectedObjects[i])
                            {
                                selectedObjects[i].transform.up = hit.normal;
                                groundedRotations[i]            = selectedObjects[i].transform.rotation;
                            }
                        }
                    }

                    // Rotation of the object is reset to the oldRot
                    selectedObjects[i].transform.rotation = oldRot;

                    try
                    {
                        // If the the selectedObject has a mesh filter the shared mesh is stored.
                        groundedMeshes[i] = selectedObjects[i].GetComponent <MeshFilter>().sharedMesh;
                    }
                    catch
                    {
                        // If it has no mesh filter a new mesh is created.
                        groundedMeshes[i] = new Mesh();
                    }
                }
            }
        }
    }