void ShowDirection(IFerr2DTMaterial aMat, int aDir) { Ferr2DT_SegmentDescription desc = aMat.GetDescriptor((Ferr2DT_TerrainDirection)aDir); settingsScroll = EditorGUILayout.BeginScrollView(settingsScroll); EditorGUILayout.LabelField("Edge Placement", EditorStyles.boldLabel); EditorGUI.indentLevel = 1; desc.zOffset = EditorGUILayout.IntField("Draw Order", Mathf.RoundToInt(desc.zOffset * 1000)) / 1000f; desc.YOffsetPercent = EditorGUILayout.Slider("Y Offset", desc.YOffsetPercent, -.5f, .5f); desc.yOffset = EditorGUILayout.FloatField("[Legacy] Y Offset", desc.yOffset); desc.capOffset = EditorGUILayout.FloatField("[Legacy] Cap Offset", desc.capOffset); EditorGUI.indentLevel = 0; Ferr2DT_TerrainMaterialUtility.EditColliders(desc); EditorGUILayout.Space(); EditorGUILayout.LabelField("Edge Segments", EditorStyles.boldLabel); simpleUVs = EditorGUILayout.Toggle("Simple Mode", simpleUVs); if (simpleUVs) { Ferr2DT_TerrainMaterialUtility.EditUVsSimple(aMat, desc); } else { Ferr2DT_TerrainMaterialUtility.EditUVsComplex(aMat, desc, width, ref currBody); } EditorGUILayout.EndScrollView(); }
void ShowDirection(IFerr2DTMaterial aMat, Ferr2DT_TerrainDirection aDir) { Ferr2DT_SegmentDescription desc = aMat.GetDescriptor(aDir); desc.zOffset = EditorGUILayout.FloatField("Z Offset", desc.zOffset); desc.yOffset = EditorGUILayout.FloatField("Y Offset", desc.yOffset); desc.capOffset = EditorGUILayout.FloatField("Cap Offset", desc.capOffset); simpleUVs = EditorGUILayout.Toggle("Simple", simpleUVs); if (simpleUVs) { Ferr2DT_TerrainMaterialUtility.EditUVsSimple(aMat, desc); } else { Ferr2DT_TerrainMaterialUtility.EditUVsComplex(aMat, desc, width, ref currBody); } }
public static void ShowSample(IFerr2DTMaterial aMat, Ferr2DT_TerrainDirection aDir, float aWidth) { if (aMat.edgeMaterial == null || aMat.edgeMaterial.mainTexture == null) { return; } Ferr2DT_SegmentDescription desc = aMat.GetDescriptor(aDir); float totalWidth = desc.leftCap.width + desc.rightCap.width + (Mathf.Max(0, desc.body[0].width) * 3); float sourceHeight = MaxHeight(desc); Texture tex = aMat.edgeMaterial != null ? aMat.edgeMaterial.mainTexture : EditorGUIUtility.whiteTexture; float scale = Mathf.Min(aWidth / totalWidth, 64 / sourceHeight); float aspect = tex.height / tex.width; GUILayout.Space(sourceHeight * scale * aspect); float x = GUILayoutUtility.GetLastRect().x; float y = GUILayoutUtility.GetLastRect().y; if (desc.leftCap.width != 0) { float yOff = ((sourceHeight - desc.leftCap.height) / 2) * scale; GUI.DrawTextureWithTexCoords(new Rect(x, y + yOff, desc.leftCap.width * scale, desc.leftCap.height * scale * aspect), tex, aMat.ToUV(desc.leftCap)); x += desc.leftCap.width * scale; } for (int i = 0; i < 3; i++) { int id = (2 - i) % desc.body.Length; float yOff = ((sourceHeight - desc.body[id].height) / 2) * scale; GUI.DrawTextureWithTexCoords(new Rect(x, y + yOff, desc.body[id].width * scale, desc.body[id].height * scale * aspect), tex, aMat.ToUV(desc.body[id])); x += desc.body[id].width * scale; } if (desc.leftCap.width != 0) { float yOff = ((sourceHeight - desc.rightCap.height) / 2) * scale; GUI.DrawTextureWithTexCoords(new Rect(x, y + yOff, desc.rightCap.width * scale, desc.rightCap.height * scale * aspect), tex, aMat.ToUV(desc.rightCap)); } }
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); } } }
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; }