浏览代码

Added wip pass for SSReflection

/main
Frédéric Vauchelles 7 年前
当前提交
659b634a
共有 23 个文件被更改,包括 461 次插入151 次删除
  1. 50
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  2. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  3. 32
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceRefractionEditor.cs
  4. 20
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/LitUI.cs
  5. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  6. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  7. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  8. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.hlsl
  9. 26
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceRefraction.cs
  10. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl
  11. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs
  12. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs.hlsl
  13. 252
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  14. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.shader
  15. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LitTessellation.shader
  16. 14
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDScreenSpaceReflectionEditor.cs
  17. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDScreenSpaceReflectionEditor.cs.meta
  18. 40
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs
  19. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs.meta
  20. 49
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs
  21. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs.meta
  22. 38
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs
  23. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs.meta

50
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs


{
// Used to debug SSRay model
// 1x32 bits
public Lit.RefractionSSRayModel tracingModel;
public Lit.SSRayModel tracingModel;
// 6x32 bits
public uint loopStartPositionSSX; // Proxy, HiZ

public static GUIContent[] debugScreenSpaceTracingHiZStrings = null;
public static int[] debugScreenSpaceTracingHiZValues = null;
Lit.RefractionSSRayModel m_LastSSRayModel = Lit.RefractionSSRayModel.None;
Lit.SSRayModel m_LastSSRayModel = Lit.SSRayModel.None;
ScreenSpaceTracingDebug m_ScreenSpaceTracingDebugData;
public ScreenSpaceTracingDebug screenSpaceTracingDebugData
{

if (m_LastSSRayModel != m_ScreenSpaceTracingDebugData.tracingModel)
{
m_LastSSRayModel = m_ScreenSpaceTracingDebugData.tracingModel;
RefreshScreenSpaceTracingDebug<Lit.RefractionSSRayModel>(null, m_LastSSRayModel);
RefreshScreenSpaceTracingDebug<Lit.SSRayModel>(null, m_LastSSRayModel);
if (m_ScreenSpaceTracingDebugData.tracingModel != Lit.RefractionSSRayModel.HiZ)
if (m_ScreenSpaceTracingDebugData.tracingModel != Lit.SSRayModel.HiZ)
{
showSSRayDepthPyramid = false;
showSSRayGrid = false;

}
}
bool IsScreenSpaceTracingReflectionDebugEnabled()
{
return fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracing
&& lightingDebugSettings.debugLightingMode == DebugLightingMode.ScreenSpaceTracingReflection;
}
void SetScreenSpaceTracingReflectionDebugEnabled(bool value)
{
if (value)
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.ScreenSpaceTracingReflection;
fullScreenDebugMode = FullScreenDebugMode.ScreenSpaceTracing;
}
else
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.None;
fullScreenDebugMode = FullScreenDebugMode.None;
}
}
void SetScreenSpaceTracingRefractionDebugMode(int value)
{
var val = (DebugScreenSpaceTracing)value;

{
var list = new List<DebugUI.Container>();
var refractionContainer = new DebugUI.Container
var settingsContainer = new DebugUI.Container
displayName = "Refraction",
displayName = "Refraction / Reflection",
new DebugUI.BoolField { displayName = "Debug Enabled", getter = IsScreenSpaceTracingRefractionDebugEnabled, setter = SetScreenSpaceTracingRefractionDebugEnabled, onValueChanged = RefreshScreenSpaceTracingDebug },
new DebugUI.BoolField { displayName = "Debug Refraction Enabled", getter = IsScreenSpaceTracingRefractionDebugEnabled, setter = SetScreenSpaceTracingRefractionDebugEnabled, onValueChanged = RefreshScreenSpaceTracingDebug },
new DebugUI.BoolField { displayName = "Debug Reflection Enabled", getter = IsScreenSpaceTracingReflectionDebugEnabled, setter = SetScreenSpaceTracingReflectionDebugEnabled, onValueChanged = RefreshScreenSpaceTracingDebug },
list.Add(refractionContainer);
list.Add(settingsContainer);
if (IsScreenSpaceTracingRefractionDebugEnabled())
if (IsScreenSpaceTracingRefractionDebugEnabled()
|| IsScreenSpaceTracingReflectionDebugEnabled())
{
var debugSettingsContainer = new DebugUI.Container
{

new DebugUI.Value { displayName = "SSRay Model", getter = () => screenSpaceTracingDebugData.tracingModel }
}
};
refractionContainer.children.Add(debugSettingsContainer);
settingsContainer.children.Add(debugSettingsContainer);
case Lit.RefractionSSRayModel.Proxy:
case Lit.SSRayModel.Proxy:
refractionContainer.children.Add(
settingsContainer.children.Add(
new DebugUI.Container
{
displayName = "Debug Values",

);
break;
}
case Lit.RefractionSSRayModel.HiZ:
case Lit.SSRayModel.HiZ:
{
debugSettingsContainer.children.Insert(1, new DebugUI.Value { displayName = string.Empty, getter = () => "Press PageUp/PageDown to Increase/Decrease the HiZ step." });
debugSettingsContainer.children.Add(

);
refractionContainer.children.Add(
settingsContainer.children.Add(
new DebugUI.Container
{
displayName = "Debug Values (loop)",

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader


float cellSDF = 0;
float debugLinearDepth = 0;
if (debug.tracingModel == REFRACTIONSSRAYMODEL_HI_Z)
if (debug.tracingModel == SSRAYMODEL_HI_Z)
{
const uint2 iterationCellSize = uint2(debug.iterationCellSizeW, debug.iterationCellSizeH);
const float hasData = iterationCellSize.x != 0 || iterationCellSize.y != 0;

32
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceRefractionEditor.cs


{
[CanEditMultipleObjects]
[VolumeComponentEditor(typeof(ScreenSpaceRefraction))]
public class ScreenSpaceRefractionEditor : VolumeComponentEditor
public class ScreenSpaceRefractionEditor : ScreenSpaceLightingEditor
SerializedDataParameter m_RayMinLevel;
SerializedDataParameter m_RayMaxLevel;
SerializedDataParameter m_RayMaxIterations;
SerializedDataParameter m_RayDepthSuccessBias;
SerializedDataParameter m_ScreenWeightDistance;
public override void OnEnable()
{
var o = new PropertyFetcher<ScreenSpaceRefraction>(serializedObject);
m_RayMinLevel = Unpack(o.Find(x => x.rayMinLevel));
m_RayMaxLevel = Unpack(o.Find(x => x.rayMaxLevel));
m_RayMaxIterations = Unpack(o.Find(x => x.rayMaxIterations));
m_RayDepthSuccessBias = Unpack(o.Find(x => x.rayDepthSuccessBias));
m_ScreenWeightDistance = Unpack(o.Find(x => x.screenWeightDistance));
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField(CoreEditorUtils.GetContent("HiZ Settings"));
PropertyField(m_RayMinLevel, CoreEditorUtils.GetContent("Ray Min Level"));
PropertyField(m_RayMaxLevel, CoreEditorUtils.GetContent("Ray Max Level"));
PropertyField(m_RayMaxIterations, CoreEditorUtils.GetContent("Ray Max Iterations"));
PropertyField(m_RayDepthSuccessBias, CoreEditorUtils.GetContent("Ray Depth Success Bias"));
EditorGUILayout.Separator();
EditorGUILayout.LabelField(CoreEditorUtils.GetContent("Common Settings"));
PropertyField(m_ScreenWeightDistance, CoreEditorUtils.GetContent("Screen Weight Distance"));
}
}
}

20
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/LitUI.cs


public static GUIContent UVBaseMappingText = new GUIContent("Base UV mapping", "");
public static GUIContent texWorldScaleText = new GUIContent("World scale", "Tiling factor applied to Planar/Trilinear mapping");
// SSReflection
public static GUIContent reflectionSSRayModelText = new GUIContent("SSRay Model", "Screen Space Ray Model");
// Details
public static string detailText = "Detail Inputs";

protected const string kRefractionModel = "_RefractionModel";
protected MaterialProperty refractionSSRayModel = null;
protected const string kRefractionSSRayModel = "_RefractionSSRayModel";
protected MaterialProperty reflectionSSRayModel = null;
protected const string kReflectionSSRayModel = "_ReflectionSSRayModel";
protected override bool showBlendModePopup
{

// Transparency
refractionModel = FindProperty(kRefractionModel, props, false);
refractionSSRayModel = FindProperty(kRefractionSSRayModel, props, false);
reflectionSSRayModel = FindProperty(kReflectionSSRayModel, props, false);
transmittanceColor = FindProperty(kTransmittanceColor, props, false);
transmittanceColorMap = FindProperty(kTransmittanceColorMap, props, false);
atDistance = FindProperty(kATDistance, props, false);

--EditorGUI.indentLevel;
}
else if (surfaceTypeValue == SurfaceType.Opaque)
{
m_MaterialEditor.ShaderProperty(reflectionSSRayModel, Styles.reflectionSSRayModelText);
}
}
protected void DoEmissiveGUI(Material material)

CoreUtils.SetKeyword(material, "_MATERIAL_FEATURE_SPECULAR_COLOR", materialId == BaseLitGUI.MaterialId.LitSpecular);
var refractionModelValue = (Lit.RefractionModel)material.GetFloat(kRefractionModel);
var refractionSSRayModelValue = (Lit.RefractionSSRayModel)material.GetFloat(kRefractionSSRayModel);
var refractionSSRayModelValue = (Lit.SSRayModel)material.GetFloat(kRefractionSSRayModel);
CoreUtils.SetKeyword(material, "_REFRACTION_SSRAY_PROXY", (refractionSSRayModelValue == Lit.RefractionSSRayModel.Proxy) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_SSRAY_HIZ", (refractionSSRayModelValue == Lit.RefractionSSRayModel.HiZ) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_SSRAY_PROXY", (refractionSSRayModelValue == Lit.SSRayModel.Proxy) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_SSRAY_HIZ", (refractionSSRayModelValue == Lit.SSRayModel.HiZ) && canHaveRefraction);
// SS Reflection
var reflectionSSRayModelValue = (Lit.SSRayModel)material.GetFloat(kReflectionSSRayModel);
CoreUtils.SetKeyword(material, "_REFLECTION_SSRAY_PROXY", (reflectionSSRayModelValue == Lit.SSRayModel.Proxy));
CoreUtils.SetKeyword(material, "_REFLECTION_SSRAY_HIZ", (reflectionSSRayModelValue == Lit.SSRayModel.HiZ));
}
}
} // namespace UnityEditor

7
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


m_VolumetricLightingSystem.PushGlobalParams(hdCamera, cmd);
var ssrefraction = VolumeManager.instance.stack.GetComponent<ScreenSpaceRefraction>()
var ssRefraction = VolumeManager.instance.stack.GetComponent<ScreenSpaceRefraction>()
ssrefraction.PushShaderParameters(cmd);
ssRefraction.PushShaderParameters(cmd);
var ssReflection = VolumeManager.instance.stack.GetComponent<ScreenSpaceReflection>()
?? ScreenSpaceReflection.@default;
ssReflection.PushShaderParameters(cmd);
}
}

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _SSRefractionRayDepthSuccessBias = Shader.PropertyToID("_SSRefractionRayDepthSuccessBias");
public static readonly int _SSRefractionInvScreenWeightDistance = Shader.PropertyToID("_SSRefractionInvScreenWeightDistance");
public static readonly int _SSReflectionRayMinLevel = Shader.PropertyToID("_SSReflectionRayMinLevel");
public static readonly int _SSReflectionRayMaxLevel = Shader.PropertyToID("_SSReflectionRayMaxLevel");
public static readonly int _SSReflectionRayMaxIterations = Shader.PropertyToID("_SSReflectionRayMaxIterations");
public static readonly int _SSReflectionRayDepthSuccessBias = Shader.PropertyToID("_SSReflectionRayDepthSuccessBias");
public static readonly int _SSReflectionInvScreenWeightDistance = Shader.PropertyToID("_SSReflectionInvScreenWeightDistance");
public static readonly int _VelocityTexture = Shader.PropertyToID("_VelocityTexture");
public static readonly int _ShadowMaskTexture = Shader.PropertyToID("_ShadowMaskTexture");
public static readonly int _DistortionTexture = Shader.PropertyToID("_DistortionTexture");

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


// Following define the maximum number of bits use in each feature category.
public static uint s_LightFeatureMaskFlags = 0xFFF000;
public static uint s_LightFeatureMaskFlagsSSReflection = (uint)LightFeatureFlags.SSReflection;
public static uint s_LightFeatureMaskFlagsOpaque = 0xFFF000 & ~((uint)LightFeatureFlags.SSRefraction); // Opaque don't support screen space refraction
public static uint s_LightFeatureMaskFlagsOpaque = 0xFFF000 & ~((uint)LightFeatureFlags.SSRefraction | (uint)LightFeatureFlags.SSReflection); // Opaque don't support screen space refraction / reflection
public static uint s_LightFeatureMaskFlagsTransparent = 0xFFF000 & ~((uint)LightFeatureFlags.SSReflection); // Transparent don't support screen space reflection
public static uint s_MaterialFeatureMaskFlags = 0x000FFF; // don't use all bits just to be safe from signed and/or float conversions :/
}

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.hlsl


envLightData = InitSkyEnvLightData(0);
}
if (featureFlags & LIGHTFEATUREFLAGS_SSREFRACTION)
if (featureFlags & LIGHTFEATUREFLAGS_SSREFLECTION)
GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION, refractionHierarchyWeight);
GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION, reflectionHierarchyWeight);
if (featureFlags & LIGHTFEATUREFLAGS_SSREFLECTION)
if (featureFlags & LIGHTFEATUREFLAGS_SSREFRACTION)
GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION, reflectionHierarchyWeight);
GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION, refractionHierarchyWeight);
AccumulateIndirectLighting(lighting, aggregateLighting);
}

