浏览代码

Working lightweight metallic master node.

/main
Tim Cooper 7 年前
当前提交
fb21bff8
共有 17 个文件被更改,包括 376 次插入584 次删除
  1. 8
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightInput.cginc
  2. 2
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightPBR.cginc
  3. 2
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightShadows.cginc
  4. 112
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template
  5. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderUnlit.template
  6. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
  7. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ShaderGraphRequirements.cs
  8. 374
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs
  9. 46
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs
  10. 179
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightUnlitMasterNode.cs
  11. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  12. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs
  13. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/BitangentNode.cs
  14. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs
  15. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  16. 163
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs.meta

8
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightInput.cginc


float4 tangent : TANGENT;
float4 texcoord0 : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
#ifdef VERTINPUT_CUSTOM
VERTINPUT_CUSTOM;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};

half4 fogCoord : TEXCOORD2; // x: fogCoord, yzw: vertexColor
half3 normal : TEXCOORD3;
half4 meshUV0 : TEXCOORD4; // uv01.xy: uv0, uv01.zw: uv1
#ifdef VERTOUTPUT_CUSTOM
VERTOUTPUT_CUSTOM
#endif
half3 tangent : TEXCOORD5;
half3 binormal : TEXCOORD6;
float4 hpos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};

2
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightPBR.cginc


return albedo * (half3(1, 1, 1) - specColor);
}
half4 FragmentLightingPBR(float4 uv, float4 posWS, float3 normal, float3 tangent, float3 binormal, float4 viewDir, float4 fogCoord,
half4 FragmentLightingPBR(float4 uv, float3 posWS, float3 normal, float3 tangent, float3 binormal, float3 viewDir, float4 fogCoord,
float3 albedo, float metallic, float3 specular, float smoothness, float3 normalMap, float occlusion, float3 emission, float alpha)
{
float2 lightmapUV = uv.zw;

2
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightShadows.cginc


return attenuation * 0.25;
}
inline half ComputeShadowAttenuation(half3 vertexNormal, half4 posWS, half3 shadowDir)
inline half ComputeShadowAttenuation(half3 vertexNormal, half3 posWS, half3 shadowDir)
{
half NdotL = dot(vertexNormal, shadowDir);
half bias = saturate(1.0 - NdotL) * _ShadowData.z;

112
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template


#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma vertex Vertex
#pragma fragment LightweightFragmentPBR
#pragma vertex vert
#pragma fragment frag
${Defines}
${Defines}
#include "CGIncludes/LightweightPBR.cginc"
//#define UNITY_SAMPLE_TEX2D(tex, coord) LIGHTWEIGHT_GAMMA_TO_LINEAR4(tex.Sample (sampler##tex,coord))
struct GraphVertexOutput
{
float4 position : POSITION;
float4 lwCustom : TEXCOORD0;
float4 fogCoord : TEXCOORD1; // x: fogCoord, yzw: vertexColor
${Interpolators}
UNITY_VERTEX_OUTPUT_STEREO
};
#define VERTEX_CUSTOM \
${VertexShaderBody}
//#define VERTINPUT_CUSTOM \
//${VertexInputs}
GraphVertexOutput vert (GraphVertexInput v)
{
v = PopulateVertexData(v);
UNITY_SETUP_INSTANCE_ID(v);
#define VERTOUTPUT_CUSTOM \
${VertexOutputs}
#include "CGIncludes/LightweightPBR.cginc"
GraphVertexOutput o = (GraphVertexOutput)0;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.position = UnityObjectToClipPos(v.vertex);
${VertexShader}
${ShaderPropertyUsages}
${ShaderFunctions}
void DefineSurface(VertOutput i, inout SurfacePBR o)
{
${PixelShaderBody}
#ifdef LIGHTMAP_ON
o.lwCustom.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
float3 lwWNormal = normalize(UnityObjectToWorldNormal(v.normal));
float3 lwWorldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
#if defined(_VERTEX_LIGHTS) && defined(_MULTIPLE_LIGHTS)
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, lwWNormal, lwWorldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, lwWNormal, atten);
}
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(lwWNormal, 1)));
#endif
UNITY_TRANSFER_FOG(o, o.position);
return o;
fixed4 frag (GraphVertexOutput IN) : SV_Target
{
SurfaceInputs surfaceInput = (SurfaceInputs)0;
${SurfaceInputs}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
float3 Albedo = float3(0.5, 0.5, 0.5);
float3 Specular = float3(0, 0, 0);
float Metallic = 0;
float3 Normal = float3(.5, .5, 1);
float3 Emission = 0;
float Smoothness = 0;
float Occlusion = 1;
float Alpha = 1;
${SurfaceOutputRemap}
return FragmentLightingPBR(
IN.lwCustom,
surfaceInput.worldSpacePosition,
surfaceInput.worldSpaceNormal,
surfaceInput.worldSpaceTangent,
surfaceInput.worldSpaceBiTangent,
surfaceInput.worldSpaceViewDirection,
IN.fogCoord,
Albedo,
Metallic,
Specular,
Smoothness,
Normal,
Occlusion,
Emission,
Alpha);
}
ENDCG
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderUnlit.template


