参考
https://lindenreid.wordpress.com/2018/09/13/using-command-buffers-in-unity-selective-bloom/
https://www.jianshu.com/p/04cf0e348179
ssao也可以这样做,可以用DrawRenderer单独渲染一个Mask,然后Lerp一下就好了
Blit 的第一个参数其实就是赋值到MainTex上
关于深度图 https://www.jianshu.com/p/80a932d1f11e
https://blog.csdn.net/puppet_master/article/details/72669977
源码:这里 Github source
主要用到了
m_GlowBuffer.DrawRenderer(r, glowMat);
这个函数可以指定对象绘制
注意 forward 和 deferred 这两个渲染方式的设置,会影响到 command buffer的渲染顺序。?
其中 main camera可以看到已经加的 commandbuffer,算是一个查看有没有添加成功的小技巧吧✌
解释
这里的m_PrePass其实就是glowMaterial渲染的图片。
注意哦
var aoRT = RenderTexture.GetTemporary(source.width, source.height, 0);
别忘记初始化RenderTexture,否则会导致全屏灰色。
还有,OnWillRenderObject所挂载的Object必须要有一个RenderComponent
这里需要注意精度问题 精度太大,会造成遮挡不住的问题
透明物体写入深度
在depthTex之后写入透明物体深度就行
多执行一次shadowcast
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
[ExecuteAlways]
public class DepthTranparentCommandBuffer : MonoBehaviour
{
private CommandBuffer m_CommandBuffer;
private Camera m_MainCamera;
private void Cleanup()
{
if ( m_MainCamera!=null && m_CommandBuffer!=null)
{
m_MainCamera.RemoveCommandBuffer(CameraEvent.AfterDepthTexture, m_CommandBuffer);
}
if (m_CommandBuffer != null)
{
m_CommandBuffer.Clear();
}
}
public void OnDisable()
{
Cleanup();
}
public void OnEnable()
{
m_MainCamera = Camera.main;
AddCmdBuffer();
}
void AddCmdBuffer()
{
Debug.Log(this.name + "======= 开始透明物体渲染深度");
var render = gameObject.activeInHierarchy && enabled;
if (!render)
{
Cleanup();
return;
}
Debug.Log(this.name + "======= s's's");
// create new command buffer
m_CommandBuffer = new CommandBuffer();
m_CommandBuffer.name = "==== Depth Transparent Prepass";
var glowSystem = CustomDepthSystem.Instance;
// draw all AO objects to it
foreach (var obj in glowSystem.GlowObjs)
{
Renderer r = obj.GetComponent<Renderer>();
Mesh mesh = null;
if (r.gameObject.GetComponent<SkinnedMeshRenderer>())
{
mesh = (r as SkinnedMeshRenderer).sharedMesh;
}
else if (r.gameObject.GetComponent<MeshFilter>())
{
mesh = r.gameObject.GetComponent<MeshFilter>().sharedMesh;
}
if (mesh)
{
for (int i = 0; i < mesh.subMeshCount; i++)
{
m_CommandBuffer.DrawRenderer(r, obj.glowMaterial, i);
Debug.Log(this.name + " Draw Depth " + obj.name + " " + i);
}
}
}
// add this command buffer to the pipeline
m_MainCamera.AddCommandBuffer(CameraEvent.AfterDepthTexture, m_CommandBuffer);
}
}
Shader "Lit/DepthWrite"
{
Properties
{
[NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// shadow casting support
UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
}
}