void Update()
        {
            if (m_HasError)
            {
                return;
            }

            if (PlayerSettings.colorSpace != m_ColorSpace)
            {
                graphEditorView = null;
                m_ColorSpace    = PlayerSettings.colorSpace;
            }

            if (GraphicsSettings.renderPipelineAsset != m_RenderPipelineAsset)
            {
                graphEditorView       = null;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
            }

            if (EditorGUIUtility.isProSkin != m_ProTheme)
            {
                if (graphObject != null && graphObject.graph != null)
                {
                    Texture2D icon = GetThemeIcon(graphObject.graph);

                    // This is adding the icon at the front of the tab
                    titleContent = EditorGUIUtility.TrTextContentWithIcon(assetName, icon);
                    m_ProTheme   = EditorGUIUtility.isProSkin;
                }
            }

            try
            {
                if (graphObject == null && selectedGuid != null)
                {
                    var guid = selectedGuid;
                    selectedGuid = null;
                    Initialize(guid);
                }

                if (graphObject == null)
                {
                    Close();
                    return;
                }

                var materialGraph = graphObject.graph as GraphData;
                if (materialGraph == null)
                {
                    return;
                }

                if (graphEditorView == null)
                {
                    messageManager.ClearAll();
                    materialGraph.messageManager = messageManager;
                    var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
                    graphEditorView = new GraphEditorView(this, materialGraph, messageManager)
                    {
                        viewDataKey = selectedGuid,
                        assetName   = asset.name.Split('/').Last()
                    };
                    m_ColorSpace          = PlayerSettings.colorSpace;
                    m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
                    graphObject.Validate();
                }

                if (m_ChangedFileDependencies.Count > 0 && graphObject != null && graphObject.graph != null)
                {
                    var subGraphNodes = graphObject.graph.GetNodes <SubGraphNode>();
                    foreach (var subGraphNode in subGraphNodes)
                    {
                        subGraphNode.Reload(m_ChangedFileDependencies);
                    }
                    if (subGraphNodes.Count() > 0)
                    {
                        // Keywords always need to be updated to test against variant limit
                        // No Keywords may indicate removal and this may have now made the Graph valid again
                        // Need to validate Graph to clear errors in this case
                        materialGraph.OnKeywordChanged();
                    }
                    foreach (var customFunctionNode in graphObject.graph.GetNodes <CustomFunctionNode>())
                    {
                        customFunctionNode.Reload(m_ChangedFileDependencies);
                    }

                    m_ChangedFileDependencies.Clear();
                }

                if (graphObject.wasUndoRedoPerformed)
                {
                    graphEditorView.HandleGraphChanges();
                    graphObject.graph.ClearChanges();
                    graphObject.HandleUndoRedo();
                }

                graphEditorView.HandleGraphChanges();
                graphObject.graph.ClearChanges();
            }
            catch (Exception e)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                Debug.LogException(e);
                throw;
            }
        }
Ejemplo n.º 2
0
        public void Initialize(string assetGuid)
        {
            try
            {
                m_ColorSpace          = PlayerSettings.colorSpace;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;

                var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(assetGuid));
                if (asset == null)
                {
                    return;
                }

                if (!EditorUtility.IsPersistent(asset))
                {
                    return;
                }

                if (selectedGuid == assetGuid)
                {
                    return;
                }

                var path      = AssetDatabase.GetAssetPath(asset);
                var extension = Path.GetExtension(path);
                if (extension == null)
                {
                    return;
                }
                // Path.GetExtension returns the extension prefixed with ".", so we remove it. We force lower case such that
                // the comparison will be case-insensitive.
                extension = extension.Substring(1).ToLowerInvariant();
                bool isSubGraph;
                switch (extension)
                {
                case ShaderGraphImporter.Extension:
                    isSubGraph = false;
                    break;

                case ShaderSubGraphImporter.Extension:
                    isSubGraph = true;
                    break;

                default:
                    return;
                }

                selectedGuid = assetGuid;

                var textGraph = File.ReadAllText(path, Encoding.UTF8);
                graphObject                      = CreateInstance <GraphObject>();
                graphObject.hideFlags            = HideFlags.HideAndDontSave;
                graphObject.graph                = JsonUtility.FromJson <GraphData>(textGraph);
                graphObject.graph.assetGuid      = assetGuid;
                graphObject.graph.isSubGraph     = isSubGraph;
                graphObject.graph.messageManager = messageManager;
                graphObject.graph.OnEnable();
                graphObject.graph.ValidateGraph();

                graphEditorView = new GraphEditorView(this, m_GraphObject.graph, messageManager)
                {
                    viewDataKey = selectedGuid,
                    assetName   = asset.name.Split('/').Last()
                };

                titleContent = new GUIContent(asset.name.Split('/').Last());

                Repaint();
            }
            catch (Exception)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                throw;
            }
        }
