Camera.targetTexture和SetTargetBuffers的区别

只愿长相守 提交于 2020-02-21 14:15:25

知识点1:Camera.targetTexture

https://docs.unity3d.com/ScriptReference/Camera-targetTexture.html
public RenderTexture targetTexture;

usually cameras render directly to screen, but for some effects it is useful to make a camera render into a texture.
this is done by creating a RenderTexture object and setting it as targetTexture on the camera. the camera will then render into that texture.

when targetTexture is null, camera renders to screen.
when rendering into a texture, the camera always renders into the whole texture;
it is also possible to make camera render into separate RenderBuffers, or into multiple textures at once, using SetTargetBuffers function.

知识点2:Camera.ImageEffects

在这里插入图片描述
这里会有一个Camera.ImageEffects,这是后处理效果。我猜测是OnRenderImage的方法调用。
这是将TempBuffer中的内容拷贝到BackBuffer的过程。
而RenderTexture.ResolveAA则是因为相机开启了抗锯齿。如果我们把它关闭了,则不会有这行了。
在这里插入图片描述

知识点3:Camera.targetTexture和SetTargetBuffers的区别

如果我们使用下面的代码:

using UnityEngine;

public class SetTargetBuffers : MonoBehaviour
{
    public Camera m_camera;
    public RenderTexture rt;
    void Start()
    {
        
        rt = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 0);
        rt.name = "xxx";
        m_camera.targetTexture = rt;
        //m_camera.SetTargetBuffers(rt.colorBuffer, rt.depthBuffer);
    }

    private void OnPostRender()
    {
        Debug.LogError(RenderTexture.active);
    }
}

在这里插入图片描述
而如果使用这样的代码:

using UnityEngine;

public class SetTargetBuffers : MonoBehaviour
{
    public Camera m_camera;
    public RenderTexture rt;
    void Start()
    {
        
        rt = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 0);
        rt.name = "xxx";
        //m_camera.targetTexture = rt;
        m_camera.SetTargetBuffers(rt.colorBuffer, rt.depthBuffer);
    }

    private void OnPostRender()
    {
        Debug.LogError(RenderTexture.active);
    }
}

在这里插入图片描述
后者没有了ImageEffects的调用了。

知识点4:RenderTexture.active

https://docs.unity3d.com/ScriptReference/RenderTexture-active.html
all rendering goes into the active RenderTexture. if the active RenderTexture is null everything is rendered in the main window.
setting the RenderTexture.active is the same as calling Graphics.SetRenderTarget.

将从render texture中读取数据:

using UnityEngine;
using System.Collections;

// Get the contents of a RenderTexture into a Texture2D
public class ExampleClass : MonoBehaviour
{
    static public Texture2D GetRTPixels(RenderTexture rt)
    {
        // Remember currently active render texture
        RenderTexture currentActiveRT = RenderTexture.active;
        // Set the supplied RenderTexture as the active one
        RenderTexture.active = rt;
        // Create a new Texture2D and read the RenderTexture image into it
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);

        // Restorie previously active render texture
        RenderTexture.active = currentActiveRT;
        return tex;
    }
}

摄像机的截图:
https://answers.unity.com/questions/27968/getpixels-of-rendertexture.html

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
 // ofc you probably don't have a class that is called CameraController :P
 Camera activeCamera = CameraController.getActiveCamera();
 // Initialize and render
 RenderTexture rt = new RenderTexture(width, height, 24);
 activeCamera.targetTexture = rt;
 activeCamera.Render();
 RenderTexture.active = rt;

 // Read pixels
 tex.ReadPixels(rectReadPicture, 0, 0);

 // Clean up
 activeCamera.targetTexture = null;
 RenderTexture.active = null; // added to avoid errors 
 DestroyImmediate(rt);

知识点5:Graphics.Blit函数

https://light11.hatenadiary.com/entry/2018/04/05/195745
https://docs.unity3d.com/ScriptReference/Graphics.Blit.html

举例1:

using UnityEngine;

public class SetTargetBuffers : MonoBehaviour
{
    public Camera m_camera;
    public RenderTexture rt;
    void Start()
    {
        rt = new RenderTexture(m_camera.pixelWidth, m_camera.pixelHeight, 0);
        rt.name = "xxx";
    }

    private void OnPostRender()
    {
        Graphics.Blit(null, rt);
    }
}

首先,我们看帧调试器:

在这里插入图片描述
这里多了个Grab RenderTexture。其实就是上面的blit的操作导致的。这里可以看到拷贝了RT0,还有一个深度:
在这里插入图片描述
这里注意的是,要将球体的位置,移动到0到1之间,这样好方便看到深度信息,如上图。
具体的项目源码在:https://gitee.com/yichichunshui/CommandBufferBlur.git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!