Пример #1
0
        public void SetTrees(List <TreeInstance> trees, List <TreePrototype> prototypeList)
        {
            Trees.Clear();
            foreach (var tree in trees)
            {
                var newTree = new MadMapsTreeInstance(tree, prototypeList);
                Trees.Add(newTree);
            }

#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }
Пример #2
0
        public static void SnapshotTrees(this MMTerrainLayer layer, Terrain terrain)
        {
            layer.Trees.Clear();
            var tSize      = terrain.terrainData.size;
            var trees      = terrain.terrainData.treeInstances;
            var prototypes = new List <TreePrototype>(terrain.terrainData.treePrototypes);

            for (var i = 0; i < trees.Length; i++)
            {
                var wPos        = terrain.TreeToWorldPos(trees[i].position);
                var h           = terrain.SampleHeight(wPos);
                var newInstance = new MadMapsTreeInstance(trees[i], prototypes);
                newInstance.Position.y = (newInstance.Position.y * tSize.y) - h;
                layer.Trees.Add(newInstance);
            }
        }
Пример #3
0
        public static MMTerrainLayer DrawExpandedGUI(TerrainWrapper wrapper, MMTerrainLayer layer)
        {
            bool isInScene = layer != null && string.IsNullOrEmpty(AssetDatabase.GetAssetPath(layer));

            EditorGUILayout.BeginHorizontal();
            layer = (MMTerrainLayer)EditorGUILayout.ObjectField(isInScene ? "Asset (In-Scene)" : "Asset", layer, typeof(MMTerrainLayer), true);
            if (layer != null && isInScene && GUILayout.Button(_saveContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                var path = EditorUtility.SaveFilePanel("Save Terrain Layer", "Assets", layer.name, "asset");
                if (!string.IsNullOrEmpty(path))
                {
                    path = path.Substring(path.LastIndexOf("Assets/", StringComparison.Ordinal));
                    AssetDatabase.CreateAsset(layer, path);
                }
            }
            EditorGUILayout.EndHorizontal();
            if (layer == null)
            {
                EditorGUILayout.HelpBox("Snapshot is Null!", MessageType.Error);
                if (GUILayout.Button("Create Snapshot", EditorStyles.toolbarButton))
                {
                    layer = ScriptableObject.CreateInstance <MMTerrainLayer>();
                    GUIUtility.ExitGUI();
                    return(layer);
                }
                EditorGUILayout.EndVertical();
                return(layer);
            }

            EditorGUILayout.BeginHorizontal();
            var previewContent = new GUIContent(GUIResources.EyeOpenIcon, "Preview");

            EditorGUILayout.LabelField("Height:", layer.Heights != null ? string.Format("{0}x{1}", layer.Heights.Width, layer.Heights.Height) : "null");
            if (layer.Heights != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                DataInspector.SetData(layer.Heights);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.Heights.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Splats", layer.SplatData != null ? string.Format("{0}", layer.SplatData.Count) : "null");
            if (layer.SplatData != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                List <IDataInspectorProvider> data = new List <IDataInspectorProvider>();
                List <object> context = new List <object>();
                foreach (var keyValuePair in layer.SplatData)
                {
                    data.Add(keyValuePair.Value);
                    context.Add(keyValuePair.Key);
                }
                DataInspector.SetData(data, context);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.SplatData.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Details", layer.DetailData != null ? string.Format("{0}", layer.DetailData.Count) : "null");
            if (layer.DetailData != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                List <IDataInspectorProvider> data = new List <IDataInspectorProvider>();
                List <object> context = new List <object>();
                foreach (var keyValuePair in layer.DetailData)
                {
                    data.Add(keyValuePair.Value);
                    context.Add(keyValuePair.Key);
                }
                DataInspector.SetData(data, context, true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.DetailData.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Trees", layer.Trees != null ? string.Format("{0}", layer.Trees.Count) : "null");
            if (layer.Trees != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var tree in layer.Trees)
                {
                    if (!data.ContainsKey(tree.Prototype))
                    {
                        data[tree.Prototype] = new PositionList();
                    }
                    (data[tree.Prototype] as PositionList).Add(tree.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.Trees.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("➥Removals", layer.TreeRemovals != null ? string.Format("{0}", layer.TreeRemovals.Count) : "null");
            if (layer.TreeRemovals != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                var compoundTrees = wrapper.GetCompoundTrees(layer, false);
                compoundTrees.AddRange(layer.Trees);
                foreach (var guid in layer.TreeRemovals)
                {
                    MadMapsTreeInstance tree = null;
                    foreach (var t in compoundTrees)
                    {
                        if (t.Guid == guid)
                        {
                            tree = t;
                            break;
                        }
                    }
                    if (tree == null)
                    {
                        Debug.LogError("Couldn't find tree removal " + guid, layer);
                        continue;
                    }
                    if (!data.ContainsKey(tree.Prototype))
                    {
                        data[tree.Prototype] = new PositionList();
                    }
                    (data[tree.Prototype] as PositionList).Add(tree.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.TreeRemovals.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();

            #if VEGETATION_STUDIO
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Vegetation Studio", layer.VSInstances != null ? string.Format("{0}", layer.VSInstances.Count) : "null");
            if (layer.VSInstances != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var tree in layer.VSInstances)
                {
                    if (!data.ContainsKey(tree.VSID))
                    {
                        data[tree.VSID] = new PositionList();
                    }
                    (data[tree.VSID] as PositionList).Add(tree.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.VSInstances.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("➥Removals", layer.VSRemovals != null ? string.Format("{0}", layer.VSRemovals.Count) : "null");
            if (layer.VSRemovals != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                var compoundVS = wrapper.GetCompoundVegetationStudioData(layer, false);
                compoundVS.AddRange(layer.VSInstances);
                foreach (var guid in layer.VSRemovals)
                {
                    VegetationStudioInstance vsInstance = null;
                    foreach (var t in compoundVS)
                    {
                        if (t.Guid == guid)
                        {
                            vsInstance = t;
                            break;
                        }
                    }
                    if (vsInstance == null)
                    {
                        Debug.LogError("Couldn't find VS removal " + guid, layer);
                        continue;
                    }
                    if (!data.ContainsKey(vsInstance.VSID))
                    {
                        data[vsInstance.VSID] = new PositionList();
                    }
                    (data[vsInstance.VSID] as PositionList).Add(vsInstance.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.VSRemovals.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();
            #endif

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Objects", layer.Objects != null ? string.Format("{0}", layer.Objects.Count) : "null");
            if (layer.Objects != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var obj in layer.Objects)
                {
                    if (!data.ContainsKey(obj.Prefab))
                    {
                        data[obj.Prefab] = new PositionList();
                    }
                    (data[obj.Prefab] as PositionList).Add(obj.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.Objects.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("➥Removals", layer.ObjectRemovals != null ? string.Format("{0}", layer.ObjectRemovals.Count) : "null");
            if (layer.ObjectRemovals != null && GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                var compoundObjs = wrapper.GetCompoundObjects(layer, false);
                compoundObjs.AddRange(layer.Objects);
                foreach (var guid in layer.ObjectRemovals)
                {
                    WorldStamps.PrefabObjectData?obj = null;
                    foreach (var t in compoundObjs)
                    {
                        if (t.Guid == guid)
                        {
                            obj = t;
                            break;
                        }
                    }
                    if (obj == null)
                    {
                        Debug.LogError("Couldn't find object removal " + guid, layer);
                        continue;
                    }
                    if (!data.ContainsKey(obj.Value.Prefab))
                    {
                        data[obj.Value.Prefab] = new PositionList();
                    }
                    (data[obj.Value.Prefab] as PositionList).Add(obj.Value.Position);
                }
                DataInspector.SetData(data.Values.ToList(), data.Keys.ToList(), true);
            }
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.ObjectRemovals.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();
            EditorExtensions.Seperator();

            EditorGUILayout.BeginHorizontal();
            bool hasStencil = layer.Stencil != null && layer.Stencil.Width > 0 && layer.Stencil.Height > 0;;
            EditorGUILayout.LabelField("Stencil" + (hasStencil ? "" : " (null)"), layer.Stencil != null ? string.Format("{0}", string.Format("{0}x{1}", layer.Stencil.Width, layer.Stencil.Height)) : "null");

            GUI.enabled = hasStencil;
            if (GUILayout.Button(EditorGUIUtility.IconContent("Terrain Icon"), EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                TerrainWrapperGUI.StencilLayerDisplay = layer;
            }
            if (GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                DataInspector.SetData(layer.Stencil);
            }
            GUI.enabled = true;
            if (GUILayout.Button(GenericEditor.DeleteContent, EditorStyles.label, GUILayout.Width(20)) && EditorUtility.DisplayDialog("Really Clear?", "", "Yes", "No"))
            {
                layer.Stencil.Clear();
                EditorUtility.SetDirty(layer);
                EditorGUIUtility.ExitGUI();
                return(layer);
            }
            EditorGUILayout.EndHorizontal();

            EditorExtensions.Seperator();

            EditorGUILayout.LabelField("Layer Commands", EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Capture From Terrain"))
            {
                if (EditorUtility.DisplayDialog(string.Format("Capture Layer {0} from current Terrain?", layer.name), "This will clear any existing data!",
                                                "Yes", "No"))
                {
                    layer.SnapshotTerrain(wrapper.Terrain);
                    EditorUtility.SetDirty(layer);
                    EditorUtility.SetDirty(wrapper.Terrain);
                    EditorSceneManager.MarkAllScenesDirty();
                    AssetDatabase.SaveAssets();
                    EditorUtility.SetDirty(layer);
                    EditorGUIUtility.ExitGUI();
                    return(layer);
                }
            }
            EditorGUILayout.Space();
            if (GUILayout.Button("Apply To Terrain"))
            {
                if (wrapper.PrepareApply())
                {
                    layer.WriteToTerrain(wrapper);
                    wrapper.FinaliseApply();

                    EditorUtility.SetDirty(layer);
                    EditorSceneManager.MarkAllScenesDirty();

                    EditorGUIUtility.ExitGUI();
                    return(layer);
                }
            }
            EditorGUILayout.Space();
            if (GUILayout.Button("Clear Layer"))
            {
                if (EditorUtility.DisplayDialog(string.Format("Clear Layer {0}?", layer.name), "This will clear any existing data!",
                                                "Yes", "No"))
                {
                    layer.Clear(wrapper);
                    EditorUtility.SetDirty(wrapper.Terrain);
                    EditorUtility.SetDirty(layer);
                    EditorSceneManager.MarkAllScenesDirty();
                    EditorGUIUtility.ExitGUI();
                    return(layer);
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            return(layer);
        }