Beispiel #1
0
 void OnDisable()
 {
     if (_painter != null)
     {
         _painter.Destroy();
         _painter = null;
     }
 }
Beispiel #2
0
        void OnSceneGUI()
        {
            var worldStamp = target as WorldStamp;

            if (!_editingMask)
            {
                var rotatedMatrix = Handles.matrix * Matrix4x4.TRS(worldStamp.transform.position, worldStamp.transform.rotation, worldStamp.transform.lossyScale);
                _boxBoundsHandle.center = Vector3.up * (worldStamp.Size.y / 2);
                _boxBoundsHandle.size   = worldStamp.ExplicitSize;
                using (new Handles.DrawingScope(rotatedMatrix))
                {
                    _boxBoundsHandle.DrawHandle();
                }
                if (worldStamp.Size != _boxBoundsHandle.size)
                {
                    //var scaled = _boxBoundsHandle.size;
                    //scaled.Scale(worldStamp.transform.lossyScale.Inverse());
                    worldStamp.ExplicitSize = _boxBoundsHandle.size;
                    SceneView.RepaintAll();
                }

                if (_painter != null)
                {
                    _painter.Destroy();
                    _painter = null;
                }
                return;
            }

            if (_painter == null)
            {
                _painter      = new Common.Painter.Painter(worldStamp.Mask, worldStamp.Data.GridManager);
                _painter.Ramp = new Gradient()
                {
                    colorKeys = new[] { new GradientColorKey(Color.red, 0), new GradientColorKey(Color.black, 0.001f), new GradientColorKey(Color.black, 1), },
                    alphaKeys = new[] { new GradientAlphaKey(1, 0), new GradientAlphaKey(0, 1), }
                };
            }

            _painter.Canvas = worldStamp.Mask;

            _painter.MaxValue    = 1;
            _painter.MinValue    = 0;
            _painter.Rect        = new Rect(0, 0, worldStamp.Data.Size.x, worldStamp.Data.Size.z);
            _painter.GridManager = worldStamp.Data.GridManager;
            _painter.TRS         = Matrix4x4.TRS(worldStamp.transform.position - worldStamp.transform.rotation * worldStamp.Size.xz().x0z() / 2,
                                                 worldStamp.transform.rotation,
                                                 new Vector3(
                                                     worldStamp.transform.lossyScale.x * (worldStamp.Size.x / worldStamp.Data.Size.x),
                                                     worldStamp.transform.lossyScale.y * (worldStamp.Size.y / worldStamp.Data.Size.y),
                                                     worldStamp.transform.lossyScale.z * (worldStamp.Size.z / worldStamp.Data.Size.z))
                                                 );

            _painter.PaintingEnabled = true;
            _painter.OnSceneGUI();
        }
