浏览代码

[shader graph]Add default input UI support (very alpha)

/main
Tim Cooper 9 年前
当前提交
a99eb964
共有 6 个文件被更改,包括 138 次插入11 次删除
  1. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  2. 18
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs
  3. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  4. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SlotValue.cs
  5. 52
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs
  6. 69
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs


base.Render(parentRect, canvas);
}
private static void RepaintDependentNodes(BaseMaterialNode bmn)
public static void RepaintDependentNodes(BaseMaterialNode bmn)
{
var dependentNodes = bmn.CollectDependentNodes();
foreach (var node in dependentNodes)

18
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs


}
}
// Add proxy inputs for when edges are not connect
var nullInputSlots = new List<NullInputProxy>();
foreach (var drawableMaterialNode in m_DrawableNodes)
{
var baseNode = drawableMaterialNode.m_Node;
// grab the input slots where there are no edges
foreach (var slot in baseNode.inputSlots.Where(x => x.edges.Count == 0))
{
// if there is no anchor, continue
// this can happen if we are in collapsed mode
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor)x).m_Slot == slot);
if (sourceAnchor == null)
continue;
nullInputSlots.Add(new NullInputProxy(slot, sourceAnchor));
}
}
toReturn.AddRange(nullInputSlots.Select(x => (CanvasElement)x));
Debug.LogFormat("REturning {0} nodes", toReturn.Count);
return toReturn.ToArray();

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs


return modified;
}
private static bool DoSlotUI(BaseMaterialNode node, Slot slot)
public static bool DoSlotUI(BaseMaterialNode node, Slot slot)
{
GUILayout.BeginHorizontal(/*EditorStyles.inspectorBig*/);
GUILayout.BeginVertical();

6
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SlotValue.cs


m_DefaultVector = EditorGUILayout.Vector4Field("Value", m_DefaultVector);
return EditorGUI.EndChangeCheck();
}
public bool OnGUI(Rect rect)
{
EditorGUI.BeginChangeCheck();
m_DefaultVector = EditorGUI.Vector4Field(rect, GUIContent.none, m_DefaultVector);
return EditorGUI.EndChangeCheck();
}
}
}

52
UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs


return result;
}
public static string AdaptNodeOutput(BaseMaterialNode node, GenerationMode mode, ConcreteSlotValueType convertToType)
{
var outputSlot = node.outputSlots.FirstOrDefault();
return AdaptNodeOutput(outputSlot, mode, convertToType);
}
return string.Empty;
return kErrorString;
var node = outputSlot.node as BaseMaterialNode;
var convertFromType = node.GetConcreteOutputSlotValueType(outputSlot);

}
}
private static string AdaptNodeOutputForPreview(Slot outputSlot, GenerationMode mode, ConcreteSlotValueType convertToType)
{
if (outputSlot == null)
return kErrorString;
var node = outputSlot.node as BaseMaterialNode;
var convertFromType = node.GetConcreteOutputSlotValueType(outputSlot);
// if we are in a normal situation, just convert!
if (convertFromType >= convertToType || convertFromType == ConcreteSlotValueType.Vector1)
return AdaptNodeOutput(outputSlot, mode, convertToType);
var rawOutput = node.GetOutputVariableNameForSlot(node.outputSlots.FirstOrDefault(), mode);
// otherwise we need to pad output for the preview!
switch (convertToType)
{
case ConcreteSlotValueType.Vector3:
switch (convertFromType)
{
case ConcreteSlotValueType.Vector2:
return string.Format("half3({0}.x, {0}.y, 0.0)", rawOutput);
default:
return kErrorString;
}
case ConcreteSlotValueType.Vector4:
switch (convertFromType)
{
case ConcreteSlotValueType.Vector2:
return string.Format("half4({0}.x, {0}.y, 0.0, 0.0)", rawOutput);
case ConcreteSlotValueType.Vector3:
return string.Format("half4({0}.x, {0}.y, {0}.z, 0.0)", rawOutput);
default:
return kErrorString;
}
default:
return kErrorString;
}
}
public static string GeneratePreviewShader(BaseMaterialNode node, out PreviewMode generatedShaderMode)
{
// figure out what kind of preview we want!

}
if (generationMode == GenerationMode.Preview2D)
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutput(node, generationMode, ConcreteSlotValueType.Vector4) + ";", true);
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutputForPreview(node.outputSlots.FirstOrDefault(), generationMode, ConcreteSlotValueType.Vector4) + ";", true);
shaderBodyVisitor.AddShaderChunk("o.Emission = " + AdaptNodeOutput(node, generationMode, ConcreteSlotValueType.Vector3) + ";", true);
shaderBodyVisitor.AddShaderChunk("o.Emission = " + AdaptNodeOutputForPreview(node.outputSlots.FirstOrDefault(), generationMode, ConcreteSlotValueType.Vector3) + ";", true);
template = template.Replace("${ShaderName}", shaderName);
template = template.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));

69
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs


using UnityEditor.Experimental;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
public class NullInputProxy : CanvasElement
{
private Slot m_InputSlot;
private NodeAnchor m_NodeAnchor;
private const int kWidth = 180;
public NullInputProxy(Slot inputSlot, NodeAnchor nodeAnchor)
{
m_InputSlot = inputSlot;
m_NodeAnchor = nodeAnchor;
var size = m_NodeAnchor.scale;
size.x = kWidth;
scale = size;
nodeAnchor.AddDependency(this);
UpdateModel(UpdateType.Update);
var position = m_NodeAnchor.canvasBoundingRect.min;
position.x -= kWidth;
translation = position;
AddManipulator(new ImguiContainer());
}
public override void Render(Rect parentRect, Canvas2D canvas)
{
base.Render(parentRect, canvas);
var size = m_NodeAnchor.scale;
size.x = kWidth;
scale = size;
var position = m_NodeAnchor.canvasBoundingRect.min;
position.x -= kWidth;
translation = position;
var bmn = (BaseMaterialNode) m_InputSlot.node;
var rect = new Rect(0, 0, scale.x, scale.y);
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
var def = bmn.GetSlotDefaultValue(m_InputSlot.name);
var changed = def.OnGUI(rect);
if (changed)
DrawableMaterialNode.RepaintDependentNodes(bmn);
}
public override void UpdateModel(UpdateType t)
{
var size = m_NodeAnchor.scale;
size.x = kWidth;
scale = size;
var position = m_NodeAnchor.canvasBoundingRect.min;
position.x -= kWidth;
translation = position;
base.UpdateModel(t);
}
}
}
正在加载...
取消
保存