浏览代码

Merge pull request #79 from Unity-Technologies/improve-texture-node

Improve texture node
/main
GitHub 7 年前
当前提交
713f6000
共有 34 个文件被更改,包括 458 次插入493 次删除
  1. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs
  3. 34
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/TextureSamplerNode.cs
  4. 80
      MaterialGraphProject/Assets/PartyPreview.ShaderGraph
  5. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BitangentMaterialSlot.cs
  6. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  7. 57
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  8. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs
  9. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs
  10. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs
  11. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/NormalMaterialSlot.cs
  12. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PositionMaterialSlot.cs
  13. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateMaterialSlot.cs
  14. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs
  15. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TangentMaterialSlot.cs
  16. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DMaterialSlot.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs
  18. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs
  19. 28
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  20. 26
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  21. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  22. 26
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  23. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs
  24. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  25. 219
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureNode.cs
  26. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs
  27. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  28. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs
  29. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs
  30. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  31. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs
  32. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs.meta
  33. 72
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs
  34. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs.meta

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


m_SerializableEdges = null;
foreach (var edge in m_Edges)
AddEdgeToNodeEdges(edge);
OnEnable();
ValidateGraph();
}
public virtual void ValidateGraph()

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs


m_DeserializedGraph = deserializedGraph; // graph.ReplaceWith(m_DeserializedGraph);
}
void Validate()
{
if (graph != null)
{
graph.OnEnable();
graph.ValidateGraph();
}
}
Validate();
Undo.undoRedoPerformed += UndoRedoPerformed;
UndoRedoPerformed();
}

34
MaterialGraphProject/Assets/NewNodes/Editor/Keep/TextureSamplerNode.cs


AddSlot(new Vector1MaterialSlot(OutputSlotGId, kOutputSlotGName, kOutputSlotGName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, 0));
AddSlot(new Texture2DMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName, SlotType.Input));
AddSlot(new Vector2MaterialSlot(UVInput, kUVInputName, kUVInputName, SlotType.Input, Vector4.zero));
AddSlot(new Texture2DInputMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName));
AddSlot(new UVMaterialSlot(UVInput, kUVInputName, kUVInputName, UVChannel.uv0));
AddSlot(new SamplerStateMaterialSlot(SamplerInput, kSamplerInputName, kSamplerInputName, SlotType.Input));
RemoveSlotsNameNotMatching(new[] { OutputSlotRGBAId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId, TextureInputId, UVInput, SamplerInput });
}

{
//Texture input slot
var textureSlot = FindInputSlot<MaterialSlot>(TextureInputId);
var edgesTexture = owner.GetEdges(textureSlot.slotReference);
// if no texture connected return nothing
if (!edgesTexture.Any())
{
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForSlot(OutputSlotRGBAId) + " = " + precision + "4(0,0,0,0);", true);
return;
}
//UV input slot
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edgesUV = owner.GetEdges(uvSlot.slotReference);
if (edgesUV.Any())
uvName = GetSlotValue(UVInput, generationMode);
var uvName = GetSlotValue(UVInput, generationMode);
//Sampler input slot
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInput);

public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
{
var uvSlot = FindInputSlot<MaterialSlot>(UVInput) as UVMaterialSlot;
if (uvSlot == null)
}
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
if (uvSlot == null)
return true;
if (uvSlot.isConnected)
return false;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
return uvSlot.RequiresMeshUV(channel);
}
}
}