${SurfaceInputs}
SurfaceDescription surf = PopulateSurfaceData(surfaceInput);
${SurfaceOutputRemap}
float3 Color = 0;
${SurfaceOutputRemap}
#ifdef _ALPHABLEND_ON
return fixed4(Color, Alpha);
#else

14
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs


requiresVertexColor = requiresVertexColor,
requiresMeshUVs = meshUV
};
reqs = reqs.Union(nodeForRequirements.GetNodeSpecificRequirements());
ListPool<INode>.Release(activeNodeList);
return reqs;
}

float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
float4 texcoord0 : TEXCOORD0;
float4 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};";

GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, surfaceInputs,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGeneratorNames.WorldSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
GenerateSpaceTranslationSurfaceInputs(requirements.requiresViewDir, surfaceInputs,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,

if ((requirements.requiresBitangent & NeededCoordinateSpace.View) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.ViewSpaceBiTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.World) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceSpaceBiTangent), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.WorldSpaceBiTangent), false);
if ((requirements.requiresBitangent & NeededCoordinateSpace.Tangent) > 0)
pixelShader.AddShaderChunk(string.Format("float3 {0} = IN.{0};", ShaderGeneratorNames.TangentSpaceBiTangent), false);

activeNode.CollectShaderProperties(shaderProperties, mode);
}
pixelShader.AddShaderChunk("SurfaceDescription surface;", false);
pixelShader.AddShaderChunk("SurfaceDescription surface = (SurfaceDescription)0;", false);
if (mode == GenerationMode.Preview)
{
foreach (var slot in node.GetOutputSlots<MaterialSlot>())

var outputRef = edge.outputSlot;
var fromNode = GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
var remapper = fromNode as INodeGroupRemapper;
if (remapper != null && !remapper.IsValidSlotConnection(outputRef.slotId))
continue;
pixelShader.AddShaderChunk(string.Format("surface.{0} = {1};", input.shaderOutputName, fromNode.GetVariableNameForSlot(outputRef.slotId)), true);

31
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/ShaderGraphRequirements.cs


public bool requiresVertexColor;
public List<UVChannel> requiresMeshUVs;
public static ShaderGraphRequirements none
{
get
{
return new ShaderGraphRequirements
{
requiresMeshUVs = new List<UVChannel>()
};
}
}
public bool NeedsTangentSpace()
{
var compoundSpaces = requiresBitangent | requiresNormal | requiresPosition

return (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
}
public ShaderGraphRequirements Union(ShaderGraphRequirements other)
{
var newReqs = new ShaderGraphRequirements();
newReqs.requiresNormal = other.requiresNormal | requiresNormal;
newReqs.requiresTangent = other.requiresTangent | requiresTangent;
newReqs.requiresBitangent = other.requiresBitangent | requiresBitangent;
newReqs.requiresViewDir = other.requiresViewDir | requiresViewDir;
newReqs.requiresPosition = other.requiresPosition | requiresPosition;
newReqs.requiresScreenPosition = other.requiresScreenPosition | requiresScreenPosition;
newReqs.requiresVertexColor = other.requiresVertexColor | requiresVertexColor;
newReqs.requiresMeshUVs = new List<UVChannel>();
if(requiresMeshUVs != null)
newReqs.requiresMeshUVs.AddRange(requiresMeshUVs);
if(other.requiresMeshUVs != null)
newReqs.requiresMeshUVs.AddRange(other.requiresMeshUVs);
return newReqs;
}
}
}

374
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs


namespace UnityEngine.MaterialGraph
{
/*[Serializable]
public abstract class AbstractLightweightPBRMasterNode : AbstractMasterNode
[Serializable]
public abstract class AbstractLightweightPBRMasterNode : AbstractLightweightMasterNode
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";

public const int AlphaSlotId = 6;
public const int VertexOffsetId = 7;
[SerializeField]
private SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();
public SurfaceMaterialOptions options
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
get { return m_MaterialOptions; }
}
public abstract string GetWorkflowName();
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
defines.AddShaderChunk("#define _GLOSSYREFLECTIONS_ON", true);
defines.AddShaderChunk("#define _SPECULARHIGHLIGHTS_ON", true);
void GenerateNodeFunctionsAndPropertyUsages(
ShaderGenerator shaderBody,
ShaderGenerator propertyUsages,
ShaderGenerator nodeFunction,
GenerationMode mode,
int[] validNodeIds)
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, NodeUtils.IncludeSelf.Include,
new List<int>(validNodeIds));
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
{
if (node is IGeneratesFunction)
(node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, mode);
node.GeneratePropertyUsages(propertyUsages, mode);
}
var nodes = ListPool<INode>.Get();
//Get the rest of the nodes for all the other slots
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, NodeUtils.IncludeSelf.Exclude, new List<int>(vertexInputs));
for (var i = 0; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, mode);
}
ListPool<INode>.Release(nodes);
if(IsNormalMapConnected())
defines.AddShaderChunk("#define _NORMALMAP 1", true);
void GenerateVertexShaderInternal(
ShaderGenerator propertyUsages,
ShaderGenerator shaderBody,
ShaderGenerator nodeFunction,
ShaderGenerator vertexShaderBlock,
GenerationMode mode)
protected override int GetInterpolatorStartIndex()
GenerateNodeFunctionsAndPropertyUsages(vertexShaderBlock, propertyUsages, nodeFunction, mode, vertexInputs);
var slot = FindInputSlot<MaterialSlot>(VertexOffsetId);
foreach (var edge in owner.GetEdges(slot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
var remapper = fromNode as INodeGroupRemapper;
if (remapper != null && !remapper.IsValidSlotConnection(outputRef.slotId))
continue;
vertexShaderBlock.AddShaderChunk("v.vertex.xyz += " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
return 2;
public override string GetSubShader(GenerationMode mode, PropertyGenerator shaderPropertiesVisitor)
public override ShaderGraphRequirements GetNodeSpecificRequirements()
var templateLocation = ShaderGenerator.GetTemplatePath("lightweightSubshaderPBR.template");
if (!File.Exists(templateLocation))
return string.Empty;
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
node.GeneratePropertyBlock(shaderPropertiesVisitor, mode);
var templateText = File.ReadAllText(templateLocation);
var shaderBodyVisitor = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var shaderPropertyUsagesVisitor = new ShaderGenerator();
var shaderInputVisitor = new ShaderGenerator();
var shaderOutputVisitor = new ShaderGenerator();
var vertexShaderBlock = new ShaderGenerator();
var definesVisitor = new ShaderGenerator();
GenerateSurfaceShaderInternal(
shaderPropertyUsagesVisitor,
shaderBodyVisitor,
shaderFunctionVisitor,
shaderInputVisitor,
shaderOutputVisitor,
vertexShaderBlock,
definesVisitor,
mode);
GenerateVertexShaderInternal(
shaderPropertyUsagesVisitor,
shaderBodyVisitor,
shaderFunctionVisitor,
vertexShaderBlock,
mode);
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
m_MaterialOptions.GetTags(tagsVisitor);
m_MaterialOptions.GetBlend(blendingVisitor);
m_MaterialOptions.GetCull(cullingVisitor);
m_MaterialOptions.GetDepthTest(zTestVisitor);
m_MaterialOptions.GetDepthWrite(zWriteVisitor);
GetDefines(definesVisitor);
var resultShader = templateText.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ShaderFunctions}", shaderFunctionVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${VertexInputs}", shaderInputVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${VertexOutputs}", shaderOutputVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${PixelShaderBody}", shaderBodyVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LOD}", "" + m_MaterialOptions.lod);
resultShader = resultShader.Replace("${Defines}", definesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${VertexShaderBody}", vertexShaderBlock.GetShaderString(3));
return resultShader;
}
public void GetDefines(ShaderGenerator visitor)
{
if(GetWorkflowName() == "Metallic")
visitor.AddShaderChunk("#define _METALLIC_SETUP 1", true);
else
visitor.AddShaderChunk("", false);
visitor.AddShaderChunk("#define _GLOSSYREFLECTIONS_ON", true);
visitor.AddShaderChunk("#define _SPECULARHIGHLIGHTS_ON", true);
}
public override string GetFullShader(GenerationMode mode, string name, out List<PropertyGenerator.TextureInfo> configuredTextures)
{
var templateLocation = ShaderGenerator.GetTemplatePath("shader.template");
if (!File.Exists(templateLocation))
{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}
var templateText = File.ReadAllText(templateLocation);
var shaderPropertiesVisitor = new PropertyGenerator();
var resultShader = templateText.Replace("${ShaderName}", name);
resultShader = resultShader.Replace("${SubShader}", GetSubShader(mode, shaderPropertiesVisitor));
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
Debug.Log(resultShader);
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
protected abstract int[] surfaceInputs
{
get;
}
protected abstract int[] vertexInputs
{
get;
var reqs = ShaderGraphRequirements.none;
reqs.requiresNormal |= NeededCoordinateSpace.World;
reqs.requiresTangent |= NeededCoordinateSpace.World;
reqs.requiresBitangent |= NeededCoordinateSpace.World;
reqs.requiresPosition |= NeededCoordinateSpace.World;
reqs.requiresViewDir |= NeededCoordinateSpace.World;
return base.GetNodeSpecificRequirements().Union(reqs);
private void GenerateSurfaceShaderInternal(
ShaderGenerator propertyUsages,
ShaderGenerator shaderBody,
ShaderGenerator nodeFunction,
ShaderGenerator shaderInputVisitor,
ShaderGenerator shaderOutputVisitor,
ShaderGenerator vertexShaderBlock,
ShaderGenerator definesVisitor,
GenerationMode mode)
bool IsNormalMapConnected()
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, NodeUtils.IncludeSelf.Include,
new List<int>(surfaceInputs));
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
{
if (node is IGeneratesFunction)
{
((IGeneratesFunction)node).GenerateNodeFunction(nodeFunction, mode);
}
node.GeneratePropertyUsages(propertyUsages, mode);
}
int vertInputIndex = 2;
int vertOutputIndex = 5;
// always add everything because why not.
/*shaderInputVisitor.AddShaderChunk("float4 vertex : POSITION; \\", true);
shaderInputVisitor.AddShaderChunk("half3 normal : NORMAL; \\", true);
shaderInputVisitor.AddShaderChunk("half4 tangent : TANGENT; \\", true);
shaderInputVisitor.AddShaderChunk("half4 color : COLOR; \\", true);
shaderInputVisitor.AddShaderChunk("half4 texcoord0 : TEXCOORD0; \\", true);
shaderInputVisitor.AddShaderChunk("float2 lightmapUV : TEXCOORD1;", true);*
shaderInputVisitor.AddShaderChunk("half4 texcoord1 : TEXCOORD1;", true);
// Need these for lighting
/*shaderOutputVisitor.AddShaderChunk("float4 posWS : TEXCOORD0; \\", true);
shaderOutputVisitor.AddShaderChunk("half4 viewDir : TEXCOORD1; \\", true);
shaderOutputVisitor.AddShaderChunk("half4 fogCoord : TEXCOORD2; \\", true);
shaderOutputVisitor.AddShaderChunk("half3 normal : TEXCOORD3; \\", true);
shaderOutputVisitor.AddShaderChunk("half4 meshUV0 : TEXCOORD4; \\", true);
shaderOutputVisitor.AddShaderChunk("float4 hpos : SV_POSITION; \\", true);*
bool requiresBitangent = activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
bool requiresTangent = activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent());
bool requiresViewDirTangentSpace = activeNodeList.OfType<IMayRequireViewDirectionTangentSpace>().Any(x => x.RequiresViewDirectionTangentSpace());
bool requiresViewDir = activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection());
bool requiresWorldPos = activeNodeList.OfType<IMayRequireWorldPosition>().Any(x => x.RequiresWorldPosition());
bool requiresNormal = activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal());
bool requiresScreenPosition = activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
foreach (var slot in GetInputSlots<MaterialSlot>())
{
if (surfaceInputs.Contains(slot.id))
{
foreach (var edge in owner.GetEdges(slot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
var remapper = fromNode as INodeGroupRemapper;
if (remapper != null && !remapper.IsValidSlotConnection(outputRef.slotId))
continue;
if (slot.id == NormalSlotId)
{
requiresBitangent = true;
requiresTangent = true;
definesVisitor.AddShaderChunk("#define _NORMALMAP 1", true);
}
}
}
}
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
{
var channel = (UVChannel)uvIndex;
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
{
if(uvIndex != 0)
{
shaderInputVisitor.AddShaderChunk(string.Format("half4 texcoord{0} : TEXCOORD{1};", uvIndex, vertInputIndex), true);
shaderOutputVisitor.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex), true);
vertInputIndex++;
vertOutputIndex++;
}
shaderBody.AddShaderChunk(string.Format("half4 {0} = i.meshUV{1};", channel.GetUVName(), uvIndex), true);
}
}
if (requiresViewDir || requiresViewDirTangentSpace)
{
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceViewDirection + " = i.viewDir;", true);
}
if (requiresWorldPos)
{
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpacePosition + " = i.posWS;", true);
}
if (requiresScreenPosition)
{
shaderOutputVisitor.AddShaderChunk(string.Format("half4 screenPos : TEXCOORD{0};", vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk("o.screenPos = ComputeScreenPos(v.vertex);", true);
shaderBody.AddShaderChunk("float4 " + ShaderGeneratorNames.ScreenPosition + " = i.screenPos;", true);
vertOutputIndex++;
}
if (requiresBitangent || requiresTangent || requiresViewDirTangentSpace)
{
shaderOutputVisitor.AddShaderChunk(string.Format("half3 tangent : TEXCOORD{0}; \\", vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk("o.tangent = normalize(UnityObjectToWorldDir(v.tangent)); \\", true);
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceTangent + " = normalize(i.tangent.xyz);", true);
vertOutputIndex++;
}
if (requiresBitangent || requiresNormal || requiresViewDirTangentSpace)
{
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceNormal + " = normalize(i.normal);", true);
}
if (requiresBitangent || requiresViewDirTangentSpace)
{
shaderOutputVisitor.AddShaderChunk(string.Format("half3 binormal : TEXCOORD{0};", vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk("o.binormal = cross(o.normal, o.tangent) * v.tangent.w;", true);
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceBitangent + " = i.binormal;", true);
vertOutputIndex++;
}
if (requiresViewDirTangentSpace)
{
shaderBody.AddShaderChunk(
"float3 " + ShaderGeneratorNames.TangentSpaceViewDirection + ";", true);
shaderBody.AddShaderChunk(
ShaderGeneratorNames.TangentSpaceViewDirection + ".x = dot(" +
ShaderGeneratorNames.WorldSpaceViewDirection + "," +
ShaderGeneratorNames.WorldSpaceTangent + ");", true);
shaderBody.AddShaderChunk(
ShaderGeneratorNames.TangentSpaceViewDirection + ".y = dot(" +
ShaderGeneratorNames.WorldSpaceViewDirection + "," +
ShaderGeneratorNames.WorldSpaceBitangent + ");", true);
shaderBody.AddShaderChunk(
ShaderGeneratorNames.TangentSpaceViewDirection + ".z = dot(" +
ShaderGeneratorNames.WorldSpaceViewDirection + "," +
ShaderGeneratorNames.WorldSpaceNormal + ");", true);
}
if (requiresVertexColor)
{
shaderOutputVisitor.AddShaderChunk(string.Format("half4 color : TEXCOORD{0};", vertOutputIndex), true);
shaderBody.AddShaderChunk("float4 " + ShaderGeneratorNames.VertexColor + " = i.color;", true);
vertInputIndex++;
vertOutputIndex++;
}
GenerateNodeCode(shaderBody, propertyUsages, mode);
var nm = FindSlot<MaterialSlot>(NormalSlotId);
return owner.GetEdges(nm.slotReference).Any();
public void GenerateNodeCode(ShaderGenerator shaderBody, ShaderGenerator propertyUsages, GenerationMode generationMode)
/* public void GenerateNodeCode(ShaderGenerator shaderBody, ShaderGenerator propertyUsages, GenerationMode generationMode)
var nodes = ListPool<INode>.Get();
//Get the rest of the nodes for all the other slots
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, NodeUtils.IncludeSelf.Exclude, new List<int>(surfaceInputs));
for (var i = 0; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
ListPool<INode>.Release(nodes);
foreach (var slot in GetInputSlots<MaterialSlot>())
{

}
}
}
}
}*/
}*/
}
}

