浏览代码

Upgraded lightweight UI to have local shadow settings. Upgrader asset version. Stripping local shadows based on pipeline capabilities.

/main
Felipe Lira 6 年前
当前提交
3bbb9197
共有 6 个文件被更改,包括 170 次插入93 次删除
  1. 65
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  2. 107
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs
  3. 4
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  4. 5
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineCore.cs
  5. 69
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightShadowPass.cs
  6. 13
      Tests/GraphicsTests/RenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset

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


public enum ShadowResolution
{
_256 = 256,
_512 = 512,
_1024 = 1024,
_2048 = 2048,

UnityBuiltinDefault
}
public class LightweightPipelineAsset : RenderPipelineAsset
public class LightweightPipelineAsset : RenderPipelineAsset, ISerializationCallbackReceiver
private const int PACKAGE_MANAGER_PATH_INDEX = 1;
[SerializeField] private int kAssetVersion = 2;
[SerializeField] private int kAssetVersion = 3;
[SerializeField] private int m_MaxPixelLights = 4;
[SerializeField] private bool m_SupportsVertexLight = false;
[SerializeField] private bool m_RequireDepthTexture = false;

[SerializeField] private bool m_SupportsHDR = false;
[SerializeField] private MSAAQuality m_MSAA = MSAAQuality._4x;
[SerializeField] private float m_RenderScale = 1.0f;
[SerializeField] private ShadowType m_ShadowType = ShadowType.HARD_SHADOWS;
[SerializeField] private bool m_DirectionalShadowsSupported = true;
[SerializeField] private bool m_LocalShadowsSupported = true;
[SerializeField] private ShadowResolution m_LocalShadowsAtlasResolution = ShadowResolution._512;
[SerializeField] private bool m_SoftShadowsSupported = false;
// Deprecated
[SerializeField] private ShadowType m_ShadowType = ShadowType.HARD_SHADOWS;
#if UNITY_EDITOR
[NonSerialized]
private LightweightPipelineEditorResources m_EditorResourcesAsset;

#endif
}
public bool AreShadowsEnabled()
{
return ShadowSetting != ShadowType.NO_SHADOW;
}
public float GetAssetVersion()
public int GetAssetVersion()
{
return kAssetVersion;
}

public bool RequireDepthTexture
{
get { return m_RequireDepthTexture; }
set { m_RequireDepthTexture = value; }
}
public bool RequireSoftParticles

public bool RequireOpaqueTexture
{
get { return m_RequireOpaqueTexture; }
set { m_RequireOpaqueTexture = value; }
set { m_OpaqueDownsampling = value; }
}
public bool SupportsHDR

set { m_RenderScale = value; }
}
public ShadowType ShadowSetting
public bool IsDirectionalShadowsSupported
get { return m_ShadowType; }
private set { m_ShadowType = value; }
get { return m_DirectionalShadowsSupported; }
public int ShadowAtlasResolution
public int DirectionalShadowAtlasResolution
private set { m_ShadowAtlasResolution = (ShadowResolution)value; }
private set { m_ShadowDistance = value; }
set { m_ShadowDistance = value; }
}
public int CascadeCount

public float Cascade2Split
{
get { return m_Cascade2Split; }
private set { m_Cascade2Split = value; }
private set { m_Cascade4Split = value; }
}
public bool IsLocalShadowsSupported
{
get { return m_LocalShadowsSupported; }
}
public int LocalShadowAtlasResolution
{
get { return (int)m_LocalShadowsAtlasResolution; }
}
public bool IsSoftShadowsSupported
{
get { return m_SoftShadowsSupported; }
}
public override Material GetDefaultMaterial()

public Shader SamplingShader
{
get { return resources != null ? resources.SamplingShader : null; }
}
public void OnBeforeSerialize()
{
}
public void OnAfterDeserialize()
{
if (kAssetVersion < 3)
{
kAssetVersion = 3;
m_SoftShadowsSupported = (m_ShadowType == ShadowType.SOFT_SHADOWS);
}
}
}
}

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


