Unity CommandBuffer 渲染指定的对象

参考
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"
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值