知识点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
来源:CSDN
作者:wodownload2
链接:https://blog.csdn.net/wodownload2/article/details/104424005