public static GUIContent requireOpaqueTexture = new GUIContent("Opaque Texture", "If enabled the pipeline will copy the screen to texture after opaque objects are drawn. For transparent objects this can be bound in shaders as _CameraOpaqueTexture.");
public static GUIContent opaqueDownsampling = new GUIContent("Opaque Downsampling", "The downsampling method that is used for the opaque texture");
public static GUIContent hdrContent = new GUIContent("HDR", "Controls the global HDR settings.");
public static GUIContent msaaContent = new GUIContent("Anti Aliasing (MSAA)", "Controls the global anti aliasing settings.");
public static GUIContent shadowType = new GUIContent("Type",
"Global shadow settings. Options are NO_SHADOW, HARD_SHADOWS and SOFT_SHADOWS.");
public static GUIContent shadowDistante = new GUIContent("Distance", "Max shadow rendering distance.");
public static GUIContent supportsSoftShadows = new GUIContent("Soft Shadows", "If enabled pipeline will perform shadow filtering. Otherwise all shadows will only perform a single shadow sample.");
public static GUIContent supportsDirectionalShadows = new GUIContent("Directional Shadows", "If disabled all directional lights won't cast shadows.");
public static GUIContent shadowDistance = new GUIContent("Distance", "Max shadow rendering distance.");
public static GUIContent shadowAtlasResolution = new GUIContent("Shadowmap Resolution",
"Resolution of shadow map texture. If cascades are enabled, cascades will be packed into an atlas and this setting controls the max shadows atlas resolution.");
public static GUIContent directionalShadowAtlasResolution = new GUIContent("Atlas Resolution",
"Resolution of the directional shadow map texture. If cascades are enabled, cascades will be packed into an atlas and this setting controls the max shadows atlas resolution.");
public static GUIContent shadowCascades = new GUIContent("Cascades",
"Number of cascades used in directional lights shadows");

public static GUIContent hdrContent = new GUIContent("HDR", "Controls the global HDR settings.");
public static GUIContent msaaContent = new GUIContent("Anti Aliasing (MSAA)", "Controls the global anti aliasing settings.");
public static GUIContent supportsLocalShadows = new GUIContent("Local Shadows", "If disabled all local lights won't cast shadows.");
public static string[] shadowTypeOptions = {"No Shadows", "Hard Shadows", "Hard and Soft Shadows"};
public static GUIContent localShadowsAtlasResolution = new GUIContent("Atlas Resolution",
"All local lights are packed into a single atlas. This setting controls the atlas size.");
public static string[] shadowCascadeOptions = {"No Cascades", "Two Cascades", "Four Cascades"};
public static string[] opaqueDownsamplingOptions = {"None", "2x (Bilinear)", "4x (Box)", "4x (Bilinear)"};
}

private SerializedProperty m_RequireSoftParticlesProp;
private SerializedProperty m_RequireOpaqueTextureProp;
private SerializedProperty m_OpaqueDownsamplingProp;
private SerializedProperty m_ShadowTypeProp;
private SerializedProperty m_HDR;
private SerializedProperty m_MSAA;
private SerializedProperty m_SoftShadowsSupportedProp;
private SerializedProperty m_DirectionalShadowsSupportedProp;
private SerializedProperty m_ShadowAtlasResolutionProp;
private SerializedProperty m_DirectionalShadowAtlasResolutionProp;
private SerializedProperty m_HDR;
private SerializedProperty m_MSAA;
private SerializedProperty m_LocalShadowSupportedProp;
private SerializedProperty m_LocalShadowsAtlasResolutionProp;
void OnEnable()
{
m_RenderScale = serializedObject.FindProperty("m_RenderScale");

m_RequireSoftParticlesProp = serializedObject.FindProperty("m_RequireSoftParticles");
m_RequireOpaqueTextureProp = serializedObject.FindProperty("m_RequireOpaqueTexture");
m_OpaqueDownsamplingProp = serializedObject.FindProperty("m_OpaqueDownsampling");
m_ShadowTypeProp = serializedObject.FindProperty("m_ShadowType");
m_HDR = serializedObject.FindProperty("m_SupportsHDR");
m_MSAA = serializedObject.FindProperty("m_MSAA");
m_DirectionalShadowsSupportedProp = serializedObject.FindProperty("m_DirectionalShadowsSupported");
m_ShadowAtlasResolutionProp = serializedObject.FindProperty("m_ShadowAtlasResolution");
m_DirectionalShadowAtlasResolutionProp = serializedObject.FindProperty("m_ShadowAtlasResolution");
m_HDR = serializedObject.FindProperty("m_SupportsHDR");
m_MSAA = serializedObject.FindProperty("m_MSAA");
m_LocalShadowSupportedProp = serializedObject.FindProperty("m_LocalShadowsSupported");
m_LocalShadowsAtlasResolutionProp = serializedObject.FindProperty("m_LocalShadowsAtlasResolution");
m_SoftShadowsSupportedProp = serializedObject.FindProperty("m_SoftShadowsSupported");
m_ShowSoftParticles.valueChanged.AddListener(Repaint);
m_ShowSoftParticles.value = m_RequireSoftParticlesProp.boolValue;

CoreEditorUtils.DrawPopup(content, prop, options);
}
public override void OnInspectorGUI()
void DrawRenderingSettings()
serializedObject.Update();
UpdateAnimationValues();
EditorGUILayout.Space();
EditorGUILayout.LabelField(Styles.renderingLabel, EditorStyles.boldLabel);
EditorGUI.indentLevel++;

