浏览代码

Fix per-stage requirements and translations part 1

- Separate Master node requirements into per-slot stages
- Add shader stage option to IMayRequire interfaces
- Remove uneeded intermediate spaces from surface description
- Change orthonormal basis to world
/main
Peter Bay Bastian 7 年前
当前提交
4dfbc988
共有 38 个文件被更改,包括 172 次插入154 次删除
  1. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/BitangentMaterialSlot.cs
  2. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/NormalMaterialSlot.cs
  3. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/PositionMaterialSlot.cs
  4. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs
  5. 29
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ShaderGraphRequirements.cs
  6. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/TangentMaterialSlot.cs
  7. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/UVMaterialSlot.cs
  8. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/VertexColorMaterialSlot.cs
  9. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs
  10. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireBitangent.cs
  11. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireMeshUV.cs
  12. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireNormal.cs
  13. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequirePosition.cs
  14. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireScreenPosition.cs
  15. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireTangent.cs
  16. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireVertexColor.cs
  17. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireViewDirection.cs
  18. 37
      ShaderGraph/com.unity.shadergraph/Editor/Data/MasterNodes/PBRMasterNode.cs
  19. 18
      ShaderGraph/com.unity.shadergraph/Editor/Data/MasterNodes/UnlitMasterNode.cs
  20. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs
  21. 16
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/CodeFunctionNode.cs
  22. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/BitangentVectorNode.cs
  23. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/NormalVectorNode.cs
  24. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/PositionNode.cs
  25. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/ScreenPositionNode.cs
  26. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/TangentVectorNode.cs
  27. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/UVNode.cs
  28. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/VertexColorNode.cs
  29. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/ViewDirectionNode.cs
  30. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/FogNode.cs
  31. 4
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleCubemapNode.cs
  32. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs
  33. 6
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Math/Vector/TransformNode.cs
  34. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/UV/FlipbookNode.cs
  35. 8
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/UV/TriplanarNode.cs
  36. 32
      ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs
  37. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs
  38. 118
      ShaderGraph/com.unity.shadergraph/Editor/Data/Util/ShaderGenerator.cs

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/BitangentMaterialSlot.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.BiTangent));
}
public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
if (isConnected)
return NeededCoordinateSpace.None;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/NormalMaterialSlot.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Normal));
}
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
if (isConnected)
return NeededCoordinateSpace.None;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/PositionMaterialSlot.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Position));
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
{
if (isConnected)
return NeededCoordinateSpace.None;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs


return m_ScreenSpaceType.ToValueAsVariable();
}
public bool RequiresScreenPosition()
public bool RequiresScreenPosition(ShaderStageCapability stageCapability)
{
return !isConnected;
}

29
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ShaderGraphRequirements.cs


return newReqs;
}
public static ShaderGraphRequirements FromNodes<T>(List<T> nodes)
public static ShaderGraphRequirements FromNodes<T>(List<T> nodes, ShaderStageCapability stageCapability = ShaderStageCapability.All, bool includeIntermediateSpaces = true)
NeededCoordinateSpace requiresNormal = nodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal());
NeededCoordinateSpace requiresBitangent = nodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresBitangent());
NeededCoordinateSpace requiresTangent = nodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent());
NeededCoordinateSpace requiresViewDir = nodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresViewDirection());
NeededCoordinateSpace requiresPosition = nodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition());
NeededCoordinateSpace requiresNormal = nodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal(stageCapability));
NeededCoordinateSpace requiresBitangent = nodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresBitangent(stageCapability));
NeededCoordinateSpace requiresTangent = nodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent(stageCapability));
NeededCoordinateSpace requiresViewDir = nodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresViewDirection(stageCapability));
NeededCoordinateSpace requiresPosition = nodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
bool requiresScreenPosition = nodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = nodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());

// if anything needs tangentspace we have make
// sure to have our othonormal basis!
var compoundSpaces = requiresBitangent | requiresNormal | requiresPosition
if(includeIntermediateSpaces)
{
var compoundSpaces = requiresBitangent | requiresNormal | requiresPosition
var needsTangentSpace = (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
if (needsTangentSpace)
{
requiresBitangent |= NeededCoordinateSpace.Object;
requiresNormal |= NeededCoordinateSpace.Object;
requiresTangent |= NeededCoordinateSpace.Object;
var needsTangentSpace = (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
if (needsTangentSpace)
{
requiresBitangent |= NeededCoordinateSpace.World;
requiresNormal |= NeededCoordinateSpace.World;
requiresTangent |= NeededCoordinateSpace.World;
}
}
var reqs = new ShaderGraphRequirements()

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/TangentMaterialSlot.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Tangent));
}
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
if (isConnected)
return NeededCoordinateSpace.None;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/UVMaterialSlot.cs


return string.Format("IN.{0}.xy", channel.GetUVName());
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
if (isConnected)
return false;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/VertexColorMaterialSlot.cs


return string.Format("IN.{0}", ShaderGeneratorNames.VertexColor);
}
public bool RequiresScreenPosition()
public bool RequiresScreenPosition(ShaderStageCapability stageCapability)
{
return !isConnected;
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.ViewDirection));
}
public NeededCoordinateSpace RequiresViewDirection()
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
if (isConnected)
return NeededCoordinateSpace.None;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireBitangent.cs


{
public interface IMayRequireBitangent
{
NeededCoordinateSpace RequiresBitangent();
NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireBitangentExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireMeshUV.cs


{
public interface IMayRequireMeshUV
{
bool RequiresMeshUV(UVChannel channel);
bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireMeshUVExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireNormal.cs


{
public interface IMayRequireNormal
{
NeededCoordinateSpace RequiresNormal();
NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireNormalExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequirePosition.cs


{
public interface IMayRequirePosition
{
NeededCoordinateSpace RequiresPosition();
NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequirePositionExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireScreenPosition.cs


{
public interface IMayRequireScreenPosition
{
bool RequiresScreenPosition();
bool RequiresScreenPosition(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireScreenPositionExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireTangent.cs


{
public interface IMayRequireTangent
{
NeededCoordinateSpace RequiresTangent();
NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireTangentExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireVertexColor.cs


{
interface IMayRequireVertexColor
{
bool RequiresVertexColor();
bool RequiresVertexColor(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireVertexColorExtensions

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireViewDirection.cs


{
interface IMayRequireViewDirection
{
NeededCoordinateSpace RequiresViewDirection();
NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireViewDirectionExtensions

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


{
[Serializable]
[Title("Master", "PBR")]
public class PBRMasterNode : MasterNode<IPBRSubShader>, IMayRequirePosition
public class PBRMasterNode : MasterNode<IPBRSubShader>, IMayRequirePosition, IMayRequireNormal
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";

}, true);
}
public NeededCoordinateSpace RequiresNormal()
protected override VisualElement CreateCommonSettingsElement()
List<ISlot> slots = new List<ISlot>();
GetSlots(slots);
return slots.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal());
return new PBRSettingsView(this);
protected override VisualElement CreateCommonSettingsElement()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
return new PBRSettingsView(this);
List<MaterialSlot> slots = new List<MaterialSlot>();
GetSlots(slots);
for(int i = 0; i < slots.Count; i++)
{
if(slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
slots.Remove(slots[i]);
}
return slots.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal(stageCapability));
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
var binding = NeededCoordinateSpace.None;
foreach (var slot in s_TempSlots)
binding |= slot.RequiresPosition();
return binding;
List<MaterialSlot> slots = new List<MaterialSlot>();
GetSlots(slots);
for(int i = 0; i < slots.Count; i++)
{
if(slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
slots.Remove(slots[i]);
}
return slots.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
}
}
}

18
ShaderGraph/com.unity.shadergraph/Editor/Data/MasterNodes/UnlitMasterNode.cs


using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.ShaderGraph.Drawing.Controls;

return new UnlitSettingsView(this);
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
var binding = NeededCoordinateSpace.None;
foreach (var slot in s_TempSlots)
binding |= slot.RequiresPosition();
return binding;
List<MaterialSlot> slots = new List<MaterialSlot>();
GetSlots(slots);
for(int i = 0; i < slots.Count; i++)
{
if(slots[i].stageCapability != ShaderStageCapability.All && slots[i].stageCapability != stageCapability)
slots.Remove(slots[i]);
}
return slots.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
}
}
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs


});
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
foreach (var slot in this.GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
{

16
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/CodeFunctionNode.cs


return attrs.FirstOrDefault();
}
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
var binding = NeededCoordinateSpace.None;
s_TempSlots.Clear();

return binding;
}
public NeededCoordinateSpace RequiresViewDirection()
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
var binding = NeededCoordinateSpace.None;
s_TempSlots.Clear();

return binding;
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

return binding;
}
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

return binding;
}
public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

return binding;
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

return false;
}
public bool RequiresScreenPosition()
public bool RequiresScreenPosition(ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

return false;
}
public bool RequiresVertexColor()
public bool RequiresVertexColor(ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/BitangentVectorNode.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.BiTangent));
}
public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/NormalVectorNode.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Normal));
}
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/PositionNode.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Position));
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/ScreenPositionNode.cs


