浏览代码

[Shader Graph] Add channel types. This allows for visual errors if you try to promote vectors.

/main
Tim Cooper 9 年前
当前提交
7f682001
共有 30 个文件被更改,包括 527 次插入101 次删除
  1. 8
      UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs
  2. 1
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  3. 4
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/SimpleWidgets.cs
  4. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Interfaces/Interfaces.cs
  5. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialSubGraph.cs
  6. 249
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  7. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/ColorNode.cs
  8. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function1Input.cs
  9. 18
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function2Input.cs
  10. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/MultiplyNode.cs
  11. 3
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  12. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyNode.cs
  13. 34
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SlotValue.cs
  14. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SubGraphInputsNode.cs
  15. 8
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SubGraphNode.cs
  16. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TextureNode.cs
  17. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector1Node.cs
  18. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector4Node.cs
  19. 10
      UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs
  20. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/ColorProperty.cs
  21. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/FloatProperty.cs
  22. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/MaterialProperties.cs
  23. 3
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/ShaderProperty.cs
  24. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/TextureProperty.cs
  25. 4
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/VectorProperty.cs
  26. 64
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs
  27. 48
      UnityProject/ProjectSettings/ProjectSettings.asset
  28. 2
      UnityProject/ProjectSettings/ProjectVersion.txt
  29. 110
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs
  30. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs.meta

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


{
get { return isAwake && nodes.Any(x => x is IRequiresTime); }
}
protected abstract void RecacheActiveNodes();
public override void RemoveEdge(Edge e)

return;
RecacheActiveNodes();
UpdateNodeErrorState();
toNode.RegeneratePreviewShaders();
}

// remove any inputs that exits before adding
foreach (var edge in inputSlot.edges.ToArray())
{
Debug.Log("Removing edge:" + edge);
Debug.Log("Removing existing edge:" + edge);
// call base here as we DO NOT want to
// do expensive shader regeneration
base.RemoveEdge(edge);

return newEdge;
RecacheActiveNodes();
UpdateNodeErrorState();
protected abstract void UpdateNodeErrorState();
public override void AddNode(Node node)
{

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


m_Title = node.name;
m_OutputType = outputType;
m_Data = data;
error = node.hasError;
const float yStart = 10.0f;
var vector3 = new Vector3(5.0f, yStart, 0.0f);

4
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/SimpleWidgets.cs


{
protected string m_Title = "simpleBox";
protected bool error = false;
public SimpleBox(Vector2 position, float width)
{
translation = position;

{
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.7f);
Color selectedColor = new Color(1.0f, 0.7f, 0.0f, 0.7f);
EditorGUI.DrawRect(new Rect(0, 0, scale.x, scale.y), selected ? selectedColor : backgroundColor);
EditorGUI.DrawRect(new Rect(0, 0, scale.x, scale.y), error ? Color.red : selected ? selectedColor : backgroundColor);
GUI.Label(new Rect(0, 0, scale.x, 26f), GUIContent.none, new GUIStyle("preToolbar"));
GUI.Label(new Rect(10, 2, scale.x - 20.0f, 16.0f), m_Title, EditorStyles.toolbarTextField);
base.Render(parentRect, canvas);

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


public interface IGenerateProperties
{
void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode);
void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode);
void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType);
}
}

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialSubGraph.cs


{
}
protected override void UpdateNodeErrorState()
{
throw new System.NotImplementedException();
}
public new void OnEnable()
{
base.OnEnable();

n.GeneratePropertyBlock(visitor, generationMode);
}
public void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
n.GeneratePropertyUsages(visitor, generationMode);
n.GeneratePropertyUsages(visitor, generationMode, slotValueType);
}
// Returns a list of preview properties that need to be set

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor.Graphs;
using UnityEditorInternal;
using UnityEngine;

[SerializeField]
private List<SlotDefaultValueKVP> m_SlotDefaultValues;
private readonly Dictionary<string, SlotValueType> m_SlotValueTypes = new Dictionary<string, SlotValueType>();
private readonly Dictionary<string, ConcreteSlotValueType> m_ConcreteInputSlotValueTypes = new Dictionary<string, ConcreteSlotValueType>();
private readonly Dictionary<string, ConcreteSlotValueType> m_ConcreteOutputSlotValueTypes = new Dictionary<string, ConcreteSlotValueType>();
#endregion
#region Properties