26
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceRefraction.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[Serializable]
public class ScreenSpaceRefraction : VolumeComponent
public class ScreenSpaceRefraction : ScreenSpaceLighting
{
static ScreenSpaceRefraction s_Default = null;
public static ScreenSpaceRefraction @default

}
}
public IntParameter rayMinLevel = new IntParameter(2);
public IntParameter rayMaxLevel = new IntParameter(6);
public IntParameter rayMaxIterations = new IntParameter(32);
public FloatParameter rayDepthSuccessBias = new FloatParameter(0.1f);
public ClampedFloatParameter screenWeightDistance = new ClampedFloatParameter(0.1f, 0, 1);
public void PushShaderParameters(CommandBuffer cmd)
protected override void FetchIDs(
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,
out int rayDepthSuccessBiasID,
out int invScreenWeightDistanceID
)
cmd.SetGlobalInt(HDShaderIDs._SSRefractionRayMinLevel, rayMinLevel.value);
cmd.SetGlobalInt(HDShaderIDs._SSRefractionRayMaxLevel, rayMaxLevel.value);
cmd.SetGlobalInt(HDShaderIDs._SSRefractionRayMaxIterations, rayMaxIterations.value);
cmd.SetGlobalFloat(HDShaderIDs._SSRefractionRayDepthSuccessBias, rayDepthSuccessBias.value);
cmd.SetGlobalFloat(HDShaderIDs._SSRefractionInvScreenWeightDistance, 1f / screenWeightDistance.value);
rayMinLevelID = HDShaderIDs._SSRefractionRayMinLevel;
rayMaxLevelID = HDShaderIDs._SSRefractionRayMaxLevel;
rayMaxIterationsID = HDShaderIDs._SSRefractionRayMaxIterations;
rayDepthSuccessBiasID = HDShaderIDs._SSRefractionRayDepthSuccessBias;
invScreenWeightDistanceID = HDShaderIDs._SSRefractionInvScreenWeightDistance;
}
}
}

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl


float2 rayOriginNDC = ComputeNormalizedDeviceCoordinates(input.rayOriginWS, GetWorldToHClipMatrix());
uint2 rayOriginSS = uint2(rayOriginNDC * _ScreenSize.xy);
debug.tracingModel = REFRACTIONSSRAYMODEL_PROXY;
debug.tracingModel = SSRAYMODEL_PROXY;
debug.loopStartPositionSSX = rayOriginSS.x;
debug.loopStartPositionSSY = rayOriginSS.y;
debug.loopStartLinearDepth = rayOriginCS.w;

ScreenSpaceTracingDebug debug;
ZERO_INITIALIZE(ScreenSpaceTracingDebug, debug);
debug.tracingModel = REFRACTIONSSRAYMODEL_HI_Z;
debug.tracingModel = SSRAYMODEL_HI_Z;
debug.loopStartPositionSSX = uint(startPositionSS.x);
debug.loopStartPositionSSY = uint(startPositionSS.y);
debug.loopStartLinearDepth = 1 / startPositionSS.z;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs


};
[GenerateHLSL]
public enum RefractionSSRayModel
public enum SSRayModel
{
None = 0,
Proxy = 1,

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs.hlsl


#define MATERIALFEATUREFLAGS_LIT_CLEAR_COAT (64)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Lit+RefractionSSRayModel: static fields
// UnityEngine.Experimental.Rendering.HDPipeline.Lit+SSRayModel: static fields
#define REFRACTIONSSRAYMODEL_NONE (0)
#define REFRACTIONSSRAYMODEL_PROXY (1)
#define REFRACTIONSSRAYMODEL_HI_Z (2)
#define SSRAYMODEL_NONE (0)
#define SSRAYMODEL_PROXY (1)
#define SSRAYMODEL_HI_Z (2)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Lit+HiZIntersectionKind: static fields

252
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


// Screen space lighting
float _SSRefractionInvScreenWeightDistance; // Distance for screen space smoothstep with fallback
float _SSReflectionInvScreenWeightDistance; // Distance for screen space smoothstep with fallback
// Ambiant occlusion
float4 _AmbientOcclusionParam; // xyz occlusion color, w directLightStrenght

#define GBufferType3 float4
#define HAS_REFRACTION (defined(_REFRACTION_PLANE) || defined(_REFRACTION_SPHERE)) && (defined(_REFRACTION_SSRAY_PROXY) || defined(_REFRACTION_SSRAY_HIZ))
#define HAS_SSREFLECTION (defined(_REFLECTION_SSRAY_PROXY) || defined(_REFLECTION_SSRAY_HIZ))
#define DEFAULT_SPECULAR_VALUE 0.04

// Helper functions/variable specific to this material
//-----------------------------------------------------------------------------
#if HAS_SSREFLECTION || HAS_REFRACTION
# include "HDRP/Lighting/Reflection/VolumeProjection.hlsl"
# include "HDRP/Lighting/LightDefinition.cs.hlsl"
#endif
# include "HDRP/Lighting/Reflection/VolumeProjection.hlsl"
# include "HDRP/Lighting/LightDefinition.cs.hlsl"
# define SSRTID Refraction
# include "HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl"
# undef SSRTID

# elif defined(_REFRACTION_SSRAY_HIZ)
# define REFRACTION_SSRAY_IN ScreenSpaceHiZRaymarchInput
# define REFRACTION_SSRAY_QUERY(input, hit) ScreenSpaceHiZRaymarchRefraction(input, hit)
# endif
#endif
#if HAS_SSREFLECTION
# define SSRTID Reflection
# include "HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl"
# undef SSRTID
# if defined(_REFLECTION_SSRAY_PROXY)
# define REFLECTION_SSRAY_IN ScreenSpaceProxyRaycastInput
# define REFLECTION_SSRAY_QUERY(input, hit) ScreenSpaceProxyRaycastReflection(input, hit)
# elif defined(_REFLECTION_SSRAY_HIZ)
# define REFLECTION_SSRAY_IN ScreenSpaceHiZRaymarchInput
# define REFLECTION_SSRAY_QUERY(input, hit) ScreenSpaceHiZRaymarchReflection(input, hit)
# endif
#endif

IndirectLighting lighting;
ZERO_INITIALIZE(IndirectLighting, lighting);
switch (GPUImageBasedLightingType)
// -------------------------------
// Early out
// -------------------------------
#if !HAS_REFRACTION
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
case GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION:
{
// No refraction, no need to go further
hierarchyWeight = 1.0;
return lighting;
}
#endif
#if !HAS_SSREFLECTION
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION)
{
return lighting;
}
#endif
// If we are here, either SSRefraction or SSReflection is active, but not both.
// Opaque pass => none, Transparent pass => refraction only, SSReflection pass => SSReflection only
// -------------------------------
// Setup macro helpers
// -------------------------------
# define SSRAY_INPUT REFRACTION_SSRAY_IN
# define SSRAY_QUERY REFRACTION_SSRAY_QUERY
# if _REFRACTION_SSRAY_HIZ
# define SSRAY_MODEL SSRAYMODEL_HI_Z
# elif _REFRACTION_SSRAY_PROXY
# define SSRAY_MODEL SSRAYMODEL_PROXY
# endif
// Refraction process:
// 1. Depending on the shape model, we calculate the refracted point in world space and the optical depth
// 2. We calculate the screen space position of the refracted point
// 3. If this point is available (ie: in color buffer and point is not in front of the object)
// a. Get the corresponding color depending on the roughness from the gaussian pyramid of the color buffer
// b. Multiply by the transmittance for absorption (depends on the optical depth)
#elif HAS_SSREFLECTION
# define SSRAY_INPUT REFLECTION_SSRAY_IN
# define SSRAY_QUERY REFLECTION_SSRAY_QUERY
# if _REFLECTION_SSRAY_HIZ
# define SSRAY_MODEL SSRAYMODEL_HI_Z
# elif _REFLECTION_SSRAY_PROXY
# define SSRAY_MODEL SSRAYMODEL_PROXY
# endif
#endif
float3 rayOriginWS = preLightData.transparentPositionWS;
float3 rayDirWS = preLightData.transparentRefractV;
#if defined(SSRAY_INPUT) && defined(SSRAY_QUERY) && defined(SSRAY_MODEL)
// -------------------------------
// Initialize screen space tracing
// -------------------------------
float3 rayOriginWS = float3(0, 0, 0);
float3 rayDirWS = float3(0, 0, 0);
float mipLevel = 0;
# if DEBUG_DISPLAY
int debugMode = 0;
# endif
float invScreenWeightDistance = 0;
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
{
// Refraction process:
// 1. Depending on the shape model, we calculate the refracted point in world space and the optical depth
// 2. We calculate the screen space position of the refracted point
// 3. If this point is available (ie: in color buffer and point is not in front of the object)
// a. Get the corresponding color depending on the roughness from the gaussian pyramid of the color buffer
// b. Multiply by the transmittance for absorption (depends on the optical depth)
rayOriginWS = preLightData.transparentPositionWS;
rayDirWS = preLightData.transparentRefractV;
mipLevel = preLightData.transparentSSMipLevel;
invScreenWeightDistance = _SSRefractionInvScreenWeightDistance;
int debugMode = DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION;
bool debug = _DebugLightingMode == debugMode
debugMode = DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION;
#endif
}
else if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION)
{
rayOriginWS = posInput.positionWS;
rayDirWS = preLightData.iblR;
mipLevel = preLightData.transparentSSMipLevel;
invScreenWeightDistance = _SSReflectionInvScreenWeightDistance;
#if DEBUG_DISPLAY
debugMode = DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFLECTION;
#endif
}
#if DEBUG_DISPLAY
bool debug = _DebugLightingMode == debugMode
// Initialize screen space tracing
REFRACTION_SSRAY_IN ssRayInput;
ZERO_INITIALIZE(REFRACTION_SSRAY_IN, ssRayInput);
// -------------------------------
// Screen space tracing query
// -------------------------------
// Common initialization
ssRayInput.rayOriginWS = rayOriginWS;
ssRayInput.rayDirWS = rayDirWS;
// Initialize screen space tracing
SSRAY_INPUT ssRayInput;
ZERO_INITIALIZE(SSRAY_INPUT, ssRayInput);
// Common initialization
ssRayInput.rayOriginWS = rayOriginWS;
ssRayInput.rayDirWS = rayDirWS;
ssRayInput.debug = debug;
ssRayInput.debug = debug;
// Algorithm specific initialization
#ifdef _REFRACTION_SSRAY_HIZ
ssRayInput.maxIterations = uint(-1);
#elif _REFRACTION_SSRAY_PROXY
ssRayInput.proxyData = envLightData;
// Algorithm specific initialization
#if SSRAY_MODEL == SSRAYMODEL_HI_Z
ssRayInput.maxIterations = uint(-1);
#elif SSRAY_MODEL == SSRAYMODEL_PROXY
ssRayInput.proxyData = envLightData;
// Perform ray query
ScreenSpaceRayHit hit;
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);
bool hitSuccessful = REFRACTION_SSRAY_QUERY(ssRayInput, hit);
// Perform ray query
ScreenSpaceRayHit hit;
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);
bool hitSuccessful = SSRAY_QUERY(ssRayInput, hit);
// Debug screen space tracing
// Debug screen space tracing
if (_DebugLightingMode == debugMode
&& _DebugLightingSubMode != DEBUGSCREENSPACETRACING_COLOR)
{
float weight = 1.0;
UpdateLightingHierarchyWeights(hierarchyWeight, weight);
lighting.specularTransmitted = hit.debugOutput;
return lighting;
}
if (_DebugLightingMode == debugMode
&& _DebugLightingSubMode != DEBUGSCREENSPACETRACING_COLOR)
{
float weight = 1.0;
UpdateLightingHierarchyWeights(hierarchyWeight, weight);
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
lighting.specularTransmitted = hit.debugOutput;
else if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION)
lighting.specularReflected = hit.debugOutput;
return lighting;
}
if (!hitSuccessful)
return lighting;
if (!hitSuccessful)
return lighting;
float2 weightNDC = clamp(min(hit.positionNDC, 1 - hit.positionNDC) * _SSRefractionInvScreenWeightDistance, 0, 1);
weightNDC = weightNDC * weightNDC * (3 - 2 * weightNDC);
float weight = weightNDC.x * weightNDC.y;
float2 weightNDC = clamp(min(hit.positionNDC, 1 - hit.positionNDC) * invScreenWeightDistance, 0, 1);
weightNDC = weightNDC * weightNDC * (3 - 2 * weightNDC);
float weight = weightNDC.x * weightNDC.y;
float hitDeviceDepth = LOAD_TEXTURE2D_LOD(_DepthPyramidTexture, hit.positionSS, 0).r;
float hitLinearDepth = LinearEyeDepth(hitDeviceDepth, _ZBufferParams);
float hitDeviceDepth = LOAD_TEXTURE2D_LOD(_DepthPyramidTexture, hit.positionSS, 0).r;
float hitLinearDepth = LinearEyeDepth(hitDeviceDepth, _ZBufferParams);
// Exit if texel is out of color buffer
// Or if the texel is from an object in front of the object
if (hitLinearDepth < posInput.linearDepth
|| weight == 0)
{
// Do nothing and don't update the hierarchy weight so we can fall back on refraction probe
return lighting;
}
// Exit if texel is out of color buffer
// Or if the texel is from an object in front of the object
if (hitLinearDepth < posInput.linearDepth
|| weight == 0)
{
// Do nothing and don't update the hierarchy weight so we can fall back on refraction probe
return lighting;
}
UpdateLightingHierarchyWeights(hierarchyWeight, weight); // Shouldn't be needed, but safer in case we decide to change hierarchy priority
UpdateLightingHierarchyWeights(hierarchyWeight, weight); // Shouldn't be needed, but safer in case we decide to change hierarchy priority
float3 preLD = SAMPLE_TEXTURE2D_LOD(
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
hit.positionNDC * _ColorPyramidScale.xy,
mipLevel
).rgb;
float3 preLD = SAMPLE_TEXTURE2D_LOD(
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
hit.positionNDC * _ColorPyramidScale.xy,
preLightData.transparentSSMipLevel
).rgb;
// We use specularFGD as an approximation of the fresnel effect (that also handle smoothness)
float3 F = preLightData.specularFGD;
// We use specularFGD as an approximation of the fresnel effect (that also handle smoothness), so take the remaining for transmission
float3 F = preLightData.specularFGD;
lighting.specularTransmitted = (1.0 - F) * preLD.rgb * preLightData.transparentTransmittance * weight;
#else
// No refraction, no need to go further
hierarchyWeight = 1.0;
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
lighting.specularTransmitted = (1.0 - F) * preLD.rgb * preLightData.transparentTransmittance * weight;
else if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION)
lighting.specularReflected = F * preLD.rgb * weight;
break;
}
case GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION:
{
break;
}
}
// -------------------------------
// Cleanup macros
// -------------------------------
#undef SSRAY_INPUT
#undef SSRAY_QUERY
#undef SSRAY_MODEL
return lighting;
return lighting;
}
//-----------------------------------------------------------------------------

