浏览代码

Merge pull request #259 from Unity-Technologies/node-bugfixes

Master node improvements
/main
GitHub 7 年前
当前提交
5e8fdfe2
共有 8 个文件被更改,包括 146 次插入33 次删除
  1. 11
      com.unity.shadergraph/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  2. 12
      com.unity.shadergraph/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  3. 16
      com.unity.shadergraph/Editor/Data/MasterNodes/AlphaMode.cs
  4. 37
      com.unity.shadergraph/Editor/Data/MasterNodes/PBRMasterNode.cs
  5. 35
      com.unity.shadergraph/Editor/Data/MasterNodes/UnlitMasterNode.cs
  6. 66
      com.unity.shadergraph/Editor/Data/Util/ShaderGenerator.cs
  7. 1
      com.unity.shadergraph/Editor/Templates/lightweightPBRExtraPasses.template
  8. 1
      com.unity.shadergraph/Editor/Templates/lightweightUnlitExtraPasses.template

11
com.unity.shadergraph/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs


if (masterNode.IsSlotConnected(PBRMasterNode.AlphaThresholdSlotId))
defines.AddShaderChunk("#define _AlphaClip 1", true);
if(masterNode.surfaceType == SurfaceType.Transparent && masterNode.alphaMode == AlphaMode.Premultiply)
defines.AddShaderChunk("#define _ALPHAPREMULTIPLY_ON 1", true);
var templateLocation = ShaderGenerator.GetTemplatePath(template);
foreach (var slot in usedSlots)

subShader.Indent();
subShader.AddShaderChunk("Tags{ \"RenderPipeline\" = \"LightweightPipeline\"}", true);
var materialOptions = ShaderGenerator.GetMaterialOptionsFromAlphaMode(masterNode.alphaMode);
var materialOptions = ShaderGenerator.GetMaterialOptions(masterNode.surfaceType, masterNode.alphaMode, masterNode.twoSided.isOn);
var tagsVisitor = new ShaderGenerator();
materialOptions.GetTags(tagsVisitor);
subShader.AddShaderChunk(tagsVisitor.GetShaderString(0), true);

var extraPassesTemplateLocation = ShaderGenerator.GetTemplatePath("lightweightPBRExtraPasses.template");
if (File.Exists(extraPassesTemplateLocation))
subShader.AddShaderChunk(File.ReadAllText(extraPassesTemplateLocation), true);
{
var extraPassesTemplate = File.ReadAllText(extraPassesTemplateLocation);
extraPassesTemplate = extraPassesTemplate.Replace("${Culling}", materialOptions.cullMode.ToString());
subShader.AddShaderChunk(extraPassesTemplate, true);
}
subShader.Deindent();
subShader.AddShaderChunk("}", true);

12
com.unity.shadergraph/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs


if (masterNode.IsSlotConnected(UnlitMasterNode.AlphaThresholdSlotId))
defines.AddShaderChunk("#define _AlphaClip 1", true);
if(masterNode.surfaceType == SurfaceType.Transparent && masterNode.alphaMode == AlphaMode.Premultiply)
defines.AddShaderChunk("#define _ALPHAPREMULTIPLY_ON 1", true);
var templateLocation = ShaderGenerator.GetTemplatePath(template);
foreach (var slot in usedSlots)

subShader.Indent();
subShader.AddShaderChunk("Tags{ \"RenderType\" = \"Opaque\" \"RenderPipeline\" = \"LightweightPipeline\"}", true);
var materialOptions = ShaderGenerator.GetMaterialOptionsFromAlphaMode(masterNode.alphaMode);
var materialOptions = ShaderGenerator.GetMaterialOptions(masterNode.surfaceType, masterNode.alphaMode, masterNode.twoSided.isOn);
var tagsVisitor = new ShaderGenerator();
materialOptions.GetTags(tagsVisitor);
subShader.AddShaderChunk(tagsVisitor.GetShaderString(0), true);

var extraPassesTemplateLocation = ShaderGenerator.GetTemplatePath("lightweightUnlitExtraPasses.template");
if (File.Exists(extraPassesTemplateLocation))
subShader.AddShaderChunk(File.ReadAllText(extraPassesTemplateLocation), true);
{
var extraPassesTemplate = File.ReadAllText(extraPassesTemplateLocation);
extraPassesTemplate = extraPassesTemplate.Replace("${Culling}", materialOptions.cullMode.ToString());
subShader.AddShaderChunk(extraPassesTemplate, true);
}
subShader.Deindent();
subShader.AddShaderChunk("}", true);

16
com.unity.shadergraph/Editor/Data/MasterNodes/AlphaMode.cs


using System;
namespace UnityEditor.ShaderGraph {
public enum AlphaMode
namespace UnityEditor.ShaderGraph
{
public enum SurfaceType
AlphaBlend,
AdditiveBlend
Transparent
}
public enum AlphaMode
{
Alpha,
Premultiply,
Additive,
Multiply
}
}