if (defaultValue == null)
return;
Debug.LogFormat("Configuring Default: {0} on {1}.{2}", defaultValue, this, slotName);
protected void SetSlotDefaultValueType(string slot, SlotValueType slotType)
{
if (string.IsNullOrEmpty(slot))
return;
m_SlotValueTypes.Add(slot, slotType);
}
public string precision
{

get { return kPreviewHeight; }
}
public bool errorsCalculated { get; set; }
private bool m_HasError;
public bool hasError
{
get
{
if (!errorsCalculated)
UpdateConcreteSlotValueTypes();
return m_HasError;
}
}
public Dictionary<string, ConcreteSlotValueType> concreteInputSlotValueTypes
{
get
{
if (!errorsCalculated)
UpdateConcreteSlotValueTypes();
return m_ConcreteInputSlotValueTypes;
}
}
public Dictionary<string, ConcreteSlotValueType> concreteOutputSlotValueTypes
{
get
{
if (!errorsCalculated)
UpdateConcreteSlotValueTypes();
return m_ConcreteOutputSlotValueTypes;
}
}
#endregion
public virtual void OnCreate()

#region Previews
public virtual bool UpdatePreviewMaterial()
{
if (hasError)
return false;
var resultShader = ShaderGenerator.GeneratePreviewShader(this, out m_GeneratedShaderMode);
InternalUpdatePreviewShader(resultShader);
return true;

}
private static Mesh[] s_Meshes = { null, null, null, null };
/// <summary>
/// RenderPreview gets called in OnPreviewGUI. Nodes can override
/// RenderPreview and do their own rendering to the render 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.Vector4:
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4);

foreach (var node in childrenNodes)
node.CollectPreviewMaterialProperties(pList);
if (!hasPreview)
return;
foreach (var prop in pList)
SetPreviewMaterialProperty (prop, previewMaterial);
}

{
if (mgSlot.slot == null)
return;
Debug.Log(mgSlot);
var slot = mgSlot.slot;
if (this[slot.name] == null)

if (slotValue == null || !slotValue.IsValid())
SetSlotDefaultValue(slot.name, new SlotValue(this, slot.name, GetNewSlotDefaultValue(mgSlot.valueType)));
SetSlotDefaultValueType(slot.name, mgSlot.valueType);
// slots are not serialzied but the default values are
// because of this we need to see if the default has
// already been set

}
public override void RemoveSlot(Slot slot)
{
SetSlotDefaultValue(slot.name, null);

public virtual void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{}
public virtual void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public virtual void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
if (!generationMode.IsPreview())
return;

if (inputSlot.edges.Count > 0)
continue;
Debug.LogFormat("On {0} and trying to genereate for {1}", this, inputSlot);
defaultForSlot.GeneratePropertyUsages(visitor, generationMode);
defaultForSlot.GeneratePropertyUsages(visitor, generationMode, concreteInputSlotValueTypes[inputSlot.name]);
}
}

else
{
var defaultValue = GetSlotDefaultValue(inputSlot.name);
Debug.LogFormat("Searching for {0} on {1}", inputSlot.name, this);
inputValue = defaultValue.GetDefaultValue(generationMode);
inputValue = defaultValue.GetDefaultValue(generationMode, concreteInputSlotValueTypes[inputSlot.name]);
}
return inputValue;
}