// Physically speaking, it should be transmittanceMask should be 1, but for artistic reasons, we let the value vary
#if HAS_REFRACTION
diffuseLighting = lerp(diffuseLighting, lighting.indirect.specularTransmitted, bsdfData.transmittanceMask);
#elif SSREFLECTION_ONLY
diffuseLighting = LOAD_TEXTURE2D_LOD(
_ColorPyramidTexture,
posInput.positionSS,
0
).rgb;
#endif
specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.shader


_ATDistance("Transmittance Absorption Distance", Float) = 1.0
[ToggleUI] _PreRefractionPass("PreRefractionPass", Float) = 0.0
// SSReflection
[Enum(None, 0, Proxy, 1, HiZ, 2)]_ReflectionSSRayModel("Reflection SSRay Model", Int) = 0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting (fixed at compile time)
[HideInInspector] _StencilWriteMask("_StencilWriteMask", Int) = 7 // StencilMask.Lighting (fixed at compile time)

#pragma shader_feature _VERTEX_WIND
#pragma shader_feature _ _REFRACTION_PLANE _REFRACTION_SPHERE
#pragma shader_feature _ _REFRACTION_SSRAY_PROXY _REFRACTION_SSRAY_HIZ
#pragma shader_feature _ _REFLECTION_SSRAY_PROXY _REFLECTION_SSRAY_HIZ
#pragma shader_feature _ _EMISSIVE_MAPPING_PLANAR _EMISSIVE_MAPPING_TRIPLANAR
#pragma shader_feature _ _MAPPING_PLANAR _MAPPING_TRIPLANAR

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LitTessellation.shader


