Example #1
0
    static GameObject CreateBaseTerrain(IFerr2DTMaterial aMaterial, bool aCreateColliders)
    {
        GameObject          obj     = new GameObject("New Terrain");
        Ferr2D_Path         path    = obj.AddComponent <Ferr2D_Path>();
        Ferr2DT_PathTerrain terrain = obj.AddComponent <Ferr2DT_PathTerrain>();

        bool hasEdges = aMaterial.Has(Ferr2DT_TerrainDirection.Bottom) ||
                        aMaterial.Has(Ferr2DT_TerrainDirection.Left) ||
                        aMaterial.Has(Ferr2DT_TerrainDirection.Right);

        if (hasEdges)
        {
            path.Add(new Vector2(-6, -3));
            path.Add(new Vector2(-6, 3));
            path.Add(new Vector2(6, 3));
            path.Add(new Vector2(6, -3));
            path.closed = true;
        }
        else
        {
            path.Add(new Vector2(-6, 6));
            path.Add(new Vector2(6, 6));
            terrain.splitCorners = false;
            path.closed          = false;
        }

        if (aMaterial.fillMaterial != null)
        {
            if (hasEdges)
            {
                terrain.fill = Ferr2DT_FillMode.Closed;
            }
            else
            {
                terrain.fill         = Ferr2DT_FillMode.Skirt;
                terrain.splitCorners = true;
            }
        }
        else
        {
            terrain.fill = Ferr2DT_FillMode.None;
        }
        terrain.smoothPath     = SmoothTerrain;
        terrain.pixelsPerUnit  = PPU;
        terrain.createCollider = aCreateColliders;
        terrain.SetMaterial(aMaterial);
        terrain.Build(true);

        obj.transform.position = GetSpawnPos();

        return(obj);
    }
    static GameObject CreateBaseTerrain(IFerr2DTMaterial aMaterial, bool aCreateColliders)
    {
        GameObject          obj     = new GameObject("New Terrain");
        Ferr2DT_PathTerrain terrain = obj.AddComponent <Ferr2DT_PathTerrain>();

        obj.transform.position = GetSpawnPos();

        bool hasEdges = aMaterial.Has(Ferr2DT_TerrainDirection.Bottom) ||
                        aMaterial.Has(Ferr2DT_TerrainDirection.Left) ||
                        aMaterial.Has(Ferr2DT_TerrainDirection.Right);

        if (hasEdges)
        {
            terrain.PathData.Add(new Vector2(5, -5), new Ferr2D_PointData(1), Ferr.PointType.Sharp);
            terrain.PathData.Add(new Vector2(5, 5), new Ferr2D_PointData(1), Ferr.PointType.Sharp);
            terrain.PathData.Add(new Vector2(-5, 5), new Ferr2D_PointData(1), Ferr.PointType.Sharp);
            terrain.PathData.Add(new Vector2(-5, -5), new Ferr2D_PointData(1), Ferr.PointType.Sharp);
            terrain.PathData.Closed = true;
        }
        else
        {
            terrain.PathData.Add(new Vector2(5, 5), new Ferr2D_PointData(1), Ferr.PointType.Auto);
            terrain.PathData.Add(new Vector2(-5, 5), new Ferr2D_PointData(1), Ferr.PointType.Auto);
            terrain.splitCorners    = false;
            terrain.PathData.Closed = false;
        }

        if (aMaterial.fillMaterial == null)
        {
            terrain.FillMode = Ferr2D_SectionMode.None;
        }
        if (aMaterial.edgeMaterial == null)
        {
            terrain.EdgeMode = Ferr2D_SectionMode.None;
        }
        terrain.pixelsPerUnit = PPU;
        terrain.ColliderMode  = aCreateColliders ? Ferr2D_ColliderMode.Polygon2D : Ferr2D_ColliderMode.None;
        terrain.SetMaterial(aMaterial);
        terrain.Build(true);

        return(obj);
    }