{
Debug.LogWarningFormat("Removing Invalid Slot Default: {0}", invalidSlot);
m_SlotDefaultValues.RemoveAll(x => x.slotName == invalidSlot);
}
}
public ConcreteSlotValueType GetConcreteOutputSlotValueType(Slot slot)
{
return m_ConcreteOutputSlotValueTypes[slot.name];
}
public ConcreteSlotValueType GetConcreteInputSlotValueType(Slot slot)
{
return m_ConcreteInputSlotValueTypes[slot.name];
}
private ConcreteSlotValueType MaximumChannels(ConcreteSlotValueType @from, ConcreteSlotValueType to)
{
// if we have a singe channel input
// return the other channel.
if (@from == ConcreteSlotValueType.Vector1 || to == ConcreteSlotValueType.Vector1)
return @from > to ? @from : to;
if (ImplicitConversionExists(@from, to))
return to;
return ConcreteSlotValueType.Error;
}
private static ConcreteSlotValueType ToConcreteType(SlotValueType svt)
{
switch (svt)
{
case SlotValueType.Vector1:
return ConcreteSlotValueType.Vector1;
case SlotValueType.Vector2:
return ConcreteSlotValueType.Vector2;
case SlotValueType.Vector3:
return ConcreteSlotValueType.Vector3;
case SlotValueType.Vector4:
return ConcreteSlotValueType.Vector4;
}
return ConcreteSlotValueType.Error;
}
private static bool ImplicitConversionExists (ConcreteSlotValueType from, ConcreteSlotValueType to)
{
return (from >= to);
}
protected virtual ConcreteSlotValueType ConvertDynamicInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)
{
var concreteSlotValueTypes = inputTypes as IList<ConcreteSlotValueType> ?? inputTypes.ToList();
if (concreteSlotValueTypes.Any(x => x == ConcreteSlotValueType.Error))
return ConcreteSlotValueType.Error;
var inputTypesDistinct = concreteSlotValueTypes.Distinct().ToList();
switch (inputTypesDistinct.Count)
{
case 0:
return ConcreteSlotValueType.Vector1;
case 1:
return inputTypesDistinct.FirstOrDefault();
case 2:
return MaximumChannels(inputTypesDistinct[0], inputTypesDistinct[1]);
default:
return ConcreteSlotValueType.Error;
}
}
public void UpdateConcreteSlotValueTypes()
{
if (errorsCalculated)
return;
// all children nodes needs to be updated first
// so do that here
foreach (var inputSlot in inputSlots)
{
foreach (var edge in inputSlot.edges)
{
var outputSlot = edge.fromSlot;
var outputNode = (BaseMaterialNode) outputSlot.node;
outputNode.UpdateConcreteSlotValueTypes();
}
}
m_ConcreteInputSlotValueTypes.Clear();
m_ConcreteOutputSlotValueTypes.Clear();
var dynamicInputSlotsToCompare = new Dictionary<string, ConcreteSlotValueType>();
var skippedDynamicSlots = new List<Slot>();
// iterate the input slots
foreach (var inputSlot in inputSlots)
{
var inputType = m_SlotValueTypes[inputSlot.name];
// if there is a connection
if (inputSlot.edges.Count == 0)
{
if (inputType != SlotValueType.Vector4Dynamic)
m_ConcreteInputSlotValueTypes.Add(inputSlot.name, ToConcreteType(inputType));
else
skippedDynamicSlots.Add(inputSlot);
continue;
}
// get the output details
var outputSlot = inputSlot.edges[0].fromSlot;
var outputNode = (BaseMaterialNode) outputSlot.node;
var outputConcreteType = outputNode.GetConcreteOutputSlotValueType(outputSlot);
// if we have a standard connection... just check the types work!
if (inputType != SlotValueType.Vector4Dynamic)
{
var inputConcreteType = ToConcreteType(inputType);
m_ConcreteInputSlotValueTypes.Add(inputSlot.name, MaximumChannels (outputConcreteType, inputConcreteType));
continue;
}
// dynamic input... depends on output from other node.
// we need to compare ALL dynamic inputs to make sure they
// are compatable.
dynamicInputSlotsToCompare.Add(inputSlot.name, outputConcreteType);
}
// we can now figure out the dynamic type
// from here set all the
var dynamicType = ConvertDynamicInputTypeToConcrete(dynamicInputSlotsToCompare.Values);
foreach (var dynamicKvP in dynamicInputSlotsToCompare)
m_ConcreteInputSlotValueTypes.Add(dynamicKvP.Key, dynamicType);
foreach (var skippedSlot in skippedDynamicSlots)
m_ConcreteInputSlotValueTypes.Add(skippedSlot.name, dynamicType);
bool inputError = m_ConcreteInputSlotValueTypes.Any(x => x.Value == ConcreteSlotValueType.Error);
// configure the output slots now
// their type will either be the default output type
// or the above dynanic type for dynamic nodes
// or error if there is an input erro
foreach (var outputSlot in outputSlots)
{
if (inputError)
{
m_ConcreteOutputSlotValueTypes.Add(outputSlot.name, ConcreteSlotValueType.Error);
continue;
}
if (m_SlotValueTypes[outputSlot.name] == SlotValueType.Vector4Dynamic)
{
m_ConcreteOutputSlotValueTypes.Add(outputSlot.name, dynamicType);
continue;
}
m_ConcreteOutputSlotValueTypes.Add(outputSlot.name, ToConcreteType(m_SlotValueTypes[outputSlot.name]));
}
m_HasError = inputError || m_ConcreteOutputSlotValueTypes.Values.Any(x => x == ConcreteSlotValueType.Error);
errorsCalculated = true;
}
public static string ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType slotValue)
{
switch (slotValue)
{
case ConcreteSlotValueType.Vector1:
return string.Empty;
case ConcreteSlotValueType.Vector2:
return "2";
case ConcreteSlotValueType.Vector3:
return "3";
case ConcreteSlotValueType.Vector4:
return "4";
default:
return "Error";
}
}
}

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


