浏览代码

Working: Properties window + new master node.

/main
Tim Cooper 7 年前
当前提交
735c401b
共有 12 个文件被更改,包括 237 次插入87 次删除
  1. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  2. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  3. 100
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
  4. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs
  5. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ShaderGraphRequirements.cs
  6. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/TextureShaderProperty.cs
  7. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  8. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/WorldSpacePositionNode.cs
  9. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs
  10. 108
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs
  11. 37
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  12. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/TextureType.cs

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs


if (graph == null)
return;
List<PropertyCollector.TextureInfo> configuredTextures = new List<PropertyCollector.TextureInfo>();
// graph.GetPreviewShader(graph.masterNode, GenerationMode.ForReals, Path.GetFileNameWithoutExtension(path), out configuredTextures);
List<PropertyCollector.TextureInfo> configuredTextures;
graph.GetShader(Path.GetFileNameWithoutExtension(path), out configuredTextures);
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter;
if (shaderImporter == null)

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs


{
var node = m_Graph.GetNodeFromGuid<AbstractMaterialNode>(nodeGuid);
node.CollectPreviewMaterialProperties(m_PreviewProperties);
foreach (var prop in m_Graph.properties)
m_PreviewProperties.Add(prop.GetPreviewMaterialProperty());
foreach (var previewProperty in m_PreviewProperties)
{
if (previewProperty.m_PropType == PropertyType.Texture && previewProperty.m_Texture != null)

if (!m_Previews.TryGetValue(nodeGuid, out previewData))
return;
if (node is MasterNode)
if (node is MasterNode && node.owner is UnityEngine.MaterialGraph.MaterialGraph)
/* var masterNode = (MasterNode)node;
List<PropertyGenerator.TextureInfo> defaultTextures;
previewData.shaderString = masterNode.(GenerationMode.Preview, node.guid + "_preview", out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;*/
var masterNode = (MasterNode)node;
var materialGraph = (UnityEngine.MaterialGraph.MaterialGraph) node.owner;
List<PropertyCollector.TextureInfo> defaultTextures;
previewData.shaderString = materialGraph.GetShader(node.guid + "_preview", out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;
}
else if (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex)
{

{
List<PropertyCollector.TextureInfo> defaultTextures;
previewData.shaderString = m_Graph.GetPreviewShader(node);
PreviewMode mode;
previewData.shaderString = m_Graph.GetPreviewShader(node, out mode);
previewData.previewMode = mode;
}
// Debug output

100
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs


bool requiresScreenPosition = activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
var meshUV = new List<UVChannel>();
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
{
var channel = (UVChannel)uvIndex;
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
meshUV.Add(channel);
}
// if anything needs tangentspace we have make
// sure to have our othonormal basis!

requiresViewDir = requiresViewDir,
requiresPosition = requiresPosition,
requiresScreenPosition = requiresScreenPosition,
requiresVertexColor = requiresVertexColor
requiresVertexColor = requiresVertexColor,
requiresMeshUVs = meshUV
};
ListPool<INode>.Release(activeNodeList);
return reqs;

surfaceInputs.AddShaderChunk(string.Format("float3 {0};", tangentSpaceName), false);
}
public string GetPreviewShader(AbstractMaterialNode node)
public string GetPreviewShader(AbstractMaterialNode node, out PreviewMode previewMode)
return GetShader(node, GenerationMode.Preview, string.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures);
return GetShader(node, GenerationMode.Preview, string.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures, out previewMode);
protected string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures)
protected string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode)
{
var vertexShader = new ShaderGenerator();
var pixelShader = new ShaderGenerator();

float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
float4 texcoord : TEXCOORD0;
float4 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};";

