浏览代码

Added code to make BeforeTransparent postfx work. We need a way to check when the effect is enabled to properly make it work.

/Add-support-for-light-specular-color-tint
Felipe Lira 7 年前
当前提交
ee5a618b
共有 1 个文件被更改,包括 89 次插入43 次删除
  1. 132
      ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs

132
ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


public bool shadowsRendered;
}
public static class CameraRenderTargetID
{
// Camera color target uses to render opaques.
// It's the same as final color except when BeforeOpaque custom PostFX is enabled
public static int opaqueColor;
// Camera color target. Not used when camera is rendering to backbuffer or camera
// is rendering to a texture (offscreen camera)
public static int finalColor;
// Camera depth target. Only used when post processing or soft particles are enabled.
public static int depth;
// If soft particles are enabled
public static int depthCopy;
}
public class LightweightPipeline : RenderPipeline
{
private readonly LightweightPipelineAsset m_Asset;

private static readonly int kMaxCascades = 4;
private int m_ShadowCasterCascadesCount = kMaxCascades;
private int m_ShadowMapTexture;
private int m_CameraColorTexture;
private int m_CameraDepthTexture;
private int m_CameraCopyDepthTexture;
private int m_ShadowMapRTID;
private RenderTargetIdentifier m_CurrCameraColorRT;
private RenderTargetIdentifier m_CameraColorRT;
private RenderTargetIdentifier m_CameraDepthRT;
private RenderTargetIdentifier m_CameraCopyDepthRT;
//private RenderTargetIdentifier m_OpaqueColorRT;
private RenderTargetIdentifier m_FinalColorRT;
private RenderTargetIdentifier m_OpaqueDepthRT;
private RenderTargetIdentifier m_FinalDepthRT;
private bool m_IntermediateTextureArray = false;

PerCameraBuffer._AdditionalLightSpotDir = Shader.PropertyToID("_AdditionalLightSpotDir");
PerCameraBuffer._AdditionalLightSpotAttenuation = Shader.PropertyToID("_AdditionalLightSpotAttenuation");
m_ShadowMapTexture = Shader.PropertyToID("_ShadowMap");
m_CameraColorTexture = Shader.PropertyToID("_CameraColorTexture");
m_CameraDepthTexture = Shader.PropertyToID("_CameraDepthTexture");
m_CameraCopyDepthTexture = Shader.PropertyToID("_CameraCopyDepthTexture");
m_ShadowMapRTID = Shader.PropertyToID("_ShadowMap");
CameraRenderTargetID.opaqueColor = Shader.PropertyToID("_CameraOpaqueColorRT");
CameraRenderTargetID.finalColor = Shader.PropertyToID("_CameraColorRT");
CameraRenderTargetID.depth = Shader.PropertyToID("_CameraDepthTexture");
CameraRenderTargetID.depthCopy = Shader.PropertyToID("_CameraCopyDepthTexture");
m_ShadowMapRT = new RenderTargetIdentifier(m_ShadowMapRTID);
m_ShadowMapRT = new RenderTargetIdentifier(m_ShadowMapTexture);
m_CameraColorRT = new RenderTargetIdentifier(m_CameraColorTexture);
m_CameraDepthRT = new RenderTargetIdentifier(m_CameraDepthTexture);
m_CameraCopyDepthRT = new RenderTargetIdentifier(m_CameraCopyDepthTexture);
//m_OpaqueColorRT = new RenderTargetIdentifier(CameraRenderTargetID.opaqueColor);
m_FinalColorRT = new RenderTargetIdentifier(CameraRenderTargetID.finalColor);
m_OpaqueDepthRT = new RenderTargetIdentifier(CameraRenderTargetID.depth);
m_FinalDepthRT = new RenderTargetIdentifier(CameraRenderTargetID.depthCopy);
m_PostProcessRenderContext = new PostProcessRenderContext();
m_CopyTextureSupport = SystemInfo.copyTextureSupport;

// Release temporary RT
var cmd = CommandBufferPool.Get("After Camera Render");
cmd.ReleaseTemporaryRT(m_ShadowMapTexture);
cmd.ReleaseTemporaryRT(m_CameraColorTexture);
cmd.ReleaseTemporaryRT(m_CameraDepthTexture);
cmd.ReleaseTemporaryRT(m_CameraCopyDepthTexture);
cmd.ReleaseTemporaryRT(m_ShadowMapRTID);
cmd.ReleaseTemporaryRT(CameraRenderTargetID.depthCopy);
cmd.ReleaseTemporaryRT(CameraRenderTargetID.depth);
cmd.ReleaseTemporaryRT(CameraRenderTargetID.finalColor);
cmd.ReleaseTemporaryRT(CameraRenderTargetID.opaqueColor);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);