return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;

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


else
{
var defaultValue = GetSlotDefaultValue(inputSlot.name);
inputValue = defaultValue.GetDefaultValue(generationMode);
inputValue = defaultValue.GetDefaultValue(generationMode, concreteInputSlotValueTypes[inputSlot.name]);
}
visitor.AddShaderChunk(precision + "4 " + GetOutputVariableNameForSlot(outputSlot, generationMode) + " = " + GetFunctionCallBody(inputValue) + ";", true);

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


string input1Value = GetSlotValue(inputSlot1, generationMode);
string input2Value = GetSlotValue(inputSlot2, generationMode);
visitor.AddShaderChunk(precision + "4 " + GetOutputVariableNameForSlot(outputSlot, generationMode) + " = " + GetFunctionCallBody(input1Value, input2Value) + ";", true);
visitor.AddShaderChunk(precision + outputDimension + " " + GetOutputVariableNameForSlot(outputSlot, generationMode) + " = " + GetFunctionCallBody(input1Value, input2Value) + ";", true);
}
public string outputDimension
{
get { return ConvertConcreteSlotValueTypeToString(concreteOutputSlotValueTypes[GetOutputSlotName()]); }
}
public string input1Dimension
{
get { return ConvertConcreteSlotValueTypeToString(concreteInputSlotValueTypes[GetInputSlot1Name()]); }
}
public string input2Dimension
{
get { return ConvertConcreteSlotValueTypeToString(concreteInputSlotValueTypes[GetInputSlot2Name()]); }
}
}
}

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/MultiplyNode.cs


