92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Rendering;
|
||
|
|
using UnityEngine.Rendering.Universal;
|
||
|
|
|
||
|
|
public class FogRenderPassFeature : ScriptableRendererFeature
|
||
|
|
{
|
||
|
|
public enum OutPutType { screen, rTexture, both }
|
||
|
|
|
||
|
|
[System.Serializable]
|
||
|
|
public class Setting
|
||
|
|
{
|
||
|
|
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
|
||
|
|
public Material materal;
|
||
|
|
public OutPutType outPutType = OutPutType.screen;
|
||
|
|
public RenderTexture outPutRt = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Setting setting = new Setting();
|
||
|
|
|
||
|
|
class FogRenderPass : ScriptableRenderPass
|
||
|
|
{
|
||
|
|
private Material material;
|
||
|
|
private OutPutType outPutType;
|
||
|
|
private RenderTexture outPutRt;
|
||
|
|
private ProfilingSampler profilingSampler;
|
||
|
|
private int tempTextureId = Shader.PropertyToID("_FogTempTexture");
|
||
|
|
|
||
|
|
public FogRenderPass(FogRenderPassFeature.Setting setting)
|
||
|
|
{
|
||
|
|
renderPassEvent = setting.renderPassEvent;
|
||
|
|
material = setting.materal;
|
||
|
|
outPutType = setting.outPutType;
|
||
|
|
outPutRt = setting.outPutRt;
|
||
|
|
profilingSampler = new ProfilingSampler("FogRenderPassFeature");
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||
|
|
{
|
||
|
|
if (renderingData.cameraData.isSceneViewCamera) return;
|
||
|
|
if (material == null) return;
|
||
|
|
|
||
|
|
CommandBuffer cmd = CommandBufferPool.Get();
|
||
|
|
using (new ProfilingScope(cmd, profilingSampler))
|
||
|
|
{
|
||
|
|
// 不渲染UI Layer
|
||
|
|
renderingData.cameraData.camera.cullingMask &= ~(1 << LayerMask.NameToLayer("UI"));
|
||
|
|
|
||
|
|
// 创建临时渲染纹理描述符
|
||
|
|
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
|
||
|
|
descriptor.depthBufferBits = 0;
|
||
|
|
cmd.GetTemporaryRT(tempTextureId, descriptor);
|
||
|
|
|
||
|
|
// 应用材质
|
||
|
|
cmd.Blit(null, tempTextureId, material);
|
||
|
|
|
||
|
|
switch (outPutType)
|
||
|
|
{
|
||
|
|
case OutPutType.screen:
|
||
|
|
cmd.Blit(tempTextureId, BuiltinRenderTextureType.CameraTarget);
|
||
|
|
break;
|
||
|
|
case OutPutType.rTexture:
|
||
|
|
if (outPutRt != null)
|
||
|
|
cmd.Blit(tempTextureId, outPutRt);
|
||
|
|
break;
|
||
|
|
case OutPutType.both:
|
||
|
|
cmd.Blit(tempTextureId, BuiltinRenderTextureType.CameraTarget);
|
||
|
|
if (outPutRt != null)
|
||
|
|
cmd.Blit(tempTextureId, outPutRt);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 释放临时纹理
|
||
|
|
cmd.ReleaseTemporaryRT(tempTextureId);
|
||
|
|
}
|
||
|
|
|
||
|
|
context.ExecuteCommandBuffer(cmd);
|
||
|
|
CommandBufferPool.Release(cmd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
FogRenderPass fogRenderPass;
|
||
|
|
|
||
|
|
public override void Create()
|
||
|
|
{
|
||
|
|
fogRenderPass = new FogRenderPass(setting);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||
|
|
{
|
||
|
|
renderer.EnqueuePass(fogRenderPass);
|
||
|
|
}
|
||
|
|
}
|