Example #3
0
    public static void ShowPreviewDirection(IFerr2DTMaterial aMat, Ferr2DT_TerrainDirection aDir, Rect aBounds, bool aSimpleUVs, bool aEditable)
    {
        Ferr2DT_SegmentDescription desc = aMat.GetDescriptor(aDir);

        if (!aMat.Has(aDir))
        {
            return;
        }

        if (!aEditable)
        {
            for (int i = 0; i < desc.body.Length; i++)
            {
                Ferr.EditorTools.DrawRect(aMat.ToScreen(desc.body[i]), aBounds);
            }
            Ferr.EditorTools.DrawRect(aMat.ToScreen(desc.leftCap), aBounds);
            Ferr.EditorTools.DrawRect(aMat.ToScreen(desc.rightCap), aBounds);
            Ferr.EditorTools.DrawRect(aMat.ToScreen(desc.innerLeftCap), aBounds);
            Ferr.EditorTools.DrawRect(aMat.ToScreen(desc.innerRightCap), aBounds);
        }
        else if (aSimpleUVs)
        {
            float   height    = MaxHeight(desc);
            float   capWidth  = Mathf.Max(desc.leftCap.width, desc.rightCap.width);
            float   bodyWidth = desc.body[0].width;
            int     bodyCount = desc.body.Length;
            float   texWidth  = aMat.edgeMaterial.mainTexture != null ? aMat.edgeMaterial.mainTexture.width  : 1;
            float   texHeight = aMat.edgeMaterial.mainTexture != null ? aMat.edgeMaterial.mainTexture.height : 1;
            Vector2 pos       = new Vector2(desc.leftCap.x, desc.leftCap.y);
            if (desc.leftCap.width == 0 && desc.leftCap.height == 0)
            {
                pos = new Vector2(desc.body[0].x, desc.body[0].y);
            }

            Rect bounds = new Rect(pos.x, pos.y, capWidth * 2 + bodyWidth * bodyCount, height);
            bounds = aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(bounds), aBounds));
            bounds = ClampRect(bounds, (Texture2D)aMat.edgeMaterial.mainTexture);

            Ferr.EditorTools.DrawVLine(new Vector2((pos.x + capWidth) * texWidth + aBounds.x, (pos.y * texHeight) + 2), height * texHeight);
            for (int i = 1; i <= desc.body.Length; i++)
            {
                Ferr.EditorTools.DrawVLine(new Vector2((pos.x + capWidth + bodyWidth * i) * texWidth + aBounds.x, (pos.y * texHeight) + 2), height * texHeight);
            }

            height    = bounds.height;
            bodyWidth = (bounds.width - capWidth * 2) / bodyCount;
            pos.x     = bounds.x;
            pos.y     = bounds.y;

            float currX = pos.x;
            desc.leftCap.x      = currX;
            desc.leftCap.y      = pos.y;
            desc.leftCap.width  = capWidth;
            desc.leftCap.height = capWidth == 0 ? 0 : height;
            currX += capWidth;

            for (int i = 0; i < desc.body.Length; i++)
            {
                desc.body[i].x      = currX;
                desc.body[i].y      = pos.y;
                desc.body[i].width  = bodyWidth;
                desc.body[i].height = height;
                currX += bodyWidth;
            }

            desc.rightCap.x      = currX;
            desc.rightCap.y      = pos.y;
            desc.rightCap.width  = capWidth;
            desc.rightCap.height = capWidth == 0 ? 0 : height;
        }
        else
        {
            for (int i = 0; i < desc.body.Length; i++)
            {
                desc.body[i] = ClampRect(aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(desc.body[i]), aBounds)), (Texture2D)aMat.edgeMaterial.mainTexture);
            }
            if (desc.leftCap.width != 0 && desc.leftCap.height != 0)
            {
                desc.leftCap = ClampRect(aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(desc.leftCap), aBounds)), (Texture2D)aMat.edgeMaterial.mainTexture);
            }
            if (desc.rightCap.width != 0 && desc.rightCap.height != 0)
            {
                desc.rightCap = ClampRect(aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(desc.rightCap), aBounds)), (Texture2D)aMat.edgeMaterial.mainTexture);
            }

            if (desc.innerLeftCap.width != 0 && desc.innerLeftCap.height != 0)
            {
                desc.innerLeftCap = ClampRect(aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(desc.innerLeftCap), aBounds)), (Texture2D)aMat.edgeMaterial.mainTexture);
            }
            if (desc.innerRightCap.width != 0 && desc.innerRightCap.height != 0)
            {
                desc.innerRightCap = ClampRect(aMat.ToNative(Ferr.EditorTools.UVRegionRect(aMat.ToPixels(desc.innerRightCap), aBounds)), (Texture2D)aMat.edgeMaterial.mainTexture);
            }
        }
    }