80
MaterialGraphProject/Assets/PartyPreview.ShaderGraph
文件差异内容过多而无法显示
查看文件

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BitangentMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
public class BitangentMaterialSlot : Vector3MaterialSlot, IMayRequireBitangent
public class BitangentMaterialSlot : SpaceMaterialSlot, IMayRequireBitangent
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
public BitangentMaterialSlot() : base()
{}
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
IShaderProperty property;
switch (concreteValueType)
{
case ConcreteSlotValueType.Vector4:
property = new Vector4ShaderProperty();
break;
case ConcreteSlotValueType.Vector3:
property = new Vector3ShaderProperty();
break;
case ConcreteSlotValueType.Vector2:
property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as DynamicVectorMaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

57
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs


case SlotValueType.Matrix2:
return new Matrix2MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Texture2D:
return new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
return slotType == SlotType.Input
? new Texture2DInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Dynamic:
return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector4:

if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
if (concreteValueType == ConcreteSlotValueType.Texture2D)
return Texture2DMaterialSlot.DefaultTextureName;
if (generationMode.IsPreview())
return matOwner.GetVariableNameForSlot(id);

return "error";
}
public void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
// share tex2d for all non connected slots :)
if (concreteValueType == ConcreteSlotValueType.Texture2D)
{
var prop = new TextureShaderProperty();
prop.overrideReferenceName = Texture2DMaterialSlot.DefaultTextureName;
prop.modifiable = false;
prop.generatePropertyBlock = true;
properties.AddShaderProperty(prop);
return;
}
if (concreteValueType == ConcreteSlotValueType.SamplerState)
return;
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
IShaderProperty property;
switch (concreteValueType)
{
case ConcreteSlotValueType.Vector4:
property = new Vector4ShaderProperty();
break;
case ConcreteSlotValueType.Vector3:
property = new Vector3ShaderProperty();
break;
case ConcreteSlotValueType.Vector2:
property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
public abstract void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode);
protected static PropertyType ConvertConcreteSlotValueTypeToPropertyType(ConcreteSlotValueType slotValue)
{

{
return null;
}
public abstract void CopyValuesFrom(MaterialSlot foundSlot);
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs


return precision + "2x2 (1,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
}
}

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs


return precision + "3x3 (1,0,0,0,1,0,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override SlotValueType valueType { get { return SlotValueType.Matrix3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix3; } }
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs


return precision + "4x4 (1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override SlotValueType valueType { get { return SlotValueType.Matrix4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix4; } }
}

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/NormalMaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class NormalMaterialSlot : Vector3MaterialSlot, IMayRequireNormal
public class NormalMaterialSlot : SpaceMaterialSlot, IMayRequireNormal
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
public NormalMaterialSlot()
{}
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{

17
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PositionMaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class PositionMaterialSlot : Vector3MaterialSlot, IMayRequirePosition
public class PositionMaterialSlot : SpaceMaterialSlot, IMayRequirePosition
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
public PositionMaterialSlot()
{}
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateMaterialSlot.cs


public override SlotValueType valueType { get { return SlotValueType.SamplerState; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.SamplerState; } }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs


using System;
using UnityEditor;
using UnityEngine;
namespace UnityEditor.ShaderGraph

{
[SerializeField] private string m_SerializedTexture;
[SerializeField]
private string m_SerializedTexture;
[Serializable]
private class TextureHelper

#if UNITY_EDITOR
public Texture texture
{
get

m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
}
}
#else
public Texture defaultTexture {get; set; }
#endif
}
}

17
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TangentMaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class TangentMaterialSlot : Vector3MaterialSlot, IMayRequireTangent
public class TangentMaterialSlot : SpaceMaterialSlot, IMayRequireTangent
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
public TangentMaterialSlot()
{}
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DMaterialSlot.cs


public class Texture2DMaterialSlot : MaterialSlot
{
public Texture2DMaterialSlot()
{
}
{}
public Texture2DMaterialSlot(
int slotId,

ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
public static readonly string DefaultTextureName = "ShaderGraph_DefaultTexture";
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs


public class TextureShaderProperty : AbstractShaderProperty<SerializableTexture>
{
[SerializeField]
private bool m_Modifiable;
private bool m_Modifiable = true;
public TextureShaderProperty()
{

var result = new StringBuilder();
if (!m_Modifiable)
{
result.Append("[HideInInspector] ");
result.Append("[NonModifiableTextureData] ");
}
result.Append("[NoScaleOffset] ");

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs


set { m_Channel = value; }
}
public UVMaterialSlot()
{}
public UVMaterialSlot(int slotId, string displayName, string shaderOutputName, UVChannel channel,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector2.zero, shaderStage, hidden)

return false;
return m_Channel == channel;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as UVMaterialSlot;
if (slot != null)
channel = slot.channel;
}
}
}

28
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


private float m_DefaultValue;
public Vector1MaterialSlot()
{
}
{}
public Vector1MaterialSlot(
int slotId,

return value.ToString();
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new FloatShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }

m_Color = new Vector4(value, value, value, value),
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector1MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

26
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs


return precision + "2 (" + value.x + "," + value.y + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Vector2ShaderProperty
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty

public override SlotValueType valueType { get { return SlotValueType.Vector2; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector2; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector2MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs


return precision + "3 (" + value.x + "," + value.y + "," + value.z + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Vector3ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty

public override SlotValueType valueType { get { return SlotValueType.Vector3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector3MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

26
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs


return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Vector4ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty

public override SlotValueType valueType { get { return SlotValueType.Vector4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector4; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector4MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

17
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class ViewDirectionMaterialSlot : Vector3MaterialSlot, IMayRequireViewDirection
public class ViewDirectionMaterialSlot : SpaceMaterialSlot, IMayRequireViewDirection
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
public ViewDirectionMaterialSlot()
{}
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs


public virtual void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
{
var edges = owner.GetEdges(inputSlot.slotReference);

private ConcreteSlotValueType ConvertDynamicInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)
{
var concreteSlotValueTypes = inputTypes as IList<ConcreteSlotValueType> ?? inputTypes.ToList();
var inputTypesDistinct = concreteSlotValueTypes.Distinct().ToList();
switch (inputTypesDistinct.Count)
{

public override void ValidateNode()
{
var isInError = false;
var isInError = false;
// all children nodes needs to be updated first
// so do that here

var edges = owner.GetEdges(inputSlot.slotReference);
foreach (var edge in edges)
foreach (var edge in edges)
{
var fromSocketRef = edge.outputSlot;
var outputNode = owner.GetNodeFromGuid(fromSocketRef.nodeGuid);

{
return false;
}
public static string GetSlotDimension(ConcreteSlotValueType slotValue)
{
switch (slotValue)

if (foundSlot == null)
return;
// now copy over values :)
if (addingSlot is DynamicVectorMaterialSlot && foundSlot is DynamicVectorMaterialSlot)
(addingSlot as DynamicVectorMaterialSlot).value = (foundSlot as DynamicVectorMaterialSlot).value;
if (addingSlot is Vector1MaterialSlot && foundSlot is Vector1MaterialSlot)
(addingSlot as Vector1MaterialSlot).value = (foundSlot as Vector1MaterialSlot).value;
if (addingSlot is Vector2MaterialSlot && foundSlot is Vector2MaterialSlot)
(addingSlot as Vector2MaterialSlot).value = (foundSlot as Vector2MaterialSlot).value;
if (addingSlot is Vector3MaterialSlot && foundSlot is Vector3MaterialSlot)
(addingSlot as Vector3MaterialSlot).value = (foundSlot as Vector3MaterialSlot).value;
if (addingSlot is Vector4MaterialSlot && foundSlot is Vector4MaterialSlot)
(addingSlot as Vector4MaterialSlot).value = (foundSlot as Vector4MaterialSlot).value;
addingSlot.CopyValuesFrom(foundSlot);
}
}
}

219
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
/* [Title("Input/Texture/Texture")]
public class TextureNode : PropertyNode, IGeneratesBodyCode, IMayRequireMeshUV
{
protected const string kUVSlotName = "UV";
protected const string kOutputSlotRGBAName = "RGBA";
protected const string kOutputSlotRName = "R";
protected const string kOutputSlotGName = "G";
protected const string kOutputSlotBName = "B";
protected const string kOutputSlotAName = "A";
public const int UvSlotId = 0;
public const int OutputSlotRgbaId = 1;
public const int OutputSlotRId = 2;
public const int OutputSlotGId = 3;
public const int OutputSlotBId = 4;
public const int OutputSlotAId = 5;
[SerializeField]
private string m_SerializedTexture;
[SerializeField]
private TextureType m_TextureType;
[Serializable]
private class TextureHelper
{
public Texture texture;
}
public override bool hasPreview { get { return true; } }
#if UNITY_EDITOR
public Texture defaultTexture
{
get
{
if (string.IsNullOrEmpty(m_SerializedTexture))
return null;
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
}
set
{
if (defaultTexture == value)
return;
var tex = new TextureHelper();
tex.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
}
}
#else
public Texture defaultTexture {get; set; }
#endif
public TextureType textureType
{
get { return m_TextureType; }
set
{
if (m_TextureType == value)
return;
m_TextureType = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public TextureNode()
{
name = "Texture";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotRId, kOutputSlotRName, kOutputSlotRName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotGId, kOutputSlotGName, kOutputSlotGName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(UvSlotId, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector2, Vector4.zero, false));
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] {OutputSlotRgbaId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId, UvSlotId}; }
}
// Node generations
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return;
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
if (edges.Count > 0)
{
var edge = edges[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
uvName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector2, true);
}
string body = "tex2D (" + propertyName + ", " + uvName + ")";
if (m_TextureType == TextureType.Bump)
body = precision + "4(UnpackNormal(" + body + "), 0)";
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + body + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
string slotOutput;
switch (slotId)
{
case OutputSlotRId:
slotOutput = ".r";
break;
case OutputSlotGId:
slotOutput = ".g";
break;
case OutputSlotBId:
slotOutput = ".b";
break;
case OutputSlotAId:
slotOutput = ".a";
break;
default:
slotOutput = "";
break;
}
return GetVariableNameForNode() + slotOutput;
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(GetPreviewProperty());
}
// Properties
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderProperty(
new TexturePropertyChunk(
propertyName,
description,
defaultTexture, m_TextureType,
PropertyChunk.HideState.Visible,
exposedState == ExposedState.Exposed ?
TexturePropertyChunk.ModifiableState.Modifiable
: TexturePropertyChunk.ModifiableState.NonModifiable));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk("sampler2D " + propertyName + ";", true);
}
/*
public override bool DrawSlotDefaultInput(Rect rect, Slot inputSlot)
{
var uvSlot = FindInputSlot(kUVSlotName);
if (uvSlot != inputSlot)
return base.DrawSlotDefaultInput(rect, inputSlot);
var rectXmax = rect.xMax;
rect.x = rectXmax - 70;
rect.width = 70;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
GUI.Label(rect, "From Mesh");
return false;
}
*
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = propertyName,
m_PropType = PropertyType.Texture,
m_Texture = defaultTexture
};
}
public override PropertyType propertyType { get { return PropertyType.Texture; } }
public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
{
return false;
}
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return true;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
}
}*/
}

64
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs


namespace UnityEditor.ShaderGraph
{
[Title("Property Node")]
public class PropertyNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV, IOnAssetEnabled
public class PropertyNode : AbstractMaterialNode, IGeneratesBodyCode, IOnAssetEnabled
[SerializeField] private string m_PropertyGuidSerialized;
[SerializeField]
private string m_PropertyGuidSerialized;
public const int ROutputSlotId = 1;
public const int GOutputSlotId = 2;
public const int BOutputSlotId = 3;
public const int AOutputSlotId = 4;
public const int TOutputSlotId = 5;
public const int UVInput = 6;
public PropertyNode()
{

}
else if (property is TextureShaderProperty)
{
AddSlot(new Vector4MaterialSlot(OutputSlotId, "RGBA", "RGBA", SlotType.Output, Vector4.zero));
AddSlot(new Vector1MaterialSlot(ROutputSlotId, "R", "R", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(GOutputSlotId, "G", "G", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(BOutputSlotId, "B", "B", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(AOutputSlotId, "A", "A", SlotType.Output, 0));
AddSlot(new Texture2DMaterialSlot(TOutputSlotId, "T", "T", SlotType.Output));
AddSlot(new Vector2MaterialSlot(UVInput, "UV", "UV", SlotType.Input, Vector4.zero));
RemoveSlotsNameNotMatching(new[] {OutputSlotId, ROutputSlotId, GOutputSlotId, BOutputSlotId, AOutputSlotId, TOutputSlotId, UVInput});
AddSlot(new Texture2DMaterialSlot(OutputSlotId, "T", "T", SlotType.Output));
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
}
}

, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is TextureShaderProperty)
{
//UV input slot
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edgesUV = owner.GetEdges(uvSlot.slotReference);
if (edgesUV.Any())
uvName = GetSlotValue(UVInput, generationMode);
//Sampler input slot
var result = string.Format("{0}4 {1} = UNITY_SAMPLE_TEX2D({2},{3});"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.referenceName
, uvName);
visitor.AddShaderChunk(result, true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.r;", precision, GetVariableNameForSlot(ROutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.g;", precision, GetVariableNameForSlot(GOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.b;", precision, GetVariableNameForSlot(BOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.a;", precision, GetVariableNameForSlot(AOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
}
}
public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
return false;
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
if (uvSlot == null)
return true;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
}
[PropertyControl]

public override string GetVariableNameForSlot(int slotId)
{
if (slotId != TOutputSlotId)
var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
if (!(property is TextureShaderProperty))
var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
return property.referenceName;
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs


}
var id = prop.guid.GetHashCode();
AddSlot(MaterialSlot.CreateMaterialSlot(slotType, id, prop.displayName, prop.referenceName, SlotType.Input, prop.defaultValue));
MaterialSlot slot = MaterialSlot.CreateMaterialSlot(slotType, id, prop.displayName, prop.referenceName, SlotType.Input, prop.defaultValue);
// copy default for texture for niceness
if (slotType == SlotValueType.Texture2D && propType == PropertyType.Texture)
{
var tSlot = slot as Texture2DInputMaterialSlot;
var tProp = prop as TextureShaderProperty;
if (tSlot != null && tProp != null)
tSlot.texture = tProp.value.texture;
}
AddSlot(slot);
validNames.Add(id);
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs


using UnityEditor;
#endif
using UnityEditor.Graphing;
using UnityEditor.Graphs;
namespace UnityEditor.ShaderGraph
{

, IOnAssetEnabled
{
[SerializeField]
private string m_SerializedSubGraph = string.Empty;

prop.overrideReferenceName = fromNode.GetSlotValue(slot.id, generationMode);
}
}
else if (inSlot.concreteValueType == ConcreteSlotValueType.Texture2D)
else if (inSlot is Texture2DInputMaterialSlot)
prop.overrideReferenceName = Texture2DMaterialSlot.DefaultTextureName;
prop.overrideReferenceName = ((Texture2DInputMaterialSlot)inSlot).GetDefaultValue(generationMode);
}
else
{

outputString.AddShaderChunk("// Subgraph ends", false);
shaderBodyVisitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public void OnEnable()
{
UpdateSlots();
}
}
}

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs


using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
using UnityEngine;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{

var dynSlot = slot as Vector2MaterialSlot;
dynSlot.value = EditorGUILayout.Vector2Field(slot.displayName, dynSlot.value);
}
if (slot is Vector3MaterialSlot)
{
var dynSlot = slot as Vector3MaterialSlot;

{
var dynSlot = slot as Vector4MaterialSlot;
dynSlot.value = EditorGUILayout.Vector4Field(slot.displayName, dynSlot.value);
}
if (slot is Texture2DInputMaterialSlot)
{
var dynslot = slot as Texture2DInputMaterialSlot;
dynslot.texture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), dynslot.texture, typeof(Texture), null) as Texture;
}
return EditorGUI.EndChangeCheck();
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


void UpdatePreviewExpandedState(bool expanded)
{
node.previewExpanded = expanded;
if (m_PreviewContainer == null)
return;
if (expanded)
{
m_PreviewContainer.AddToClassList("expanded");

void UpdateTitle()
{
var subGraphNode = node as SubGraphNode;
if (subGraphNode != null)
if (subGraphNode != null && subGraphNode.subGraphAsset != null)
title = subGraphNode.subGraphAsset.name;
else
title = node.name;

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public abstract class SpaceMaterialSlot : Vector3MaterialSlot
{
[SerializeField]
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
protected SpaceMaterialSlot()
{}
protected SpaceMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as NormalMaterialSlot;
if (slot != null)
space = slot.space;
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs.meta


fileFormatVersion: 2
guid: 66199806246c45e493afdfc09208cbdc
timeCreated: 1509290088

72
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Texture2DInputMaterialSlot : Texture2DMaterialSlot
{
[SerializeField]
private SerializableTexture m_Texture = new SerializableTexture();
public Texture texture
{
get { return m_Texture.texture; }
set { m_Texture.texture = value; }
}
public Texture2DInputMaterialSlot()
{}
public Texture2DInputMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
return matOwner.GetVariableNameForSlot(id);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var prop = new TextureShaderProperty();
prop.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
prop.modifiable = false;
prop.generatePropertyBlock = true;
prop.value.texture = texture;
properties.AddShaderProperty(prop);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = PropertyType.Texture,
m_Texture = texture
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Texture2DInputMaterialSlot;
if (slot != null)
m_Texture = slot.m_Texture;
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs.meta


fileFormatVersion: 2
guid: ebeb3a21d05d49caa5d3318fb49b7612
timeCreated: 1509276977
正在加载...
取消
保存