namespace UnityEditor.MaterialGraph
{
[Title("Math/Multiply Node")]
class MultiplyNode : Function2Input, IGeneratesFunction
internal class MultiplyNode : Function2Input, IGeneratesFunction
{
public override void OnCreate()
{

protected override string GetFunctionName() { return "unity_multiply_" + precision; }
protected override string GetFunctionName()
{
return "unity_multiply_" + precision;
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{

outputString.AddShaderChunk("inline " + precision + "4 unity_multiply_" + precision + " (" + precision + "4 arg1, " + precision + "4 arg2)", false);
outputString.AddShaderChunk("inline " + precision + outputDimension + " unity_multiply_" + precision + " (" + precision + input1Dimension + " arg1, " + precision + input2Dimension + " arg2)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg1 * arg2;", false);

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


public override bool UpdatePreviewMaterial()
{
if (hasError)
return false;
var shaderName = "Hidden/PreviewShader/" + name + "_" + Math.Abs(GetInstanceID());
Dictionary<string, Texture> defaultTextures;
var resultShader = ShaderGenerator.GenerateSurfaceShader(pixelGraph.owner, shaderName, true, out defaultTextures);

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


namespace UnityEditor.MaterialGraph
{
public abstract class PropertyNode : BaseMaterialNode, IGenerateProperties
public abstract class PropertyNode : BaseMaterialNode
{
[SerializeField]
private ShaderProperty m_BoundProperty;

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


Vector2,
Vector1
}
public enum ConcreteSlotValueType
{
Vector4 = 4,
Vector3 = 3,
Vector2 = 2,
Vector1 = 1,
Error = 0
}
[Serializable]
public class SlotValue : IGenerateProperties

get { return nodeName + "_" + slotName; }
}
public void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
visitor.AddShaderChunk("float4 " + inputName + ";", true);
visitor.AddShaderChunk("float" + BaseMaterialNode.ConvertConcreteSlotValueTypeToString(slotValueType) + " " + inputName + ";", true);
public string GetDefaultValue(GenerationMode generationMode)
public string GetDefaultValue(GenerationMode generationMode, ConcreteSlotValueType slotValueType)
if (!generationMode.IsPreview())
return "half4 (" + m_DefaultVector.x + "," + m_DefaultVector.y + "," + m_DefaultVector.z + "," + m_DefaultVector.w + ")";
else
if (generationMode.IsPreview())
switch (slotValueType)
{
case ConcreteSlotValueType.Vector1:
return m_DefaultVector.x.ToString();
case ConcreteSlotValueType.Vector2:
return "half2 (" + m_DefaultVector.x + "," + m_DefaultVector.y + ")";
case ConcreteSlotValueType.Vector3:
return "half3 (" + m_DefaultVector.x + "," + m_DefaultVector.y + "," + m_DefaultVector.z + ")";
case ConcreteSlotValueType.Vector4:
return "half4 (" + m_DefaultVector.x + "," + m_DefaultVector.y + "," + m_DefaultVector.z + "," + m_DefaultVector.w + ")";
default:
return "error";
}
return inputName;
}
public bool OnGUI()

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


}
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
base.GeneratePropertyUsages(visitor, generationMode);
base.GeneratePropertyUsages(visitor, generationMode, slotValueType);
if (!generationMode.IsPreview())
return;

var defaultValue = GetSlotDefaultValue(slot.name);
if (defaultValue != null)
defaultValue.GeneratePropertyUsages(visitor, generationMode);
defaultValue.GeneratePropertyUsages(visitor, generationMode, slotValueType);
}
}

8
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SubGraphNode.cs


var slotDefaultValue = GetSlotDefaultValue(slot.name);
if (slotDefaultValue != null)
{
varValue = slotDefaultValue.GetDefaultValue(generationMode);
varValue = slotDefaultValue.GetDefaultValue(generationMode, concreteInputSlotValueTypes[slot.name]);
}
bool externallyWired = slot.edges.Count > 0;
if (externallyWired)

m_SubGraphAsset.GeneratePropertyBlock(visitor, GenerationMode.SurfaceShader);
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
base.GeneratePropertyUsages(visitor, generationMode);
m_SubGraphAsset.GeneratePropertyUsages(visitor, GenerationMode.SurfaceShader);
base.GeneratePropertyUsages(visitor, generationMode, slotValueType);
m_SubGraphAsset.GeneratePropertyUsages(visitor, GenerationMode.SurfaceShader, slotValueType);
}
protected override void CollectPreviewMaterialProperties (List<PreviewProperty> properties)

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


public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
if (HasBoundProperty())
return;

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


return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;

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


return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;

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


if (node is IGenerateProperties)
{
(node as IGenerateProperties).GeneratePropertyBlock(shaderProperties, genMode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, genMode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, genMode, ConcreteSlotValueType.Vector4);
}
}

return null;
return pixelMasterNode.previewMaterial;
}
protected override void UpdateNodeErrorState()
{
var bmns = nodes.Where(x => x is BaseMaterialNode).Cast<BaseMaterialNode>().ToList();
foreach (var node in bmns)
node.errorsCalculated = false;
}
}
}

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/ColorProperty.cs


visitor.AddShaderProperty(new ColorPropertyChunk(name, m_PropertyDescription, m_DefaultColor, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
visitor.AddShaderChunk("float4 " + name + ";", true);
}

6
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/FloatProperty.cs


visitor.AddShaderProperty(new FloatPropertyChunk(name, m_PropertyDescription, m_DefaultValue, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
visitor.AddShaderChunk("float4 " + name + ";", true);
visitor.AddShaderChunk("float " + name + ";", true);
return "half4 (" + m_DefaultValue + "," + m_DefaultValue + "," + m_DefaultValue + "," + m_DefaultValue + ")";
return m_DefaultValue.ToString();
}
}
}

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/MaterialProperties.cs