EditorGUI.indentLevel--;
EditorGUILayout.Space();
EditorGUILayout.Space();
}
void DrawShadowSettings()
{
bool directionalShadows = false;
bool localShadows = false;
CoreEditorUtils.DrawPopup(Styles.shadowType, m_ShadowTypeProp, Styles.shadowTypeOptions);
EditorGUILayout.PropertyField(m_ShadowAtlasResolutionProp, Styles.shadowAtlasResolution);
m_ShadowDistanceProp.floatValue = Mathf.Max(0.0f, EditorGUILayout.FloatField(Styles.shadowDistante, m_ShadowDistanceProp.floatValue));
CoreEditorUtils.DrawPopup(Styles.shadowCascades, m_ShadowCascadesProp, Styles.shadowCascadeOptions);
EditorGUILayout.PropertyField(m_DirectionalShadowsSupportedProp, Styles.supportsDirectionalShadows);
directionalShadows = m_DirectionalShadowsSupportedProp.boolValue;
if (directionalShadows)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_DirectionalShadowAtlasResolutionProp, Styles.directionalShadowAtlasResolution);
m_ShadowDistanceProp.floatValue = Mathf.Max(0.0f,
EditorGUILayout.FloatField(Styles.shadowDistance, m_ShadowDistanceProp.floatValue));
CoreEditorUtils.DrawPopup(Styles.shadowCascades, m_ShadowCascadesProp, Styles.shadowCascadeOptions);
ShadowCascades cascades = (ShadowCascades) m_ShadowCascadesProp.intValue;
if (cascades == ShadowCascades.FOUR_CASCADES)
CoreEditorUtils.DrawCascadeSplitGUI<Vector3>(ref m_ShadowCascade4SplitProp);
else if (cascades == ShadowCascades.TWO_CASCADES)
CoreEditorUtils.DrawCascadeSplitGUI<float>(ref m_ShadowCascade2SplitProp);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
}
EditorGUILayout.PropertyField(m_LocalShadowSupportedProp, Styles.supportsLocalShadows);
localShadows = m_LocalShadowSupportedProp.boolValue;
if (localShadows)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_LocalShadowsAtlasResolutionProp, Styles.localShadowsAtlasResolution);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
}
ShadowCascades cascades = (ShadowCascades)m_ShadowCascadesProp.intValue;
if (cascades == ShadowCascades.FOUR_CASCADES)
CoreEditorUtils.DrawCascadeSplitGUI<Vector3>(ref m_ShadowCascade4SplitProp);
else if (cascades == ShadowCascades.TWO_CASCADES)
CoreEditorUtils.DrawCascadeSplitGUI<float>(ref m_ShadowCascade2SplitProp);
if (directionalShadows || localShadows)
EditorGUILayout.PropertyField(m_SoftShadowsSupportedProp, Styles.supportsSoftShadows);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
UpdateAnimationValues();
DrawRenderingSettings();
DrawShadowSettings();
serializedObject.ApplyModifiedProperties();
}
}

