浏览代码

[MatGraph]Use generated names for properties so that they do not conflict and are formatted properly.

/main
Tim Cooper 7 年前
当前提交
779d0288
共有 25 个文件被更改,包括 65 次插入64 次删除
  1. 2
      MaterialGraphProject/Assets/NewNodes/Keep/SamplerStateNode.cs
  2. 2
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPBR.shader
  3. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/PropertyNodePresenter.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss.meta
  6. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template
  7. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs
  8. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ColorShaderProperty.cs
  9. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/FloatShaderProperty.cs
  10. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/IShaderProperty.cs
  11. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs
  12. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/SamplerStateShaderProperty.cs
  13. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/TextureShaderProperty.cs
  14. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs
  16. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector4ShaderProperty.cs
  17. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/VectorShaderProperty.cs
  18. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/ColorNode.cs
  19. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector1Node.cs
  20. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector2Node.cs
  21. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector3Node.cs
  22. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector4Node.cs
  23. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs
  24. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  25. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/PropertyCollector.cs

2
MaterialGraphProject/Assets/NewNodes/Keep/SamplerStateNode.cs


{
properties.AddShaderProperty(new SamplerStateShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = new TextureSamplerState()
{

2
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPBR.shader


#ifndef _EMISSION
s.Emission = 0;
#else
s.Emission = LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv01.xy).rgb) * _EmissionColor.rgb;
s.Emission = LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.meshUV0.xy).rgb) * _EmissionColor.rgb;
#endif
// Alpha
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


using System;
using System.Linq;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing.Util;
using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;

using Object = UnityEngine.Object;
namespace UnityEditor.MaterialGraph.Drawing.Inspector
{

EditorGUI.BeginChangeCheck();
foreach (var property in m_Presenter.graph.properties.ToArray())
{
property.name = EditorGUILayout.DelayedTextField("Name", property.name);
property.description = EditorGUILayout.DelayedTextField("Description", property.description);
property.displayName = EditorGUILayout.DelayedTextField("Display Name", property.displayName);
if (property is FloatShaderProperty)
{

{
var fProp = property as TextureShaderProperty;
fProp.value.texture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), fProp.value.texture, typeof(Texture), null) as Texture;
fProp.modifiable = EditorGUILayout.Toggle("Modifiable", fProp.modifiable);
}
if (GUILayout.Button("Remove"))

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/PropertyNodePresenter.cs


var propertiesGUID = properties.Select(x => x.guid).ToList();
var currentSelectedIndex = propertiesGUID.IndexOf(currentGUID);
var newIndex = EditorGUILayout.Popup("Property", currentSelectedIndex, properties.Select(x => x.name).ToArray());
var newIndex = EditorGUILayout.Popup("Property", currentSelectedIndex, properties.Select(x => x.referenceName).ToArray());
if (newIndex != currentSelectedIndex)
tNode.propertyGuid = propertiesGUID[newIndex];

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss.meta


fileFormatVersion: 2
guid: 466e6b269ff1900438e46c66af51cef8
timeCreated: 1478070440
licenseType: Pro
ScriptedImporter:
userData:
assetBundleName:

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template


${SurfaceOutputRemap}
#if defined(UNITY_COLORSPACE_GAMMA)
Albedo = Albedo * Albedo;
Emission = Emission * Emission;
#endif
return FragmentLightingPBR(
IN.lwCustom,
surfaceInput.worldSpacePosition,

13
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs


set { m_Value = value; }
}
public string name
public string displayName
{
get
{

}
set { m_Name = value; }
}
public string description
public string referenceName
return string.IsNullOrEmpty(m_Description) ? name : m_Description;
return string.IsNullOrEmpty(overrideReferenceName)
? string.Format("{0}_{1}", propertyType, GuidEncoder.Encode(guid))
: overrideReferenceName;
set { m_Description = value; }
public string overrideReferenceName { get; set; } = string.Empty;
public abstract PropertyType propertyType { get; }

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ColorShaderProperty.cs


var result = new StringBuilder();
if (HDR)
result.Append("[HDR]");
result.Append(name);
result.Append(referenceName);
result.Append(description);
result.Append(displayName);
result.Append("\", Color) = (");
result.Append(value.r);
result.Append(",");

public override string GetPropertyDeclarationString()
{
return "float4 " + name + ";";
return "float4 " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()

m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Color,
m_Color = value
};

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/FloatShaderProperty.cs


// result.Append("[Toggle]");
// else if (m_FloatType == FloatPropertyChunk.FloatType.PowerSlider)
// result.Append("[PowerSlider(" + m_rangeValues.z + ")]");
result.Append(name);
result.Append(referenceName);
result.Append(description);
result.Append(displayName);
//if (m_FloatType == FloatPropertyChunk.FloatType.Float || m_FloatType == FloatPropertyChunk.FloatType.Toggle)
//{

public override string GetPropertyDeclarationString()
{
return "float " + name + ";";
return "float " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()

m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Float,
m_Float = value
};

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


{
public interface IShaderProperty
{
string name { get; set; }
string description { get; set; }
string displayName { get; set; }
string referenceName { get; }
string overrideReferenceName { get; set; }
string GetPropertyBlockString();
string GetPropertyDeclarationString();

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs


using System;
using System.Collections.Generic;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph

if (concreteValueType == ConcreteSlotValueType.Texture2D)
{
var prop = new TextureShaderProperty();
prop.name = DefaultTextureName;
prop.overrideReferenceName = DefaultTextureName;
prop.modifiable = false;
prop.generatePropertyBlock = true;
properties.AddShaderProperty(prop);

throw new ArgumentOutOfRangeException();
}
property.name = matOwner.GetVariableNameForSlot(id);
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/SamplerStateShaderProperty.cs


public override string GetPropertyDeclarationString()
{
string ss = name + "_"
string ss = referenceName + "_"
+ Enum.GetName(typeof(TextureSamplerState.FilterMode), value.filter) + "_"
+ Enum.GetName(typeof(TextureSamplerState.WrapMode), value.wrap) + "_sampler;";

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


}
result.Append("[NoScaleOffset] ");
result.Append(name);
result.Append(referenceName);
result.Append(description);
result.Append(displayName);
result.Append("\", 2D) = \"white\" {}");
return result.ToString();
}

return "UNITY_DECLARE_TEX2D(" + name + ");";
return "UNITY_DECLARE_TEX2D(" + referenceName + ");";
}

{
m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Texture,
m_Texture = value.texture
};

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Vector2,
m_Vector4 = value
};

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Vector3,
m_Vector4 = value
};

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector4ShaderProperty.cs