46
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
/* [Serializable]
[Serializable]
[Title("Master/Lightweight/PBR Metallic")]
public class LightweightMetallicMasterNode : AbstractLightweightPBRMasterNode
{

// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
VertexOffsetId
});
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
VertexOffsetId
});
}
protected override string GetTemplateName()
{
return "lightweightSubshaderPBR.template";
}
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
{
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
defines.AddShaderChunk("#define _METALLIC_SETUP 1", true);
protected override int[] surfaceInputs
protected override IEnumerable<int> masterSurfaceInputs
{
get
{

}
}
protected override int[] vertexInputs
protected override IEnumerable<int> masterVertexInputs
{
get
{

};
}
}
public override string GetWorkflowName()
{
return WorkflowName;
}
}*/
}
}

179
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightUnlitMasterNode.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Text.RegularExpressions;
using UnityEngine.Graphing;

[Title("Master/Lightweight/Unlit")]
public class LightweightUnlitMasterNode : MasterNode
public class LightweightUnlitMasterNode : AbstractLightweightMasterNode
{
public const string ColorSlotName = "Color";
public const string AlphaSlotName = "Alpha";

});
}
protected int[] masterSurfaceInputs
protected override IEnumerable<int> masterSurfaceInputs
{
get
{

}
}
protected int[] masterVertexInputs
protected override IEnumerable<int> masterVertexInputs
{
get
{

}
}
public override string GetSubShader(ShaderGraphRequirements shaderGraphRequirements)
protected override string GetTemplateName()
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
m_MaterialOptions.GetTags(tagsVisitor);
m_MaterialOptions.GetBlend(blendingVisitor);
m_MaterialOptions.GetCull(cullingVisitor);
m_MaterialOptions.GetDepthTest(zTestVisitor);
m_MaterialOptions.GetDepthWrite(zWriteVisitor);
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
var interpolators = new ShaderGenerator();
var vertexShader = new ShaderGenerator();
var surfaceInput = new ShaderGenerator();
// bitangent needs normal for x product
if (shaderGraphRequirements.requiresNormal > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : NORMAL;", ShaderGeneratorNames.ObjectSpaceNormal), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.normal;", ShaderGeneratorNames.ObjectSpaceNormal), false);
surfaceInput.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceNormal), false);
}
if (shaderGraphRequirements.requiresTangent > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TANGENT;", ShaderGeneratorNames.ObjectSpaceTangent), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.tangent;", ShaderGeneratorNames.ObjectSpaceTangent), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceTangent), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = normalize(cross(normalize(IN.{1}), normalize(IN.{2}.xyz)) * IN.{2}.w);",
ShaderGeneratorNames.ObjectSpaceBiTangent,
ShaderGeneratorNames.ObjectSpaceTangent,
ShaderGeneratorNames.ObjectSpaceNormal), false);
}
return "lightweightSubshaderUnlit.template";
}
int interpolatorIndex = 0;
if (shaderGraphRequirements.requiresViewDir > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpaceViewDirection, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ObjSpaceViewDir(v.vertex);", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.requiresPosition > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpacePosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.vertex;", ShaderGeneratorNames.ObjectSpacePosition), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpacePosition), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.NeedsTangentSpace())
{
surfaceInput.AddShaderChunk(string.Format("float3x3 tangentSpaceTransform = float3x3({0},{1},{2});",
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ObjectSpaceNormal), false);
}
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresNormal, surfaceInput,
ShaderGeneratorNames.ObjectSpaceNormal, ShaderGeneratorNames.ViewSpaceNormal,
ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.TangentSpaceNormal);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresTangent, surfaceInput,
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ViewSpaceTangent,
ShaderGeneratorNames.WorldSpaceTangent, ShaderGeneratorNames.TangentSpaceTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresBitangent, surfaceInput,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresViewDir, surfaceInput,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,
ShaderGeneratorNames.WorldSpaceViewDirection, ShaderGeneratorNames.TangentSpaceViewDirection);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresPosition, surfaceInput,
ShaderGeneratorNames.ObjectSpacePosition, ShaderGeneratorNames.ViewSpacePosition,
ShaderGeneratorNames.WorldSpacePosition, ShaderGeneratorNames.TangentSpacePosition);
if (shaderGraphRequirements.requiresVertexColor)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : COLOR;", ShaderGeneratorNames.VertexColor), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = color", ShaderGeneratorNames.VertexColor), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.VertexColor), false);
}
if (shaderGraphRequirements.requiresScreenPosition)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ScreenPosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ComputeScreenPos(UnityObjectToClipPos(v.vertex)", ShaderGeneratorNames.ScreenPosition), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.ScreenPosition), false);
interpolatorIndex++;
}
foreach (var channel in shaderGraphRequirements.requiresMeshUVs.Distinct())
{
interpolators.AddShaderChunk(string.Format("half4 {0} : TEXCOORD{1};", channel.GetUVName(), interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.texcoord{1};", channel.GetUVName(), interpolatorIndex == 0 ? "" : interpolatorIndex.ToString()), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", channel.GetUVName()), false);
interpolatorIndex++;
}
ShaderGenerator defines = new ShaderGenerator();
ShaderGenerator surfaceOutputRemap = new ShaderGenerator();
var color = FindInputSlot<MaterialSlot>(ColorSlotId);
var colorIn = owner.GetEdges(color.slotReference).FirstOrDefault();
if (colorIn != null)
{
var outputRef = colorIn.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
surfaceOutputRemap.AddShaderChunk(ConvertConcreteSlotValueTypeToString(precision, color.concreteValueType)
+ " "
+ color.shaderOutputName
+ " = surf."
+ color.shaderOutputName + ";", true);
}
else
{
surfaceOutputRemap.AddShaderChunk(ConvertConcreteSlotValueTypeToString(precision, color.concreteValueType)
+ " " + color.shaderOutputName + " = 0;", false);
}
var alpha = FindInputSlot<MaterialSlot>(AlphaSlotId);
var alphaIn = owner.GetEdges(alpha.slotReference).FirstOrDefault();
if (alphaIn != null)
{
var outputRef = alphaIn.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
surfaceOutputRemap.AddShaderChunk(ConvertConcreteSlotValueTypeToString(precision, alpha.concreteValueType)
+ " "
+ alpha.shaderOutputName
+ " = surf."
+ alpha.shaderOutputName + ";", true);
}
else
{
surfaceOutputRemap.AddShaderChunk(ConvertConcreteSlotValueTypeToString(precision, alpha.concreteValueType)
+ " " + alpha.shaderOutputName + " = 0;", false);
}
var templateLocation = ShaderGenerator.GetTemplatePath("lightweightSubshaderUnlit.template");
if (!File.Exists(templateLocation))
return string.Empty;
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultShader = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultShader = resultShader.Replace("${Interpolators}", interpolators.GetShaderString(3));
resultShader = resultShader.Replace("${VertexShader}", vertexShader.GetShaderString(3));
resultShader = resultShader.Replace("${SurfaceInputs}", surfaceInput.GetShaderString(0));
resultShader = resultShader.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(0));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LOD}", "" + m_MaterialOptions.lod);
return resultShader;
protected override int GetInterpolatorStartIndex()
{
return 0;
}
}
}

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


// preserve the old current value.
addingSlot.currentValue = foundSlot.currentValue;
}
public virtual ShaderGraphRequirements GetNodeSpecificRequirements()
{
return ShaderGraphRequirements.none;
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs


case Binding.WorldSpaceTangent:
return ShaderGeneratorNames.WorldSpaceTangent;
case Binding.WorldSpaceBitangent:
return ShaderGeneratorNames.WorldSpaceSpaceBiTangent;
return ShaderGeneratorNames.WorldSpaceBiTangent;
case Binding.WorldSpacePosition:
return ShaderGeneratorNames.WorldSpacePosition;
case Binding.TangentSpaceNormal:

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Geometry/BitangentNode.cs


public override string GetVariableNameForSlot(int slotId)
{
return ShaderGeneratorNames.WorldSpaceSpaceBiTangent;
return ShaderGeneratorNames.WorldSpaceBiTangent;
}
public NeededCoordinateSpace RequiresBitangent()

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs


ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresBitangent, pixelShader,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGeneratorNames.WorldSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresViewDir, pixelShader,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,

10
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


public const string ObjectSpaceBiTangent = "objectSpaceBiTangent";
public const string ViewSpaceBiTangent = "viewSpaceBiTangent";
public const string WorldSpaceSpaceBiTangent = "worldSpaceSpaceBiTangent";
public const string WorldSpaceBiTangent = "worldSpaceBiTangent";
public const string WorldSpaceTangent = "worldSpaceSpaceTangent";
public const string TangentSpaceTangent = "tangentSpaceSpaceTangent";
public const string WorldSpaceTangent = "worldSpaceTangent";
public const string TangentSpaceTangent = "tangentSpaceTangent";
public const string ObjectSpaceViewDirection = "objectSpaceViewDirection";
public const string ViewSpaceViewDirection = "viewSpaceViewDirection";

GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresBitangent, pixelShader,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGeneratorNames.WorldSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresViewDir, pixelShader,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,

if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
{
interpolators.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex == 0 ? "" : uvIndex.ToString()), false);
vertexShader.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex), false);
pixelShader.AddShaderChunk(string.Format("surfaceInput.{0} = IN.meshUV{1};", channel.GetUVName(), uvIndex), false);
interpolatorIndex++;
}