_ATDistance("Transmittance Absorption Distance", Float) = 1.0
[ToggleUI] _PreRefractionPass("PreRefractionPass", Float) = 0.0
// SSReflection
[Enum(None, 0, Proxy, 1, HiZ, 2)]_ReflectionSSRayModel("Reflection SSRay Model", Int) = 0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting (fixed at compile time)
[HideInInspector] _StencilWriteMask("_StencilWriteMask", Int) = 7 // StencilMask.Lighting (fixed at compile time)

#pragma shader_feature _ _TESSELLATION_PHONG
#pragma shader_feature _ _REFRACTION_PLANE _REFRACTION_SPHERE
#pragma shader_feature _ _REFRACTION_SSRAY_PROXY _REFRACTION_SSRAY_HIZ
#pragma shader_feature _ _REFLECTION_SSRAY_PROXY _REFLECTION_SSRAY_HIZ
#pragma shader_feature _ _EMISSIVE_MAPPING_PLANAR _EMISSIVE_MAPPING_TRIPLANAR
#pragma shader_feature _ _MAPPING_PLANAR _MAPPING_TRIPLANAR

14
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDScreenSpaceReflectionEditor.cs


using System.Collections;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[CanEditMultipleObjects]
[VolumeComponentEditor(typeof(ScreenSpaceReflection))]
public class HDScreenSpaceReflectionEditor : ScreenSpaceLightingEditor
{
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDScreenSpaceReflectionEditor.cs.meta


fileFormatVersion: 2
guid: e1a6d5f7801c9c749a3f7d3bf4294e87
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

40
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs


using System.Collections;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public class ScreenSpaceLightingEditor : VolumeComponentEditor
{
SerializedDataParameter m_RayMinLevel;
SerializedDataParameter m_RayMaxLevel;
SerializedDataParameter m_RayMaxIterations;
SerializedDataParameter m_RayDepthSuccessBias;
SerializedDataParameter m_ScreenWeightDistance;
public override void OnEnable()
{
var o = new PropertyFetcher<ScreenSpaceRefraction>(serializedObject);
m_RayMinLevel = Unpack(o.Find(x => x.rayMinLevel));
m_RayMaxLevel = Unpack(o.Find(x => x.rayMaxLevel));
m_RayMaxIterations = Unpack(o.Find(x => x.rayMaxIterations));
m_RayDepthSuccessBias = Unpack(o.Find(x => x.rayDepthSuccessBias));
m_ScreenWeightDistance = Unpack(o.Find(x => x.screenWeightDistance));
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField(CoreEditorUtils.GetContent("HiZ Settings"));
PropertyField(m_RayMinLevel, CoreEditorUtils.GetContent("Ray Min Level"));
PropertyField(m_RayMaxLevel, CoreEditorUtils.GetContent("Ray Max Level"));
PropertyField(m_RayMaxIterations, CoreEditorUtils.GetContent("Ray Max Iterations"));
PropertyField(m_RayDepthSuccessBias, CoreEditorUtils.GetContent("Ray Depth Success Bias"));
EditorGUILayout.Separator();
EditorGUILayout.LabelField(CoreEditorUtils.GetContent("Common Settings"));
PropertyField(m_ScreenWeightDistance, CoreEditorUtils.GetContent("Screen Weight Distance"));
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs.meta


fileFormatVersion: 2
guid: 5a0cc6d88bd0be54283043dc53877a64
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

49
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs


using System;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[Serializable]
public abstract class ScreenSpaceLighting : VolumeComponent
{
int m_RayMinLevelID;
int m_RayMaxLevelID;
int m_RayMaxIterationsID;
int m_RayDepthSuccessBiasID;
int m_InvScreenWeightDistanceID;
public IntParameter rayMinLevel = new IntParameter(2);
public IntParameter rayMaxLevel = new IntParameter(6);
public IntParameter rayMaxIterations = new IntParameter(32);
public FloatParameter rayDepthSuccessBias = new FloatParameter(0.1f);
public ClampedFloatParameter screenWeightDistance = new ClampedFloatParameter(0.1f, 0, 1);
public virtual void PushShaderParameters(CommandBuffer cmd)
{
cmd.SetGlobalInt(m_RayMinLevelID, rayMinLevel.value);
cmd.SetGlobalInt(m_RayMaxLevelID, rayMaxLevel.value);
cmd.SetGlobalInt(m_RayMaxIterationsID, rayMaxIterations.value);
cmd.SetGlobalFloat(m_RayDepthSuccessBiasID, rayDepthSuccessBias.value);
cmd.SetGlobalFloat(m_InvScreenWeightDistanceID, 1f / screenWeightDistance.value);
}
protected abstract void FetchIDs(
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,
out int rayDepthSuccessBiasID,
out int invScreenWeightDistanceID
);
void Awake()
{
FetchIDs(
out m_RayMinLevelID,
out m_RayMaxLevelID,
out m_RayMaxIterationsID,
out m_RayDepthSuccessBiasID,
out m_InvScreenWeightDistanceID
);
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs.meta


fileFormatVersion: 2
guid: abafd8c0d3ca56d44b9469b2b67fc7cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

38
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs


using System;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[Serializable]
public class ScreenSpaceReflection : ScreenSpaceLighting
{
static ScreenSpaceReflection s_Default = null;
public static ScreenSpaceReflection @default
{
get
{
if (s_Default == null)
{
s_Default = ScriptableObject.CreateInstance<ScreenSpaceReflection>();
s_Default.hideFlags = HideFlags.HideAndDontSave;
}
return s_Default;
}
}
protected override void FetchIDs(
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,
out int rayDepthSuccessBiasID,
out int screenWeightDistanceID
)
{
rayMinLevelID = HDShaderIDs._SSReflectionRayMinLevel;
rayMaxLevelID = HDShaderIDs._SSReflectionRayMaxLevel;
rayMaxIterationsID = HDShaderIDs._SSReflectionRayMaxIterations;
rayDepthSuccessBiasID = HDShaderIDs._SSReflectionRayDepthSuccessBias;
screenWeightDistanceID = HDShaderIDs._SSReflectionInvScreenWeightDistance;
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs.meta


fileFormatVersion: 2
guid: 384c4d03a551c44448145f4093304119
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存