4
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


//TIM: Not used in shader for V1 to reduce keywords
CoreUtils.SetKeyword(cmd, LightweightKeywords.MainLightCookieText, mainLightIndex != -1 && LightweightUtils.IsSupportedCookieType(visibleLights[mainLightIndex].lightType) && visibleLights[mainLightIndex].light.cookie != null);
CoreUtils.SetKeyword(cmd, LightweightKeywords.DirectionalShadowsText, m_ShadowPass.HasDirectionalShadowmap);
CoreUtils.SetKeyword(cmd, LightweightKeywords.LocalShadowsText, m_ShadowPass.HasLocalLightsShadowmap);
CoreUtils.SetKeyword(cmd, LightweightKeywords.DirectionalShadowsText, m_ShadowPass.DirectionalShadowsRendered);
CoreUtils.SetKeyword(cmd, LightweightKeywords.LocalShadowsText, m_ShadowPass.LocalShadowsRendered);
// TODO: Remove this. legacy particles support will be removed from Unity in 2018.3. This should be a shader_feature instead with prop exposed in the Standard particles shader.
CoreUtils.SetKeyword(cmd, "SOFTPARTICLES_ON", m_RequireDepthTexture && m_Asset.RequireSoftParticles);

5
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineCore.cs


if (pipelineAsset.SupportsVertexLight)
pipelineCapabilities |= PipelineCapabilities.VertexLights;
if (pipelineAsset.ShadowSetting != ShadowType.NO_SHADOW)
if (pipelineAsset.IsDirectionalShadowsSupported)
pipelineCapabilities |= PipelineCapabilities.LocalShadows;
if (pipelineAsset.IsLocalShadowsSupported)
pipelineCapabilities |= PipelineCapabilities.LocalShadows;
}
}
}

69
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightShadowPass.cs


[Serializable]
public class ShadowSettings
{
public LightShadows directionalShadowQuality;
public bool supportsDirectionalShadows;
public LightShadows localLightsShadowQuality;
public int localShadowAtlasWidth;
public int localShadowAtlasHeight;
public int bufferBitCount;
public bool supportsLocalShadows;
public int localShadowAtlasWidth;
public int localShadowAtlasHeight;
public bool supportsSoftShadows;
public int bufferBitCount;
public RenderTextureFormat shadowmapTextureFormat;
public RenderTextureFormat screenspaceShadowmapTextureFormat;

if (defaultShadowSettings == null)
{
defaultShadowSettings = new ShadowSettings();
defaultShadowSettings.directionalShadowQuality = LightShadows.None;
defaultShadowSettings.supportsDirectionalShadows = true;
defaultShadowSettings.localLightsShadowQuality = LightShadows.None;
defaultShadowSettings.directionalLightCascadeCount = 1;
defaultShadowSettings.directionalLightCascades = new Vector3(0.067f, 0.2f, 0.467f);
defaultShadowSettings.supportsLocalShadows = true;
defaultShadowSettings.directionalLightCascadeCount = 1;
defaultShadowSettings.directionalLightCascades = new Vector3(0.05F, 0.2F, 0.3F);
defaultShadowSettings.supportsSoftShadows = false;
}
return defaultShadowSettings;
}

