增加全屏叠色+高斯模糊

main
刘涛 2023-11-03 13:41:15 +08:00
parent 2fe4e90335
commit 36d0e4b9d8
14 changed files with 643 additions and 7 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f1e62f1a88a64e12b75ffc2fcc9153f6
timeCreated: 1698986042

View File

@ -0,0 +1,127 @@
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Serialization;
public class FullBlurRPF : ScriptableRendererFeature
{
[Serializable] //序列化 会在ForwardRenderer下创建Blur时看到m_Data就是它
public class BlurData
{
// 模糊材质
public Material blurMat;
// 叠加材质
public Material overlayMat;
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
[Tooltip("渲染次数,次数越多越模糊,但是性能消耗也越大")]
[Range(0, 4)]
public int iterations = 3;
[Tooltip("采样间距X,值越大越模糊")]
[Range(0, 2f)]
public float blurSizeX = 0.5f;
[Tooltip("采样间距Y,值越大越模糊")]
[Range(0, 2f)]
public float blurSizeY = 0.5f;
[Tooltip("降采样,值越小越模糊,性能越好")]
[Range(0.1f, 1f)]
public float downScale = 0.5f;
[Tooltip("叠加颜色")]
public Color overlayColor;
}
public class BlurRenderPass : ScriptableRenderPass
{
private BlurData _data;
private int _buffer0Id;
private int _buffer1Id;
private int _blurSizeX = Shader.PropertyToID("_BlurSizeX");
private int _blurSizeY = Shader.PropertyToID("_BlurSizeY");
private int _overlayColor = Shader.PropertyToID("_OverlayColor");
public BlurRenderPass(BlurData data)
{
_data = data;
}
public void Setup()
{
_buffer0Id = Shader.PropertyToID("_BlurBuffer0");
_buffer1Id = Shader.PropertyToID("_BlurBuffer1");
}
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
var width = Mathf.FloorToInt(cameraTextureDescriptor.width * _data.downScale);
var height = Mathf.FloorToInt(cameraTextureDescriptor.height * _data.downScale);
cmd.GetTemporaryRT(_buffer0Id, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
cmd.GetTemporaryRT(_buffer1Id, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
}
public override void FrameCleanup(CommandBuffer cmd)
{
cmd.ReleaseTemporaryRT(_buffer0Id);
cmd.ReleaseTemporaryRT(_buffer1Id);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (renderingData.cameraData.camera != Camera.main) return; //只对主摄像机进行处理
var mat = _data.blurMat;
if (mat == null) return;
//renderingData.cameraData.camera 可获取摄像机判定哪个摄像机才进行处理渲染(每个摄像机都会进一遍pass 如果摄像机有多个且都有这个Blur(ScriptableRendererFeature资源)
CommandBuffer cmd = CommandBufferPool.Get("FullBlur"); //从池获取一个cmd 命名为MyBlurCmd 能在FrameDebugger看到
var cameraRt = renderingData.cameraData.renderer.cameraColorTarget; //获取摄像机主纹理
// 模糊
cmd.Blit(cameraRt, _buffer0Id);//将摄像机图渲染至buffer0
//开始iterations次渲染,每次都会进行2次Pass操作横向、纵向谁先谁后都无所谓
for (int i = 0; i < _data.iterations; i++)
{
//指定采样间距,逐级递增的形式
mat.SetFloat(_blurSizeX, 1.0f + i * _data.blurSizeX);
mat.SetFloat(_blurSizeY, 1.0f + i * _data.blurSizeY);
cmd.Blit(_buffer0Id, _buffer1Id, mat);
(_buffer0Id, _buffer1Id) = (_buffer1Id, _buffer0Id);
}
// 叠色
mat = _data.overlayMat;
if (mat == null) return;
mat.SetColor(_overlayColor, _data.overlayColor);
cmd.Blit(_buffer0Id, _buffer1Id, mat);
cmd.Blit(_buffer1Id, cameraRt); //将最终结果渲染到摄像机上
//执行cmd
context.ExecuteCommandBuffer(cmd);
//清空回收cmd
cmd.Clear();
CommandBufferPool.Release(cmd);
}
}
public BlurData data = new BlurData();
private BlurRenderPass _blurPass;
public override void Create()
{
_blurPass = new BlurRenderPass(data); //创建一个pass
_blurPass.renderPassEvent = data.renderPassEvent;
}
public override void AddRenderPasses(ScriptableRenderer renderer,
ref RenderingData renderingData)
{
_blurPass.Setup();
renderer.EnqueuePass(_blurPass); //入队渲染pass
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 685341af175240debcb555e29a642b4d
timeCreated: 1698986060

View File

@ -0,0 +1,59 @@
Shader "Hide/ColorOverlay"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Tags
{
"RenderType"="Opaque"
"RenderPipeline" = "UniversalPipeline"
}
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _OverlayColor;
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb = (_OverlayColor.rgb * _OverlayColor.a) + col.rgb * (1 - _OverlayColor.a);
return col;
}
ENDHLSL
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d3153a8c25114ea8862517db03c850e2
timeCreated: 1698989350

View File

@ -0,0 +1,122 @@
Shader "Hide/FullBlur"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags
{
"RenderType"="Opaque"
"RenderPipeline" = "UniversalPipeline"
}
LOD 100
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
sampler2D _MainTex;
half4 _MainTex_TexelSize;
float _BlurSizeX;
float _BlurSizeY;
//URP下没有appdata_img结构 要自定义
struct a2v
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
half2 uv[5] : TEXCOORD0;
};
//我们只需要appdata_img内置结构的数据传入即可有顶点、纹理坐标
v2f vertBlurVertical(a2v v) {
v2f o;
//o.pos = UnityObjectToClipPos(v.vertex); //CG
VertexPositionInputs vertexInputs = GetVertexPositionInputs(v.vertex.xyz);
o.pos = vertexInputs.positionCS;
half2 uv = v.texcoord;
//纵向的5个像素点纹理坐标
o.uv[0] = uv;
o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSizeY;
o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSizeY;
o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSizeY;
o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSizeY;
return o;
}
v2f vertBlurHorizontal(a2v v) {
v2f o;
//o.pos = UnityObjectToClipPos(v.vertex); //CG
VertexPositionInputs vertexInputs = GetVertexPositionInputs(v.vertex.xyz);
o.pos = vertexInputs.positionCS;
half2 uv = v.texcoord;
//横向的5个像素点纹理坐标
o.uv[0] = uv;
o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSizeX;
o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSizeX;
o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSizeX;
o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSizeX;
return o;
}
//无论是纵向还是横向,它们都会使用这个片元着色器,处理手法一样
half4 fragBlur(v2f i) : SV_Target{
float weight[3] = {0.4026, 0.2442, 0.0545};
//采样RGB然后进行乘以对应的权重累加到sum
half3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
for (int it = 1; it < 3; it++) {
sum += tex2D(_MainTex, i.uv[it * 2 - 1]).rgb * weight[it];
sum += tex2D(_MainTex, i.uv[it * 2]).rgb * weight[it];
}
//是的这样就完成了,模糊。。。。
return half4(sum, 1.0);
}
ENDHLSL
//上面都是INCLUDE内容即下面Pass都可使用的内容
//标配写法
ZTest Always Cull Off ZWrite Off
//第一个PASS,纵向模糊处理
Pass
{
NAME "GAUSSIAN_BLUR_VERTICAL"
//CGPROGRAM //CG
HLSLPROGRAM
//纵向的顶点着色器
#pragma vertex vertBlurVertical
//片元着色器
#pragma fragment fragBlur
//ENDCG //CG
ENDHLSL
}
//第二个Pass 横向模糊处理
Pass
{
NAME "GAUSSIAN_BLUR_HORIZONTAL"
//CGPROGRAM
HLSLPROGRAM
//横向的顶点着色器
#pragma vertex vertBlurHorizontal
//片元着色器
#pragma fragment fragBlur
//ENDCG
ENDHLSL
}
}//完成SubShader
Fallback Off
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e21cf96664224c9f9f88e6835a1beb57
timeCreated: 1698986814

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cf55c1e8160a5fa46b4003f046682f53
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9bf8decd901a98d40a5590532bb5e84b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,133 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: mat_fullblur
m_Shader: {fileID: 4800000, guid: e21cf96664224c9f9f88e6835a1beb57, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 0
- _BlendModePreserveSpecular: 1
- _BlurSize: 1.6
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _DstBlendAlpha: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SrcBlendAlpha: 1
- _Surface: 0
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []
--- !u!114 &8979992128057803927
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 7

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fe052cf893ff1924a86ecdcc48d2d9cb
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,132 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &-7827177799749252253
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 7
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: mat_overlay
m_Shader: {fileID: 4800000, guid: d3153a8c25114ea8862517db03c850e2, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 0
- _BlendModePreserveSpecular: 1
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _DstBlendAlpha: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SrcBlendAlpha: 1
- _Surface: 0
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 11d26a0972c4266449c4e9fbc81da631
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -19,7 +19,8 @@ MonoBehaviour:
m_RendererFeatures:
- {fileID: 5252115105658233815}
- {fileID: 4752922562742531071}
m_RendererFeatureMap: d7e33898dc42e348ff333585e3c5f541
- {fileID: 4160655972380288011}
m_RendererFeatureMap: d7e33898dc42e348ff333585e3c5f5410ba45512909ebd39
m_UseNativeRenderPass: 0
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
xrSystemData: {fileID: 11400000, guid: 60e1133243b97e347b653163a8c01b64, type: 2}
@ -40,6 +41,8 @@ MonoBehaviour:
type: 3}
objectMotionVector: {fileID: 4800000, guid: 7b3ede40266cd49a395def176e1bc486,
type: 3}
dataDrivenLensFlare: {fileID: 4800000, guid: 6cda457ac28612740adb23da5d39ea92,
type: 3}
m_AssetVersion: 2
m_OpaqueLayerMask:
serializedVersion: 2
@ -60,6 +63,28 @@ MonoBehaviour:
m_CopyDepthMode: 0
m_AccurateGbufferNormals: 0
m_IntermediateTextureMode: 1
--- !u!114 &4160655972380288011
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 685341af175240debcb555e29a642b4d, type: 3}
m_Name: FullBlurRPF
m_EditorClassIdentifier:
m_Active: 1
data:
blurMat: {fileID: 2100000, guid: fe052cf893ff1924a86ecdcc48d2d9cb, type: 2}
overlayMat: {fileID: 2100000, guid: 11d26a0972c4266449c4e9fbc81da631, type: 2}
renderPassEvent: 600
iterations: 1
blurSizeX: 0.41
blurSizeY: 0.42
downScale: 0.5
overlayColor: {r: 0, g: 0, b: 0, a: 0}
--- !u!114 &4752922562742531071
MonoBehaviour:
m_ObjectHideFlags: 0
@ -88,9 +113,3 @@ MonoBehaviour:
m_Active: 1
settings:
renderPassEvent: 550
QueueMin: 2000
QueueMax: 2500
m_UseShader: {fileID: 4800000, guid: 71bde3c64bec9944bb3ad01d1cd79379, type: 3}
layer:
serializedVersion: 2
m_Bits: 0