浏览代码

Fixing with help from peter

/main
Tim Cooper 7 年前
当前提交
9e0273b3
共有 9 个文件被更改,包括 164 次插入276 次删除
  1. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs
  2. 122
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  3. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GraphEditorPresenter.cs
  4. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  5. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters.meta
  6. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Views.meta
  7. 262
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs

24
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs


{
PreviewData m_PreviewHandle;
public IGraph graph { get; set; }
[SerializeField]
private int m_Version;
[Flags]
public enum ChangeType

public OnChange onChange;
public void Initialize(string assetName, IGraph graph, PreviewSystem previewSystem)
[SerializeField]
private HelperMaterialGraphEditWindow m_Owner;
public AbstractMaterialGraph graph
var masterNode = graph.GetNodes<AbstractMasterNode>().FirstOrDefault();
get { return m_Owner.GetMaterialGraph(); }
}
public void Dirty()
{
m_Version++;
}
public void Initialize(string assetName, PreviewSystem previewSystem, HelperMaterialGraphEditWindow window)
{
var masterNode = graph.GetNodes<MasterNode>().FirstOrDefault();
this.graph = graph;
m_Owner = window;
this.assetName = assetName;
selectedNodes = new List<INode>();

122
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Graphing;
using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
using Object = UnityEngine.Object;

int m_SelectionHash;
VisualElement m_Title;
VisualElement m_PropertiesContainer;
VisualElement m_ContentContainer;
AbstractNodeEditorView m_EditorView;

}
Add(topContainer);
ReaddProps();
var bottomContainer = new VisualElement { name = "bottom" };
{
m_Preview = new Image { name = "preview", image = Texture2D.blackTexture};

// - SubGraphOutputNode
m_TypeMapper = new TypeMapper(typeof(INode), typeof(AbstractNodeEditorView), typeof(StandardNodeEditorView))
{
{ typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeEditorView) }
// { typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeEditorView) }
};
}
private void ReaddProps()
{
if (m_PropertiesContainer != null)
Remove(m_PropertiesContainer);
m_PropertiesContainer = new VisualElement()
{
new IMGUIContainer(OnGuiHandler)
m_PropertiesContainer.style.flexDirection = StyleValue<FlexDirection>.Create(FlexDirection.Column);
Add(m_PropertiesContainer);
}
private void OnGuiHandler()
{
if (m_Presenter == null)
return;
if (GUILayout.Button("Add Property"))
{
var gm = new GenericMenu();
gm.AddItem(new GUIContent("Float"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new FloatShaderProperty());
ReaddProps();
});
gm.AddItem(new GUIContent("Vector2"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new Vector2ShaderProperty());
ReaddProps();
});
gm.AddItem(new GUIContent("Vector3"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new Vector3ShaderProperty());
ReaddProps();
});
gm.AddItem(new GUIContent("Vector4"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new Vector4ShaderProperty());
ReaddProps();
});
gm.AddItem(new GUIContent("Color"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new ColorShaderProperty());
ReaddProps();
});
gm.AddItem(new GUIContent("Texture"), false, () =>
{
m_Presenter.graph.AddShaderProperty(new TextureShaderProperty());
ReaddProps();
});
gm.ShowAsContext();
}
EditorGUI.BeginChangeCheck();
foreach (var property in m_Presenter.graph.properties.ToArray())
{
property.name = EditorGUILayout.DelayedTextField("Name", property.name);
property.description = EditorGUILayout.DelayedTextField("Description", property.description);
if (property is FloatShaderProperty)
{
var fProp = property as FloatShaderProperty;
fProp.value = EditorGUILayout.FloatField("Value", fProp.value);
}
else if (property is Vector2ShaderProperty)
{
var fProp = property as Vector2ShaderProperty;
fProp.value = EditorGUILayout.Vector2Field("Value", fProp.value);
}
else if (property is Vector3ShaderProperty)
{
var fProp = property as Vector3ShaderProperty;
fProp.value = EditorGUILayout.Vector3Field("Value", fProp.value);
}
else if (property is Vector4ShaderProperty)
{
var fProp = property as Vector4ShaderProperty;
fProp.value = EditorGUILayout.Vector4Field("Value", fProp.value);
}
else if (property is ColorShaderProperty)
{
var fProp = property as ColorShaderProperty;
fProp.value = EditorGUILayout.ColorField("Value", fProp.value);
}
else if (property is TextureShaderProperty)
{
var fProp = property as TextureShaderProperty;
fProp.value.texture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), fProp.value.texture, typeof(Texture), null) as Texture;
}
if (GUILayout.Button("Remove"))
{
m_Presenter.graph.RemoveShaderProperty(property.guid);
}
EditorGUILayout.Separator();
}
if (EditorGUI.EndChangeCheck())
{
foreach (var node in m_Presenter.graph.GetNodes<PropertyNode>())
node.onModified(node, ModificationScope.Node);
}
}
public void OnChange(GraphInspectorPresenter.ChangeType changeType)

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GraphEditorPresenter.cs