{
return new PreviewProperty()
{
m_Name = name,
m_Name = referenceName,
m_PropType = PropertyType.Vector4,
m_Vector4 = value
};

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/VectorShaderProperty.cs


public override string GetPropertyBlockString()
{
var result = new StringBuilder();
result.Append(name);
result.Append(referenceName);
result.Append(description);
result.Append(displayName);
result.Append("\", Vector) = (");
result.Append(value.x);
result.Append(",");

public override string GetPropertyDeclarationString()
{
return "float4 " + name + ";";
return "float4 " + referenceName + ";";
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/ColorNode.cs


properties.AddShaderProperty(new ColorShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = color,
HDR = HDR

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector1Node.cs


properties.AddShaderProperty(new FloatShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector2Node.cs


properties.AddShaderProperty(new Vector2ShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector3Node.cs


properties.AddShaderProperty(new Vector3ShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector4Node.cs


properties.AddShaderProperty(new Vector4ShaderProperty()
{
name = GetVariableNameForNode(),
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});

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


var result = string.Format("{0} {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name);
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector2ShaderProperty)

, GetVariableNameForSlot(OutputSlotId)
, property.name);
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector3ShaderProperty)

, GetVariableNameForSlot(OutputSlotId)
, property.name);
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is Vector4ShaderProperty)

, GetVariableNameForSlot(OutputSlotId)
, property.name);
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is ColorShaderProperty)

, GetVariableNameForSlot(OutputSlotId)
, property.name);
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is TextureShaderProperty)

var result = string.Format("{0}4 {1} = UNITY_SAMPLE_TEX2D({2},{3});"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.name
, property.referenceName
, uvName);
visitor.AddShaderChunk(result, true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.r;", precision, GetVariableNameForSlot(ROutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);

var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
return property.name;
return property.referenceName;
}
protected override bool CalculateNodeHasError()

15
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs


}
var id = prop.guid.GetHashCode();
AddSlot(new MaterialSlot(id, prop.name, prop.name, SlotType.Input, slotType, prop.defaultValue));
AddSlot(new MaterialSlot(id, prop.referenceName, prop.referenceName, SlotType.Input, slotType, prop.defaultValue));
validNames.Add(id);
}

// First we assign the input variables to the subgraph
// we do this by renaming the properties to be the names of where the variables come from
// weird, but works.
var sSubGraph = SerializationHelper.Serialize<SubGraph>(subGraphAsset.subGraph);
var sSubGraph = SerializationHelper.Serialize(subGraphAsset.subGraph);
//todo:
// copy whole subgraph
// rename properties to match what we want (external scope)
// then generate graph
var propertyGen = new PropertyCollector();
subGraph.CollectShaderProperties(propertyGen, GenerationMode.ForReals);

{
var slot = fromNode.FindOutputSlot<MaterialSlot>(fromSocketRef.slotId);
if (slot != null)
prop.name = fromNode.GetSlotValue(slot.id, generationMode);
prop.overrideReferenceName = fromNode.GetSlotValue(slot.id, generationMode);
prop.name = MaterialSlot.DefaultTextureName;
prop.overrideReferenceName = MaterialSlot.DefaultTextureName;
var varName = prop.name;
var varName = prop.referenceName;
outputString.AddShaderChunk(
ConvertConcreteSlotValueTypeToString(precision, inSlot.concreteValueType)
+ " "

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/PropertyCollector.cs


public void AddShaderProperty(IShaderProperty chunk)
{
if (m_Properties.Any(x => x.name == chunk.name))
if (m_Properties.Any(x => x.referenceName == chunk.referenceName))
return;
m_Properties.Add(chunk);
}

foreach (var prop in m_Properties.OfType<TextureShaderProperty>())
{
if (prop.name != null)
if (prop.referenceName != null)
name = prop.name,
name = prop.referenceName,
textureId = prop.value.texture != null ? prop.value.texture.GetInstanceID() : 0,
modifiable = prop.modifiable
};

正在加载...
取消
保存