浏览代码

[shader graph]data refactor pt2

/main
Tim Cooper 9 年前
当前提交
6d78b98e
共有 9 个文件被更改,包括 85 次插入116 次删除
  1. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs
  2. 30
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Light/BaseLightFunction.cs
  3. 10
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs
  4. 4
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialOptions.cs
  5. 3
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AddNode.cs
  6. 74
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  7. 7
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function2Input.cs
  8. 44
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  9. 27
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs


namespace UnityEditor.MaterialGraph
{
[Serializable]
public class SlotReference
{

30
UnityProject/Assets/UnityShaderEditor/Editor/Source/Light/BaseLightFunction.cs


using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{

public override string GetSurfaceOutputStructureName() { return "SurfaceOutputStandard"; }
public override void DoSlotsForConfiguration(PixelShaderNode node)
{
node.AddSlot(new Slot(node.guid, kAlbedoSlotName, SlotValueType.Vector3, new SlotValue(), ));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kNormalSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kEmissionSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kMetallicSlotName), SlotValueType.Vector1));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kSmoothnessSlotName), SlotValueType.Vector1));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kOcclusion), SlotValueType.Vector1));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kAlphaSlotName), SlotValueType.Vector1));
node.AddSlot(new Slot(node.guid, kAlbedoSlotName, kAlbedoSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kNormalSlotName, kNormalSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kMetallicSlotName, kMetallicSlotName, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
node.AddSlot(new Slot(node.guid, kSmoothnessSlotName, kSmoothnessSlotName, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
node.AddSlot(new Slot(node.guid, kOcclusion, kOcclusion, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
node.AddSlot(new Slot(node.guid, kAlphaSlotName, kAlphaSlotName, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support
node.RemoveSlotsNameNotMatching(

public override string GetSurfaceOutputStructureName() { return "SurfaceOutputStandardSpecular"; }
public override void DoSlotsForConfiguration(PixelShaderNode node)
{
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kAlbedoSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kNormalSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kSpecularSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kEmissionSlotName), SlotValueType.Vector3));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kSmoothnessSlotName), SlotValueType.Vector1));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kOcclusion), SlotValueType.Vector1));
node.AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.InputSlot, slotType: kAlphaSlotName), SlotValueType.Vector1));
node.AddSlot(new Slot(node.guid, kAlbedoSlotName, kAlbedoSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kNormalSlotName, kNormalSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kSpecularSlotName, kSpecularSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kEmissionSlotName, kEmissionSlotName, Slot.SlotType.Input, SlotValueType.Vector3, Vector4.zero));
node.AddSlot(new Slot(node.guid, kSmoothnessSlotName, kSmoothnessSlotName, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
node.AddSlot(new Slot(node.guid, kOcclusion, kOcclusion, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
node.AddSlot(new Slot(node.guid, kAlphaSlotName, kAlphaSlotName, Slot.SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support

10
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs


[SerializeField]
private PixelGraph m_PixelGraph;
public int GetShaderInstanceID()
{
return -1;
//return m_Shader.GetInstanceID();
}
public MaterialOptions materialOptions { get { return m_MaterialOptions; } }
public BaseMaterialGraph currentGraph { get { return m_PixelGraph; } }

4
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialOptions.cs


using System;
public class MaterialOptions : ScriptableObject
[Serializable]
public class MaterialOptions
{
public enum BlendMode
{

3
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AddNode.cs


[Title("Math/Add Node")]
public class AddNode : Function2Input, IGeneratesFunction
{
public override void OnCreate()
public AddNode()
base.OnCreate();
}
protected override string GetFunctionName()

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


public string name
{
get { return name; }
set { name = value; }
public BaseMaterialGraph owner { private get; set; }
public string precision
{
get { return "half"; }
}
public BaseMaterialGraph owner { protected get; set; }
private IEnumerable<Slot> inputSlots
protected IEnumerable<Slot> inputSlots
{
get { return m_Slots.Where(x => x.isInputSlot); }
}

}
}
var bmg = (graph as BaseMaterialGraph);
if (bmg == null)
return null;
var previewUtil = bmg.previewUtility;
var previewUtil = owner.previewUtility;
previewUtil.BeginPreview(targetSize, GUIStyle.none);
if (m_GeneratedShaderMode == PreviewMode.Preview3D)

protected virtual void CollectPreviewMaterialProperties (List<PreviewProperty> properties)
{
var validSlots = ListPool<Slot>.Get();
GetValidInputSlots(validSlots);
var validSlots = inputSlots.ToArray();
for (int index = 0; index < validSlots.Count; index++)
for (int index = 0; index < validSlots.Length; index++)
if (s.edges.Count > 0)
continue;
var defaultInput = GetSlotDefaultValue(s.name);
if (defaultInput == null)
var edges = owner.GetEdges(s);
if (edges.Any())
m_Name = defaultInput.inputName,
m_Name = s.GetInputName(this),
m_Vector4 = defaultInput.defaultValue
m_Vector4 = s.currentValue
ListPool<Slot>.Release(validSlots);
}
public static void UpdateMaterialProperties(BaseMaterialNode target, Material material)

#region Slots
public virtual void GetValidInputSlots(List<Slot> slotsToFill)
{
for (int i = 0; i < m_Slots.Count; ++i)
{
var slot = m_Slots[i];
if (slot != null && slot.slotType == Slot.SlotType.Input)
slotsToFill.Add(slot);
}
}
if (s.slotType == Slot.SlotType.Input) Debug.LogError("Attempting to use input slot (" + s + ") for output!");
if (s.isInputSlot) Debug.LogError("Attempting to use input slot (" + s + ") for output!");
if (!m_Slots.Contains(s)) Debug.LogError("Attempting to use slot (" + s + ") for output on a node that does not have this slot!");
return GetOutputVariableNameForNode() + "_" + s.name;

{
GUILayout.Label("Slot Defaults", EditorStyles.boldLabel);
bool modified = false;
foreach (var slot in slots.Where(x => x.isInputSlot && x.edges.Count == 0))
modified |= DoSlotUI(this, slot);
foreach (var slot in inputSlots)
{
if(!owner.GetEdges(slot).Any())
modified |= DoSlotUI(this, slot);
}
return modified;
}

GUILayout.BeginHorizontal(/*EditorStyles.inspectorBig*/);
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Slot " + slot.title, EditorStyles.largeLabel);
GUILayout.Label("Slot " + slot.name, EditorStyles.largeLabel);
return DoMaterialSlotUIBody(node, slot);
}
private static bool DoMaterialSlotUIBody(BaseMaterialNode node, Slot slot)
{
SlotValue value = node.GetSlotDefaultValue(slot.name);
if (value == null)
return false;
var def = node.GetSlotDefaultValue(slot.name);
return def.OnGUI();
return slot.OnGUI();
var def = GetSlotDefaultValue(inputSlot.name);
return def.OnGUI(rect, inputSlotType);
return inputSlot.OnGUI(rect, inputSlotType);
return inputSlots.Where(x => x.edges.Count == 0);
return inputSlots.Where(x => !owner.GetEdges(x).Any());
}
public abstract void OnBeforeSerialize();

7
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function2Input.cs


get { return true; }
}
public override void OnEnable()
public Function2Input()
base.OnEnable();
AddSlot(GetInputSlot1());
AddSlot(GetInputSlot2());
AddSlot(GetOutputSlot());

get { return new[] {GetInputSlot1Name(), GetInputSlot2Name(), GetOutputSlotName()}; }
}
protected virtual MaterialGraphSlot GetInputSlot1()
protected virtual Slot GetInputSlot1()
return new MaterialGraphSlot(slot, SlotValueType.Dynamic);
return new Slot(slot, SlotValueType.Dynamic);
}
protected virtual MaterialGraphSlot GetInputSlot2()

44
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs


private static List<BaseLightFunction> s_LightFunctions;
public PixelShaderNode()
{
name = "PixelMaster";
GetLightFunction().DoSlotsForConfiguration(this);
}
protected override int previewWidth
{
get { return 300; }

public override bool canDeleteNode { get { return false; } }
public override void OnEnable()
{
base.OnEnable();
var lightFunction = GetLightFunction();
lightFunction.DoSlotsForConfiguration(this);
}
public override void OnCreate()
{
name = "PixelMaster";
base.OnCreate();
}
private static List<BaseLightFunction> GetLightFunctions()
{
if (s_LightFunctions == null)

(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
for (int index = 0; index < firstPassSlot.edges.Count; index++)
foreach (var edge in owner.GetEdges(firstPassSlot))
var edge = firstPassSlot.edges[index];
var node = edge.fromSlot.node as BaseMaterialNode;
shaderBody.AddShaderChunk("o." + firstPassSlot.name + " = " + node.GetOutputVariableNameForSlot(edge.fromSlot, generationMode) + ";", true);
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGUID(outputRef.nodeGuid);
var fromSlot = fromNode.FindOutputSlot(outputRef.slotName);
shaderBody.AddShaderChunk("o." + firstPassSlot.name + " = " + fromNode.GetOutputVariableNameForSlot(fromSlot, generationMode) + ";", true);
}
// track the last index of nodes... they have already been processed :)

{
if (slot == firstPassSlot)
continue;
foreach (var edge in slot.edges)
foreach (var edge in owner.GetEdges(slot))
var node = edge.fromSlot.node as BaseMaterialNode;
shaderBody.AddShaderChunk("o." + slot.name + " = " + node.GetOutputVariableNameForSlot(edge.fromSlot, generationMode) + ";", true);
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGUID(outputRef.nodeGuid);
var fromSlot = fromNode.FindOutputSlot(outputRef.slotName);
shaderBody.AddShaderChunk("o." + slot.name + " = " + fromNode.GetOutputVariableNameForSlot(fromSlot, generationMode) + ";", true);
}
}
}

{
var function = GetLightFunction();
function.DoSlotsForConfiguration(this);
pixelGraph.RevalidateGraph();
owner.RevalidateGraph();
return GUIModificationType.ModelChanged;
}
return GUIModificationType.None;

var shaderName = "Hidden/PreviewShader/" + name + "_" + guid;
List<PropertyGenerator.TextureInfo> defaultTextures;
var resultShader = ShaderGenerator.GenerateSurfaceShader(pixelGraph.owner, shaderName, true, out defaultTextures);
var resultShader = ShaderGenerator.GenerateSurfaceShader(owner.owner, shaderName, true, out defaultTextures);
m_GeneratedShaderMode = PreviewMode.Preview3D;
hasError = !InternalUpdatePreviewShader(resultShader);
return true;

27
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs


private string m_Name;
[SerializeField]
private string m_DisplayName;
[SerializeField]
private SlotType m_SlotType;
[SerializeField]

[NonSerialized]
private GUID m_NodeGUID;
public Slot(GUID nodeGuid, string name, SlotType slotType, SlotValueType valueType, Vector4 defaultValue)
public Slot(GUID nodeGuid, string name, string displayName, SlotType slotType, SlotValueType valueType, Vector4 defaultValue)
m_DisplayName = displayName;
m_SlotType = slotType;
m_ValueType = valueType;
m_DefaultValue = defaultValue;

get { return m_Name; }
}
public string displayName
{
get { return m_DisplayName; }
}
public GUID nodeGuid
{
get { return m_NodeGUID; }

set { m_CurrentValue = value; }
}
public void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
public string GetInputName (BaseMaterialNode node)
// no need to generate a property block.
// we can just set the uniforms.
return string.Format( "{0}_{1}", node.name, name);
public string GetInputName (string nodeName)
{
return string.Format( "{0}_{1}", nodeName, name);
}
visitor.AddShaderChunk("float" + BaseMaterialNode.ConvertConcreteSlotValueTypeToString(slotValueType) + " " + GetInputName(owner.name) + ";", true);
visitor.AddShaderChunk("float" + BaseMaterialNode.ConvertConcreteSlotValueTypeToString(slotValueType) + " " + GetInputName(owner) + ";", true);
return GetInputName(owner.name);
return GetInputName(owner);
switch (slotValueType)
{

正在加载...
取消
保存