Example #4
0
    void OnGUI()
    {
        if (material == null)
        {
            return;
        }

        // if this was an undo, repaint it
        if (Event.current.type == EventType.ValidateCommand)
        {
            switch (Event.current.commandName)
            {
            case "UndoRedoPerformed":
                GUI.changed = true;
                Repaint();
                break;
            }
        }

        Undo.RecordObject((UnityEngine.Object)material, "Modified Terrain Material");

        if (Ferr.EditorTools.ResetHandles())
        {
            GUI.changed = true;
        }

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.BeginVertical(GUILayout.Width(width));

        Ferr.EditorTools.Box(5, () => {
            if (currDir != Ferr2DT_TerrainDirection.None)
            {
                Ferr2DT_TerrainMaterialUtility.ShowSample(material, currDir, width - 10);
            }
            Ferr.EditorTools.Box(2, () => {
                if (GUILayout.Button("Top"))
                {
                    currDir = Ferr2DT_TerrainDirection.Top;
                }
                if (currDir == Ferr2DT_TerrainDirection.Top)
                {
                    if (prevDir != currDir)
                    {
                        simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.GetDescriptor(currDir));
                    }

                    bool showTop = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Top), "Use Top");
                    material.Set(Ferr2DT_TerrainDirection.Top, showTop);
                    if (showTop)
                    {
                        ShowDirection(material, currDir);
                    }
                }
            }, width - 10, 0);
            Ferr.EditorTools.Box(2, () => {
                if (GUILayout.Button("Left"))
                {
                    currDir = Ferr2DT_TerrainDirection.Left;
                }
                if (currDir == Ferr2DT_TerrainDirection.Left)
                {
                    if (prevDir != currDir)
                    {
                        simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.GetDescriptor(currDir));
                    }

                    bool showLeft = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Left), "Use Left");
                    material.Set(Ferr2DT_TerrainDirection.Left, showLeft);
                    if (showLeft)
                    {
                        ShowDirection(material, currDir);
                    }
                }
            }, width - 10, 0);
            Ferr.EditorTools.Box(2, () => {
                if (GUILayout.Button("Right"))
                {
                    currDir = Ferr2DT_TerrainDirection.Right;
                }
                if (currDir == Ferr2DT_TerrainDirection.Right)
                {
                    if (prevDir != currDir)
                    {
                        simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.GetDescriptor(currDir));
                    }

                    bool showRight = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Right), "Use Right");
                    material.Set(Ferr2DT_TerrainDirection.Right, showRight);
                    if (showRight)
                    {
                        ShowDirection(material, currDir);
                    }
                }
            }, width - 10, 0);
            Ferr.EditorTools.Box(2, () => {
                if (GUILayout.Button("Bottom"))
                {
                    currDir = Ferr2DT_TerrainDirection.Bottom;
                }
                if (currDir == Ferr2DT_TerrainDirection.Bottom)
                {
                    if (prevDir != currDir)
                    {
                        simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.GetDescriptor(currDir));
                    }

                    bool showBottom = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Bottom), "Use Bottom");
                    material.Set(Ferr2DT_TerrainDirection.Bottom, showBottom);
                    if (showBottom)
                    {
                        ShowDirection(material, currDir);
                    }
                }
            }, width - 10, 0);
        }, 0, (int)this.position.height);

        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical();
        scroll = EditorGUILayout.BeginScrollView(scroll);
        if (currDir != Ferr2DT_TerrainDirection.None)
        {
            Ferr2DT_TerrainMaterialUtility.ShowPreview(material, currDir, simpleUVs, true, width);
        }
        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndHorizontal();

        if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag)
        {
            Repaint();
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty((UnityEngine.Object)material);

            bool updatedTerrain           = false;
            Ferr2DT_PathTerrain[] terrain = GameObject.FindObjectsOfType(typeof(Ferr2DT_PathTerrain)) as Ferr2DT_PathTerrain[];
            for (int i = 0; i < terrain.Length; i++)
            {
                if (terrain[i].TerrainMaterial == material)
                {
                    terrain[i].Build(true);
                    updatedTerrain = true;
                }
            }

            if (updatedTerrain)
            {
                EditorSceneManager.MarkAllScenesDirty();
            }
        }

        prevDir = currDir;
    }