return;
CommandBuffer cmd = CommandBufferPool.Get("After Opaque");
cmd.SetGlobalTexture(m_CameraDepthTexture, m_CameraDepthRT);
cmd.SetGlobalTexture(CameraRenderTargetID.depth, m_OpaqueDepthRT);
// When soft particles are enabled we have to copy depth to another RT so we can read and write to depth
// Only takes effect if custom BeforeTransparent PostProcessing effects are active
// Custom BeforeTransparent custom postfx disabled for VR.
// TODO: Need to check when post process volume is active/in effect to apply before opaque post processing
// If active we blit Opaque to FinalColor
//if (LightweightUtils.HasFlag(config, FrameRenderingConfiguration.PostProcess) && !XRSettings.isDeviceActive)
//{
// RenderPostProcess(cmd, m_OpaqueColorRT, m_FinalColorRT, true);
// m_CurrCameraColorRT = (m_IsOffscreenCamera) ? BuiltinRenderTextureType.CameraTarget : m_FinalColorRT;
//}
RenderTargetIdentifier colorRT = (m_IsOffscreenCamera) ? BuiltinRenderTextureType.CameraTarget : m_CameraColorRT;
CopyTexture(cmd, m_CameraDepthRT, m_CameraCopyDepthTexture);
SetupRenderTargets(cmd, colorRT, m_CameraCopyDepthRT);
RenderTargetIdentifier colorRT = (m_IsOffscreenCamera) ? BuiltinRenderTextureType.CameraTarget : m_FinalColorRT;
CopyTexture(cmd, m_OpaqueDepthRT, m_FinalDepthRT, m_CopyDepthMaterial);
SetupRenderTargets(cmd, colorRT, m_FinalDepthRT);
// Only takes effect if custom BeforeTransparent PostProcessing effects are active
if (LightweightUtils.HasFlag(config, FrameRenderingConfiguration.PostProcess))
RenderPostProcess(cmd , true);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);

return;
CommandBuffer cmd = CommandBufferPool.Get("After Transparent");
RenderPostProcess(cmd, false);
RenderPostProcess(cmd, BuiltinRenderTextureType.CurrentActive, BuiltinRenderTextureType.CameraTarget, false);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

