Home > Software design >  How to copy destination to source on OnRenderImage
How to copy destination to source on OnRenderImage

Time:09-28

I'm a complete Unity Newbie, here.

I have the following script attached to the MainCamara of my app:

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

public class CameraRenderToImage : MonoBehaviour
{

    private int counter;

    public CameraRenderToImage(){
        counter = 0;
    }

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        Debug.Log("Render finalized "   counter);
        counter  ;        
        //Graphics.CopyTexture(src, dest);
        dest = src;
    }

}

I need access to the finalized renderized image in src, but I don't want to actually modify the output. Just by adding this function the output of my screen (and Headset as this is using VR) goes black.

If I comment the function it works again.

I'm mostly sure that what is going on is that the source texture is not getting properly copied to the destination texture.

What I tried to do is to make equal (I assume this does not work becuase they are the equivalent of pointers) and use Graphics.CopyTexture which gives the following error:

Graphics.CopyTexture can only copy between same texture format groups (d3d11 base formats: src=9 dst=27)

Can anyone help me out? All I want is to make dest equal to source.

CodePudding user response:

Note that this

dest = src;

does absolutely nothing. I know what you expected this to do but all it does is overwriting the reference stored in dest locally. It doesn't affect the previously passed in dest at all.

You assume correctly about the black screen. As soon as you implement OnRenderImage you are responsible for assuring that something is rendered into dest. You are not doing that / the error prevents it from happening.

Usually for this you rather use Graphics.Blit which doesn't underlay the format matching limitation

Graphics.Blit(src, dest);
  • Related