浏览代码

Added toggle to raymarch behind objects

several stability fixes
/main
Frédéric Vauchelles 7 年前
当前提交
d5c1a104
共有 9 个文件被更改,包括 35 次插入15 次删除
  1. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  2. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  4. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs
  5. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs
  6. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceRefraction.cs
  7. 15
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl
  8. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  9. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl

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


new DebugUI.Value { displayName = "Position SS", getter = () => new Vector2(screenSpaceTracingDebugData.iterationPositionSS.x, screenSpaceTracingDebugData.iterationPositionSS.y) },
new DebugUI.Value { displayName = "Depth", getter = () => 1f / screenSpaceTracingDebugData.iterationPositionSS.z },
new DebugUI.Value { displayName = "Depth Buffer Min/Min + Thickness/Max", getter = () => string.Format("{0}/{1}/{2}", screenSpaceTracingDebugData.iterationLinearDepthBufferMin, screenSpaceTracingDebugData.iterationLinearDepthBufferMinThickness, screenSpaceTracingDebugData.iterationLinearDepthBufferMax) },
new DebugUI.Value { displayName = "Intersection Thickness", getter = () => (screenSpaceTracingDebugData.iterationLinearDepthBufferMinThickness - 1f / screenSpaceTracingDebugData.iterationPositionSS.z).ToString("F6") },
new DebugUI.Value { displayName = "Depth Buffer Diff (Max - Min)", getter = () => (screenSpaceTracingDebugData.iterationLinearDepthBufferMax - screenSpaceTracingDebugData.iterationLinearDepthBufferMin).ToString("F6") },
new DebugUI.Value { displayName = "Intersect Depth Buffer", getter = () => screenSpaceTracingDebugData.intersectDepthBuffer },
new DebugUI.Value { displayName = "Mip Level", getter = () => screenSpaceTracingDebugData.iterationMipLevel },

new DebugUI.Value { displayName = "Iteration", getter = () => string.Format("{0}/{1}", screenSpaceTracingDebugData.iteration, screenSpaceTracingDebugData.loopIterationMax) },
new DebugUI.Value { displayName = "Position SS", getter = () => new Vector2(screenSpaceTracingDebugData.iterationPositionSS.x, screenSpaceTracingDebugData.iterationPositionSS.y) },
new DebugUI.Value { displayName = "Depth", getter = () => 1f / screenSpaceTracingDebugData.iterationPositionSS.z },
new DebugUI.Value { displayName = "Intersection Thickness", getter = () => (screenSpaceTracingDebugData.iterationLinearDepthBufferMinThickness - 1f / screenSpaceTracingDebugData.iterationPositionSS.z).ToString("F6") },
new DebugUI.Value { displayName = "Depth Buffer Min/Min + Thickness/Max", getter = () => string.Format("{0}/{1}/{2}", screenSpaceTracingDebugData.iterationLinearDepthBufferMin, screenSpaceTracingDebugData.iterationLinearDepthBufferMinThickness, screenSpaceTracingDebugData.iterationLinearDepthBufferMax) },
new DebugUI.Value { displayName = "Intersect Depth Buffer", getter = () => screenSpaceTracingDebugData.intersectDepthBuffer },
new DebugUI.Value { displayName = "Mip Level", getter = () => screenSpaceTracingDebugData.iterationMipLevel },

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


protected SerializedDataParameter m_ScreenWeightDistance;
protected SerializedDataParameter m_RayMaxScreenDistance;
protected SerializedDataParameter m_RayBlendScreenDistance;
protected SerializedDataParameter m_RayMarchBehindObjects;
public override void OnEnable()
{

m_ScreenWeightDistance = Unpack(o.Find(x => x.screenWeightDistance));
m_RayMaxScreenDistance = Unpack(o.Find(x => x.rayMaxScreenDistance));
m_RayBlendScreenDistance = Unpack(o.Find(x => x.rayBlendScreenDistance));
m_RayMarchBehindObjects = Unpack(o.Find(x => x.rayMarchBehindObjects));
}
public override void OnInspectorGUI()

PropertyField(m_DepthBufferThickness, CoreEditorUtils.GetContent("Depth Buffer Thickness"));
PropertyField(m_RayMaxScreenDistance, CoreEditorUtils.GetContent("Max Raymarched Distance (NDC)"));
PropertyField(m_RayBlendScreenDistance, CoreEditorUtils.GetContent("Blended Raymarched Distance (NDC)"));
PropertyField(m_RayMarchBehindObjects, CoreEditorUtils.GetContent("Ray March Behind Objects"));
m_RayMinLevel.value.intValue = Mathf.Clamp(m_RayMinLevel.value.intValue, 0, m_RayMaxLevel.value.intValue);
m_RayMaxLevel.value.intValue = Mathf.Max(0, m_RayMaxLevel.value.intValue);

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


Shader.PropertyToID("_SSSBufferTexture3"),
};
public static readonly int _SSRefractionRayMarchBehindObjects = Shader.PropertyToID("_SSRefractionRayMarchBehindObjects");
public static readonly int _SSRefractionRayMaxScreenDistance = Shader.PropertyToID("_SSRefractionRayMaxScreenDistance");
public static readonly int _SSRefractionRayBlendScreenDistance = Shader.PropertyToID("_SSRefractionRayBlendScreenDistance");
public static readonly int _SSRefractionRayLevel = Shader.PropertyToID("_SSRefractionRayLevel");

public static readonly int _SSRefractionDepthBufferThickness = Shader.PropertyToID("_SSRefractionDepthBufferThickness");
public static readonly int _SSRefractionInvScreenWeightDistance = Shader.PropertyToID("_SSRefractionInvScreenWeightDistance");
public static readonly int _SSReflectionRayMarchBehindObjects = Shader.PropertyToID("_SSReflectionRayMarchBehindObjects");
public static readonly int _SSReflectionRayMaxScreenDistance = Shader.PropertyToID("_SSReflectionRayMaxScreenDistance");
public static readonly int _SSReflectionRayBlendScreenDistance = Shader.PropertyToID("_SSReflectionRayBlendScreenDistance");
public static readonly int _SSReflectionRayLevel = Shader.PropertyToID("_SSReflectionRayLevel");

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


int m_InvScreenWeightDistanceID;
int m_RayMaxScreenDistanceID;
int m_RayBlendScreenDistanceID;
int m_RayMarchBehindObjectsID;
public IntParameter rayLevel = new IntParameter(2);
public IntParameter rayMinLevel = new IntParameter(2);

public ClampedFloatParameter screenWeightDistance = new ClampedFloatParameter(0.1f, 0, 1);
public ClampedFloatParameter rayMaxScreenDistance = new ClampedFloatParameter(0.3f, 0, 1);
public ClampedFloatParameter rayBlendScreenDistance = new ClampedFloatParameter(0.1f, 0, 1);
public BoolParameter rayMarchBehindObjects = new BoolParameter(true);
public virtual void PushShaderParameters(CommandBuffer cmd)
{

cmd.SetGlobalFloat(m_InvScreenWeightDistanceID, 1f / screenWeightDistance.value);
cmd.SetGlobalFloat(m_RayMaxScreenDistanceID, rayMaxScreenDistance.value);
cmd.SetGlobalFloat(m_RayBlendScreenDistanceID, rayBlendScreenDistance.value);
cmd.SetGlobalInt(m_RayMarchBehindObjectsID, rayMarchBehindObjects.value ? 1 : 0);
}
protected abstract void FetchIDs(

out int DepthBufferThicknessID,
out int invScreenWeightDistanceID,
out int rayMaxScreenDistanceID,
out int rayBlendScreenDistanceID
out int rayBlendScreenDistanceID,
out int rayMarchBehindObjectsID
);
void Awake()

out m_DepthBufferThicknessID,
out m_InvScreenWeightDistanceID,
out m_RayMaxScreenDistanceID,
out m_RayBlendScreenDistanceID
out m_RayBlendScreenDistanceID,
out m_RayMarchBehindObjectsID
);
}
}

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


out int depthBufferThicknessID,
out int screenWeightDistanceID,
out int rayMaxScreenDistanceID,
out int rayBlendScreenDistanceID
out int rayBlendScreenDistanceID,
out int rayMarchBehindObjectsID
)
{
rayLevelID = HDShaderIDs._SSReflectionRayLevel;

screenWeightDistanceID = HDShaderIDs._SSReflectionInvScreenWeightDistance;
rayMaxScreenDistanceID = HDShaderIDs._SSReflectionRayMaxScreenDistance;
rayBlendScreenDistanceID = HDShaderIDs._SSReflectionRayBlendScreenDistance;
rayMarchBehindObjectsID = HDShaderIDs._SSReflectionRayMarchBehindObjects;
}
public override void PushShaderParameters(CommandBuffer cmd)

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


out int depthBufferThicknessID,
out int invScreenWeightDistanceID,
out int rayMaxScreenDistanceID,
out int rayBlendScreenDistanceID
out int rayBlendScreenDistanceID,
out int rayMarchBehindObjectsID
)
{
rayLevelID = HDShaderIDs._SSRefractionRayLevel;

invScreenWeightDistanceID = HDShaderIDs._SSRefractionInvScreenWeightDistance;
rayMaxScreenDistanceID = HDShaderIDs._SSRefractionRayMaxScreenDistance;
rayBlendScreenDistanceID = HDShaderIDs._SSRefractionRayBlendScreenDistance;
rayMarchBehindObjectsID = HDShaderIDs._SSRefractionRayMarchBehindObjects;
}
}
}

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


uint settingsRayMinLevel, // Minimum mip level to use for ray marching the depth buffer in HiZ
uint settingsRayMaxLevel, // Maximum mip level to use for ray marching the depth buffer in HiZ
uint settingsRayMaxIterations, // Maximum number of iteration for the HiZ raymarching (= number of depth sample for HiZ)
float settingsDepthBufferThickness, // Bias to use when trying to detect whenever we raymarch behind a surface
float settingsDepthBufferThickness, // Bias to use when trying to detect whenever we raymarch behind a surface
bool settingsRayMarchBehindObjects, // Whether to raymarch behind objects
int settingsDebuggedAlgorithm, // currently debugged algorithm (see PROJECTIONMODEL defines)
// out
out ScreenSpaceRayHit hit,

positionSS = positionSS + raySS * t;
}
bool isBehindDepth = false;
while (currentLevel >= minMipLevel)
{
hitSuccessful = true;

minLinearDepthWithThickness = minLinearDepth + settingsDepthBufferThickness;
bool isAboveDepth = positionLinearDepth < minLinearDepth;
bool isAboveThickness = positionLinearDepth < minLinearDepthWithThickness;
bool isBehindDepth = !isAboveThickness;
isBehindDepth = !isAboveThickness;
bool intersectWithDepth = minLinearDepth >= positionLinearDepth && isAboveThickness;
intersectionKind = HIZINTERSECTIONKIND_NONE;

positionSS = candidatePositionSS;
}
// Raymarching behind object in depth buffer, this case degenerate into a linear search
else if (isBehindDepth && currentLevel <= (minMipLevel + 1))
else if (settingsRayMarchBehindObjects && isBehindDepth && currentLevel <= (minMipLevel + 1))
{
const int2 cellId = int2(positionSS.xy) / cellSize;

hit.linearDepth = positionLinearDepth;
hit.positionSS = uint2(positionSS.xy);
// Move one pixel in the ray direction
// This is where the ray actually hit
hit.positionSS += int2(raySS.xy * abs(invRaySS));
hit.positionNDC = float2(hit.positionSS) / float2(bufferSize);
// Detect when we go behind an object given a thickness

settingsRayBlendScreenDistance
);
if (hitWeight <= 0)
if (hitWeight <= 0 || isBehindDepth)
hitSuccessful = false;
#ifdef DEBUG_DISPLAY

float SSRT_SETTING(DepthBufferThickness, SSRTID);
float SSRT_SETTING(RayMaxScreenDistance, SSRTID);
float SSRT_SETTING(RayBlendScreenDistance, SSRTID);
int SSRT_SETTING(RayMarchBehindObjects, SSRTID);
#ifdef DEBUG_DISPLAY
int SSRT_SETTING(DebuggedAlgorithm, SSRTID);

max(0.01, SSRT_SETTING(DepthBufferThickness, SSRTID)),
SSRT_SETTING(RayMaxScreenDistance, SSRTID),
SSRT_SETTING(RayBlendScreenDistance, SSRTID),
SSRT_SETTING(RayMarchBehindObjects, SSRTID) == 1,
#ifdef DEBUG_DISPLAY
SSRT_SETTING(DebuggedAlgorithm, SSRTID),
#else

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


// -------------------------------
// Resolve weight and color
// -------------------------------
// Fade pixels near the texture buffers' borders
// TODO: Fade pixels with normal non facing the ray direction
// TODO: Fade pixels marked as foreground in stencil
// 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)
// Exit if texel is discarded
if (weight == 0)
{
// Do nothing and don't update the hierarchy weight so we can fall back on refraction probe
return lighting;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl


float _SSReflectionInvScreenWeightDistance; // Distance for screen space smoothstep with fallback
int _SSReflectionEnabled;
int _SSReflectionProjectionModel;
int _SSReflectionHiZRayMarchBehindObject;
int _SSRefractionHiZRayMarchBehindObject;
// Ambiant occlusion
float4 _AmbientOcclusionParam; // xyz occlusion color, w directLightStrenght

正在加载...
取消
保存