Ejemplo n.º 3
0
        void Update()
        {
            if (m_HasError)
            {
                return;
            }

            if (PlayerSettings.colorSpace != m_ColorSpace)
            {
                graphEditorView = null;
                m_ColorSpace    = PlayerSettings.colorSpace;
            }

            if (GraphicsSettings.renderPipelineAsset != m_RenderPipelineAsset)
            {
                graphEditorView       = null;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
            }

            try
            {
                if (graphObject == null && selectedGuid != null)
                {
                    var guid = selectedGuid;
                    selectedGuid = null;
                    Initialize(guid);
                }

                if (graphObject == null)
                {
                    Close();
                    return;
                }

                var materialGraph = graphObject.graph as GraphData;
                if (materialGraph == null)
                {
                    return;
                }

                if (graphEditorView == null)
                {
                    messageManager.ClearAll();
                    var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
                    graphEditorView = new GraphEditorView(this, materialGraph, messageManager)
                    {
                        viewDataKey = selectedGuid,
                        assetName   = asset.name.Split('/').Last()
                    };
                    materialGraph.messageManager = messageManager;
                    m_ColorSpace          = PlayerSettings.colorSpace;
                    m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
                    graphObject.Validate();
                }

                if (updatePreviewShaders)
                {
                    m_GraphEditorView.UpdatePreviewShaders();
                    updatePreviewShaders = false;
                }

                graphEditorView.HandleGraphChanges();
                graphObject.graph.ClearChanges();
                Repaint();
            }
            catch (Exception e)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                Debug.LogException(e);
                throw;
            }
        }
Ejemplo n.º 4
0
        void Update()
        {
            if (m_HasError)
            {
                return;
            }

            if (PlayerSettings.colorSpace != m_ColorSpace)
            {
                graphEditorView = null;
                m_ColorSpace    = PlayerSettings.colorSpace;
            }

            if (GraphicsSettings.renderPipelineAsset != m_RenderPipelineAsset)
            {
                graphEditorView       = null;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
            }

            try
            {
                if (graphObject == null && selectedGuid != null)
                {
                    var guid = selectedGuid;
                    selectedGuid = null;
                    Initialize(guid);
                }

                if (graphObject == null)
                {
                    Close();
                    return;
                }

                var materialGraph = graphObject.graph as AbstractMaterialGraph;
                if (materialGraph == null)
                {
                    return;
                }

                if (graphEditorView == null)
                {
                    var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
                    graphEditorView = new GraphEditorView(this, materialGraph)
                    {
                        persistenceKey = selectedGuid,
                        assetName      = asset.name.Split('/').Last()
                    };
                    m_ColorSpace          = PlayerSettings.colorSpace;
                    m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
                }

                if (forceRedrawPreviews)
                {
                    // Redraw all previews
                    foreach (INode node in m_GraphObject.graph.GetNodes <INode>())
                    {
                        node.Dirty(ModificationScope.Node);
                    }
                    forceRedrawPreviews = false;
                }

                graphEditorView.HandleGraphChanges();
                graphObject.graph.ClearChanges();
            }
            catch (Exception e)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                Debug.LogException(e);
                throw;
            }
        }
Ejemplo n.º 5
0
        public void Initialize(string assetGuid)
        {
            try
            {
                m_ColorSpace          = PlayerSettings.colorSpace;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;

                var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(assetGuid));
                if (asset == null)
                {
                    return;
                }

                if (!EditorUtility.IsPersistent(asset))
                {
                    return;
                }

                if (selectedGuid == assetGuid)
                {
                    return;
                }

                var path      = AssetDatabase.GetAssetPath(asset);
                var extension = Path.GetExtension(path);
                if (extension == null)
                {
                    return;
                }
                // Path.GetExtension returns the extension prefixed with ".", so we remove it. We force lower case such that
                // the comparison will be case-insensitive.
                extension = extension.Substring(1).ToLowerInvariant();
                Type graphType;
                switch (extension)
                {
                case ShaderGraphImporter.Extension:
                    graphType = typeof(MaterialGraph);
                    break;

                case ShaderSubGraphImporter.Extension:
                    graphType = typeof(SubGraph);
                    break;

                default:
                    return;
                }

                selectedGuid = assetGuid;

                var textGraph = File.ReadAllText(path, Encoding.UTF8);
                graphObject           = CreateInstance <GraphObject>();
                graphObject.hideFlags = HideFlags.HideAndDontSave;
                graphObject.graph     = JsonUtility.FromJson(textGraph, graphType) as IGraph;
                graphObject.graph.OnEnable();
                graphObject.graph.ValidateGraph();

                graphEditorView = new GraphEditorView(this, m_GraphObject.graph as AbstractMaterialGraph)
                {
                    persistenceKey = selectedGuid,
                    assetName      = asset.name.Split('/').Last()
                };
                m_FrameAllAfterLayout = true;
                graphEditorView.RegisterCallback <GeometryChangedEvent>(OnGeometryChanged);

                titleContent = new GUIContent(asset.name.Split('/').Last());

                Repaint();
            }
            catch (Exception)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                throw;
            }
        }
