浏览代码
Add drawing of custom node preview
Add drawing of custom node preview
Delegate drawing to a custom drawer class that is looked up via attribute, it will search for most specific drawer first, then walk up the inheritence chain./main
Tim Cooper
9 年前
当前提交
a2d8ad62
共有 27 个文件被更改,包括 479 次插入 和 379 次删除
-
7MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableNode.cs
-
79MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphDataSource.cs
-
3MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs
-
10MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/GUIModificationType.cs
-
8MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
-
5MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableNode.cs
-
9MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/DrawingData.cs
-
1MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/IGraph.cs
-
2MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/INode.cs
-
83MaterialGraphProject/Assets/UnityShaderEditor/Editor/Editors/MaterialGraphEditor.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/AbstractMaterialGraphTests.cs
-
23MaterialGraphProject/Assets/UnityShaderEditor/Runtime/AbstractMaterialGraph.cs
-
264MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function2Input.cs
-
11MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs
-
38MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector4Node.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
-
6.gitignore
-
9MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers.meta
-
201MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/BaseMaterialNodeUI.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/BaseMaterialNodeUI.cs.meta
-
41MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/Vector4NodeUI.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/Vector4NodeUI.cs.meta
-
5MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Util.meta
|
|||
using System; |
|||
|
|||
[Flags] |
|||
None, |
|||
Repaint, |
|||
ModelChanged |
|||
None = 0, |
|||
Repaint = 1 << 0, |
|||
ModelChanged = 1 << 1, |
|||
DataChanged = 1 << 2 |
|||
} |
|||
} |
|
|||
MaterialGraphProject/.vs |
|||
MaterialGraphProject/Library |
|||
|
|||
MaterialGraphProject/MaterialGraphProject.CSharp.csproj |
|||
*.csproj |
|||
MaterialGraphProject/MaterialGraphProject.sln |
|
|||
fileFormatVersion: 2 |
|||
guid: d53f98da57e19bd47aa9f6aed99ae412 |
|||
folderAsset: yes |
|||
timeCreated: 1464865885 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Reflection; |
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
using UnityEngine.MaterialGraph; |
|||
using Object = UnityEngine.Object; |
|||
|
|||
namespace UnityEditor.MaterialGraph |
|||
{ |
|||
|
|||
[CustomNodeUI(typeof(AbstractMaterialNode))] |
|||
class BaseMaterialNodeUI : ICustomNodeUi |
|||
{ |
|||
|
|||
private const int kPreviewWidth = 64; |
|||
private const int kPreviewHeight = 64; |
|||
|
|||
private MaterialGraphPreviewGenerator m_PreviewGenerator; |
|||
|
|||
[NonSerialized] |
|||
private int m_LastShaderVersion = -1; |
|||
|
|||
[NonSerialized] |
|||
private Material m_PreviewMaterial; |
|||
|
|||
[NonSerialized] |
|||
private Shader m_PreviewShader; |
|||
|
|||
private AbstractMaterialNode m_Node; |
|||
private 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; |
|||
} |
|||
} |
|||
|
|||
public MaterialGraphPreviewGenerator previewGenerator |
|||
{ |
|||
get |
|||
{ |
|||
if (m_PreviewGenerator == null) |
|||
{ |
|||
m_PreviewGenerator = new MaterialGraphPreviewGenerator(); |
|||
|
|||
} |
|||
return m_PreviewGenerator; |
|||
} |
|||
} |
|||
|
|||
public virtual float GetNodeUiHeight(float width) |
|||
{ |
|||
return kPreviewHeight; |
|||
} |
|||
|
|||
protected virtual int previewWidth |
|||
{ |
|||
get { return kPreviewWidth; } |
|||
} |
|||
|
|||
protected virtual int previewHeight |
|||
{ |
|||
get { return kPreviewHeight; } |
|||
} |
|||
|
|||
public void SetNode(INode node) |
|||
{ |
|||
if (node is AbstractMaterialNode) |
|||
m_Node = (AbstractMaterialNode)node; |
|||
} |
|||
|
|||
public virtual GUIModificationType Render(Rect area) |
|||
{ |
|||
if (m_Node == null) |
|||
return GUIModificationType.None; |
|||
|
|||
if (m_LastShaderVersion != m_Node.version) |
|||
{ |
|||
UpdatePreviewShader(); |
|||
m_LastShaderVersion = m_Node.version; |
|||
} |
|||
|
|||
GUILayout.BeginHorizontal(GUILayout.MinWidth(previewWidth + 10), GUILayout.MinWidth(previewHeight + 10)); |
|||
GUILayout.FlexibleSpace(); |
|||
var rect = GUILayoutUtility.GetRect(previewWidth, previewHeight, GUILayout.ExpandWidth(false)); |
|||
var preview = RenderPreview(rect); |
|||
GUILayout.FlexibleSpace(); |
|||
GUILayout.EndHorizontal(); |
|||
GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear; |
|||
GUI.DrawTexture(rect, preview, ScaleMode.StretchToFill, false); |
|||
GL.sRGBWrite = false; |
|||
return GUIModificationType.None; |
|||
} |
|||
|
|||
private bool UpdatePreviewShader() |
|||
{ |
|||
if (m_Node == null) |
|||
return false; |
|||
|
|||
var resultShader = ShaderGenerator.GeneratePreviewShader(m_Node, out m_GeneratedShaderMode); |
|||
|
|||
Debug.Log("RecreateShaderAndMaterial : " + m_Node.name + "_" + m_Node.guid.ToString().Replace("-", "_") + "\n" + resultShader); |
|||
|
|||
// 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 && ShaderHasError(m_PreviewShader)) |
|||
{ |
|||
Object.DestroyImmediate(m_PreviewShader, true); |
|||
m_PreviewShader = null; |
|||
} |
|||
|
|||
if (m_PreviewShader == null) |
|||
{ |
|||
m_PreviewShader = ShaderUtil.CreateShaderAsset(resultShader); |
|||
m_PreviewShader.hideFlags = HideFlags.HideInHierarchy; |
|||
} |
|||
else |
|||
{ |
|||
ShaderUtil.UpdateShaderAsset(m_PreviewShader, resultShader); |
|||
} |
|||
|
|||
return !ShaderHasError(m_PreviewShader); |
|||
} |
|||
|
|||
private static bool ShaderHasError(Shader shader) |
|||
{ |
|||
var hasErrorsCall = typeof(ShaderUtil).GetMethod("GetShaderErrorCount", BindingFlags.Static | BindingFlags.NonPublic); |
|||
var result = hasErrorsCall.Invoke(null, new object[] {shader}); |
|||
return (int) result != 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// RenderPreview gets called in OnPreviewGUI. Nodes can override
|
|||
/// RenderPreview and do their own rendering to the render texture
|
|||
/// </summary>
|
|||
public Texture RenderPreview(Rect targetSize) |
|||
{ |
|||
previewMaterial.shader = m_PreviewShader; |
|||
UpdateMaterialProperties(m_Node, previewMaterial); |
|||
return previewGenerator.DoRenderPreview(previewMaterial, m_GeneratedShaderMode, targetSize); |
|||
} |
|||
|
|||
private static void SetPreviewMaterialProperty(PreviewProperty previewProperty, Material mat) |
|||
{ |
|||
switch (previewProperty.m_PropType) |
|||
{ |
|||
case PropertyType.Texture2D: |
|||
mat.SetTexture(previewProperty.m_Name, previewProperty.m_Texture); |
|||
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); |
|||
} |
|||
|
|||
foreach (var prop in pList) |
|||
SetPreviewMaterialProperty(prop, material); |
|||
|
|||
ListPool<INode>.Release(childNodes); |
|||
ListPool<PreviewProperty>.Release(pList); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3ed1e0e3aec9a634e83fa340200854c4 |
|||
timeCreated: 1464868519 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
namespace UnityEditor.MaterialGraph |
|||
{ |
|||
[CustomNodeUI(typeof(Vector4Node))] |
|||
public class Vector4NodeUI : ICustomNodeUi |
|||
{ |
|||
private Vector4Node m_Node; |
|||
|
|||
public float GetNodeUiHeight(float width) |
|||
{ |
|||
return 2 * EditorGUIUtility.singleLineHeight; |
|||
} |
|||
|
|||
public GUIModificationType Render(Rect area) |
|||
{ |
|||
if (m_Node == null) |
|||
return GUIModificationType.None; |
|||
|
|||
EditorGUI.BeginChangeCheck(); |
|||
m_Node.value = EditorGUI.Vector4Field(new Rect(area.x, area.y, area.width, EditorGUIUtility.singleLineHeight), "Value", m_Node.value); |
|||
if (EditorGUI.EndChangeCheck()) |
|||
{ |
|||
//TODO:tidy this shit.
|
|||
//EditorUtility.SetDirty(materialGraphOwner.owner);
|
|||
return GUIModificationType.Repaint; |
|||
} |
|||
return GUIModificationType.None; |
|||
} |
|||
|
|||
public void SetNode(INode node) |
|||
{ |
|||
if (node is Vector4Node) |
|||
m_Node = (Vector4Node) node; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f970378d854a8e6458eb369bfb46bc42 |
|||
timeCreated: 1464865885 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 03ed77116f3480d4c85e8d92a684b096 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
userData: |
撰写
预览
正在加载...
取消
保存
Reference in new issue