visitor.AddShaderChunk(string.Format("{0}4 {1} = {2};", precision, GetVariableNameForSlot(kOutputSlotId), m_ScreenSpaceType.ToValueAsVariable()), true);
}
public bool RequiresScreenPosition()
public bool RequiresScreenPosition(ShaderStageCapability stageCapability)
{
return true;
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/TangentVectorNode.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.Tangent));
}
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/UVNode.cs


visitor.AddShaderChunk(string.Format("{0}4 {1} = IN.{2};", precision, GetVariableNameForSlot(OutputSlotId), m_OutputChannel.GetUVName()), true);
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
return channel == uvChannel;
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/VertexColorNode.cs


return string.Format("IN.{0}", ShaderGeneratorNames.VertexColor);
}
public bool RequiresVertexColor()
public bool RequiresVertexColor(ShaderStageCapability stageCapability)
{
return true;
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Geometry/ViewDirectionNode.cs


return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.ViewDirection));
}
public NeededCoordinateSpace RequiresViewDirection()
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/FogNode.cs


});
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
{
return CoordinateSpace.Object.ToNeededCoordinateSpace();
}

4
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleCubemapNode.cs


visitor.AddShaderChunk(result, true);
}
public NeededCoordinateSpace RequiresViewDirection()
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
var viewDirSlot = FindInputSlot<MaterialSlot>(ViewDirInputId);
var edgesViewDir = owner.GetEdges(viewDirSlot.slotReference);

return NeededCoordinateSpace.None;
}
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
var normalSlot = FindInputSlot<MaterialSlot>(NormalInputId);
var edgesNormal = owner.GetEdges(normalSlot.slotReference);

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleTexture2DNode.cs


visitor.AddShaderChunk(string.Format("{0} {1} = {2}.a;", precision, GetVariableNameForSlot(OutputSlotAId), GetVariableNameForSlot(OutputSlotRGBAId)), true);
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

6
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Math/Vector/TransformNode.cs


return false;
}
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
if(RequiresWorldSpaceTangentTransform())
return NeededCoordinateSpace.World;

public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
if (RequiresWorldSpaceTangentTransform())
return NeededCoordinateSpace.World;

public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
if (RequiresWorldSpaceTangentTransform())
return NeededCoordinateSpace.World;

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/UV/FlipbookNode.cs


});
}
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

8
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/UV/TriplanarNode.cs


visitor.AddShaderChunk(sb.ToString(), false);
}
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
switch(m_TextureType)
{

}
}
public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
switch(m_TextureType)
{

32
ShaderGraph/com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs


referencedGraph.GenerateSubGraphFunction(SubGraphFunctionName(), registry, ShaderGraphRequirements.FromNodes(new List<INode> {this}), GenerationMode.ForReals);
}
public NeededCoordinateSpace RequiresNormal()
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)
{
if (referencedGraph == null)
return NeededCoordinateSpace.None;

mask |= node.RequiresNormal();
mask |= node.RequiresNormal(stageCapability);
public bool RequiresMeshUV(UVChannel channel)
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
return referencedGraph.activeNodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel));
return referencedGraph.activeNodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel, stageCapability));
public bool RequiresScreenPosition()
public bool RequiresScreenPosition(ShaderStageCapability stageCapability)
return referencedGraph.activeNodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
return referencedGraph.activeNodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition(stageCapability));
public NeededCoordinateSpace RequiresViewDirection()
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
if (referencedGraph == null)
return NeededCoordinateSpace.None;

mask |= node.RequiresViewDirection();
mask |= node.RequiresViewDirection(stageCapability);
public NeededCoordinateSpace RequiresPosition()
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability)
{
if (referencedGraph == null)
return NeededCoordinateSpace.None;

mask |= node.RequiresPosition();
mask |= node.RequiresPosition(stageCapability);
public NeededCoordinateSpace RequiresTangent()
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability)
{
if (referencedGraph == null)
return NeededCoordinateSpace.None;

mask |= node.RequiresTangent();
mask |= node.RequiresTangent(stageCapability);
return mask;
});
}

