Requirement : The texture generated by my own renderer is A, and the texture generated by the virtual camera in Unity is B. Normally, what I see in the glasses is B. I want to replace B with A directly.
Understand the life cycle of Unity rendering scripts:
For our needs, the key is the content of Scene Rendering . What we have to do is to change OnRenderImage
the content, cut it off, and prevent it from transferring the original texture content to the next step.
solution:
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
//获取自己写的Main脚本中的buffer数据
byte[] Texbuffer = root.GetComponent<Main>().buffer;
Texture2D tex2d = new Texture2D(width, height, TextureFormat.RGBA32, false);
tex2d.LoadRawTextureData(Texbuffer);
tex2d.Apply();
RenderTexture out_renderTexture = new RenderTexture(width, height, 0);
//将buffer数据替换到相机最终的显示纹理
RenderTexture.active = out_renderTexture;
Graphics.Blit(tex2d, out_renderTexture);
Graphics.Blit(out_renderTexture, dest);
}