Ejemplo n.º 6
0
        public void ChangeSelection(string newSelectionGuid)
        {
            try
            {
                var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(newSelectionGuid));
                if (asset == null)
                {
                    return;
                }

                if (!EditorUtility.IsPersistent(asset))
                {
                    return;
                }

                if (selectedGuid == newSelectionGuid)
                {
                    return;
                }

                var  path      = AssetDatabase.GetAssetPath(asset);
                var  extension = Path.GetExtension(path);
                Type graphType;
                switch (extension)
                {
                case ".ShaderGraph":
                    graphType = typeof(MaterialGraph);
                    break;

                case ".ShaderSubGraph":
                    graphType = typeof(SubGraph);
                    break;

                default:
                    return;
                }

                selectedGuid = newSelectionGuid;

                var textGraph = File.ReadAllText(path, Encoding.UTF8);
                graphObject           = CreateInstance <GraphObject>();
                graphObject.hideFlags = HideFlags.HideAndDontSave;
                graphObject.graph     = JsonUtility.FromJson(textGraph, graphType) as IGraph;
                graphObject.graph.OnEnable();
                graphObject.graph.ValidateGraph();

                graphEditorView = new GraphEditorView(this, m_GraphObject.graph as AbstractMaterialGraph, asset.name)
                {
                    persistenceKey = selectedGuid
                };
                graphEditorView.RegisterCallback <PostLayoutEvent>(OnPostLayout);

                titleContent = new GUIContent(asset.name);

                Repaint();
            }
            catch (Exception)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                throw;
            }
        }
Ejemplo n.º 7
0
        void Update()
        {
            if (m_HasError)
            {
                return;
            }

            bool updateTitle = false;

            if (m_AssetMaybeDeleted)
            {
                m_AssetMaybeDeleted = false;
                if (AssetFileExists())
                {
                    // it exists... just to be sure, let's check if it changed
                    m_AssetMaybeChangedOnDisk = true;
                }
                else
                {
                    // it was really deleted, ask the user what to do
                    bool handled = DisplayDeletedFromDiskDialog(true);
                }
                updateTitle = true;
            }

            if (PlayerSettings.colorSpace != m_ColorSpace)
            {
                graphEditorView = null;
                m_ColorSpace    = PlayerSettings.colorSpace;
            }

            if (GraphicsSettings.renderPipelineAsset != m_RenderPipelineAsset)
            {
                graphEditorView       = null;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
            }

            if (EditorGUIUtility.isProSkin != m_ProTheme)
            {
                if (graphObject != null && graphObject.graph != null)
                {
                    updateTitle = true; // trigger icon swap
                    m_ProTheme  = EditorGUIUtility.isProSkin;
                }
            }

            if (m_AssetMaybeChangedOnDisk)
            {
                m_AssetMaybeChangedOnDisk = false;

                // if we don't have any graph, then it doesn't really matter if the file on disk changed or not
                // as we're going to reload it below anyways
                if (graphObject?.graph != null)
                {
                    // check if it actually did change on disk
                    if (FileOnDiskHasChanged())
                    {
                        // don't worry people about "losing changes" unless there are changes to lose
                        bool graphChanged = GraphHasChangedSinceLastSerialization();

                        if (EditorUtility.DisplayDialog(
                                "Graph has changed on disk",
                                AssetDatabase.GUIDToAssetPath(selectedGuid) + "\n\n" +
                                (graphChanged ? "Do you want to reload it and lose the changes made in the graph?" : "Do you want to reload it?"),
                                graphChanged ? "Discard Changes And Reload" : "Reload",
                                "Don't Reload"))
                        {
                            // clear graph, trigger reload
                            graphObject = null;
                        }
                    }
                }
                updateTitle = true;
            }

            try
            {
                if (graphObject == null && selectedGuid != null)
                {
                    var guid = selectedGuid;
                    selectedGuid = null;
                    Initialize(guid);
                }

                if (graphObject == null)
                {
                    Close();
                    return;
                }

                var materialGraph = graphObject.graph as GraphData;
                if (materialGraph == null)
                {
                    return;
                }

                if (graphEditorView == null)
                {
                    messageManager.ClearAll();
                    materialGraph.messageManager = messageManager;
                    var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
                    graphEditorView = new GraphEditorView(this, materialGraph, messageManager)
                    {
                        viewDataKey = selectedGuid,
                    };
                    m_ColorSpace          = PlayerSettings.colorSpace;
                    m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;
                    graphObject.Validate();

                    // update blackboard title for the new graphEditorView
                    updateTitle = true;
                }

                if (m_ChangedFileDependencies.Count > 0 && graphObject != null && graphObject.graph != null)
                {
                    bool reloadedSomething = false;
                    var  subGraphNodes     = graphObject.graph.GetNodes <SubGraphNode>();
                    foreach (var subGraphNode in subGraphNodes)
                    {
                        var reloaded = subGraphNode.Reload(m_ChangedFileDependencies);
                        reloadedSomething |= reloaded;
                    }
                    if (subGraphNodes.Count() > 0)
                    {
                        // Keywords always need to be updated to test against variant limit
                        // No Keywords may indicate removal and this may have now made the Graph valid again
                        // Need to validate Graph to clear errors in this case
                        materialGraph.OnKeywordChanged();
                    }
                    foreach (var customFunctionNode in graphObject.graph.GetNodes <CustomFunctionNode>())
                    {
                        var reloaded = customFunctionNode.Reload(m_ChangedFileDependencies);
                        reloadedSomething |= reloaded;
                    }

                    // reloading files may change serilization
                    if (reloadedSomething)
                    {
                        updateTitle = true;
                    }

                    m_ChangedFileDependencies.Clear();
                }

                var wasUndoRedoPerformed = graphObject.wasUndoRedoPerformed;

                if (wasUndoRedoPerformed)
                {
                    graphEditorView.HandleGraphChanges(true);
                    graphObject.graph.ClearChanges();
                    graphObject.HandleUndoRedo();
                }

                if (graphObject.isDirty || wasUndoRedoPerformed)
                {
                    updateTitle         = true;
                    graphObject.isDirty = false;
                }

                // Called again to handle changes from deserialization in case an undo/redo was performed
                graphEditorView.HandleGraphChanges(wasUndoRedoPerformed);
                graphObject.graph.ClearChanges();

                if (updateTitle)
                {
                    UpdateTitle();
                }
            }
            catch (Exception e)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                Debug.LogException(e);
                throw;
            }
        }
