浏览代码

Extend PBR master functionality

- Add Fade and Multiply blend modes
- Expose culling options
- Change UV enum to caps
- Add camel case string converter to utils
- Convert camel case on CodeFunctionNode
/main
Matt Dean 7 年前
当前提交
25109a19
共有 9 个文件被更改,包括 86 次插入31 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs
  2. 33
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  3. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/MasterNodes/PBRMasterNode.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs
  5. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  6. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs
  7. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  8. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs
  9. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ToggleControl.cs

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


[Serializable]
public class UVMaterialSlot : Vector2MaterialSlot, IMayRequireMeshUV
{
private UVChannel m_Channel = UVChannel.uv0;
private UVChannel m_Channel = UVChannel.UV0;
public UVChannel channel
{

33
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs


modelRequiements.requiresBitangent |= NeededCoordinateSpace.World;
modelRequiements.requiresPosition |= NeededCoordinateSpace.World;
modelRequiements.requiresViewDir |= NeededCoordinateSpace.World;
modelRequiements.requiresMeshUVs.Add(UVChannel.uv1);
modelRequiements.requiresMeshUVs.Add(UVChannel.UV1);
GenerateApplicationVertexInputs(requirements.Union(modelRequiements), vertexInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);

switch (masterNode.alphaMode)
{
case PBRMasterNode.AlphaMode.AlphaBlend:
case PBRMasterNode.AlphaMode.AdditiveBlend:
case PBRMasterNode.AlphaMode.Transparent:
case PBRMasterNode.AlphaMode.Fade:
case PBRMasterNode.AlphaMode.Additive:
case PBRMasterNode.AlphaMode.Multiply:
defines.AddShaderChunk("#define _AlphaOut 1", true);
break;
}

case PBRMasterNode.AlphaMode.Opaque:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.Zero;
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
case PBRMasterNode.AlphaMode.AlphaBlend:
case PBRMasterNode.AlphaMode.Transparent:
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Transparent;
break;
case PBRMasterNode.AlphaMode.Fade:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.OneMinusSrcAlpha;
case PBRMasterNode.AlphaMode.AdditiveBlend:
case PBRMasterNode.AlphaMode.Additive:
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Transparent;
break;
case PBRMasterNode.AlphaMode.Multiply:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.DstColor;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.Zero;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;

materialOptions.cullMode = masterNode.cullMode;
var tagsVisitor = new ShaderGenerator();
materialOptions.GetTags(tagsVisitor);

29
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/MasterNodes/PBRMasterNode.cs


public enum AlphaMode
{
Opaque,
AlphaBlend,
AdditiveBlend
Transparent,
Fade,
Additive,
Multiply
[EnumControl("")]
[EnumControl("Model")]
public Model model
{
get { return m_Model; }

[SerializeField]
private AlphaMode m_AlphaMode;
[EnumControl("")]
[EnumControl("Blend")]
public AlphaMode alphaMode
{
get { return m_AlphaMode; }

}
}
[SerializeField]
private SurfaceMaterialOptions.CullMode m_CullMode;
[EnumControl("Culling")]
public SurfaceMaterialOptions.CullMode cullMode
{
get { return m_CullMode; }
set
{
if (m_CullMode == value)
return;
m_CullMode = value;
Dirty(ModificationScope.Graph);
}
}
public PBRMasterNode()
{
UpdateNodeAfterDeserialization();

AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0.5f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaThresholdSlotId, AlphaClipThresholdSlotName, AlphaClipThresholdSlotName, SlotType.Input, 0f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaThresholdSlotId, GraphUtil.ConvertCamelCase(AlphaClipThresholdSlotName, true), AlphaClipThresholdSlotName, SlotType.Input, 0f, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Texture2DInputMaterialSlot(TextureInputId, k_TextureInputName, k_TextureInputName));
AddSlot(new UVMaterialSlot(UVInputId, k_UVInputName, k_UVInputName, UVChannel.uv0));
AddSlot(new UVMaterialSlot(UVInputId, k_UVInputName, k_UVInputName, UVChannel.UV0));
AddSlot(new SamplerStateMaterialSlot(SamplerInputId, k_SamplerInputName, k_SamplerInputName, SlotType.Input));
AddSlot(new Vector1MaterialSlot(OffsetInputId, k_OffsetInputName, k_OffsetInputName, SlotType.Input, 0.5f));
AddSlot(new Vector1MaterialSlot(StrengthInputId, k_StrengthInputName, k_StrengthInputName, SlotType.Input, 8f));

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs


foreach (var par in method.GetParameters())
{
var attribute = GetSlotAttribute(par);
var name = GraphUtil.ConvertCamelCase(par.Name, true);
s = new ColorRGBAMaterialSlot(attribute.slotId, par.Name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
s = new ColorRGBAMaterialSlot(attribute.slotId, name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
s = new ColorRGBAMaterialSlot(attribute.slotId, par.Name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
s = new ColorRGBAMaterialSlot(attribute.slotId, name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
s = new ColorRGBMaterialSlot(attribute.slotId, par.Name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
s = new ColorRGBMaterialSlot(attribute.slotId, name, par.Name, SlotType.Input, attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden);
par.Name,
name,
s = CreateBoundSlot(attribute.binding, attribute.slotId, par.Name, par.Name, attribute.hidden);
s = CreateBoundSlot(attribute.binding, attribute.slotId, name, par.Name, attribute.hidden);
slots.Add(s);
m_Slots.Add(attribute);

case Binding.TangentSpacePosition:
return new PositionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.MeshUV0:
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv0);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.UV0);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv1);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.UV1);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv2);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.UV2);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv3);
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.UV3);
case Binding.ScreenPosition:
return new ScreenPositionMaterialSlot(slotId, displayName, shaderOutputName);
case Binding.ObjectSpaceViewDirection:

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs


const string kOutputSlotGName = "G";
const string kOutputSlotBName = "B";
const string kOutputSlotAName = "A";
const string kTextureInputName = "Tex";
const string kTextureInputName = "Texture";
const string kUVInputName = "UV";
const string kSamplerInputName = "Sampler";

AddSlot(new Vector1MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, 0));
AddSlot(new Texture2DInputMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName));
AddSlot(new UVMaterialSlot(UVInput, kUVInputName, kUVInputName, UVChannel.uv0));
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 });
}

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;

{
static class GraphUtil
{
internal static string ConvertCamelCase(string text, bool preserveAcronyms)
{
if (string.IsNullOrEmpty(text))
return string.Empty;
StringBuilder newText = new StringBuilder(text.Length * 2);
newText.Append(text[0]);
for (int i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]))
if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
(preserveAcronyms && char.IsUpper(text[i - 1]) &&
i < text.Length - 1 && !char.IsUpper(text[i + 1])))
newText.Append(' ');
newText.Append(text[i]);
}
return newText.ToString();
}
internal static void GenerateApplicationVertexInputs(ShaderGraphRequirements graphRequiements, ShaderGenerator vertexInputs)
{
vertexInputs.AddShaderChunk("struct GraphVertexInput", false);

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs


namespace UnityEditor.ShaderGraph {
public enum UVChannel
{
uv0 = 0,
uv1 = 1,
uv2 = 2,
uv3 = 3,
UV0 = 0,
UV1 = 1,
UV2 = 2,
UV3 = 3,
}
}

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ToggleControl.cs


public class ToggleControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;

正在加载...
取消
保存