浏览代码

Changed sampelr2D to Texture2D, added wrappers for HLSL to make decoupled texture samplers work with surface shaders, added a Sampler node, etc.

/main
vlad 7 年前
当前提交
5f08fc4e
共有 22 个文件被更改,包括 708 次插入94 次删除
  1. 54
      MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
  2. 8
      MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
  3. 2
      MaterialGraphProject/Assets/NewNodes/WIP/MultiLayerParallaxNode.cs
  4. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  5. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/subshader.template
  6. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Filters/ConvolutionFilterNode.cs
  8. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs
  9. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyType.cs
  10. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
  11. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/AbstractSurfaceMasterNode.cs
  12. 9
      MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs
  13. 122
      MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs
  14. 2
      MaterialGraphProject/Assets/_MingWai/ScatterNode.cs
  15. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs
  16. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs.meta
  17. 52
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs
  18. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs.meta
  19. 133
      MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs
  20. 12
      MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs.meta
  21. 246
      MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs
  22. 12
      MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs.meta

54
MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs


else
nextSlotId = GetInputSlots<MaterialSlot>().Count() + 1;
bool useDefaultValue = (valueType != SlotValueType.sampler2D);
bool useDefaultValue = (valueType != SlotValueType.Texture2D);
AddSlot(new MaterialSlot(nextSlotId, displayName, nameInShader, slotType, valueType, defaultValue, useDefaultValue));
return nextSlotId;
}

public string GetSlotTypeName(int slotId)
{
return ConvertConcreteSlotValueTypeToString(FindSlot<MaterialSlot>(slotId).concreteValueType);
}
public ConcreteSlotValueType GetSlotValueType(int slotId)
{
return FindSlot<MaterialSlot>(slotId).concreteValueType;
}
protected string GetShaderOutputName(int slotId)

if (inSlot.isOutputSlot)
param += "out ";
if (FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.sampler2D)
if (FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.Texture2D
&& FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.SamplerState
)
param += precision;
param += GetSlotTypeName(inSlot.id) + " ";
param += GetShaderOutputName(inSlot.id);

int remainingParams = GetSlots<ISlot>().Count();
foreach (ISlot inSlot in GetSlots<ISlot>())
{
param += GetSlotValue(inSlot.id, generationMode);
if (FindSlot<MaterialSlot>(inSlot.id).concreteValueType == ConcreteSlotValueType.SamplerState)
param += GetSamplerInput(inSlot.id);
else
param += GetSlotValue(inSlot.id, generationMode);
if (remainingParams > 1)
param += ", ";

private string GetFunctionCall(GenerationMode generationMode)
{
return GetFunctionName() +
string prefix = "";
string sufix = "";
foreach (ISlot slot in GetInputSlots<MaterialSlot>())
{
if (GetSlotValueType(slot.id) == ConcreteSlotValueType.Texture2D || GetSlotValueType(slot.id) == ConcreteSlotValueType.SamplerState)
{
prefix = "#ifdef UNITY_COMPILER_HLSL \n";
sufix = "\n #endif";
}
}
return prefix + GetFunctionName() +
");";
");" + sufix;
}
private string GetOutputDeclaration()

outputString.AddShaderChunk(GetFunctionCall(generationMode), false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public string GetSamplerInput(int slotID)
{
//default sampler if no input is provided
var samplerName = "my_linear_repeat_sampler";
//Sampler input slot
var samplerSlot = FindInputSlot<MaterialSlot>(slotID);
if (samplerSlot != null)
{
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);
}
}
return samplerName;
}
}
}

8
MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs


public HeightToNormalNode()
{
name = "HeightToNormal";
AddSlot("HeightMap", "heightmap", Graphing.SlotType.Input, SlotValueType.sampler2D, Vector4.zero);
AddSlot("HeightMap", "heightmap", Graphing.SlotType.Input, SlotValueType.Texture2D, Vector4.zero);
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("Offset", "texOffset", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.005f, 0,0,0));
AddSlot("Strength", "strength", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(8,0,0,0));

