您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

605 行
24 KiB

using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
[FormerName("UnityEditor.ShaderGraph.HDPBRSubShader")]
public class HDPBRSubShader : IPBRSubShader
{
Pass m_PassGBuffer = new Pass()
{
Name = "GBuffer",
LightMode = "GBuffer",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_GBUFFER",
StencilOverride = new List<string>()
{
"// Stencil setup for gbuffer",
"Stencil",
"{",
" WriteMask 7", // [_StencilWriteMask] // default: StencilMask.Lighting (fixed at compile time)
" Ref 2", // [_StencilRef] // default: StencilLightingUsage.RegularLighting (fixed at compile time)
" Comp Always",
" Pass Replace",
"}"
},
ExtraDefines = new List<string>()
{
"#pragma multi_compile _ DEBUG_DISPLAY",
"#pragma multi_compile _ LIGHTMAP_ON",
"#pragma multi_compile _ DIRLIGHTMAP_COMBINED",
"#pragma multi_compile _ DYNAMICLIGHTMAP_ON",
"#pragma multi_compile _ SHADOWS_SHADOWMASK",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassGBuffer.hlsl\"",
},
RequiredFields = new List<string>()
{
"FragInputs.worldToTangent",
"FragInputs.positionRWS",
"FragInputs.texCoord1",
"FragInputs.texCoord2"
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
}
};
Pass m_PassGBufferWithPrepass = new Pass()
{
Name = "GBufferWithPrepass",
LightMode = "GBufferWithPrepass",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_GBUFFER",
StencilOverride = new List<string>()
{
"// Stencil setup for GBufferWithPrepass",
"Stencil",
"{",
" WriteMask 7", // _StencilWriteMask // StencilMask.Lighting (fixed at compile time)
" Ref 2", // _StencilRef // StencilLightingUsage.RegularLighting (fixed at compile time)
" Comp Always",
" Pass Replace",
"}"
},
ExtraDefines = new List<string>()
{
"#pragma multi_compile _ DEBUG_DISPLAY",
"#pragma multi_compile _ LIGHTMAP_ON",
"#pragma multi_compile _ DIRLIGHTMAP_COMBINED",
"#pragma multi_compile _ DYNAMICLIGHTMAP_ON",
"#pragma multi_compile _ SHADOWS_SHADOWMASK",
"#define SHADERPASS_GBUFFER_BYPASS_ALPHA_TEST",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassGBuffer.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
}
};
Pass m_PassMETA = new Pass()
{
Name = "META",
LightMode = "Meta",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_LIGHT_TRANSPORT",
CullOverride = "Cull Off",
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassLightTransport.hlsl\"",
},
RequiredFields = new List<string>()
{
"AttributesMesh.normalOS",
"AttributesMesh.tangentOS", // Always present as we require it also in case of anisotropic lighting
"AttributesMesh.uv0",
"AttributesMesh.uv1",
"AttributesMesh.color",
"AttributesMesh.uv2", // SHADERPASS_LIGHT_TRANSPORT always uses uv2
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
//PBRMasterNode.PositionSlotId
}
};
Pass m_PassShadowCaster = new Pass()
{
Name = "ShadowCaster",
LightMode = "ShadowCaster",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_SHADOWS",
ColorMaskOverride = "ColorMask 0",
ExtraDefines = new List<string>()
{
"#define USE_LEGACY_UNITY_MATRIX_VARIABLES",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassDepthOnly.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
}
};
Pass m_PassDepthOnly = new Pass()
{
Name = "DepthOnly",
LightMode = "DepthOnly",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_DEPTH_ONLY",
ColorMaskOverride = "ColorMask 0",
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassDepthOnly.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
}
};
Pass m_PassMotionVectors = new Pass()
{
Name = "Motion Vectors",
LightMode = "MotionVectors",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_VELOCITY",
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassVelocity.hlsl\"",
},
RequiredFields = new List<string>()
{
"FragInputs.positionRWS",
},
StencilOverride = new List<string>()
{
"// If velocity pass (motion vectors) is enabled we tag the stencil so it don't perform CameraMotionVelocity",
"Stencil",
"{",
" WriteMask 128", // [_StencilWriteMaskMV] (int) HDRenderPipeline.StencilBitMask.ObjectVelocity // this requires us to pull in the HD Pipeline assembly...
" Ref 128", // [_StencilRefMV]
" Comp Always",
" Pass Replace",
"}"
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
Pass m_PassDistortion = new Pass()
{
Name = "Distortion",
LightMode = "DistortionVectors",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_DISTORTION",
BlendOverride = "Blend One One, One One", // [_DistortionSrcBlend] [_DistortionDstBlend], [_DistortionBlurSrcBlend] [_DistortionBlurDstBlend]
BlendOpOverride = "BlendOp Add, Add", // Add, [_DistortionBlurBlendOp]
ZTestOverride = "ZTest LEqual", // [_ZTestModeDistortion]
ZWriteOverride = "ZWrite Off",
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassDistortion.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
Pass m_PassTransparentDepthPrepass = new Pass()
{
Name = "TransparentDepthPrepass",
LightMode = "TransparentDepthPrepass",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_DEPTH_ONLY",
ColorMaskOverride = "ColorMask 0",
ExtraDefines = new List<string>()
{
"#define CUTOFF_TRANSPARENT_DEPTH_PREPASS",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassDepthOnly.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
Pass m_PassTransparentBackface = new Pass()
{
Name = "TransparentBackface",
LightMode = "TransparentBackface",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_FORWARD",
CullOverride = "Cull Front",
ExtraDefines = new List<string>()
{
"#pragma multi_compile _ DEBUG_DISPLAY",
"#pragma multi_compile _ LIGHTMAP_ON",
"#pragma multi_compile _ DIRLIGHTMAP_COMBINED",
"#pragma multi_compile _ DYNAMICLIGHTMAP_ON",
"#pragma multi_compile _ SHADOWS_SHADOWMASK",
"#pragma multi_compile LIGHTLOOP_SINGLE_PASS LIGHTLOOP_TILE_PASS",
"#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassForward.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
Pass m_PassForward = new Pass()
{
Name = "Forward",
LightMode = "Forward",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_FORWARD",
ExtraDefines = new List<string>()
{
"#pragma multi_compile _ DEBUG_DISPLAY",
"#pragma multi_compile _ LIGHTMAP_ON",
"#pragma multi_compile _ DIRLIGHTMAP_COMBINED",
"#pragma multi_compile _ DYNAMICLIGHTMAP_ON",
"#pragma multi_compile _ SHADOWS_SHADOWMASK",
"#pragma multi_compile LIGHTLOOP_SINGLE_PASS LIGHTLOOP_TILE_PASS",
"#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST"
},
StencilOverride = new List<string>()
{
"// Stencil setup for forward",
"Stencil",
"{",
" WriteMask 7", // [_StencilWriteMask] // default: StencilMask.Lighting (fixed at compile time)
" Ref 2", // [_StencilRef] // default: StencilLightingUsage.RegularLighting (fixed at compile time)
" Comp Always",
" Pass Replace",
"}"
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassForward.hlsl\"",
},
RequiredFields = new List<string>()
{
"FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlbedoSlotId,
PBRMasterNode.NormalSlotId,
PBRMasterNode.MetallicSlotId,
PBRMasterNode.SpecularSlotId,
PBRMasterNode.EmissionSlotId,
PBRMasterNode.SmoothnessSlotId,
PBRMasterNode.OcclusionSlotId,
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
Pass m_PassTransparentDepthPostpass = new Pass()
{
Name = "TransparentDepthPostpass",
LightMode = "TransparentDepthPostpass",
TemplateName = "HDPBRPass.template",
ShaderPassName = "SHADERPASS_DEPTH_ONLY",
ColorMaskOverride = "ColorMask 0",
ExtraDefines = new List<string>()
{
"#define CUTOFF_TRANSPARENT_DEPTH_POSTPASS",
},
Includes = new List<string>()
{
"#include \"HDRP/ShaderPass/ShaderPassDepthOnly.hlsl\"",
},
RequiredFields = new List<string>()
{
// "FragInputs.worldToTangent",
// "FragInputs.positionRWS",
},
PixelShaderSlots = new List<int>()
{
PBRMasterNode.AlphaSlotId,
PBRMasterNode.AlphaThresholdSlotId
},
VertexShaderSlots = new List<int>()
{
PBRMasterNode.PositionSlotId
},
};
private static HashSet<string> GetActiveFieldsFromMasterNode(INode iMasterNode, Pass pass)
{
HashSet<string> activeFields = new HashSet<string>();
PBRMasterNode masterNode = iMasterNode as PBRMasterNode;
if (masterNode == null)
{
return activeFields;
}
if (masterNode.twoSided.isOn)
{
activeFields.Add("DoubleSided");
if (pass.ShaderPassName != "SHADERPASS_VELOCITY") // HACK to get around lack of a good interpolator dependency system
{ // we need to be able to build interpolators using multiple input structs
// also: should only require isFrontFace if Normals are required...
activeFields.Add("DoubleSided.Mirror"); // TODO: change this depending on what kind of normal flip you want..
activeFields.Add("FragInputs.isFrontFace"); // will need this for determining normal flip mode
}
}
switch (masterNode.model)
{
case PBRMasterNode.Model.Metallic:
break;
case PBRMasterNode.Model.Specular:
activeFields.Add("Material.SpecularColor");
break;
default:
// TODO: error!
break;
}
float constantAlpha = 0.0f;
if (masterNode.IsSlotConnected(PBRMasterNode.AlphaThresholdSlotId) ||
(float.TryParse(masterNode.GetSlotValue(PBRMasterNode.AlphaThresholdSlotId, GenerationMode.ForReals), out constantAlpha) && (constantAlpha > 0.0f)))
{
activeFields.Add("AlphaTest");
}
// Keywords for transparent
// #pragma shader_feature _SURFACE_TYPE_TRANSPARENT
if (masterNode.surfaceType != SurfaceType.Opaque)
{
// transparent-only defines
activeFields.Add("SurfaceType.Transparent");
// #pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
if (masterNode.alphaMode == AlphaMode.Alpha)
{
activeFields.Add("BlendMode.Alpha");
}
else if (masterNode.alphaMode == AlphaMode.Additive)
{
activeFields.Add("BlendMode.Add");
}
// else if (masterNode.alphaMode == PBRMasterNode.AlphaMode.PremultiplyAlpha) // TODO
// {
// defines.AddShaderChunk("#define _BLENDMODE_PRE_MULTIPLY 1", true);
// }
}
else
{
// opaque-only defines
}
// enable dithering LOD crossfade
// #pragma multi_compile _ LOD_FADE_CROSSFADE
// TODO: We should have this keyword only if VelocityInGBuffer is enable, how to do that ?
//#pragma multi_compile VELOCITYOUTPUT_OFF VELOCITYOUTPUT_ON
return activeFields;
}
private static bool GenerateShaderPassLit(AbstractMaterialNode masterNode, Pass pass, GenerationMode mode, SurfaceMaterialOptions materialOptions, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
{
// apply master node options to active fields
HashSet<string> activeFields = GetActiveFieldsFromMasterNode(masterNode, pass);
// use standard shader pass generation
return HDSubShaderUtilities.GenerateShaderPass(masterNode, pass, mode, materialOptions, activeFields, result, sourceAssetDependencyPaths);
}
public string GetSubshader(IMasterNode iMasterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
{
if (sourceAssetDependencyPaths != null)
{
// HDPBRSubShader.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("c4e8610eb7ce19747bb637c68acc55cd"));
// HDSubShaderUtilities.cs
sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("713ced4e6eef4a44799a4dd59041484b"));
}
var masterNode = iMasterNode as PBRMasterNode;
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);
subShader.AddShaderChunk("{", true);
subShader.Indent();
{
SurfaceMaterialOptions materialOptions = HDSubShaderUtilities.BuildMaterialOptions(masterNode.surfaceType, masterNode.alphaMode, masterNode.twoSided.isOn);
// Add tags at the SubShader level
{
var tagsVisitor = new ShaderStringBuilder();
materialOptions.GetTags(tagsVisitor);
subShader.AddShaderChunk(tagsVisitor.ToString(), false);
}
// generate the necessary shader passes
bool opaque = (masterNode.surfaceType == SurfaceType.Opaque);
bool transparent = (masterNode.surfaceType != SurfaceType.Opaque);
bool distortionActive = false;
bool transparentDepthPrepassActive = transparent && false;
bool transparentBackfaceActive = transparent && false;
bool transparentDepthPostpassActive = transparent && false;
if (opaque)
{
GenerateShaderPassLit(masterNode, m_PassGBuffer, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPassLit(masterNode, m_PassGBufferWithPrepass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
GenerateShaderPassLit(masterNode, m_PassMETA, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPassLit(masterNode, m_PassShadowCaster, mode, materialOptions, subShader, sourceAssetDependencyPaths);
if (opaque)
{
GenerateShaderPassLit(masterNode, m_PassDepthOnly, mode, materialOptions, subShader, sourceAssetDependencyPaths);
GenerateShaderPassLit(masterNode, m_PassMotionVectors, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
if (distortionActive)
{
GenerateShaderPassLit(masterNode, m_PassDistortion, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
if (transparentDepthPrepassActive)
{
GenerateShaderPassLit(masterNode, m_PassTransparentDepthPrepass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
if (transparentBackfaceActive)
{
GenerateShaderPassLit(masterNode, m_PassTransparentBackface, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
GenerateShaderPassLit(masterNode, m_PassForward, mode, materialOptions, subShader, sourceAssetDependencyPaths);
if (transparentDepthPostpassActive)
{
GenerateShaderPassLit(masterNode, m_PassTransparentDepthPostpass, mode, materialOptions, subShader, sourceAssetDependencyPaths);
}
}
subShader.Deindent();
subShader.AddShaderChunk("}", true);
return subShader.GetShaderString(0);
}
public bool IsPipelineCompatible(RenderPipelineAsset renderPipelineAsset)
{
return renderPipelineAsset is HDRenderPipelineAsset;
}
}
}