浏览代码

- Unlit shader graph node

- Plug materialID (automatically deducted from GetMatchingMaterialID)
/scriptablerenderloop-materialgraph
Paul Demeulenaere 8 年前
当前提交
102b0ab8
共有 8 个文件被更改,包括 282 次插入92 次删除
  1. 129
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderloopMaterialGraph.cs
  2. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template
  3. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template
  4. 20
      Assets/ShaderGenerator/Editor/ShaderTypeGeneration.cs
  5. 142
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Unlit.template
  6. 8
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Unlit.template.meta
  7. 21
      Assets/ShaderGenerator/ShaderGeneratorHelper.cs
  8. 12
      Assets/ShaderGenerator/ShaderGeneratorHelper.cs.meta

129
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderloopMaterialGraph.cs


{
public AbstractHDRenderLoopMasterNode()
{
name = GetName();
name = GetType().Name;
protected abstract Type GetSurfaceType();
protected abstract string GetName();
protected abstract int GetMatchingMaterialID();
public sealed override void UpdateNodeAfterDeserialization()
{

var slotOutputName = slot.shaderOutputName;
var inputStruct = typeof(Lit.SurfaceData).GetFields().Any(o => o.Name == slotOutputName) ? "surfaceData" : "builtinData";
pixelShaderBodyVisitor.AddShaderChunk(inputStruct + "." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
var inputStruct = GetSurfaceType().GetFields().Any(o => o.Name == slotOutputName) ? "surfaceData" : "builtinData";
pixelShaderBodyVisitor.AddShaderChunk(inputStruct + "." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", false);
var type = GetMatchingMaterialID();
var typeString = type.ToString();
var fieldsSurface = GetSurfaceType().GetFields();
var materialIdField = fieldsSurface.FirstOrDefault(o => o.FieldType.IsEnum);
if (materialIdField != null)
{
var enumValue = Enum.ToObject(materialIdField.FieldType, GetMatchingMaterialID()).ToString();
var define = string.Format("{0}_{1}", materialIdField.Name, ShaderGeneratorHelper.InsertUnderscore(enumValue));
define = define.ToUpper();
pixelShaderBodyVisitor.AddShaderChunk(string.Format("surfaceData.{0} = {1};", materialIdField.Name, define), false);
}
var resultShader = litShareTemplate.Replace("${VaryingAttributes}", vayringVisitor.GetShaderString(1));
resultShader = resultShader.Replace("${PixelShaderInitialize}", pixelShaderInitVisitor.GetShaderString(1));
resultShader = resultShader.Replace("${PixelShaderBody}", pixelShaderBodyVisitor.GetShaderString(1));

{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
var templatePath = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template";
if (!System.IO.File.Exists(templatePath))
return "";
var templatePathPass = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template";
if (!System.IO.File.Exists(templatePathPass))
return "";
var templateText = System.IO.File.ReadAllText(templatePath);
var templatePassText = System.IO.File.ReadAllText(templatePathPass);
var templateText = GetTemplateText();
var templatePassText = GetTemplatePassText();
var shaderPropertiesVisitor = new PropertyGenerator();
var propertyUsagesVisitor = new ShaderGenerator();

var findLitShareTemplate = new System.Text.RegularExpressions.Regex("#{LitTemplate.*}");
var findLitShareTemplate = new System.Text.RegularExpressions.Regex("#{TemplatePass.*}");
var findUseDataInput = new System.Text.RegularExpressions.Regex("useSurfaceData:{(.*?)}");
var findNeedFragInput = new System.Text.RegularExpressions.Regex("useFragInput:{(.*?)}");
foreach (System.Text.RegularExpressions.Match match in findLitShareTemplate.Matches(templateText))

configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
resultShader = Regex.Replace(resultShader, @"\t", " ");
protected abstract Type GetSurfaceType();
protected abstract int GetMatchingMaterialID();
protected abstract string GetTemplateText();
protected abstract string GetTemplatePassText();
[Title("HDRenderLoop/StandardLit")]
public class StandardtLit : AbstractHDRenderLoopMasterNode
public abstract class LitNode : AbstractHDRenderLoopMasterNode
protected override string GetName()
protected override sealed Type GetSurfaceType()
return "MasterNodeStandardLit";
return typeof(Lit.SurfaceData);
protected override Type GetSurfaceType()
protected sealed override string GetTemplateText()
return typeof(Lit.SurfaceData);
var templatePath = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template";
if (!System.IO.File.Exists(templatePath))
return "";
return System.IO.File.ReadAllText(templatePath);
protected override int GetMatchingMaterialID()
protected sealed override string GetTemplatePassText()
return (int)Lit.MaterialId.LitStandard;
var templatePathPass = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template";
if (!System.IO.File.Exists(templatePathPass))
return "";
return System.IO.File.ReadAllText(templatePathPass);
[Title("HDRenderLoop/SubsurfaceScatteringLit")]
public class SubsurfaceScatteringLit : AbstractHDRenderLoopMasterNode
[Title("HDRenderLoop/Lit/Standard")]
public class StandardtLitNode : LitNode
protected override string GetName()
protected override int GetMatchingMaterialID()
return "MasterNodeSubsurfaceScatteringLit";
return (int)Lit.MaterialId.LitStandard;
}
protected override Type GetSurfaceType()
[Serializable]
[Title("HDRenderLoop/Lit/SubsurfaceScattering")]
public class SubsurfaceScatteringLitNode : LitNode
{
protected override int GetMatchingMaterialID()
return typeof(Lit.SurfaceData);
return (int)Lit.MaterialId.LitSSS;
}
[Serializable]
[Title("HDRenderLoop/Lit/SubsurfaceClearCoat")]
public class SubsurfaceClearCoatLitNode : LitNode
{
return (int)Lit.MaterialId.LitSSS;
return (int)Lit.MaterialId.LitClearCoat;
[Title("HDRenderLoop/SubsurfaceClearCoatLit")]
public class SubsurfaceClearCoatLit : AbstractHDRenderLoopMasterNode
[Title("HDRenderLoop/Lit/SpecularColor")]
public class SpecularColorLitNode : LitNode
protected override string GetName()
protected override int GetMatchingMaterialID()
return "MasterNodeSubsurfaceClearCoatLit";
return (int)Lit.MaterialId.LitSpecular;
}
[Serializable]
[Title("HDRenderLoop/Unlit")]
public class UnlitNode : AbstractHDRenderLoopMasterNode
{
return typeof(Lit.SurfaceData);
return typeof(Unlit.SurfaceData);
return (int)Lit.MaterialId.LitClearCoat;
}
}
[Serializable]
[Title("HDRenderLoop/SpecularColorLit")]
public class SpecularColorLit : AbstractHDRenderLoopMasterNode
{
protected override string GetName()
{
return "MasterNodeSpecularColorLit";
return -1;
protected override Type GetSurfaceType()
protected sealed override string GetTemplateText()
return typeof(Lit.SurfaceData);
var templatePath = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Unlit.template";
if (!System.IO.File.Exists(templatePath))
return "";
return System.IO.File.ReadAllText(templatePath);
protected override int GetMatchingMaterialID()
protected sealed override string GetTemplatePassText()
return (int)Lit.MaterialId.LitSpecular;
var templatePathPass = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template";
if (!System.IO.File.Exists(templatePathPass))
return "";
return System.IO.File.ReadAllText(templatePathPass);
}
}
}

40
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template


//-------------------------------------------------------------------------------------
#include "common.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderConfig.cs.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderConfig.cs.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Attributes.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPass.cs.hlsl"

#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
//Generated code : Begin
#{LitTemplate|useSurfaceData:{.*}|useFragInput:{positionWS}}
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{positionWS}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassGBuffer.hlsl"

// Debug pass
// ------------------------------------------------------------------
Pass
{
Name "Debug"
Tags{ "LightMode" = "DebugViewMaterial" }
{
Name "Debug"
Tags{ "LightMode" = "DebugViewMaterial" }
Cull[_CullMode]
Cull[_CullMode]
HLSLPROGRAM
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
#pragma vertex VertDefault
#pragma fragment Frag
#define SHADERPASS SHADERPASS_DEBUG_VIEW_MATERIAL
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
#define SHADERPASS SHADERPASS_DEBUG_VIEW_MATERIAL
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
#{LitTemplate|useSurfaceData:{.*}|useFragInput:{.*}}
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{.*}}
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDebugViewMaterial.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDebugViewMaterial.hlsl"
ENDHLSL
}
ENDHLSL
}
// ------------------------------------------------------------------
// Extracts information for lightmapping, GI (emission, albedo, ...)

#define SHADERPASS SHADERPASS_LIGHT_TRANSPORT
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
#{LitTemplate|useSurfaceData:{.*}|useFragInput:{.*}}
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{.*}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassLightTransport.hlsl"

#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
//Generated code : Begin
#{LitTemplate|useSurfaceData:{opacity}|useFragInput:{}}
#{TemplatePass|useSurfaceData:{opacity}|useFragInput:{}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDepthOnly.hlsl"

// ------------------------------------------------------------------
// forward pass
// ------------------------------------------------------------------
Pass
Pass
{
Name "Forward" // Name is not used
Tags{ "LightMode" = "Forward" } // This will be only for transparent object based on the RenderQueue index

#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Lighting.hlsl"
//Generated code : Begin
#{LitTemplate|useSurfaceData:{.*}|useFragInput:{positionWS}}
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{positionWS}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassForward.hlsl"

2
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template


clip(builtinData.opacity - _AlphaCutoff);
#endif
#ifdef UNITY_MATERIAL_LIT
#endif
}
struct Attributes

20
Assets/ShaderGenerator/Editor/ShaderTypeGeneration.cs


return EmitDefines() + EmitTypeDecl() + EmitAccessors();
}
// This function is a helper to follow unity convertion
// when converting fooBar ro FOO_BAR
string InsertUnderscore(string name)
{
for (int i = 1; i < name.Length; i++)
{
if (char.IsLower(name[i - 1]) && char.IsUpper(name[i]))
{
// case switch, insert underscore
name = name.Insert(i, "_");
}
}
return name;
}
public bool Generate()
{
m_Statics = new Dictionary<string, string>();

if (!field.IsSpecialName)
{
string name = field.Name;
name = InsertUnderscore(name);
name = ShaderGeneratorHelper.InsertUnderscore(name);
m_Statics[(type.Name + "_" + name).ToUpper()] = field.GetRawConstantValue().ToString();
}
}

if (attr.needParamDefines)
{
string subNamespace = type.Namespace.Substring(type.Namespace.LastIndexOf((".")) + 1);
string name = InsertUnderscore(field.Name);
string name = ShaderGeneratorHelper.InsertUnderscore(field.Name);
m_Statics[("DEBUGVIEW_" + subNamespace + "_" + type.Name + "_" + name).ToUpper()] = Convert.ToString(attr.paramDefinesStart + debugCounter++);
}

142
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Unlit.template


Shader "HDRenderLoop/Unlit"
{
Properties
{
${ShaderPropertiesHeader}
[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
[HideInInspector] _SurfaceType("__surfacetype", Float) = 0.0
[HideInInspector] _BlendMode ("__blendmode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
[HideInInspector] _CullMode("__cullmode", Float) = 2.0
[Enum(None, 0, DoubleSided, 1)] _DoubleSidedMode("Double sided mode", Float) = 0
}
HLSLINCLUDE
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
//-------------------------------------------------------------------------------------
// Define
//-------------------------------------------------------------------------------------
#define UNITY_MATERIAL_UNLIT // Need to be define before including Material.hlsl
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
//-------------------------------------------------------------------------------------
// Include
//-------------------------------------------------------------------------------------
#include "common.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderConfig.cs.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Attributes.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPass.cs.hlsl"
//-------------------------------------------------------------------------------------
// variable declaration
//-------------------------------------------------------------------------------------
${ShaderFunctions}
${ShaderPropertyUsages}
float _AlphaCutoff;
ENDHLSL
SubShader
{
Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 300
// ------------------------------------------------------------------
// Debug pass
Pass
{
Name "Debug"
Tags { "LightMode" = "DebugViewMaterial" }
Cull[_CullMode]
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
#define SHADERPASS SHADERPASS_DEBUG_VIEW_MATERIAL
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
//Generated code : Begin
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{.*}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassDebugViewMaterial.hlsl"
ENDHLSL
}
// ------------------------------------------------------------------
// forward pass
Pass
{
Name "ForwardUnlit"
Tags { "LightMode" = "ForwardUnlit" }
Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]
Cull [_CullMode]
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
#define SHADERPASS SHADERPASS_FORWARD_UNLIT
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
//Generated code : Begin
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{positionWS}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassForwardUnlit.hlsl"
ENDHLSL
}
// ------------------------------------------------------------------
// Extracts information for lightmapping, GI (emission, albedo, ...)
// This pass it not used during regular rendering.
// ------------------------------------------------------------------
Pass
{
Name "META"
Tags{ "LightMode" = "Meta" }
Cull Off
HLSLPROGRAM
// Lightmap memo
// DYNAMICLIGHTMAP_ON is used when we have an "enlighten lightmap" ie a lightmap updated at runtime by enlighten.This lightmap contain indirect lighting from realtime lights and realtime emissive material.Offline baked lighting(from baked material / light,
// both direct and indirect lighting) will hand up in the "regular" lightmap->LIGHTMAP_ON.
#pragma vertex VertDefault
#pragma fragment Frag
#define SHADERPASS SHADERPASS_LIGHT_TRANSPORT
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
//Generated code : Begin
#{TemplatePass|useSurfaceData:{.*}|useFragInput:{.*}}
//Generated code : End
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderPass/ShaderPassLightTransport.hlsl"
ENDHLSL
}
}
}

8
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Unlit.template.meta


fileFormatVersion: 2
guid: 7fae8307bc8d7854d905e06099720f28
timeCreated: 1480345582
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

21
Assets/ShaderGenerator/ShaderGeneratorHelper.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
public class ShaderGeneratorHelper
{
// This function is a helper to follow unity convertion
// when converting fooBar ro FOO_BAR
public static string InsertUnderscore(string name)
{
for (int i = 1; i < name.Length; i++)
{
if (char.IsLower(name[i - 1]) && char.IsUpper(name[i]))
{
// case switch, insert underscore
name = name.Insert(i, "_");
}
}
return name;
}
}
}

12
Assets/ShaderGenerator/ShaderGeneratorHelper.cs.meta


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