outputString.AddShaderChunk("float2 offsetU = float2(texCoord.x + texOffset, texCoord.y);", false);
outputString.AddShaderChunk("float2 offsetV = float2(texCoord.x, texCoord.y + texOffset);", false);
outputString.AddShaderChunk("float normalSample = tex2D(heightmap, texCoord).r;", false);
outputString.AddShaderChunk("float uSample = tex2D(heightmap, offsetU).r;", false);
outputString.AddShaderChunk("float vSample = tex2D(heightmap, offsetV).r;", false);
outputString.AddShaderChunk("float normalSample = heightmap.Sample(my_linear_repeat_sampler, texCoord).r;", false);
outputString.AddShaderChunk("float uSample = heightmap.Sample(my_linear_repeat_sampler, offsetU).r;", false);
outputString.AddShaderChunk("float vSample = heightmap.Sample(my_linear_repeat_sampler, offsetV).r;", false);
outputString.AddShaderChunk("float uMinusNormal = uSample - normalSample;", false);
outputString.AddShaderChunk("float vMinusNormal = vSample - normalSample;", false);

2
MaterialGraphProject/Assets/NewNodes/WIP/MultiLayerParallaxNode.cs


protected virtual MaterialSlot GetTextureSlot()
{
return new MaterialSlot(TextureSlotId, GetTextureSlotName(), kTextureSlotShaderName, SlotType.Input, SlotValueType.sampler2D, Vector4.zero);
return new MaterialSlot(TextureSlotId, GetTextureSlotName(), kTextureSlotShaderName, SlotType.Input, SlotValueType.Texture2D, Vector4.zero);
}
protected virtual MaterialSlot GetOutputSlot()

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


typeMapper[typeof(GradientNode)] = typeof(GradientNodePresenter);
typeMapper[typeof(ScatterNode)] = typeof(ScatterNodePresenter);
typeMapper[typeof(TextureNode)] = typeof(TextureNodePresenter);
typeMapper[typeof(TextureSamplerNode)] = typeof(TextureSamplerNodePresenter);
typeMapper[typeof(TextureAssetNode)] = typeof(TextureAssetNodePresenter);
typeMapper[typeof(TextureLODNode)] = typeof(TextureLODNodePresenter);
typeMapper[typeof(CubemapNode)] = typeof(CubeNodePresenter);

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/subshader.template


#pragma glsl
#pragma debug
${ShaderPropertyUsages}
${ShaderPropertyUsages}
struct Input
{

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


if (edges.Any())
continue;
inputSlot.GeneratePropertyUsages(visitor, generationMode);
if (inputSlot.valueType == SlotValueType.SamplerState)
{
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false);
visitor.AddShaderChunk(inputSlot.valueType + " my_linear_repeat_sampler;", false);
visitor.AddShaderChunk("#endif", false);
}
else
inputSlot.GeneratePropertyUsages(visitor, generationMode);
}
}

return ConcreteSlotValueType.Vector3;
case SlotValueType.Vector4:
return ConcreteSlotValueType.Vector4;
case SlotValueType.sampler2D:
return ConcreteSlotValueType.sampler2D;
case SlotValueType.Texture2D:
return ConcreteSlotValueType.Texture2D;
case SlotValueType.Matrix2:
return ConcreteSlotValueType.Matrix2;
case SlotValueType.Matrix3:

case SlotValueType.SamplerState:
return ConcreteSlotValueType.SamplerState;
}
return ConcreteSlotValueType.Error;
}

return "3";
case ConcreteSlotValueType.Vector4:
return "4";
case ConcreteSlotValueType.sampler2D:
return "sampler2D";
case ConcreteSlotValueType.Texture2D:
return "Texture2D";
case ConcreteSlotValueType.Matrix2:
return "2x2";
case ConcreteSlotValueType.Matrix3:

case ConcreteSlotValueType.SamplerState:
return "SamplerState";
default:
return "Error";
}

{
switch (slotValue)
{
case ConcreteSlotValueType.sampler2D:
case ConcreteSlotValueType.Texture2D:
return PropertyType.Texture;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;

return PropertyType.Matrix3;
case ConcreteSlotValueType.Matrix4:
return PropertyType.Matrix4;
case ConcreteSlotValueType.SamplerState:
return PropertyType.SamplerState;
default:
return PropertyType.Vector4;
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Filters/ConvolutionFilterNode.cs


protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, kTextureSlotName, kTextureSlotName, SlotType.Input, SlotValueType.sampler2D, Vector4.zero, false);
return new MaterialSlot(InputSlot1Id, kTextureSlotName, kTextureSlotName, SlotType.Input, SlotValueType.Texture2D, Vector4.zero, false);
}
protected override MaterialSlot GetInputSlot2()

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs


case SlotValueType.Matrix4:
concreteValueType = ConcreteSlotValueType.Matrix4;
break;
case SlotValueType.Texture2D:
concreteValueType = ConcreteSlotValueType.Texture2D;
break;
case SlotValueType.SamplerState:
concreteValueType = ConcreteSlotValueType.SamplerState;
break;
default:
concreteValueType = ConcreteSlotValueType.Vector4;
break;

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyType.cs


Vector4,
Matrix2,
Matrix3,
Matrix4
Matrix4,
SamplerState
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs


[Serializable]
public enum SlotValueType
{
SamplerState,
sampler2D,
Texture2D,
Dynamic,
Vector4,
Vector3,

public enum ConcreteSlotValueType
{
SamplerState = 9,
sampler2D = 5,
Texture2D = 5,
Vector4 = 4,
Vector3 = 3,
Vector2 = 2,

16
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/AbstractSurfaceMasterNode.cs


foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
{
string prefix = "";
string sufix = "";
foreach (ISlot slot in node.GetInputSlots<MaterialSlot>())
{
SlotValueType slotValueType = FindSlot<MaterialSlot>(slot.id).valueType;
if (slotValueType == SlotValueType.Texture2D || slotValueType == SlotValueType.SamplerState)
{
prefix = "#ifdef UNITY_COMPILER_HLSL \n";
sufix = "\n #endif";
}
}
{
nodeFunction.AddShaderChunk(prefix, false);
nodeFunction.AddShaderChunk(sufix, false);
}
node.GeneratePropertyUsages(propertyUsages, mode);
}

9
MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.sampler2D, Vector4.zero, false));
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Texture2D, Vector4.zero, false));
RemoveSlotsNameNotMatching(validSlots);
}

public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk("sampler2D " + propertyName + ";", true);
visitor.AddShaderChunk(precision + "4 " + propertyName + "_TexelSize;", true);
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", true);
visitor.AddShaderChunk("Texture2D " + propertyName + ";", true);
visitor.AddShaderChunk("#endif", true);
public override PreviewProperty GetPreviewProperty()
{

122
MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs


namespace UnityEngine.MaterialGraph
{
[Title("UV/Tri-Planar Mapping")]
public class UVTriPlanar : Function3Input, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition
public class UVTriPlanar : FunctionNInNOut, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition
private int slot0, slot1, slot2, slot3, slot4, slot5, slot6 = 0;
private const string kTextureSlotName = "Texture";
private const string kTileSlotName = "Tile";
private const string kBlendSlotName = "Blend";
private string slot0Name = "texRef";
private string slot1Name = "tileFactor";
private string slot2Name = "blendFactor";
private string slot3Name = "samplerRef";
private string slot4Name = "normalRef";
private string slot5Name = "posRef";
private string slot6Name = "outputRef";
protected override string GetFunctionName()
{

protected override MaterialSlot GetInputSlot1()
public UVTriPlanar()
return new MaterialSlot(InputSlot1Id, kTextureSlotName, kTextureSlotName, SlotType.Input, SlotValueType.sampler2D, Vector4.zero, false);
}
name = "UVTriPlanar";
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, kTileSlotName, kTileSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.one);
}
slot0 = AddSlot("Texture", slot0Name, Graphing.SlotType.Input, SlotValueType.Texture2D, Vector4.zero);
slot1 = AddSlot("Tile", slot1Name, Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
slot2 = AddSlot("Blend", slot2Name, Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
slot3 = AddSlot("Sampler", slot3Name, Graphing.SlotType.Input, SlotValueType.SamplerState, Vector4.zero);
slot4 = AddSlot("Normals", slot4Name, Graphing.SlotType.Input, SlotValueType.Vector3, Vector3.one);
slot5 = AddSlot("Position", slot5Name, Graphing.SlotType.Input, SlotValueType.Vector3, Vector3.one);
slot6 = AddSlot("RGBA ", slot6Name, Graphing.SlotType.Output, SlotValueType.Vector4, Vector4.zero);
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, kBlendSlotName, kBlendSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.one);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, kTextureSlotName, kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector4, Vector4.zero);
}
public UVTriPlanar()
{
name = "UVTriPlanar";
UpdateNodeAfterDeserialization();
}

}
}
protected override string GetFunctionPrototype(string arg1Name, string arg2Name, string arg3Name)
{
return "inline " + precision + outputDimension + " " + GetFunctionName() + " ("
+ "sampler2D " + arg1Name + ", "
+ precision + " " + arg2Name + ", "
+ precision + " " + arg3Name +
", float3 normal, float3 pos)";
}
protected override string GetFunctionCallBody(string input1Value, string input2Value, string input3Value)
{
return GetFunctionName() + " (" + input1Value + ", " + input2Value + ", " + input3Value + ", IN.worldNormal, IN.worldPos)";
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
base.GeneratePropertyUsages(visitor, generationMode);
}
//TODO:Externalize
//Reference code from:http://www.chilliant.com/rgb2hsv.html

