LWRP(URP)学习笔记三——阴影

LWRP中是有两种阴影实现的

阴影实现

  • 传统ShadowMap
  • 屏幕空间阴影

传统ShadowMap

传统shadowmap的实现网上很多,这里就不多说了 简单讲下就是通过shadowcaster的pass 以灯光为视角渲染一张shadowmap ,使用的时候在去用自己的深度和shadowmap中进行比较检测是否被遮挡 被遮挡就处于阴影范围内

屏幕空间阴影

值得一提的是 在LWRP中 unity的内置阴影是在条件允许的情况下(非OpenGLES2)使用屏幕空间阴影的

bool resolveShadowsInScreenSpace = mainLightShadows && renderingData.shadowData.requiresScreenSpaceShadowResolve;
shadowData.requiresScreenSpaceShadowResolve = shadowData.supportsMainLightShadows && supportsScreenSpaceShadows && settings.shadowCascadeOption != ShadowCascadesOption.NoCascades;

先看开启条件 通过上面2行代码我们可以得知 当设备支持主阴影的时候,开启阴影连级 就会使用屏幕空间阴影ScreenSpaceShadowResolvePass

using System;

namespace UnityEngine.Rendering.LWRP
{
    internal class ScreenSpaceShadowResolvePass : ScriptableRenderPass
    {
        Material m_ScreenSpaceShadowsMaterial;
        RenderTargetHandle m_ScreenSpaceShadowmap;
        RenderTextureDescriptor m_RenderTextureDescriptor;
        const string m_ProfilerTag = "Resolve Shadows";

        public ScreenSpaceShadowResolvePass(RenderPassEvent evt, Material screenspaceShadowsMaterial)
        {
            m_ScreenSpaceShadowsMaterial = screenspaceShadowsMaterial;
            m_ScreenSpaceShadowmap.Init("_ScreenSpaceShadowmapTexture");
            renderPassEvent = evt;
        }

        public void Setup(RenderTextureDescriptor baseDescriptor)
        {
            m_RenderTextureDescriptor = baseDescriptor;
            m_RenderTextureDescriptor.depthBufferBits = 0;
            m_RenderTextureDescriptor.msaaSamples = 1;
            m_RenderTextureDescriptor.colorFormat = RenderingUtils.SupportsRenderTextureFormat(RenderTextureFormat.R8)
                ? RenderTextureFormat.R8
                : RenderTextureFormat.ARGB32;
        }

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            cmd.GetTemporaryRT(m_ScreenSpaceShadowmap.id, m_RenderTextureDescriptor, FilterMode.Bilinear);

            RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
            ConfigureTarget(screenSpaceOcclusionTexture);
            ConfigureClear(ClearFlag.All, Color.white);
        }

        /// 
        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            if (m_ScreenSpaceShadowsMaterial == null)
            {
                Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_ScreenSpaceShadowsMaterial, GetType().Name);
                return;
            }

            if (renderingData.lightData.mainLightIndex == -1)
                return;

            Camera camera = renderingData.cameraData.camera;
            bool stereo = renderingData.cameraData.isStereoEnabled;

            CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
            if (!stereo)
            {
                cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
                cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_ScreenSpaceShadowsMaterial);
                cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
            }
            else
            {
                // Avoid setting and restoring camera view and projection matrices when in stereo.
                RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
                Blit(cmd, screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, m_ScreenSpaceShadowsMaterial);
            }

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }

        /// 
        public override void FrameCleanup(CommandBuffer cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");

            cmd.ReleaseTemporaryRT(m_ScreenSpaceShadowmap.id);
        }
    }
}

代码不是很多 也比较简单。可以看出是使用了m_ScreenSpaceShadowsMaterial材质对屏幕内的mesh进行绘制保存在_ScreenSpaceShadowmapTexture中。后面drawobject的时候就可以根据自己在屏幕上的位置直接去_ScreenSpaceShadowmapTexture中获取对应的阴影颜色。
有两点值得注意的是
一:使用屏幕空间阴影会需要一张深度图,这张深度图在延迟渲染中是本来就有的 如果是正向渲染 或者现阶段的lwrp中是需要调用m_DepthPrepass提前绘制一遍物体。来得到这张深度图
二:_ScreenSpaceShadowmapTexture的生成也是基于shadowmap的,所以shadowcaster的pass不可少

最近比较忙,好久没更新了 写的比较急 大概就这样吧

你可能感兴趣的:(LWRP(URP)学习笔记)