set { m_GraphInspectorPresenter = value; }
}
public void Initialize(AbstractMaterialGraph graph, IMaterialGraphEditWindow container, string assetName)
public void Initialize(AbstractMaterialGraph graph, HelperMaterialGraphEditWindow container, string assetName)
{
m_PreviewSystem = new PreviewSystem(graph);

m_GraphInspectorPresenter = CreateInstance<GraphInspectorPresenter>();
m_GraphInspectorPresenter.Initialize(assetName, graph, m_previewSystem, container);
m_GraphInspectorPresenter.Initialize(assetName, m_PreviewSystem, container);
m_GraphPresenter = CreateInstance<MaterialGraphPresenter>();
m_GraphPresenter.Initialize(graph, container, m_PreviewSystem);

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs


}
var node = m_Graph.GetNodeFromGuid(nodeGuid);
m_PreviewMaterial.shader = previewData.shader;
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.previewMode, node is IMasterNode, time, m_PreviewPropertyBlock);
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.previewMode, node is MasterNode, time, m_PreviewPropertyBlock);
previewData.texture = previewData.renderTexture;
}

if (!m_Previews.TryGetValue(nodeGuid, out previewData))
return;
if (node is IMasterNode)
if (node is MasterNode)
var masterNode = (IMasterNode)node;
/* var masterNode = (MasterNode)node;
previewData.shaderString = masterNode.GetFullShader(GenerationMode.Preview, node.guid + "_preview", out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;
previewData.shaderString = masterNode.(GenerationMode.Preview, node.guid + "_preview", out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;*/
}
else if (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex)
{

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters.meta


fileFormatVersion: 2
guid: 2d08dfb360644031a5e64558d4a93263
folderAsset: yes
timeCreated: 1503663071
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Views.meta


fileFormatVersion: 2
guid: 0969ff891b874694aa732e1240ee159b
folderAsset: yes
timeCreated: 1503663067
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

262
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs


using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
using Object = UnityEngine.Object;
using UnityEditor.Experimental.UIElements.GraphView;
namespace UnityEditor.MaterialGraph.Drawing
{
[Serializable]
public class NodePreviewPresenter : ScriptableObject
{
protected NodePreviewPresenter()
{}
MaterialGraphPreviewGenerator m_PreviewGenerator;
[NonSerialized]
int m_LastShaderVersion = -1;
[NonSerialized]
Material m_PreviewMaterial;
[NonSerialized]
Shader m_PreviewShader;
AbstractMaterialNode m_Node;
[SerializeField]
ModificationScope m_ModificationScope;
// Null means no modification is currently in progress.
public ModificationScope modificationScope
{
get
{
return m_ModificationScope;
}
set
{
// External changes can only set the modification scope higher, to prevent missing out on a previously set shader regeneration.
if (m_ModificationScope >= value)
return;
m_ModificationScope = value;
}
}
[NonSerialized]
Texture m_Texture;
public Texture texture
{
get { return m_Texture; }
}
PreviewMode m_GeneratedShaderMode = PreviewMode.Preview2D;
public Material previewMaterial
{
get
{
if (m_PreviewMaterial == null)
{
m_PreviewMaterial = new Material(Shader.Find("Unlit/Color")) {hideFlags = HideFlags.HideInHierarchy};
m_PreviewMaterial.hideFlags = HideFlags.HideInHierarchy;
}
return m_PreviewMaterial;
}
}
MaterialGraphPreviewGenerator previewGenerator
{
get
{
if (m_PreviewGenerator == null)
{
m_PreviewGenerator = new MaterialGraphPreviewGenerator(m_Node is MasterNode);
}
return m_PreviewGenerator;
}
}
public void Initialize(AbstractMaterialNode node)
{
m_Node = node;
m_ModificationScope = ModificationScope.Graph;
}
public void UpdateTexture()
{
if (m_Node == null)
{
m_Texture = null;
return;
}
if (m_Node.hasPreview == false)
{
m_Texture = null;
return;
}
var scope = m_ModificationScope;
m_ModificationScope = ModificationScope.Nothing;
if (scope != ModificationScope.Nothing)
{
if (scope >= ModificationScope.Graph)
{
var stage = NodeUtils.FindEffectiveShaderStage(m_Node, true);
/* if (!(m_Node is AbstractSurfaceMasterNode) && stage == ShaderStage.Vertex)
{
// Debug.Log(m_Node.name + ":" + stage);
m_Texture = null;
return;
}*/
// TODO: Handle shader regeneration error
var status = UpdatePreviewShader();
}
m_Texture = RenderPreview(new Vector2(256, 256));
}
else if (texture == null)
{
m_Texture = RenderPreview(new Vector2(256, 256));
}
}
void OnDisable()
{
if (m_PreviewGenerator != null)
m_PreviewGenerator.Dispose();
}
protected virtual string GetPreviewShaderString()
{
// TODO: this is a workaround right now.
/* if (m_Node is IMasterNode)
{
var localNode = (IMasterNode)m_Node;
if (localNode == null)
return string.Empty;
List<PropertyCollector.TextureInfo> defaultTextures;
var resultShader = ((IMasterNode)m_Node).GetFullShader(GenerationMode.Preview, m_Node.guid + "_preview", out defaultTextures);
if (((IMasterNode)m_Node).has3DPreview())
{
m_GeneratedShaderMode = PreviewMode.Preview3D;
}
return resultShader;
}*/
return ShaderGenerator.GeneratePreviewShader(m_Node, out m_GeneratedShaderMode);
}
bool UpdatePreviewShader()
{
if (m_Node == null || m_Node.hasError)
return false;
var resultShader = GetPreviewShaderString();
Debug.Log("RecreateShaderAndMaterial : " + m_Node.GetVariableNameForNode() + Environment.NewLine + resultShader);
string shaderOuputString = resultShader.Replace("UnityEngine.MaterialGraph", "Generated");
System.IO.File.WriteAllText(Application.dataPath + "/../GeneratedShader.shader", shaderOuputString);
if (string.IsNullOrEmpty(resultShader))
return false;
// workaround for some internal shader compiler weirdness
// if we are in error we sometimes to not properly clean
// out the error flags and will stay in error, even
// if we are now valid
if (m_PreviewShader && MaterialGraphAsset.ShaderHasError(m_PreviewShader))
{
Object.DestroyImmediate(m_PreviewShader, true);
m_PreviewShader = null;
}
if (m_PreviewShader == null)
{
m_PreviewShader = ShaderUtil.CreateShaderAsset(resultShader);
m_PreviewShader.hideFlags = HideFlags.HideAndDontSave;
}
else
{
ShaderUtil.UpdateShaderAsset(m_PreviewShader, resultShader);
}
return !MaterialGraphAsset.ShaderHasError(m_PreviewShader);
}
/// <summary>
/// RenderPreview gets called in OnPreviewGUI. Nodes can override
/// RenderPreview and do their own rendering to the render texture
/// </summary>
Texture RenderPreview(Vector2 targetSize)
{
previewMaterial.shader = m_PreviewShader;
UpdateMaterialProperties(m_Node, previewMaterial);
return previewGenerator.DoRenderPreview(previewMaterial, m_GeneratedShaderMode, new Rect(0, 0, targetSize.x, targetSize.y));
}
static void SetPreviewMaterialProperty(PreviewProperty previewProperty, Material mat)
{
switch (previewProperty.m_PropType)
{
case PropertyType.Texture:
mat.SetTexture(previewProperty.m_Name, previewProperty.m_Texture);
break;
case PropertyType.Cubemap:
mat.SetTexture(previewProperty.m_Name, previewProperty.m_Cubemap);
break;
case PropertyType.Color:
mat.SetColor(previewProperty.m_Name, previewProperty.m_Color);
break;
case PropertyType.Vector2:
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
break;
case PropertyType.Vector3:
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
break;
case PropertyType.Vector4:
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);
break;
case PropertyType.Float:
mat.SetFloat(previewProperty.m_Name, previewProperty.m_Float);
break;
}
}
public static void UpdateMaterialProperties(AbstractMaterialNode target, Material material)
{
var childNodes = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(childNodes, target);
var pList = ListPool<PreviewProperty>.Get();
for (var index = 0; index < childNodes.Count; index++)
{
var node = childNodes[index] as AbstractMaterialNode;
if (node == null)
continue;
node.CollectPreviewMaterialProperties(pList);
}
var graph = target.owner as AbstractMaterialGraph;
foreach (var prop in graph.properties)
{
SetPreviewMaterialProperty(prop.GetPreviewMaterialProperty(), material);
}
foreach (var prop in pList)
SetPreviewMaterialProperty(prop, material);
ListPool<INode>.Release(childNodes);
ListPool<PreviewProperty>.Release(pList);
}
}
}
正在加载...
取消
保存