Camera.targetTexture和SetTargetBuffers的区别

知识点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);

发布了616 篇原创文章 · 获赞 96 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/104424005
今日推荐