return referencedGraph.activeNodes.OfType<IMayRequireTime>().Any(x => x.RequiresTime());
}
public NeededCoordinateSpace RequiresBitangent()
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability)
{
if (referencedGraph == null)
return NeededCoordinateSpace.None;

mask |= node.RequiresBitangent();
mask |= node.RequiresBitangent(stageCapability);
public bool RequiresVertexColor()
public bool RequiresVertexColor(ShaderStageCapability stageCapability)
return referencedGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
return referencedGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor(stageCapability));
}
}
}

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs


NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
}
var requirements = ShaderGraphRequirements.FromNodes(activeNodeList);
var requirements = ShaderGraphRequirements.FromNodes(activeNodeList, ShaderStageCapability.Fragment);
GenerateApplicationVertexInputs(requirements, vertexInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);

118
ShaderGraph/com.unity.shadergraph/Editor/Data/Util/ShaderGenerator.cs


ShaderGenerator vertexShader,
ShaderGenerator pixelShader,
ShaderGenerator surfaceInputs,
ShaderGraphRequirements graphRequiements,
ShaderGraphRequirements pixelRequirements,
ShaderGraphRequirements surfaceRequirements,
ShaderGraphRequirements vertexDescriptionRequirements,
ShaderGraphRequirements vertexRequirements,
CoordinateSpace preferredCoordinateSpace)
{
if (preferredCoordinateSpace == CoordinateSpace.Tangent)

// *generate needed interpolators
// *generate output from the vertex shader that writes into these interpolators
// *generate the pixel shader code that declares needed variables in the local scope
var graphModelRequirements = graphRequiements.Union(modelRequiements);
var combinedRequirements = vertexDescriptionRequirements.Union(graphModelRequirements);
var graphModelRequirements = pixelRequirements.Union(modelRequiements);
var combinedRequirements = vertexRequirements.Union(graphModelRequirements);
int interpolatorIndex = interpolatorStartIndex;

var name = preferredCoordinateSpace.ToVariableName(InterpolatorType.Normal);
vertexDescriptionInputs.AppendLine("float3 {0} = {1};", name, ConvertBetweenSpace("v.normal", CoordinateSpace.Object, preferredCoordinateSpace, InputType.Normal));
if (vertexDescriptionRequirements.requiresNormal > 0 || vertexDescriptionRequirements.requiresBitangent > 0)
if (vertexRequirements.requiresNormal > 0 || vertexRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", name), false);
interpolatorIndex++;
}
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", name), false);
interpolatorIndex++;
}
}
if (combinedRequirements.requiresTangent > 0 || combinedRequirements.requiresBitangent > 0)

if (vertexDescriptionRequirements.requiresTangent > 0 || vertexDescriptionRequirements.requiresBitangent > 0)
if (vertexRequirements.requiresTangent > 0 || vertexRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
}
if (combinedRequirements.requiresBitangent > 0)

preferredCoordinateSpace.ToVariableName(InterpolatorType.Normal),
preferredCoordinateSpace.ToVariableName(InterpolatorType.Tangent),
"v.tangent.w");
if (vertexDescriptionRequirements.requiresBitangent > 0)
if (vertexRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
}
if (combinedRequirements.requiresViewDir > 0)

var preferredSpaceViewDir = ConvertBetweenSpace(worldSpaceViewDir, CoordinateSpace.World, preferredCoordinateSpace, InputType.Vector);
if (vertexDescriptionRequirements.requiresViewDir > 0)
if (vertexRequirements.requiresViewDir > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", name), false);
interpolatorIndex++;
}
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", name), false);
interpolatorIndex++;
}
}
if (combinedRequirements.requiresPosition > 0)

if (vertexDescriptionRequirements.requiresPosition > 0)
if (vertexRequirements.requiresPosition > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", name), false);
interpolatorIndex++;
}
}
if (combinedRequirements.NeedsTangentSpace())