163
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs


using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public abstract class AbstractLightweightMasterNode : MasterNode
{
protected abstract IEnumerable<int> masterSurfaceInputs { get; }
protected abstract IEnumerable<int> masterVertexInputs { get; }
protected abstract string GetTemplateName();
protected virtual void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
{
foreach (var slot in GetInputSlots<MaterialSlot>())
{
var edge = owner.GetEdges(slot.slotReference).FirstOrDefault();
if (edge == null)
continue;
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
}
public override string GetSubShader(ShaderGraphRequirements shaderGraphRequirements)
{
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
m_MaterialOptions.GetTags(tagsVisitor);
m_MaterialOptions.GetBlend(blendingVisitor);
m_MaterialOptions.GetCull(cullingVisitor);
m_MaterialOptions.GetDepthTest(zTestVisitor);
m_MaterialOptions.GetDepthWrite(zWriteVisitor);
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
var interpolators = new ShaderGenerator();
var vertexShader = new ShaderGenerator();
var surfaceInput = new ShaderGenerator();
// bitangent needs normal for x product
if (shaderGraphRequirements.requiresNormal > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : NORMAL;", ShaderGeneratorNames.ObjectSpaceNormal), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.normal;", ShaderGeneratorNames.ObjectSpaceNormal), false);
surfaceInput.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceNormal), false);
}
if (shaderGraphRequirements.requiresTangent > 0 || shaderGraphRequirements.requiresBitangent > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TANGENT;", ShaderGeneratorNames.ObjectSpaceTangent), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.tangent;", ShaderGeneratorNames.ObjectSpaceTangent), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpaceTangent), false);
surfaceInput.AddShaderChunk(string.Format("float3 {0} = normalize(cross(normalize(IN.{1}), normalize(IN.{2}.xyz)) * IN.{2}.w);",
ShaderGeneratorNames.ObjectSpaceBiTangent,
ShaderGeneratorNames.ObjectSpaceNormal,
ShaderGeneratorNames.ObjectSpaceTangent), false);
}
int interpolatorIndex = GetInterpolatorStartIndex();
if (shaderGraphRequirements.requiresViewDir > 0)
{
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpaceViewDirection, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ObjSpaceViewDir(v.vertex);", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
surfaceInput.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", ShaderGeneratorNames.ObjectSpaceViewDirection), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.requiresPosition > 0)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ObjectSpacePosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.vertex;", ShaderGeneratorNames.ObjectSpacePosition), false);
surfaceInput.AddShaderChunk(string.Format("float4 {0} = IN.{0};", ShaderGeneratorNames.ObjectSpacePosition), false);
interpolatorIndex++;
}
if (shaderGraphRequirements.NeedsTangentSpace())
{
surfaceInput.AddShaderChunk(string.Format("float3x3 tangentSpaceTransform = float3x3({0},{1},{2});",
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ObjectSpaceNormal), false);
}
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresNormal, surfaceInput,
ShaderGeneratorNames.ObjectSpaceNormal, ShaderGeneratorNames.ViewSpaceNormal,
ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.TangentSpaceNormal);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresTangent, surfaceInput,
ShaderGeneratorNames.ObjectSpaceTangent, ShaderGeneratorNames.ViewSpaceTangent,
ShaderGeneratorNames.WorldSpaceTangent, ShaderGeneratorNames.TangentSpaceTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresBitangent, surfaceInput,
ShaderGeneratorNames.ObjectSpaceBiTangent, ShaderGeneratorNames.ViewSpaceBiTangent,
ShaderGeneratorNames.WorldSpaceBiTangent, ShaderGeneratorNames.TangentSpaceBiTangent);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresViewDir, surfaceInput,
ShaderGeneratorNames.ObjectSpaceViewDirection, ShaderGeneratorNames.ViewSpaceViewDirection,
ShaderGeneratorNames.WorldSpaceViewDirection, ShaderGeneratorNames.TangentSpaceViewDirection);
ShaderGenerator.GenerateSpaceTranslationPixelShader(shaderGraphRequirements.requiresPosition, surfaceInput,
ShaderGeneratorNames.ObjectSpacePosition, ShaderGeneratorNames.ViewSpacePosition,
ShaderGeneratorNames.WorldSpacePosition, ShaderGeneratorNames.TangentSpacePosition);
if (shaderGraphRequirements.requiresVertexColor)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : COLOR;", ShaderGeneratorNames.VertexColor), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = color", ShaderGeneratorNames.VertexColor), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.VertexColor), false);
}
if (shaderGraphRequirements.requiresScreenPosition)
{
interpolators.AddShaderChunk(string.Format("float4 {0} : TEXCOORD{1};", ShaderGeneratorNames.ScreenPosition, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = ComputeScreenPos(UnityObjectToClipPos(v.vertex)", ShaderGeneratorNames.ScreenPosition), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", ShaderGeneratorNames.ScreenPosition), false);
interpolatorIndex++;
}
foreach (var channel in shaderGraphRequirements.requiresMeshUVs.Distinct())
{
interpolators.AddShaderChunk(string.Format("half4 {0} : TEXCOORD{1};", channel.GetUVName(), interpolatorIndex == 0 ? "" : interpolatorIndex.ToString()), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = v.texcoord{1};", channel.GetUVName(), (int)channel), false);
surfaceInput.AddShaderChunk(string.Format("surfaceInput.{0} = IN.{0};", channel.GetUVName()), false);
interpolatorIndex++;
}
ShaderGenerator defines = new ShaderGenerator();
ShaderGenerator surfaceOutputRemap = new ShaderGenerator();
GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
var templateLocation = ShaderGenerator.GetTemplatePath(GetTemplateName());
if (!File.Exists(templateLocation))
return string.Empty;
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultShader = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultShader = resultShader.Replace("${Interpolators}", interpolators.GetShaderString(3));
resultShader = resultShader.Replace("${VertexShader}", vertexShader.GetShaderString(3));
resultShader = resultShader.Replace("${SurfaceInputs}", surfaceInput.GetShaderString(0));
resultShader = resultShader.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(0));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LOD}", "" + m_MaterialOptions.lod);
return resultShader;
}
protected abstract int GetInterpolatorStartIndex();
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs.meta


fileFormatVersion: 2
guid: 72c867ea29464bec864ea612e2bcecd3
timeCreated: 1505799928
正在加载...
取消
保存