使用CommandBuffer实现描边效果
1.什么是CommandBuffer
2.C#脚本常用命令
3.描边实现思路
1.什么是CommandBuffer
上图是摄像机渲染的两个路径,也是大多数引擎的常用法宝。这两种通用的方法,能很好的解决常会物体的渲染的光影效果。但一些特效会需要在次基础上进行改动。CommandBuffer可以在上图任意的绿点插入式渲染。
比如屁股中黑百合的大招。透视的看见敌人。但常规的渲染,在深度测试时,这些物体人物将会被剔除。
为了解决次问题常规的方法,是设置一个和MainCamera参数大致相同的摄像机,用材质覆盖的方法去渲染RT;
然后将RT通过后期处理的方式,叠在MainCamera的ColorBuffer中。
但此过程过于麻烦,CommandBuffer脚本很好的简化了此过程;
2.CommandBuffer类的常规操作
//声明
CommandBuffer commandBuffer = new CommandBuffer();
//设置渲染目标
commandBuffer.SetRenderTarget(renderTexture);
commandBuffer.ClearRenderTarget(true,true,Color.black);
//设置渲染数据
commandBuffer.DrawRenderer( targetRenderer,material);
//将commandBuffer的渲染结果进行后期处理效果
commandBuffer.Bilt(renderTexture,DestTexture,material);
//向主camera中插入CommandBuffer
commandBuffer.AddCommandBuffer(CameraEvent.BeforeForwardOpaque,commandBuffer);
//用Graphics的方法调用绘制
Graphics.ExecuteCommandBuffer(CommandBuffer);
3.描边实现思路
1.将人物渲染乘纯色到一个RT
2.进行多步的高斯模糊,再将RT2的颜色减去RT1中的颜色
3.将最后的RT叠通过后期处理叠在最终图片上
脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
[ExecuteInEditMode]
public class CommendBuffer : MonoBehaviour
{
//obj content
private CommandBuffer commandBuffer=null;
public Material renderMat=null;
private RenderTexture renderTex=null;
public Material effectMat=null;
private Renderer targetEBO=null;
public GameObject targetOBJ=null;
//value set
public Color outLineColor=Color.black; //renderMat
public int outLineSize = 4;
public int BlurSize=3;
// Start is called before the first frame update
void Start()
{
if(renderMat&&targetOBJ!=null){
//data
targetEBO = targetOBJ.GetComponent<Renderer>();
commandBuffer = new CommandBuffer();
renderTex = RenderTexture.GetTemporary(Screen.width , Screen.height , 0);
commandBuffer.SetRenderTarget(renderTex);
commandBuffer.ClearRenderTarget(true,true,Color.black);
commandBuffer.DrawRenderer(targetEBO,renderMat);
}else
{
enabled=false;
}
}
private void OnEnable() {
if (renderTex)
{
RenderTexture.ReleaseTemporary(renderTex);
renderTex = null;
}
if (commandBuffer != null)
{
commandBuffer.Release();
commandBuffer = null;
}
}
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
if(renderMat&&renderTex&&commandBuffer!=null){
//render commandBuffer
renderMat.SetColor("_outLineColor",outLineColor);
Graphics.ExecuteCommandBuffer(commandBuffer);
//声明用来模糊的RT
RenderTexture temp1 = RenderTexture.GetTemporary(src.width,src.width,0);
RenderTexture temp2 = RenderTexture.GetTemporary(src.width,src.width,0);
effectMat.SetInt("_outLineSize",outLineSize);
//先进行一次模糊,因为无法直接用循环叠加commandBuffer
Graphics.Blit(renderTex,temp1,effectMat,0);
Graphics.Blit(temp1,temp2,effectMat,1);
//设置模糊次数
for (int i = 0; i < BlurSize; i++)
{
Graphics.Blit(temp2,temp1,effectMat,0);
Graphics.Blit(temp1,temp2,effectMat,1);
}
//将模糊后的图片减去commandBuffer中的实心剪影
effectMat.SetTexture("_renderTex",renderTex);
Graphics.Blit(temp2,temp1,effectMat,2);
//后期处理,叠入渲染成果
effectMat.SetTexture("_outLineTex",temp1);
Graphics.Blit(src,dest,effectMat,3);
//释放RT
RenderTexture.ReleaseTemporary(temp1);
RenderTexture.ReleaseTemporary(temp2);
}else
{
Graphics.Blit(src,dest);
}
}
}
shader = pss0 pass1(横向、纵向高斯模糊) + pass2(减法)+ pass3(叠加)
commandBuffer弄纯色的shader太简单就不写了
Shader "LZ/effectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_outLineSize("outLineSize",int)=4
_outLineTex("outLineTex",2D)="black"{}
_renderTex("renderTex",2D)="black"{}
}
//Founction
CGINCLUDE
float _outLineSize;
sampler2D _MainTex;
float4 _MainTex_TexelSize;
struct a2v{
float4 vertex:POSITION;
float2 uv:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float2 uv[5]:TEXCOORD0;
};
v2f vert_heng(a2v v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float2 uv=v.uv;
o.uv[0] = uv;
o.uv[1] = uv + float2(1,0) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[2] = uv + float2(-1,0) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[3] = uv + float2(2,0) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[4] = uv + float2(-2,0) * _outLineSize * _MainTex_TexelSize.xy;
return o;
}
v2f vert_shu(a2v v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float2 uv=v.uv;
o.uv[0] = uv;
o.uv[1] = uv + float2(0,1) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[2] = uv + float2(0,-1) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[3] = uv + float2(0,2) * _outLineSize * _MainTex_TexelSize.xy;
o.uv[4] = uv + float2(0,-2) * _outLineSize * _MainTex_TexelSize.xy;
return o;
}
fixed4 frag(v2f i):SV_TARGET{
float3 col = tex2D(_MainTex,i.uv[0]).xyz *0.4026;
float3 col1 = tex2D(_MainTex,i.uv[1]).xyz *0.2442;
float3 col2 = tex2D(_MainTex,i.uv[2]).xyz *0.2442;
float3 col3 = tex2D(_MainTex,i.uv[3]).xyz *0.0545;
float3 col4 = tex2D(_MainTex,i.uv[4]).xyz *0.0545;
float3 finalCol = col+col1+col2+col3+col4;
return fixed4(finalCol,1.0);
}
ENDCG
SubShader
{
Cull Off ZWrite Off ZTest Always
pass{
CGPROGRAM
#include"UnityCG.cginc"
#pragma vertex vert_heng
#pragma fragment frag
ENDCG
}
pass{
CGPROGRAM
#include"UnityCG.cginc"
#pragma vertex vert_shu
#pragma fragment frag
ENDCG
}
//pass 2 ---renderTex------------------------------------------------------------------------------
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f1
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f1 vert (appdata v)
{
v2f1 o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
//sampler2D _MainTex;
sampler2D _renderTex;
fixed4 frag (v2f1 i) : SV_Target
{
float3 col= tex2D(_MainTex,i.uv).xyz;
float3 commandCol=tex2D(_renderTex,i.uv).xyz;
float3 finalCol=col-commandCol;
return fixed4(finalCol,1.0);
}
ENDCG
}
//pass3 add outlineTex--------------------------------------------------------------------------------------------------
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f2
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f2 vert (appdata v)
{
v2f2 o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
//sampler2D _MainTex;
sampler2D _outLineTex;
fixed4 frag (v2f2 i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 lineCol=tex2D(_outLineTex,i.uv);
col.xyz+=lineCol.xyz;
return col;
}
ENDCG
}
}
}