var normal = preferredCoordinateSpace.ToVariableName(InterpolatorType.Normal);
if (vertexDescriptionRequirements.NeedsTangentSpace())
if (vertexRequirements.NeedsTangentSpace())
pixelShader.AddShaderChunk(string.Format("float3x3 tangentSpaceTransform = float3x3({0},{1},{2});",
pixelShader.AddShaderChunk(string.Format("float3x3 tangentSpaceTransform = float3x3({0},{1},{2});",
tangent, bitangent, normal), false);
}

GenerateSpaceTranslations(vertexDescriptionRequirements.requiresNormal, InterpolatorType.Normal, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexRequirements.requiresNormal, InterpolatorType.Normal, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexDescriptionRequirements.requiresTangent, InterpolatorType.Tangent, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexRequirements.requiresTangent, InterpolatorType.Tangent, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexDescriptionRequirements.requiresBitangent, InterpolatorType.BiTangent, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexRequirements.requiresBitangent, InterpolatorType.BiTangent, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexDescriptionRequirements.requiresViewDir, InterpolatorType.ViewDirection, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexRequirements.requiresViewDir, InterpolatorType.ViewDirection, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexDescriptionRequirements.requiresPosition, InterpolatorType.Position, preferredCoordinateSpace,
GenerateSpaceTranslations(vertexRequirements.requiresPosition, InterpolatorType.Position, preferredCoordinateSpace,
InputType.Position, sg, Dimension.Three);
vertexDescriptionInputs.AppendLines(sg.GetShaderString(0));
}

GenerateSpaceTranslations(graphModelRequirements.requiresPosition, InterpolatorType.Position, preferredCoordinateSpace,
InputType.Position, pixelShader, Dimension.Three);
if (vertexDescriptionRequirements.requiresVertexColor)
if (vertexRequirements.requiresVertexColor)
vertexDescriptionInputs.AppendLine("vdi.{0} = v.color;", ShaderGeneratorNames.VertexColor);
if (graphModelRequirements.requiresVertexColor)
{

if (combinedRequirements.requiresScreenPosition)
{
var screenPosition = "ComputeScreenPos(mul(GetWorldToHClipMatrix(), mul(GetObjectToWorldMatrix(), v.vertex.xyz)), _ProjectionParams.x)";
if (vertexDescriptionRequirements.requiresScreenPosition)
if (vertexRequirements.requiresScreenPosition)
{
vertexDescriptionInputs.AppendLine("float4 {0} = {1};", ShaderGeneratorNames.ScreenPosition, screenPosition);
}

}
}
foreach (var channel in vertexDescriptionRequirements.requiresMeshUVs.Distinct())
foreach (var channel in vertexRequirements.requiresMeshUVs.Distinct())
{
vertexDescriptionInputs.AppendLine("vdi.{0} = v.texcoord{1};", channel.GetUVName(), (int)channel);
}

// additional requirements have come from the lighting model
// and are not needed in the shader graph
{
var replaceString = "surfaceInput.{0} = {0};";
GenerateSpaceTranslationSurfaceInputs(graphRequiements.requiresNormal, InterpolatorType.Normal, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(graphRequiements.requiresTangent, InterpolatorType.Tangent, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(graphRequiements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(graphRequiements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(graphRequiements.requiresPosition, InterpolatorType.Position, surfaceInputs, replaceString);
var replaceString = "surfaceInput.{0} = {0};";
GenerateSpaceTranslationSurfaceInputs(surfaceRequirements.requiresNormal, InterpolatorType.Normal, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(surfaceRequirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(surfaceRequirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(surfaceRequirements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(surfaceRequirements.requiresPosition, InterpolatorType.Position, surfaceInputs, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexDescriptionRequirements.requiresNormal, InterpolatorType.Normal, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexDescriptionRequirements.requiresTangent, InterpolatorType.Tangent, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexDescriptionRequirements.requiresBitangent, InterpolatorType.BiTangent, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexDescriptionRequirements.requiresViewDir, InterpolatorType.ViewDirection, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexDescriptionRequirements.requiresPosition, InterpolatorType.Position, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexRequirements.requiresNormal, InterpolatorType.Normal, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexRequirements.requiresTangent, InterpolatorType.Tangent, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexRequirements.requiresBitangent, InterpolatorType.BiTangent, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexRequirements.requiresViewDir, InterpolatorType.ViewDirection, sg, replaceString);
GenerateSpaceTranslationSurfaceInputs(vertexRequirements.requiresPosition, InterpolatorType.Position, sg, replaceString);
if (graphRequiements.requiresVertexColor)
if (pixelRequirements.requiresVertexColor)
if (graphRequiements.requiresScreenPosition)
if (pixelRequirements.requiresScreenPosition)
foreach (var channel in graphRequiements.requiresMeshUVs.Distinct())
foreach (var channel in pixelRequirements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} = {0};", channel.GetUVName()), false);
}

vertexShader,
pixelShader,
surfaceInputs,
shaderGraphRequirements,
shaderGraphRequirements,
ShaderGraphRequirements.none,
ShaderGraphRequirements.none,

正在加载...
取消
保存