var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
previewMode = PreviewMode.Preview2D;
foreach (var pNode in activeNodeList.OfType<AbstractMaterialNode>())
var channel = (UVChannel) uvIndex;
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
surfaceInputs.AddShaderChunk(string.Format("half4 meshUV{0};", uvIndex), false);
if (pNode.previewMode == PreviewMode.Preview3D)
{
previewMode = PreviewMode.Preview3D;
break;
}
foreach (var channel in requirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
surfaceInputs.Deindent();
surfaceInputs.AddShaderChunk("};", false);

pixelShader.AddShaderChunk("SurfaceDescription PopulateSurfaceData(SurfaceInputs IN) {", false);
pixelShader.Indent();
var generationMode = GenerationMode.ForReals;
if ((requirements.requiresNormal & NeededCoordinateSpace.Object) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceNormal), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpaceNormal), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceNormal), false);
if ((requirements.requiresNormal & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpaceNormal), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.Object) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceTangent), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpaceTangent), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceTangent), false);
if ((requirements.requiresTangent & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpaceTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.Object) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceBiTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpaceBiTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceSpaceBiTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpaceBiTangent), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.Object) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpaceViewDirection), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceViewDirection), false);
if ((requirements.requiresViewDir & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpaceViewDirection), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.Object) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpacePosition), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpacePosition), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpacePosition), false);
if ((requirements.requiresPosition & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpacePosition), false);
if (requirements.requiresScreenPosition)
pixelShader.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ScreenPosition), false);
if (requirements.requiresVertexColor)
pixelShader.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.VertexColor), false);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
pixelShader.AddShaderChunk(string.Format("half4 {0} = IN.{0};", channel.GetUVName()), false);
CollectShaderProperties(shaderProperties, generationMode);
CollectShaderProperties(shaderProperties, mode);
(activeNode as IGeneratesFunction).GenerateNodeFunction(shaderFunctionVisitor, generationMode);
(activeNode as IGeneratesFunction).GenerateNodeFunction(shaderFunctionVisitor, mode);
(activeNode as IGeneratesBodyCode).GenerateNodeCode(pixelShader, generationMode);
(activeNode as IGeneratesBodyCode).GenerateNodeCode(pixelShader, mode);
activeNode.CollectShaderProperties(shaderProperties, generationMode);
activeNode.CollectShaderProperties(shaderProperties, mode);
}
pixelShader.AddShaderChunk("SurfaceDescription surface;", false);

finalShader.AddShaderChunk(pixelShader.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
if (generationMode == GenerationMode.Preview)
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, GetRequierments(node)), false);
if (mode == GenerationMode.Preview)
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
else
{
var master = (MasterNode) node;

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs


public string GetShader(string name, out List<PropertyCollector.TextureInfo> configuredTextures)
{
return GetShader(masterNode, GenerationMode.ForReals, name, out configuredTextures);
PreviewMode mode;
return GetShader(masterNode, GenerationMode.ForReals, name, out configuredTextures, out mode);
}
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ShaderGraphRequirements.cs


namespace UnityEngine.MaterialGraph
using System.Collections.Generic;
namespace UnityEngine.MaterialGraph
{
public struct ShaderGraphRequirements
{

public NeededCoordinateSpace requiresPosition;
public bool requiresScreenPosition;
public bool requiresVertexColor;
public List<UVChannel> requiresMeshUVs;
public bool NeedsTangentSpace()
{

return (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
}
}
}
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/TextureShaderProperty.cs


result.Append(name);
result.Append("(\"");
result.Append(description);
result.Append("\", 2D) = \"");
result.Append(Enum.GetName(typeof(TextureType), value.texture).ToLower());
result.Append("\" {}");
result.Append("\", 2D) = \"white\" {}");
return result.ToString();
}

9
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs


}
}
/*
public virtual bool DrawSlotDefaultInput(Rect rect, MaterialSlot inputSlot)
{
var inputSlotType = inputSlot.concreteValueType;
return inputSlot.OnGUI(rect, inputSlotType);
}
*/
public virtual void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
var validSlots = GetInputSlots<MaterialSlot>().ToArray();

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/WorldSpacePositionNode.cs


public override string GetVariableNameForSlot(int slotId)
{
return "IN." + ShaderGeneratorNames.WorldSpacePosition;
return ShaderGeneratorNames.WorldSpacePosition;
}
public NeededCoordinateSpace RequiresPosition()

21
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs


if (shaderGraphRequirements.requiresScreenPosition)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};;", ShaderGeneratorNames.ScreenPosition, interpolatorIndex), false);
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ScreenPosition, interpolatorIndex), false);
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
foreach (var channel in shaderGraphRequirements.requiresMeshUVs.Distinct())
var channel = (UVChannel)uvIndex;
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
{
interpolators.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex == 0 ? "" : uvIndex.ToString()), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.meshUV{1};", channel.GetUVName(), uvIndex), false);
interpolatorIndex++;
}
interpolators.AddShaderChunk(string.Format("half4 {0} : TEXCOORD{1};", channel.GetUVName(), interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.texcoord{1};", channel.GetUVName(), interpolatorIndex == 0 ? "" : interpolatorIndex.ToString()), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", channel.GetUVName()), false);
interpolatorIndex++;
outputs.AddShaderChunk(string.Format("return surf.{0};", FindSlot<MaterialSlot>(0).shaderOutputName), true);
var slot = FindSlot<MaterialSlot>(0);
var result = string.Format("surf.{0}", slot.shaderOutputName);
outputs.AddShaderChunk(string.Format("return {0};", ShaderGenerator.AdaptNodeOutputForPreview(this, 0, result)), true);
var res = subShaderTemplate.Replace("{0}", interpolators.GetShaderString(0));
res = res.Replace("{1}", vertexShader.GetShaderString(0));

