Unity的OnImageRender的实现原理解析

https://gameinstitute.qq.com/community/detail/112744

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class TestCommandBuffer : MonoBehaviour
{
    public RenderTexture rt;
    public Camera m_camera;
    public GameObject cube;
    public Shader shader;
    CommandBuffer cb ;

    Mesh quad;
    void Start()
    {
        cb = new CommandBuffer();
        cb.name = "testCb";
        rt = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 24);
        cb.SetRenderTarget(rt);
        cb.ClearRenderTarget(false, true, Color.red);
        MeshFilter mf = cube.GetComponent<MeshFilter>();
        Material mat = new Material(shader);
        cb.DrawMesh(mf.mesh, Matrix4x4.identity, mat);
        //Graphics.ExecuteCommandBuffer(cb);
        m_camera.AddCommandBuffer(CameraEvent.AfterImageEffects, cb);
        Graphics.Blit(rt, m_camera.targetTexture);

        quad = new Mesh();
        Vector3[] vertices = new Vector3[4];
        vertices[0] = m_camera.ScreenToWorldPoint(new Vector3(0, 0, 10));
        vertices[1] = m_camera.ScreenToWorldPoint(new Vector3(0, m_camera.pixelHeight, 10));
        vertices[2] = m_camera.ScreenToWorldPoint(new Vector3(m_camera.pixelWidth, m_camera.pixelHeight, 10));
        vertices[3] = m_camera.ScreenToWorldPoint(new Vector3(m_camera.pixelWidth, 0, 10));

        Vector2[] uvs = new Vector2[4];
        uvs[0] = new Vector2(0, 0);
        uvs[1] = new Vector2(0, 1);
        uvs[2] = new Vector2(1, 1);
        uvs[3] = new Vector2(1, 0);
        quad.vertices = vertices;
        quad.uv = uvs;

    }

    private void Update()
    {
        CommandBuffer cb2 = new CommandBuffer();
        Material mat = new Material(shader);
        mat.SetTexture("_MainTex", rt);
        cb2.DrawMesh(quad, Matrix4x4.identity, mat);
    }
}

Unity的OnImageRender的实现原理解析Unity的OnImageRender的实现原理解析 wodownload2 发布了610 篇原创文章 · 获赞 96 · 访问量 33万+ 他的留言板 关注
上一篇:Egg上层框架CabloyJS是如何输出SQL语句日志的?


下一篇:红色小恐龙团队--冲刺DAY4