Ejemplo n.º 1
0
    static GameObject CreateBaseTerrain(Ferr2DT_TerrainMaterial 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, 0));
            path.Add(new Vector2(-6, 6));
            path.Add(new Vector2(6, 6));
            path.Add(new Vector2(6, 0));
            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     = Ferr_Menu.SmoothTerrain;
        terrain.pixelsPerUnit  = Ferr_Menu.PPU;
        terrain.createCollider = aCreateColliders;
        terrain.SetMaterial(aMaterial);
        terrain.RecreatePath(true);

        obj.isStatic           = true;
        obj.transform.position = GetSpawnPos();

        return(obj);
    }
Ejemplo n.º 2
0
    static GameObject CreateBaseTerrain(Ferr2DT_TerrainMaterial 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, 0));
            path.Add(new Vector2(-6, 6));
            path.Add(new Vector2( 6, 6));
            path.Add(new Vector2( 6, 0));
            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     = Ferr_Menu.SmoothTerrain;
        terrain.pixelsPerUnit  = Ferr_Menu.PPU;
        terrain.createCollider = aCreateColliders;
        terrain.SetMaterial (aMaterial);
        terrain.RecreatePath(true);

        obj.isStatic           = true;
        obj.transform.position = GetSpawnPos();

        return obj;
    }
    public static void ShowPreviewDirection(Ferr2DT_TerrainMaterial aMat, Ferr2DT_TerrainDirection aDir, Rect aBounds, bool aSimpleUVs, bool aEditable)
    {
        Ferr2DT_SegmentDescription desc = aMat.descriptors[(int)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);
        }
        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;
            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 = Ferr_EditorTools.UVRegionRect(bounds,  aBounds);
            bounds = ClampRect(bounds, (Texture2D)aMat.edgeMaterial.mainTexture);
            Ferr_EditorTools.DrawVLine(new Vector2(pos.x + capWidth + aBounds.x, pos.y+1), height);
            for (int i = 1; i <= desc.body.Length; i++) {
                Ferr_EditorTools.DrawVLine(new Vector2(pos.x + capWidth + bodyWidth*i + aBounds.x, pos.y+1), height);
            }

            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(Ferr_EditorTools.UVRegionRect(desc.body[i], aBounds), (Texture2D)aMat.edgeMaterial.mainTexture);
            }
            if (desc.leftCap.width != 0 && desc.leftCap.height != 0)
                desc.leftCap  = ClampRect(Ferr_EditorTools.UVRegionRect(desc.leftCap,  aBounds), (Texture2D)aMat.edgeMaterial.mainTexture);
            if (desc.rightCap.width != 0 && desc.rightCap.height != 0)
                desc.rightCap = ClampRect(Ferr_EditorTools.UVRegionRect(desc.rightCap, aBounds), (Texture2D)aMat.edgeMaterial.mainTexture);
        }
    }
    public static void ShowPreviewDirection(Ferr2DT_TerrainMaterial 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);
            }
        }
    }
    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":
                Repaint();
                return;
            }
        }

        Undo.RecordObject(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(material);

            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);
                }
            }
        }

        prevDir = currDir;
    }
Ejemplo n.º 6
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":
                Repaint();
                return;
            }
        }

                #if !(UNITY_4_2 || UNITY_4_1 || UNITY_4_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_1 || UNITY_3_0)
        Undo.RecordObject(material, "Modified Terrain Material");
                #else
        Undo.SetSnapshotTarget(material, "Modified Terrain Material");
        if (!Ferr_EditorTools.HandlesMoving())
        {
            Undo.CreateSnapshot();
        }
                #endif
        if (Ferr_EditorTools.ResetHandles())
        {
            GUI.changed = true;
        }

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

        if (EditorGUILayout.Foldout(currDir == Ferr2DT_TerrainDirection.Top, "Top", foldoutStyle))
        {
            if (currDir != Ferr2DT_TerrainDirection.Top)
            {
                simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.descriptors[(int)Ferr2DT_TerrainDirection.Top]);
            }
            currDir = Ferr2DT_TerrainDirection.Top;
            bool showTop = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Top), "Use Top");
            material.Set(Ferr2DT_TerrainDirection.Top, showTop);
            if (showTop)
            {
                ShowDirection(material, currDir);
            }
        }
        if (EditorGUILayout.Foldout(currDir == Ferr2DT_TerrainDirection.Left, "Left", foldoutStyle))
        {
            if (currDir != Ferr2DT_TerrainDirection.Left)
            {
                simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.descriptors[(int)Ferr2DT_TerrainDirection.Left]);
            }
            currDir = Ferr2DT_TerrainDirection.Left;
            bool showLeft = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Left), "Use Left");
            material.Set(Ferr2DT_TerrainDirection.Left, showLeft);
            if (showLeft)
            {
                ShowDirection(material, currDir);
            }
        }
        if (EditorGUILayout.Foldout(currDir == Ferr2DT_TerrainDirection.Right, "Right", foldoutStyle))
        {
            if (currDir != Ferr2DT_TerrainDirection.Right)
            {
                simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.descriptors[(int)Ferr2DT_TerrainDirection.Right]);
            }
            currDir = Ferr2DT_TerrainDirection.Right;
            bool showRight = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Right), "Use Right");
            material.Set(Ferr2DT_TerrainDirection.Right, showRight);
            if (showRight)
            {
                ShowDirection(material, currDir);
            }
        }
        if (EditorGUILayout.Foldout(currDir == Ferr2DT_TerrainDirection.Bottom, "Bottom", foldoutStyle))
        {
            if (currDir != Ferr2DT_TerrainDirection.Bottom)
            {
                simpleUVs = Ferr2DT_TerrainMaterialUtility.IsSimple(material.descriptors[(int)Ferr2DT_TerrainDirection.Bottom]);
            }
            currDir = Ferr2DT_TerrainDirection.Bottom;
            bool showBottom = GUILayout.Toggle(material.Has(Ferr2DT_TerrainDirection.Bottom), "Use Bottom");
            material.Set(Ferr2DT_TerrainDirection.Bottom, showBottom);
            if (showBottom)
            {
                ShowDirection(material, currDir);
            }
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical();
        if (currDir != Ferr2DT_TerrainDirection.None)
        {
            Ferr2DT_TerrainMaterialUtility.ShowPreview(material, currDir, simpleUVs, true, width);
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndHorizontal();

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

        if (GUI.changed)
        {
            EditorUtility.SetDirty(material);
                        #if (UNITY_4_2 || UNITY_4_1 || UNITY_4_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_1 || UNITY_3_0)
            Undo.RegisterSnapshot();
                        #endif

            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].RecreatePath();
                }
            }
        }
                #if (UNITY_4_2 || UNITY_4_1 || UNITY_4_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_1 || UNITY_3_0)
        Undo.ClearSnapshotTarget();
                #endif
    }