rtDesc.colorFormat = m_ColorFormat;
rtDesc.msaaSamples = msaaSamples;
cmd.GetTemporaryRT(m_CameraColorTexture, rtDesc, FilterMode.Bilinear);
cmd.GetTemporaryRT(CameraRenderTargetID.finalColor, rtDesc, FilterMode.Bilinear);
//if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.PostProcess))
// cmd.GetTemporaryRT(CameraRenderTargetID.opaqueColor, rtDesc, FilterMode.Bilinear);
cmd.GetTemporaryRT(m_CameraColorTexture, rtWidth, rtHeight, kCameraDepthBufferBits,
cmd.GetTemporaryRT(CameraRenderTargetID.finalColor, rtWidth, rtHeight, kCameraDepthBufferBits,
//if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.PostProcess))
// cmd.GetTemporaryRT(CameraRenderTargetID.opaqueColor, rtWidth, rtHeight, kCameraDepthBufferBits,
// FilterMode.Bilinear, m_ColorFormat, RenderTextureReadWrite.Default, msaaSamples);
// When postprocessing is enabled we might have a before transparent effect. In that case we need to
// use the camera render target as input. We blit to an opaque RT and then after before postprocessing is done
// we blit to the final camera RT. If no postprocessing we blit to final camera RT from beginning.
// TODO: We need to have way to check if before opaque post processing is enabled and only then set camera RT to opaqueColorRT
//if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.PostProcess))
// m_CurrCameraColorRT = m_OpaqueColorRT;
//else
// m_CurrCameraColorRT = m_FinalColorRT;
//else
//{
// m_CurrCameraColorRT = BuiltinRenderTextureType.CameraTarget;
//}
cmd.GetTemporaryRT(m_CameraDepthTexture, rtWidth, rtHeight, kCameraDepthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Depth);
cmd.GetTemporaryRT(CameraRenderTargetID.depth, rtWidth, rtHeight, kCameraDepthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Depth);
cmd.GetTemporaryRT(m_CameraCopyDepthTexture, rtWidth, rtHeight, kCameraDepthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Depth);
cmd.GetTemporaryRT(CameraRenderTargetID.depthCopy, rtWidth, rtHeight, kCameraDepthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Depth);
}
context.ExecuteCommandBuffer(cmd);

var setRenderTargetCommandBuffer = CommandBufferPool.Get();
setRenderTargetCommandBuffer.name = "Render packed shadows";
setRenderTargetCommandBuffer.GetTemporaryRT(m_ShadowMapTexture, m_ShadowSettings.shadowAtlasWidth,
setRenderTargetCommandBuffer.GetTemporaryRT(m_ShadowMapRTID, m_ShadowSettings.shadowAtlasWidth,
m_ShadowSettings.shadowAtlasHeight, kShadowDepthBufferBits, FilterMode.Bilinear, RenderTextureFormat.Depth);
setRenderTargetCommandBuffer.SetRenderTarget(m_ShadowMapRT);
setRenderTargetCommandBuffer.ClearRenderTarget(true, true, Color.black);

if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.IntermediateTexture))
{
if (!m_IsOffscreenCamera)
colorRT = m_CameraColorRT;
colorRT = m_FinalColorRT;
depthRT = m_CameraDepthRT;
depthRT = m_OpaqueDepthRT;
}
SetupRenderTargets(cmd, colorRT, depthRT);

if (m_IntermediateTextureArray)
{
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
cmd.Blit(m_CameraColorRT, BuiltinRenderTextureType.CurrentActive);
cmd.Blit(m_FinalColorRT, BuiltinRenderTextureType.CurrentActive);
}
else if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.IntermediateTexture))
{

cmd.SetRenderTarget(colorRT, 0, CubemapFace.Unknown, depthSlice);
}
private void RenderPostProcess(CommandBuffer cmd, bool opaqueOnly)
private void RenderPostProcess(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, bool opaqueOnly)
m_PostProcessRenderContext.source = BuiltinRenderTextureType.CurrentActive;
m_PostProcessRenderContext.source = source;
m_PostProcessRenderContext.destination = BuiltinRenderTextureType.CameraTarget;
m_PostProcessRenderContext.destination = dest;
m_PostProcessRenderContext.command = cmd;
m_PostProcessRenderContext.flip = true;

}
}
private void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier sourceRT, RenderTargetIdentifier destRT)
private void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier sourceRT, RenderTargetIdentifier destRT, Material copyMaterial)
cmd.CopyTexture(m_CameraDepthRT, m_CameraCopyDepthRT);
cmd.CopyTexture(sourceRT, destRT);
cmd.Blit(m_CameraDepthRT, m_CameraCopyDepthRT, m_CopyDepthMaterial);
cmd.Blit(sourceRT, destRT, copyMaterial);
}
}
}
正在加载...
取消
保存