var textureSlot = FindInputSlot<MaterialSlot>(InputSlot1Id);
if (textureSlot == null)
return;
var textureName = "";
//Sampler input
var samplerName = GetSamplerInput(3);
outputString.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false);
var edges = owner.GetEdges(textureSlot.slotReference).ToList();
if (edges.Count > 0)
{
var edge = edges[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
textureName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.sampler2D, true);
}
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2", "arg3"), false);
// outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2", "arg3", samplerName), false);
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.Indent();
outputString.Indent();
outputString.AddShaderChunk("fixed3 uvs = pos * arg2;", false);
outputString.AddShaderChunk("float3 uvs = " + slot5Name + "*" + slot1Name + ";", false);
outputString.AddShaderChunk("half3 blend = pow(abs(normal), arg3);", false);
outputString.AddShaderChunk("half3 blend = pow(abs(" + slot4Name + ")," + slot2Name + ");", false);
outputString.AddShaderChunk("fixed4 cx = tex2D(arg1, uvs.yz);", false);
outputString.AddShaderChunk("fixed4 cy = tex2D(arg1, uvs.xz);", false);
outputString.AddShaderChunk("fixed4 cz = tex2D(arg1, uvs.xy);", false);
outputString.AddShaderChunk("float4 cx = " + slot0Name + ".Sample(" + slot3Name + ", uvs.yz);", false);
outputString.AddShaderChunk("float4 cy = " + slot0Name + ".Sample(" + slot3Name + ", uvs.xz);", false);
outputString.AddShaderChunk("float4 cz = " + slot0Name + ".Sample(" + slot3Name + ", uvs.xy);", false);
outputString.AddShaderChunk("fixed4 c = cx * blend.x + cy * blend.y + cz * blend.z;", false);
outputString.AddShaderChunk("return " + "c;", false);
outputString.AddShaderChunk(slot6Name + " = cx * blend.x + cy * blend.y + cz * blend.z;", false);
//outputString.AddShaderChunk("return " + "c;", false);
outputString.AddShaderChunk("#endif", true);
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
//Sampler input slot
base.GeneratePropertyUsages(visitor, generationMode);
var samplerSlot = FindInputSlot<MaterialSlot>(slot3);
if (samplerSlot != null)
{
var samplerName = GetSamplerInput(slot3);
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false);
visitor.AddShaderChunk("SamplerState " + samplerName + ";", true);
visitor.AddShaderChunk("#endif", false);
}
}
var slot = FindInputSlot<MaterialSlot>(InputSlot1Id);
var slot = FindInputSlot<MaterialSlot>(0);
if (slot == null)
return;

2
MaterialGraphProject/Assets/_MingWai/ScatterNode.cs


public ScatterNode()
{
name = "Scatter";
AddSlot("Texture", "inputTex", Graphing.SlotType.Input, SlotValueType.sampler2D, Vector4.zero);
AddSlot("Texture", "inputTex", Graphing.SlotType.Input, SlotValueType.Texture2D, Vector4.zero);
AddSlot("UV", "inputUV", Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.one);
AddSlot("Seed", "seed", Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.one);
AddSlot("PositionRange", "p_range", Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);

64
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs


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 };
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/SamplerStatePresenter.cs.meta


fileFormatVersion: 2
guid: 8ffcc48ccff1aea44b90acb362bc0dfc
timeCreated: 1495659152
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

52
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs


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 };
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureSamplerPresenter.cs.meta


fileFormatVersion: 2
guid: 86fe3d2371b24d34399f4b116a238d45
timeCreated: 1495638407
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

133
MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs


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";
}
}
}

12
MaterialGraphProject/Assets/Vlad/SamplerStateNode.cs.meta


fileFormatVersion: 2
guid: 5fe2c333e043511478cc507ee61a8c21
timeCreated: 1495658099
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

246
MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs


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;
}
}
}

12
MaterialGraphProject/Assets/Vlad/TextureSamplerNode.cs.meta


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