37
com.unity.shadergraph/Editor/Data/MasterNodes/PBRMasterNode.cs


[SerializeField]
private Model m_Model = Model.Metallic;
[EnumControl("")]
[EnumControl("Workflow")]
public Model model
{
get { return m_Model; }

m_Model = value;
UpdateNodeAfterDeserialization();
Dirty(ModificationScope.Topological);
}
}
[SerializeField]
private SurfaceType m_SurfaceType;
[EnumControl("Surface")]
public SurfaceType surfaceType
{
get { return m_SurfaceType; }
set
{
if (m_SurfaceType == value)
return;
m_SurfaceType = value;
Dirty(ModificationScope.Graph);
}
}

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

return;
m_AlphaMode = value;
Dirty(ModificationScope.Graph);
}
}
[SerializeField]
private bool m_TwoSided;
[ToggleControl("Two Sided")]
public Toggle twoSided
{
get { return new Toggle(m_TwoSided); }
set
{
if (m_TwoSided == value.isOn)
return;
m_TwoSided = value.isOn;
Dirty(ModificationScope.Graph);
}
}

35
com.unity.shadergraph/Editor/Data/MasterNodes/UnlitMasterNode.cs


[SerializeField]
private SurfaceType m_SurfaceType;
[EnumControl("Surface")]
public SurfaceType surfaceType
{
get { return m_SurfaceType; }
set
{
if (m_SurfaceType == value)
return;
m_SurfaceType = value;
Dirty(ModificationScope.Graph);
}
}
[SerializeField]
[EnumControl("")]
[EnumControl("Blend")]
public AlphaMode alphaMode
{
get { return m_AlphaMode; }

return;
m_AlphaMode = value;
Dirty(ModificationScope.Graph);
}
}
[SerializeField]
private bool m_TwoSided;
[ToggleControl("Two Sided")]
public Toggle twoSided
{
get { return new Toggle(m_TwoSided); }
set
{
if (m_TwoSided == value.isOn)
return;
m_TwoSided = value.isOn;
Dirty(ModificationScope.Graph);
}
}

66
com.unity.shadergraph/Editor/Data/Util/ShaderGenerator.cs


return res;
}
public static SurfaceMaterialOptions GetMaterialOptionsFromAlphaMode(AlphaMode alphaMode)
public static SurfaceMaterialOptions GetMaterialOptions(SurfaceType surfaceType, AlphaMode alphaMode, bool twoSided)
switch (alphaMode)
switch (surfaceType)
case AlphaMode.Opaque:
case SurfaceType.Opaque:
materialOptions.cullMode = SurfaceMaterialOptions.CullMode.Back;
materialOptions.cullMode = twoSided ? SurfaceMaterialOptions.CullMode.Off : SurfaceMaterialOptions.CullMode.Back;
case AlphaMode.AlphaBlend:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.SrcAlpha;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.OneMinusSrcAlpha;
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 AlphaMode.AdditiveBlend:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.One;
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;
case SurfaceType.Transparent:
switch (alphaMode)
{
case AlphaMode.Alpha:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.SrcAlpha;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.OneMinusSrcAlpha;
materialOptions.cullMode = twoSided ? SurfaceMaterialOptions.CullMode.Off : 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 AlphaMode.Premultiply:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.OneMinusSrcAlpha;
materialOptions.cullMode = twoSided ? SurfaceMaterialOptions.CullMode.Off : 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 AlphaMode.Additive:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.One;
materialOptions.cullMode = twoSided ? SurfaceMaterialOptions.CullMode.Off : 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 AlphaMode.Multiply:
materialOptions.srcBlend = SurfaceMaterialOptions.BlendMode.DstColor;
materialOptions.dstBlend = SurfaceMaterialOptions.BlendMode.Zero;
materialOptions.cullMode = twoSided ? SurfaceMaterialOptions.CullMode.Off : SurfaceMaterialOptions.CullMode.Back;
materialOptions.zTest = SurfaceMaterialOptions.ZTest.LEqual;
materialOptions.zWrite = SurfaceMaterialOptions.ZWrite.Off;
materialOptions.renderQueue = SurfaceMaterialOptions.RenderQueue.Transparent;
materialOptions.renderType = SurfaceMaterialOptions.RenderType.Transparent;
break;
}
return materialOptions;
}

1
com.unity.shadergraph/Editor/Templates/lightweightPBRExtraPasses.template


ZWrite On
ZTest LEqual
Cull ${Culling}
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library

1
com.unity.shadergraph/Editor/Templates/lightweightUnlitExtraPasses.template


ZWrite On
ZTest LEqual
Cull ${Culling}
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library

正在加载...
取消
保存