Ejemplo n.º 8
0
        public void Initialize(string assetGuid)
        {
            try
            {
                EditorApplication.wantsToQuit -= PromptSaveIfDirtyOnQuit;
                m_ColorSpace          = PlayerSettings.colorSpace;
                m_RenderPipelineAsset = GraphicsSettings.renderPipelineAsset;

                var asset = AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(assetGuid));
                if (asset == null)
                {
                    return;
                }

                if (!EditorUtility.IsPersistent(asset))
                {
                    return;
                }

                if (selectedGuid == assetGuid)
                {
                    return;
                }

                var path      = AssetDatabase.GetAssetPath(asset);
                var extension = Path.GetExtension(path);
                if (extension == null)
                {
                    return;
                }
                // Path.GetExtension returns the extension prefixed with ".", so we remove it. We force lower case such that
                // the comparison will be case-insensitive.
                extension = extension.Substring(1).ToLowerInvariant();
                bool isSubGraph;
                switch (extension)
                {
                case ShaderGraphImporter.Extension:
                    isSubGraph = false;
                    break;

                case ShaderSubGraphImporter.Extension:
                    isSubGraph = true;
                    break;

                default:
                    return;
                }

                selectedGuid = assetGuid;

                using (GraphLoadMarker.Auto())
                {
                    m_LastSerializedFileContents = File.ReadAllText(path, Encoding.UTF8);
                    graphObject           = CreateInstance <GraphObject>();
                    graphObject.hideFlags = HideFlags.HideAndDontSave;
                    graphObject.graph     = new GraphData
                    {
                        assetGuid = assetGuid, isSubGraph = isSubGraph, messageManager = messageManager
                    };
                    MultiJson.Deserialize(graphObject.graph, m_LastSerializedFileContents);
                    graphObject.graph.OnEnable();
                    graphObject.graph.ValidateGraph();
                }

                using (CreateGraphEditorViewMarker.Auto())
                {
                    graphEditorView = new GraphEditorView(this, m_GraphObject.graph, messageManager)
                    {
                        viewDataKey = selectedGuid,
                    };
                }

                UpdateTitle();

                Repaint();
                EditorApplication.wantsToQuit += PromptSaveIfDirtyOnQuit;
            }
            catch (Exception)
            {
                m_HasError        = true;
                m_GraphEditorView = null;
                graphObject       = null;
                throw;
            }
        }