108
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Property Node")]
public class PropertyNode : AbstractMaterialNode
public class PropertyNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV
[SerializeField]
private string m_PropertyGuidSerialized;
[SerializeField] private string m_PropertyGuidSerialized;
public const int OutputSlotId = 0;

public const int AOutputSlotId = 4;
public const int TOutputSlotId = 5;
public const int UVInput = 6;
public PropertyNode()
{

if (property is FloatShaderProperty)
{
AddSlot(new MaterialSlot(OutputSlotId, "float", "float", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
}
else if (property is TextureShaderProperty)
{

AddSlot(new MaterialSlot(BOutputSlotId, "B", "B", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(AOutputSlotId, "A", "A", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(TOutputSlotId, "T", "T", SlotType.Output, SlotValueType.Texture2D, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId, ROutputSlotId, GOutputSlotId, BOutputSlotId, AOutputSlotId, TOutputSlotId});
AddSlot(new MaterialSlot(UVInput, "UV", "UV", SlotType.Input, SlotValueType.Vector2, Vector4.zero));
RemoveSlotsNameNotMatching(new[] {OutputSlotId, ROutputSlotId, GOutputSlotId, BOutputSlotId, AOutputSlotId, TOutputSlotId,UVInput});
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
if (property == null)
return;
if (property is FloatShaderProperty)
{
var result = string.Format("{0} {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector2ShaderProperty)
{
var result = string.Format("{0}2 {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector3ShaderProperty)
{
var result = string.Format("{0}3 {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector4ShaderProperty)
{
var result = string.Format("{0}4 {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
visitor.AddShaderChunk(result, true);
}
else if (property is ColorShaderProperty)
{
var result = string.Format("{0}4 {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
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.name
, 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;
}
public Guid propertyGuid
{
get { return m_PropertyGuid; }

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

if (!string.IsNullOrEmpty(m_PropertyGuidSerialized))
m_PropertyGuid = new Guid(m_PropertyGuidSerialized);
}
}
}

37
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


public static string AdaptNodeOutputForPreview(AbstractMaterialNode node, int outputSlotId)
{
var outputSlot = node.FindOutputSlot<MaterialSlot>(outputSlotId);
var rawOutput = node.GetVariableNameForSlot(outputSlotId);
return AdaptNodeOutputForPreview(node, outputSlotId, rawOutput);
}
if (outputSlot == null)
return kErrorString;
public static string AdaptNodeOutputForPreview(AbstractMaterialNode node, int slotId, string variableName)
{
var slot = node.FindSlot<MaterialSlot>(slotId);
var convertFromType = outputSlot.concreteValueType;
if (slot == null)
return kErrorString;
var rawOutput = node.GetVariableNameForSlot(outputSlotId);
var convertFromType = slot.concreteValueType;
return string.Format("half4({0}, {0}, {0}, 1.0)", rawOutput);
return string.Format("half4({0}, {0}, {0}, 1.0)", variableName);
return string.Format("half4({0}.x, {0}.y, 0.0, 1.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, 0.0, 1.0)", variableName);
return string.Format("half4({0}.x, {0}.y, {0}.z, 1.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, {0}.z, 1.0)", variableName);
return string.Format("half4({0}.x, {0}.y, {0}.z, 1.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, {0}.z, 1.0)", variableName);
default:
return kErrorString;
}

{
if ((neededSpaces & NeededCoordinateSpace.Object) > 0)
{
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = {0};", objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToWorldNormal(IN.{1});", worldSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToWorldNormal({1});", worldSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToWorldDir(IN.{1});", worldSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToWorldDir({1});", worldSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToViewPos(IN.{1});", viewSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = UnityObjectToViewPos({1});", viewSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = mul(tangentSpaceTransform, IN.{1})", tangentSpaceName, objectSpaceName), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = mul(tangentSpaceTransform, {1})", tangentSpaceName, objectSpaceName), false);
}
}

var outputs = new ShaderGenerator();
var outputSlot = node.GetOutputSlots<MaterialSlot>().FirstOrDefault();
if (outputSlot != null)
outputs.AddShaderChunk(string.Format("return surf.{0};", node.GetVariableNameForSlot(outputSlot.id)), true);
{
var result = string.Format("surf.{0}", node.GetVariableNameForSlot(outputSlot.id));
outputs.AddShaderChunk(string.Format("return {0};", AdaptNodeOutputForPreview(node, outputSlot.id, result)), true);
}
else
outputs.AddShaderChunk("return 0;", true);

10
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/TextureType.cs


namespace UnityEngine.MaterialGraph
{
public enum TextureType
{
White,
Gray,
Black,
Bump
}
}
正在加载...
取消
保存