foreach (var property in m_ShaderProperties)
{
property.GeneratePropertyBlock(shaderProperties, generationMode);
property.GeneratePropertyUsages(propertyUsages, generationMode);
property.GeneratePropertyUsages(propertyUsages, generationMode, ConcreteSlotValueType.Vector4);
}
}
}

3
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/ShaderProperty.cs


Color,
Texture2D,
Float,
Vector2,
Vector4
}

public abstract PropertyType propertyType { get; }
public abstract void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode);
public abstract void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode);
public abstract void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType);
public abstract string GenerateDefaultValue();
}
}

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/TextureProperty.cs


m_DefaultTextureType, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
visitor.AddShaderChunk("sampler2D " + name + ";", true);
visitor.AddShaderChunk("float4 " + name + "_ST;", true);

4
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/VectorProperty.cs


set { m_DefaultVector = (Vector4)value; }
}
public Color defaultVector
public Vector4 defaultVector
{
get { return m_DefaultVector; }
set { m_DefaultVector = value; }

visitor.AddShaderProperty(new VectorPropertyChunk(name, m_PropertyDescription, m_DefaultVector, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)
{
visitor.AddShaderChunk("float4 " + name + ";", true);
}

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


using System.IO;
using System.Linq;
using System.Text;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

return result;
}
private static string AdaptNodeOutput(BaseMaterialNode node, GenerationMode mode, ConcreteSlotValueType previewChannelConcreteType)
{
var outputSlot = node.outputSlots.FirstOrDefault();
if (outputSlot == null)
return string.Empty;
var outputConcreteType = node.GetConcreteOutputSlotValueType(outputSlot);
var rawOutput = node.GetOutputVariableNameForSlot(node.outputSlots.FirstOrDefault(), mode);
if (outputConcreteType == previewChannelConcreteType)
return rawOutput;
switch (previewChannelConcreteType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("({0}).x", rawOutput);
case ConcreteSlotValueType.Vector2:
switch (outputConcreteType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("half2(({0}).x, 0.0f)", rawOutput);
case ConcreteSlotValueType.Vector3:
case ConcreteSlotValueType.Vector4:
return string.Format("half2(({0}).x, ({0}).y)", rawOutput);
default:
return string.Empty;
}
case ConcreteSlotValueType.Vector3:
switch (outputConcreteType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("half3(({0}).x, 0.0f, 0.0f)", rawOutput);
case ConcreteSlotValueType.Vector2:
return string.Format("half3(({0}).x, ({0}).y, 0.0f)", rawOutput);
case ConcreteSlotValueType.Vector4:
return string.Format("half3(({0}).x, ({0}).y, ({0}).z)", rawOutput);
default:
return string.Empty;
}
case ConcreteSlotValueType.Vector4:
switch (outputConcreteType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("half4(({0}).x, 0.0f, 0.0f, 0.0f)", rawOutput);
case ConcreteSlotValueType.Vector2:
return string.Format("half4(({0}).x, ({0}).y, 0.0f, 0.0f)", rawOutput);
case ConcreteSlotValueType.Vector3:
return string.Format("half4(({0}).x, ({0}).y, ({0}).z, 0.0f)", rawOutput);
default:
return string.Empty;
}
default:
return string.Empty;
}
}
public static string GeneratePreviewShader(BaseMaterialNode node, out PreviewMode generatedShaderMode)
{
// figure out what kind of preview we want!

(activeNode as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(vertexShaderBlock, generationMode);
activeNode.GeneratePropertyBlock(shaderPropertiesVisitor, generationMode);
activeNode.GeneratePropertyUsages(shaderPropertyUsagesVisitor, generationMode);
activeNode.GeneratePropertyUsages(shaderPropertyUsagesVisitor, generationMode, ConcreteSlotValueType.Vector4);
if (shaderInputVisitor.numberOfChunks == 0)
{

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

48
UnityProject/ProjectSettings/ProjectSettings.asset


--- !u!129 &1
PlayerSettings:
m_ObjectHideFlags: 0
serializedVersion: 7
serializedVersion: 8
AndroidProfiler: 0
defaultScreenOrientation: 0
targetDevice: 2

ps4ReprojectionSupport: 0
ps4UseAudio3dBackend: 0
ps4Audio3dVirtualSpeakerCount: 14
ps4PatchPkgPath:
ps4PatchLatestPkgPath:
ps4PatchChangeinfoPath:
ps4attribUserManagement: 0
ps4attribMoveSupport: 0
ps4attrib3DSupport: 0

spritePackerPolicy:
scriptingDefineSymbols: {}
metroPackageName:
metroPackageLogo:
metroPackageLogo140:
metroPackageLogo180:
metroPackageLogo240:
metroPackageVersion:
metroCertificatePath:
metroCertificatePassword:

metroApplicationDescription:
metroStoreTileLogo80:
metroStoreTileLogo:
metroStoreTileLogo140:
metroStoreTileLogo180:
metroStoreTileWideLogo80:
metroStoreTileWideLogo:
metroStoreTileWideLogo140:
metroStoreTileWideLogo180:
metroStoreTileSmallLogo80:
metroStoreTileSmallLogo:
metroStoreTileSmallLogo140:
metroStoreTileSmallLogo180:
metroStoreSmallTile80:
metroStoreSmallTile:
metroStoreSmallTile140:
metroStoreSmallTile180:
metroStoreLargeTile80:
metroStoreLargeTile:
metroStoreLargeTile140:
metroStoreLargeTile180:
metroStoreSplashScreenImage:
metroStoreSplashScreenImage140:
metroStoreSplashScreenImage180:
metroPhoneAppIcon:
metroPhoneAppIcon140:
metroPhoneAppIcon240:
metroPhoneSmallTile:
metroPhoneSmallTile140:
metroPhoneSmallTile240:
metroPhoneMediumTile:
metroPhoneMediumTile140:
metroPhoneMediumTile240:
metroPhoneWideTile:
metroPhoneWideTile140:
metroPhoneWideTile240:
metroPhoneSplashScreenImage:
metroPhoneSplashScreenImage140:
metroPhoneSplashScreenImage240:
wsaImages: {}
metroTileShortName:
metroCommandLineArgsFile:
metroTileShowName: 0

2
UnityProject/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 5.3.0b5
m_EditorVersion: 5.3.0b6
m_StandardAssetsVersion: 0

110
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs


using System;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
[Title("Generate/Vector 2 Node")]
class Vector2Node : PropertyNode, IGeneratesBodyCode
{
private const string kOutputSlotName = "Value";
[SerializeField]
private Vector2 m_Value;
public override void OnCreate()
{
base.OnCreate();
name = "V2Node";
}
public override void OnEnable()
{
base.OnEnable();
AddSlot(new MaterialGraphSlot(new Slot(SlotType.OutputSlot, kOutputSlotName), SlotValueType.Vector2));
}
public override PropertyType propertyType
{
get { return PropertyType.Vector2; }
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{}
public override string GetOutputVariableNameForSlot(Slot s, GenerationMode generationMode)
{
return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;
visitor.AddShaderChunk("float2 " + GetPropertyName() + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (HasBoundProperty() || generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "2 " + GetPropertyName() + " = " + precision + "2 (" + m_Value.x + ", " + m_Value.y + ");", true);
}
public override float GetNodeUIHeight(float width)
{
return 2*EditorGUIUtility.singleLineHeight;
}
public override bool NodeUI(Rect drawArea)
{
base.NodeUI(drawArea);
EditorGUI.BeginChangeCheck();
m_Value = EditorGUI.Vector2Field(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), "Value", m_Value);
if (EditorGUI.EndChangeCheck())
{
var boundProp = boundProperty as VectorProperty;
if (boundProp != null)
{
boundProp.defaultVector = m_Value;
}
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;
}
public override void BindProperty(ShaderProperty property, bool rebuildShaders)
{
base.BindProperty(property, rebuildShaders);
var vectorProp = property as VectorProperty;
if (vectorProp)
{
m_Value = vectorProp.defaultVector;
}
if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = GetPropertyName(),
m_PropType = PropertyType.Vector2,
m_Vector4 = m_Value
};
}
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs.meta


fileFormatVersion: 2
guid: 8361d1f9d12a489438c0b1e2e48c8542
timeCreated: 1446473341
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存