Beispiel #3
0
        private void DoSingleInstanceInfo()
        {
            var stamp = target as WorldStamp;

            EditorGUILayout.BeginVertical("Box");

            //FIX ISSUES
            if (stamp.Data.Objects.Exists(data => data.Prefab == null))
            {
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                EditorGUILayout.LabelField("Stamp contains objects with null prefabs");
                if (GUILayout.Button("Fix"))
                {
                    var prevCount = stamp.Data.Objects.Count;
                    stamp.Data.Objects.RemoveAll(data => data.Prefab == null);
                    Debug.Log(string.Format("Removed {0} missing prefabs from stamp {1}", (prevCount - stamp.Data.Objects.Count), stamp.name), stamp);
                    EditorUtility.SetDirty(stamp);
                    var prefab = GetPrefab(stamp);
                    if (prefab != null)
                    {
                        EditorUtility.SetDirty(prefab);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            if (stamp.Data.Objects.Exists(data => data.Scale.x < 0 || data.Scale.y < 0 || data.Scale.z < 0))
            {
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                EditorGUILayout.LabelField("Stamp contains objects with negative scales");
                if (GUILayout.Button("Fix"))
                {
                    for (int i = 0; i < stamp.Data.Objects.Count; i++)
                    {
                        var obj = stamp.Data.Objects[i];
                        obj.Scale             = new Vector3(Mathf.Abs(stamp.Data.Objects[i].Scale.x), Mathf.Abs(stamp.Data.Objects[i].Scale.y), Mathf.Abs(stamp.Data.Objects[i].Scale.z));
                        stamp.Data.Objects[i] = obj;
                        EditorUtility.SetDirty(stamp);
                        var prefab = GetPrefab(stamp);
                        if (prefab != null)
                        {
                            EditorUtility.SetDirty(prefab);
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            if (stamp.Data.Objects.Exists(data => PrefabUtility.FindPrefabRoot(data.Prefab) != data.Prefab))
            {
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                EditorGUILayout.LabelField("Stamp contains references to prefab subobjects");
                if (GUILayout.Button("Fix"))
                {
                    for (int i = 0; i < stamp.Data.Objects.Count; i++)
                    {
                        var obj = stamp.Data.Objects[i];
                        obj.Prefab            = PrefabUtility.FindPrefabRoot(obj.Prefab);
                        stamp.Data.Objects[i] = obj;
                        EditorUtility.SetDirty(stamp);
                        var prefab = GetPrefab(stamp);
                        if (prefab != null)
                        {
                            EditorUtility.SetDirty(prefab);
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            #if VEGETATION_STUDIO
            if (stamp.Data.VSData.Exists(data => data.Package == null))
            {
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                EditorGUILayout.LabelField("Stamp contains Vegetation Studio with no package!");
                if (GUILayout.Button("Remove"))
                {
                    var prevCount = stamp.Data.VSData.Count;
                    stamp.Data.VSData.RemoveAll(data => data.Package == null);
                    Debug.Log(string.Format("Removed {0} invalid instances from stamp {1}", (prevCount - stamp.Data.VSData.Count), stamp.name), stamp);
                    EditorUtility.SetDirty(stamp);
                    var prefab = PrefabUtility.GetPrefabParent(stamp);
                    if (prefab != null)
                    {
                        EditorUtility.SetDirty(prefab);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            #endif
            // END FIX ISSUES

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

            if (stamp == null)
            {
                return;
            }

            bool isPrefab  = PrefabUtility.GetPrefabType(target) == PrefabType.Prefab;
            bool hasPrefab = PrefabUtility.GetPrefabObject(target);
            //var dc = stamp.GetComponentInChildren<WorldStampDataContainer>();
            //bool isProxy = !isPrefab && dc.Redirect != null;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Heights", stamp.Data.Heights != null ? string.Format("{0}x{1}", stamp.Data.Heights.Width, stamp.Data.Heights.Height) : "null");
            if (GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                DataInspector.SetData(stamp.Data.Heights, null, true);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Splats", stamp.Data.SplatData.Count.ToString());
            if (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 splatData in stamp.Data.SplatData)
                {
                    data.Add(splatData.Data);
                    context.Add(splatData.Wrapper);
                }
                DataInspector.SetData(data, context);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Details", stamp.Data.DetailData.Count.ToString());
            if (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 detailData in stamp.Data.DetailData)
                {
                    data.Add(detailData.Data);
                    context.Add(detailData.Wrapper);
                }
                DataInspector.SetData(data, context, true);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Trees", stamp.Data.Trees.Count.ToString());
            if (GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var tree in stamp.Data.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);
            }
            EditorGUILayout.EndHorizontal();

            #if VEGETATION_STUDIO
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Vegetation Studio Instances", stamp.Data.VSData.Count.ToString());
            if (GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var tree in stamp.Data.VSData)
                {
                    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);
            }
            EditorGUILayout.EndHorizontal();
            #endif

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Objects", stamp.Data.Objects.Count.ToString());
            if (GUILayout.Button(previewContent, EditorStyles.label, GUILayout.Width(20), GUILayout.Height(16)))
            {
                Dictionary <object, IDataInspectorProvider> data = new Dictionary <object, IDataInspectorProvider>();
                foreach (var obj in stamp.Data.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);
            }
            EditorGUILayout.EndHorizontal();

            GUI.enabled = !isPrefab;
            bool alreadyHasMaskInstance = !hasPrefab || (stamp.Mask != null && stamp.Mask.Count > 0);

            if (GUILayout.Button(_editingMask ? "Finish Editing" : alreadyHasMaskInstance ? "Edit Mask" : "Edit Mask (Create Instance)"))
            {
                if (_editingMask)
                {
                    EditorUtility.SetDirty(stamp);
                    if (_painter != null)
                    {
                        _painter.Destroy();
                        _painter = null;
                    }
                }
                else if (stamp.Mask == null || stamp.Mask.Count == 0)
                {
                    stamp.Data.Mask.OnBeforeSerialize();
                    stamp.Mask = JsonUtility.FromJson <WorldStampMask>(JsonUtility.ToJson(stamp.Data.Mask));
                }
                _editingMask = !_editingMask;
                GUIUtility.ExitGUI();
                return;
            }
            GUI.enabled = !_editingMask && hasPrefab && alreadyHasMaskInstance;
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Revert Mask"))
            {
                if (EditorUtility.DisplayDialog("Really Revert Mask?",
                                                "You will lose all mask data you changed on this instance!", "Yes", "No"))
                {
                    stamp.Mask   = null;
                    _editingMask = false;
                    if (_painter != null)
                    {
                        _painter.Destroy();
                        _painter = null;
                    }
                }
                GUIUtility.ExitGUI();
                return;
            }
            if (GUILayout.Button("Write Mask To Prefab") && stamp.Mask != null)
            {
                stamp.Mask.OnBeforeSerialize();
                stamp.Data.Mask = JsonUtility.FromJson <WorldStampMask>(JsonUtility.ToJson(stamp.Mask));
                var prefab = GetPrefab(stamp);
                if (prefab != null)
                {
                    Debug.Log("Wrote mask back to prefab");
                    EditorUtility.SetDirty(prefab);
                }
                stamp.Mask   = null;
                _editingMask = false;
                if (_painter != null)
                {
                    _painter.Destroy();
                    _painter = null;
                }
                GUIUtility.ExitGUI();
                return;
            }

            EditorGUILayout.EndHorizontal();
            GUI.enabled = true;

            EditorGUILayout.EndVertical();
        }