浏览代码
Changed sampelr2D to Texture2D, added wrappers for HLSL to make decoupled texture samplers work with surface shaders, added a Sampler node, etc.
/main
Changed sampelr2D to Texture2D, added wrappers for HLSL to make decoupled texture samplers work with surface shaders, added a Sampler node, etc.
/main
vlad
8 年前
当前提交
5f08fc4e
共有 22 个文件被更改,包括 708 次插入 和 94 次删除
-
54MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
-
8MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
-
2MaterialGraphProject/Assets/NewNodes/WIP/MultiLayerParallaxNode.cs
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/subshader.template
-
25MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Filters/ConvolutionFilterNode.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyType.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
-
16MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/AbstractSurfaceMasterNode.cs
-
9MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs
-
122MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs
-
2MaterialGraphProject/Assets/_MingWai/ScatterNode.cs
-
64MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs.meta
-
52MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs.meta
-
133MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs
-
12MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs.meta
-
246MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs
-
12MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs.meta
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine.MaterialGraph; |
|||
using RMGUI.GraphView; |
|||
using UnityEditor.Graphing.Drawing; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
[Serializable] |
|||
class SamplerStateControlPresenter : GraphControlPresenter |
|||
{ |
|||
|
|||
private string[] samplerFilterMode; |
|||
private string[] samplerWrapMode; |
|||
|
|||
private string[] _samplerFilterMode |
|||
{ |
|||
get |
|||
{ |
|||
if (samplerFilterMode == null) |
|||
samplerFilterMode = Enum.GetNames(typeof(SamplerStateNode.FilterMode)); |
|||
return samplerFilterMode; |
|||
} |
|||
} |
|||
|
|||
private string[] _samplerWrapMode |
|||
{ |
|||
get |
|||
{ |
|||
if (samplerWrapMode == null) |
|||
samplerWrapMode = Enum.GetNames(typeof(SamplerStateNode.WrapMode)); |
|||
return samplerWrapMode; |
|||
} |
|||
} |
|||
|
|||
public override void OnGUIHandler() |
|||
{ |
|||
base.OnGUIHandler(); |
|||
|
|||
var cNode = node as UnityEngine.MaterialGraph.SamplerStateNode; |
|||
if (cNode == null) |
|||
return; |
|||
|
|||
cNode.filter = (SamplerStateNode.FilterMode)EditorGUILayout.Popup((int)cNode.filter, _samplerFilterMode, EditorStyles.popup); |
|||
cNode.wrap = (SamplerStateNode.WrapMode)EditorGUILayout.Popup((int)cNode.wrap, _samplerWrapMode, EditorStyles.popup); |
|||
} |
|||
|
|||
public override float GetHeight() |
|||
{ |
|||
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public class SamplerStateNodePresenter : MaterialNodePresenter |
|||
{ |
|||
protected override IEnumerable<GraphElementPresenter> GetControlData() |
|||
{ |
|||
var instance = CreateInstance<SamplerStateControlPresenter>(); |
|||
instance.Initialize(node); |
|||
return new List<GraphElementPresenter> { instance }; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8ffcc48ccff1aea44b90acb362bc0dfc |
|||
timeCreated: 1495659152 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using RMGUI.GraphView; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEngine; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
class TextureSamplerContolPresenter : GraphControlPresenter |
|||
{ |
|||
private string[] m_TextureTypeNames; |
|||
private string[] textureTypeNames |
|||
{ |
|||
get |
|||
{ |
|||
if (m_TextureTypeNames == null) |
|||
m_TextureTypeNames = Enum.GetNames(typeof(TextureType)); |
|||
return m_TextureTypeNames; |
|||
} |
|||
} |
|||
|
|||
public override void OnGUIHandler() |
|||
{ |
|||
base.OnGUIHandler(); |
|||
|
|||
var tNode = node as UnityEngine.MaterialGraph.TextureAssetNode; |
|||
if (tNode == null) |
|||
return; |
|||
|
|||
tNode.exposedState = (PropertyNode.ExposedState)EditorGUILayout.EnumPopup(new GUIContent("Exposed"), tNode.exposedState); |
|||
tNode.defaultTexture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), tNode.defaultTexture, typeof(Texture), null) as Texture; |
|||
tNode.textureType = (TextureType)EditorGUILayout.Popup((int)tNode.textureType, textureTypeNames, EditorStyles.popup); |
|||
} |
|||
|
|||
public override float GetHeight() |
|||
{ |
|||
return 3 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) + EditorGUIUtility.standardVerticalSpacing; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public class TextureSamplerNodePresenter : MaterialNodePresenter |
|||
{ |
|||
protected override IEnumerable<GraphElementPresenter> GetControlData() |
|||
{ |
|||
var instance = CreateInstance<TextureSamplerContolPresenter>(); |
|||
instance.Initialize(node); |
|||
return new List<GraphElementPresenter> { instance }; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 86fe3d2371b24d34399f4b116a238d45 |
|||
timeCreated: 1495638407 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine.MaterialGraph; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("Input/Sampler State")] |
|||
public class SamplerStateNode : AbstractMaterialNode, IGeneratesBodyCode |
|||
{ |
|||
|
|||
public enum FilterMode |
|||
{ |
|||
Linear, |
|||
Point |
|||
} |
|||
|
|||
public enum WrapMode |
|||
{ |
|||
Repeat, |
|||
Clamp |
|||
} |
|||
|
|||
static Dictionary<FilterMode, string> filterMode = new Dictionary<FilterMode, string> |
|||
{ |
|||
{FilterMode.Linear, "_linear"}, |
|||
{FilterMode.Point, "_point"}, |
|||
}; |
|||
|
|||
static Dictionary<WrapMode, string> wrapMode = new Dictionary<WrapMode, string> |
|||
{ |
|||
{WrapMode.Repeat, "_repeat"}, |
|||
{WrapMode.Clamp, "_clamp"}, |
|||
}; |
|||
|
|||
|
|||
[SerializeField] |
|||
private FilterMode m_filter = FilterMode.Linear; |
|||
|
|||
|
|||
public FilterMode filter |
|||
{ |
|||
get { return m_filter; } |
|||
set |
|||
{ |
|||
if (m_filter == value) |
|||
return; |
|||
|
|||
m_filter = value; |
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Graph); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[SerializeField] |
|||
private WrapMode m_wrap = WrapMode.Repeat; |
|||
|
|||
|
|||
public WrapMode wrap |
|||
{ |
|||
get { return m_wrap; } |
|||
set |
|||
{ |
|||
if (m_wrap == value) |
|||
return; |
|||
|
|||
m_wrap = value; |
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Graph); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public SamplerStateNode() |
|||
{ |
|||
name = "SamplerState"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public override bool hasPreview { get { return false; } } |
|||
|
|||
private const int kOutputSlotId = 0; |
|||
private const string kOutputSlotName = "Sampler Output"; |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.SamplerState, Vector4.zero, false)); |
|||
RemoveSlotsNameNotMatching(new[] { kOutputSlotId }); |
|||
} |
|||
|
|||
/* |
|||
public override PropertyType propertyType |
|||
{ |
|||
get { return PropertyType.SamplerState; } |
|||
} |
|||
|
|||
public override PreviewProperty GetPreviewProperty() |
|||
{ |
|||
return new PreviewProperty |
|||
{ |
|||
m_Name = propertyName + "HEEEEEEYYYYY", |
|||
m_PropType = PropertyType.Float, |
|||
// m_Float = filterMode[filter];
|
|||
}; |
|||
} |
|||
|
|||
*/ |
|||
|
|||
|
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
// GetVariableNameForSlot();
|
|||
} |
|||
|
|||
public override string GetVariableNameForSlot(int slotId) |
|||
{ |
|||
// var slot = FindSlot<MaterialSlot>(slotId);
|
|||
//if (slot == null)
|
|||
// throw new ArgumentException(string.Format("Attempting to use MaterialSlot({0}) on node of type {1} where this slot can not be found", slotId, this), "slotId");
|
|||
|
|||
return GetVariableNameForNode(); |
|||
} |
|||
|
|||
//my_linear_repeat_sampler
|
|||
public override string GetVariableNameForNode() |
|||
{ |
|||
return "my" + filterMode[filter] + wrapMode[wrap] + "_sampler"; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5fe2c333e043511478cc507ee61a8c21 |
|||
timeCreated: 1495658099 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
#if UNITY_EDITOR
|
|||
using UnityEditor; |
|||
#endif
|
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("Input/Texture/Texture Sampler")] |
|||
public class TextureSamplerNode : PropertyNode, IGeneratesBodyCode, IMayRequireMeshUV |
|||
{ |
|||
private const string kTextureAssetName = "Texture Asset"; |
|||
private const string kUVSlotName = "UV"; |
|||
private const string kSamplerName = "Sampler"; |
|||
protected const string kOutputSlotRGBAName = "RGBA"; |
|||
|
|||
public const int TextureAssetSlotId = 0; |
|||
public const int UVSlotId = 1; |
|||
public const int SamplerSlotId = 2; |
|||
public const int OutputSlotRGBAId = 3; |
|||
|
|||
[SerializeField] |
|||
private string m_SerializedTexture; |
|||
|
|||
[SerializeField] |
|||
private TextureType m_TextureType; |
|||
|
|||
[Serializable] |
|||
private class TextureHelper |
|||
{ |
|||
public Texture2D texture; |
|||
} |
|||
|
|||
public override bool hasPreview { get { return true; } } |
|||
|
|||
#if UNITY_EDITOR
|
|||
public Texture2D 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 Texture2D 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 TextureSamplerNode() |
|||
{ |
|||
name = "TextureSamplerNode"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new MaterialSlot(TextureAssetSlotId, kTextureAssetName, kTextureAssetName, SlotType.Input, SlotValueType.Texture2D, Vector4.zero, false)); |
|||
AddSlot(new MaterialSlot(UVSlotId, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector2, Vector4.zero, false)); |
|||
AddSlot(new MaterialSlot(SamplerSlotId, kSamplerName, kSamplerName, SlotType.Input, SlotValueType.SamplerState, Vector4.zero, false)); |
|||
AddSlot(new MaterialSlot(OutputSlotRGBAId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector4, Vector4.zero)); |
|||
RemoveSlotsNameNotMatching(validSlots); |
|||
} |
|||
|
|||
protected int[] validSlots |
|||
{ |
|||
get { return new[] { OutputSlotRGBAId, SamplerSlotId, UVSlotId, TextureAssetSlotId }; } |
|||
} |
|||
|
|||
// Node generations
|
|||
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
|
|||
//Texture input slot
|
|||
var textureSlot = FindInputSlot<MaterialSlot>(TextureAssetSlotId); |
|||
var textureAssetName = ""; |
|||
var edgesTexture = owner.GetEdges(textureSlot.slotReference).ToList(); |
|||
|
|||
if (edgesTexture.Count > 0) |
|||
{ |
|||
var edge = edgesTexture[0]; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid); |
|||
textureAssetName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Texture2D, true); |
|||
} |
|||
|
|||
//UV input slot
|
|||
var uvSlot = FindInputSlot<MaterialSlot>(UVSlotId); |
|||
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName()); |
|||
if (uvSlot == null) |
|||
return; |
|||
|
|||
var edgesUV = owner.GetEdges(uvSlot.slotReference).ToList(); |
|||
|
|||
if (edgesUV.Count > 0) |
|||
{ |
|||
var edge = edgesUV[0]; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid); |
|||
uvName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector2, true); |
|||
} |
|||
|
|||
|
|||
//Sampler input slot
|
|||
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerSlotId); |
|||
var samplerName = "my_linear_repeat_sampler"; |
|||
|
|||
if (samplerSlot == null) |
|||
return; |
|||
|
|||
var edgesSampler = owner.GetEdges(samplerSlot.slotReference).ToList(); |
|||
|
|||
if (edgesSampler.Count > 0) |
|||
{ |
|||
var edge = edgesSampler[0]; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid); |
|||
samplerName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.SamplerState, true); |
|||
} |
|||
|
|||
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + "_Uniform = " + precision + "4(0,0,0,0);", true); |
|||
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", true); |
|||
string body = textureAssetName + ".Sample(" + samplerName + ", " + uvName + ");"; |
|||
if (m_TextureType == TextureType.Bump) |
|||
body = precision + "4(UnpackNormal(" + body + "), 0)"; |
|||
|
|||
visitor.AddShaderChunk(GetVariableNameForNode() + "_Uniform" + " = " + body, true); |
|||
|
|||
visitor.AddShaderChunk("#endif", true); |
|||
} |
|||
|
|||
// 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) |
|||
{ |
|||
|
|||
//Sampler input slot
|
|||
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerSlotId); |
|||
var samplerName = "my_linear_repeat_sampler"; |
|||
|
|||
if (samplerSlot == null) |
|||
return; |
|||
|
|||
var edgesSampler = owner.GetEdges(samplerSlot.slotReference).ToList(); |
|||
|
|||
if (edgesSampler.Count > 0) |
|||
{ |
|||
var edge = edgesSampler[0]; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid); |
|||
samplerName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.SamplerState, true); |
|||
} |
|||
|
|||
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false); |
|||
visitor.AddShaderChunk(samplerSlot.valueType + " " + samplerName + ";", true); |
|||
visitor.AddShaderChunk("#endif", 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; |
|||
} |
|||
|
|||
//prevent validation errors when a sampler2D input is missing
|
|||
//use on any input requiring a TextureAssetNode
|
|||
public override void ValidateNode() |
|||
{ |
|||
base.ValidateNode(); |
|||
var slot = FindInputSlot<MaterialSlot>(TextureAssetSlotId); |
|||
if (slot == null) |
|||
return; |
|||
|
|||
var edges = owner.GetEdges(slot.slotReference).ToList(); |
|||
hasError |= edges.Count == 0; |
|||
} |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5ca9d434229e11f44888abcbf66062a5 |
|||
timeCreated: 1495637741 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue