浏览代码

Merge ScreenSpaceShadows

/projects-TheLastStand
John 7 年前
当前提交
d56fa578
共有 7 个文件被更改,包括 65 次插入50 次删除
  1. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  2. 9
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs
  3. 25
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  4. 5
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/LightweightPassLit.hlsl
  5. 17
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl
  6. 51
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightScreenSpaceShadows.shader
  7. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandard.shader

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs


[SerializeField] private bool m_SupportsVertexLight = false;
[SerializeField] private bool m_RequireDepthTexture = false;
[SerializeField] private bool m_RequireSoftParticles = false;
[SerializeField] private bool m_RequireScreenSpaceShadows = false;
[SerializeField] private bool m_SupportsHDR = false;
[SerializeField] private MSAAQuality m_MSAA = MSAAQuality._4x;
[SerializeField] private float m_RenderScale = 1.0f;

public bool RequireSoftParticles
{
get { return m_RequireSoftParticles; }
}
public bool RequireScreenSpaceShadows
{
get { return m_RequireScreenSpaceShadows; }
}
public bool SupportsHDR

9
ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs


public static GUIContent requireSoftParticles = new GUIContent("Soft Particles", "If enabled the pipeline will enable SOFT_PARTICLES keyword.");
public static GUIContent requireScreenSpaceShadows = new GUIContent("Screen Space Shadows", "TODO");
public static GUIContent shadowType = new GUIContent("Type",
"Global shadow settings. Options are NO_SHADOW, HARD_SHADOWS and SOFT_SHADOWS.");

private SerializedProperty m_MaxPixelLights;
private SerializedProperty m_SupportsVertexLightProp;
private SerializedProperty m_RequireDepthTextureProp;
private SerializedProperty m_RequireScreenSpaceShadowsProp;
private SerializedProperty m_RequireSoftParticlesProp;
private SerializedProperty m_ShadowTypeProp;
private SerializedProperty m_ShadowNearPlaneOffsetProp;

m_SupportsVertexLightProp = serializedObject.FindProperty("m_SupportsVertexLight");
m_RequireDepthTextureProp = serializedObject.FindProperty("m_RequireDepthTexture");
m_RequireSoftParticlesProp = serializedObject.FindProperty("m_RequireSoftParticles");
m_RequireScreenSpaceShadowsProp = serializedObject.FindProperty("m_RequireScreenSpaceShadows");
m_ShadowTypeProp = serializedObject.FindProperty("m_ShadowType");
m_ShadowNearPlaneOffsetProp = serializedObject.FindProperty("m_ShadowNearPlaneOffset");
m_ShadowDistanceProp = serializedObject.FindProperty("m_ShadowDistance");

m_ShowSoftParticles.valueChanged.AddListener(Repaint);
m_ShowSoftParticles.value = m_RequireSoftParticlesProp.boolValue;
m_ShowScreenSpaceShadows.valueChanged.AddListener(Repaint);
m_ShowScreenSpaceShadows.value = m_RequireScreenSpaceShadowsProp.boolValue;
}
void OnDisable()

{
EditorGUILayout.PropertyField(m_ShadowCascade2SplitProp, Styles.shadowCascadeSplit);
}
DrawAnimatedProperty(m_RequireScreenSpaceShadowsProp, Styles.requireScreenSpaceShadows, m_ShowScreenSpaceShadows);
EditorGUI.indentLevel--;

25
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


// we need use copyColor RT as a work RT.
public static int copyColor;
// Camera depth target. Only used when post processing or soft particles are enabled.
// Camera depth target. Only used when post processing, soft particles, or screen space shadows are enabled.
public static int depth;
// If soft particles are enabled and no depth prepass is performed we need to copy depth.

"_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN_SOFT",
"_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN_SOFT",
"_MAIN_LIGHT_SPOT_SHADOW",
"_MAIN_LIGHT_SPOT_SHADOW_SOFT"

private bool m_RequireDepthTexture;
private bool m_RequireCopyColor;
private bool m_DepthRenderBuffer;
private bool m_RequireScreenSpaceShadows;
private MixedLightingSetup m_MixedLightingSetup;
private const int kDepthStencilBufferBits = 32;

DepthPass(ref context);
RenderMSVO(ref context);
if(m_RequireScreenSpaceShadows) //NOTE: Should this be added to the FrameRenderingConfiguration?
if(m_ShadowCasterCascadesCount > 1)
ShadowCollectPass(ref context, visibleLights, ref lightData);
}

}
}
bool requireScreenSpaceShadows = m_Asset.CascadeCount > 1;
if (requireScreenSpaceShadows)
{
m_RequireDepthTexture = true;
if(!msaaEnabled)
intermediateTexture = true;
}
if (msaaEnabled)
{
configuration |= FrameRenderingConfiguration.Msaa;

{
// If msaa is enabled we don't use a depth renderbuffer as we might not have support to Texture2DMS to resolve depth.
// Instead we use a depth prepass and whenever depth is needed we use the 1 sample depth from prepass.
if (!msaaEnabled)
if (!msaaEnabled && !requireScreenSpaceShadows)
{
bool supportsDepthCopy = m_CopyTextureSupport != CopyTextureSupport.None && m_Asset.CopyDepthShader.isSupported;
m_DepthRenderBuffer = true;

}
}
m_RequireScreenSpaceShadows = m_Asset.RequireScreenSpaceShadows && LightweightUtils.HasFlag(configuration, FrameRenderingConfiguration.DepthPrePass);
Rect cameraRect = m_CurrCamera.rect;
if (!(Math.Abs(cameraRect.x) > 0.0f || Math.Abs(cameraRect.y) > 0.0f || Math.Abs(cameraRect.width) < 1.0f || Math.Abs(cameraRect.height) < 1.0f))
configuration |= FrameRenderingConfiguration.DefaultViewport;

m_MainLightKeywordString.Append("_DIRECTIONAL_SHADOW");
if (m_Asset.CascadeCount > 1)
m_MainLightKeywordString.Append("_CASCADE");
if(m_RequireScreenSpaceShadows)
m_MainLightKeywordString.Append("_SCREEN");
}
else
{

5
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/LightweightPassLit.hlsl


half fogFactor = ComputeFogFactor(o.clipPos.z);
o.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
//TODO: Macro?
#if defined(_SHADOWS_SCREEN)
#if defined(_SHADOWS_CASCADE) //Assume screen space shadows when cascades enabled
#elif !defined(_SHADOWS_CASCADE)
#else
o.shadowCoord = ComputeShadowCoord(o.posWS.xyz);
#endif
#endif

17
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl


#define _SHADOWS_PERSPECTIVE
#endif
#if defined(_MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN) || defined(_MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN_SOFT) || defined(_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN) || defined(_MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN_SOFT)
#define _SHADOWS_SCREEN
#endif
TEXTURE2D(_ScreenSpaceShadowMap);
SAMPLER(sampler_ScreenSpaceShadowMap);

inline half SampleScreenSpaceShadowMap(float4 shadowCoord)
{
//NOTE: No macro for proj sample?
return SAMPLE_TEXTURE2D(_ScreenSpaceShadowMap, sampler_ScreenSpaceShadowMap, shadowCoord.xy / shadowCoord.w).x;
shadowCoord.xyz = shadowCoord.xyz / shadowCoord.w;
return SAMPLE_TEXTURE2D(_ScreenSpaceShadowMap, sampler_ScreenSpaceShadowMap, shadowCoord.xy).x;
}
inline half SampleShadowmap(float4 shadowCoord)

attenuation4.w = SAMPLE_TEXTURE2D_SHADOW(_ShadowMap, sampler_ShadowMap, shadowCoord.xyz + _ShadowOffset3.xyz);
attenuation = dot(attenuation4, 0.25);
#else
#ifdef _SHADOWS_SCREEN
#ifdef _SHADOWS_CASCADE //Assume screen space shadows when cascades enabled
real fetchesWeights[16];
real2 fetchesUV[16];
SampleShadow_ComputeSamples_Tent_7x7(_ShadowmapSize, shadowCoord.xy, fetchesWeights, fetchesUV);

return 1.0;
#endif
#ifdef _SHADOWS_SCREEN
#ifdef _SHADOWS_CASCADE //Assume screen space shadows when cascades are enabled
#ifdef _SHADOWS_CASCADE
shadowCoord = ComputeShadowCoord(positionWS);
#endif
return SampleShadowmap(shadowCoord);
return SampleShadowmap(shadowCoord);
#endif
}

51
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightScreenSpaceShadows.shader


float4 vertex : POSITION;
float2 uv : TEXCOORD0;
uint id : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
half4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
float3 ray : TEXCOORD1;
half4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
//Perspective Case
float3 ray : TEXCOORD1;
//Orthographic Case
float3 orthoPosNear : TEXCOORD2;
float3 orthoPosFar : TEXCOORD3;
UNITY_VERTEX_INPUT_INSTANCE_ID
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);
return i.ray * Linear01Depth(depth, _ZBufferParams);
float zDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);
float depth = Linear01Depth(zDepth, _ZBufferParams);
#ifdef UNITY_REVERSED_Z
zDepth = 1 - zDepth;
#endif
//Perspective Case
float3 vposPersp = i.ray * depth;
//Orthographics Case
float3 vposOrtho = lerp(i.orthoPosNear, i.orthoPosFar, zDepth);
return lerp(vposPersp, vposOrtho, unity_OrthoParams.w);
UNITY_SETUP_INSTANCE_ID(i);
UNITY_TRANSFER_INSTANCE_ID(i, o);
//Perspective Case
//Orthographic Case
float4 clipPos = o.pos;
clipPos.y *= _ProjectionParams.x;
float3 orthoPosNear = mul(unity_CameraInvProjection, float4(clipPos.x, clipPos.y, -1, 1)).xyz;
float3 orthoPosFar = mul(unity_CameraInvProjection, float4(clipPos.x, clipPos.y, 1, 1)).xyz;
orthoPosNear.z *= -1;
orthoPosFar.z *= -1;
o.orthoPosNear = orthoPosNear;
o.orthoPosFar = orthoPosFar;
UNITY_SETUP_INSTANCE_ID(i);
//Reconstruct the world position.
float3 vpos = ComputeViewSpacePositionGeometric(i); //TODO: Profile against unprojection method in core library.
float3 wpos = mul(unity_CameraToWorld, float4(vpos, 1)).xyz;

// Lightweight shader library declares defines based on these keywords to avoid having to check them in the shaders
// Core.hlsl defines _MAIN_LIGHT_DIRECTIONAL and _MAIN_LIGHT_SPOT (point lights can't be main light)
// Shadow.hlsl defines _SHADOWS_ENABLED, _SHADOWS_SOFT, _SHADOWS_CASCADE, _SHADOWS_PERSPECTIVE
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN _MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN_SOFT
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT
#pragma vertex Vertex
#pragma fragment Fragment

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandard.shader


// Lightweight shader library declares defines based on these keywords to avoid having to check them in the shaders
// Core.hlsl defines _MAIN_LIGHT_DIRECTIONAL and _MAIN_LIGHT_SPOT (point lights can't be main light)
// Shadow.hlsl defines _SHADOWS_ENABLED, _SHADOWS_SOFT, _SHADOWS_CASCADE, _SHADOWS_PERSPECTIVE
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SCREEN_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN _MAIN_LIGHT_DIRECTIONAL_SHADOW_SCREEN_SOFT
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ _VERTEX_LIGHTS

正在加载...
取消
保存