public class LightweightShadowPass
{
public bool IsDirectionalShadowsEnabled { get { return m_ShadowSettings.directionalShadowQuality != LightShadows.None; } }
public bool IsLocalShadowsEnabled { get { return m_ShadowSettings.localLightsShadowQuality != LightShadows.None; }}
public bool IsDirectionalShadowsEnabled { get { return m_ShadowSettings.supportsDirectionalShadows; } }
public bool IsLocalShadowsEnabled { get { return m_ShadowSettings.supportsLocalShadows; } }
public bool HasDirectionalShadowmap { get { return m_DirectionalShadowmapQuality != LightShadows.None; } }
public bool HasLocalLightsShadowmap { get { return m_LocalShadowmapQuality != LightShadows.None; } }
public bool DirectionalShadowsRendered { get { return m_DirectionalShadowmapQuality != LightShadows.None; } }
public bool LocalShadowsRendered { get { return m_LocalShadowmapQuality != LightShadows.None; } }
public bool IsSoftShadowsEnabled { get { return m_ShadowSettings.supportsSoftShadows; } }
public float RenderingDistance { get { return m_ShadowSettings.maxShadowDistance; } }

// Until we can have keyword stripping forcing single cascade hard shadows on gles2
bool supportsScreenSpaceShadows = SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2;
// TODO: Add pipeline setting for this when we oficially add support to multiple shadow casting lights
bool supportsLocalShadows = true;
m_ShadowSettings.directionalShadowQuality = (LightShadows)pipelineAsset.ShadowSetting;
m_ShadowSettings.screenSpace = supportsScreenSpaceShadows;
m_ShadowSettings.supportsDirectionalShadows = pipelineAsset.IsDirectionalShadowsSupported;
m_ShadowSettings.screenSpace = m_ShadowSettings.supportsDirectionalShadows && supportsScreenSpaceShadows;
m_ShadowSettings.directionalShadowAtlasWidth = pipelineAsset.ShadowAtlasResolution;
m_ShadowSettings.directionalShadowAtlasHeight = pipelineAsset.ShadowAtlasResolution;
m_ShadowSettings.directionalShadowAtlasWidth = pipelineAsset.DirectionalShadowAtlasResolution;
m_ShadowSettings.directionalShadowAtlasHeight = pipelineAsset.DirectionalShadowAtlasResolution;
m_ShadowSettings.shadowmapTextureFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap)
? RenderTextureFormat.Shadowmap
: RenderTextureFormat.Depth;
m_ShadowSettings.screenspaceShadowmapTextureFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.R8)
? RenderTextureFormat.R8
: RenderTextureFormat.ARGB32;
switch (m_ShadowSettings.directionalLightCascadeCount)
{

break;
}
// Until we can have keyword stripping we disable local light shadows on mobile
m_ShadowSettings.localLightsShadowQuality = (supportsLocalShadows) ? LightShadows.Hard : LightShadows.None;
m_ShadowSettings.supportsLocalShadows = pipelineAsset.IsLocalShadowsSupported;
m_ShadowSettings.localShadowAtlasWidth = m_ShadowSettings.localShadowAtlasHeight = pipelineAsset.LocalShadowAtlasResolution;
m_ShadowSettings.supportsSoftShadows = pipelineAsset.IsSoftShadowsSupported;
m_ShadowSettings.bufferBitCount = 16;
m_ShadowSettings.shadowmapTextureFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap)
? RenderTextureFormat.Shadowmap
: RenderTextureFormat.Depth;
m_ShadowSettings.screenspaceShadowmapTextureFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.R8)
? RenderTextureFormat.R8
: RenderTextureFormat.ARGB32;
}
private void Clear()

if (success)
{
m_DirectionalShadowmapQuality = (m_ShadowSettings.directionalShadowQuality != LightShadows.Soft) ? LightShadows.Hard : light.shadows;
m_DirectionalShadowmapQuality = (IsSoftShadowsEnabled) ? light.shadows : LightShadows.Hard;
// In order to avoid shader variants explosion we only do hard shadows when sampling shadowmap in the lit pass.
// GLES2 platform is forced to hard single cascade shadows.

SetupLocalLightsShadowReceiverConstants(cmd, ref context);
m_LocalShadowmapQuality = (LightShadows)Math.Min(shadowSampling, (int)m_ShadowSettings.directionalShadowQuality);
m_LocalShadowmapQuality = (IsSoftShadowsEnabled) ? (LightShadows)shadowSampling : LightShadows.Hard;
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

13
Tests/GraphicsTests/RenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset


--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1

m_EditorClassIdentifier:
kAssetVersion: 2
kAssetVersion: 3
m_RequireOpaqueTexture: 0
m_OpaqueDownsampling: 1
m_ShadowType: 1
m_DirectionalShadowsSupported: 1
m_ShadowNearPlaneOffset: 2
m_LocalShadowsSupported: 1
m_LocalShadowsAtlasResolution: 256
m_SoftShadowsSupported: 1
m_ShadowType: 0
正在加载...
取消
保存