Search code examples
c#unity-game-enginerender-to-texture

Load and set Render Texture


I have got CamFeed.rendertexture and CamFeed.mat files. Im loading them using my script and setting it up to the camera as target texture and material to another quad respectively but it just shows a black screen.

Material newMat = Resources.Load("CamFeed", typeof(Material)) as Material;
RenderTexture rendertexture = Resources.Load("CamFeed", typeof(Material)) as RenderTexture;

FirstPersonCamera.targetTexture = rendertexture;
quad.GetComponent<Renderer>().material = newMat;

How can I be able to sort it out?


Solution

  • Well, firstly, why are you converting a typeof(Material) into RenderTexture? You should have one of each in your Resources folder, named distinctively, so that you instantiate them the following way:

    Material newMat = Resources.Load<Material>("CamFeedMaterial");
    RenderTexture rendertexture = Resources.Load<RenderTexture>("CamFeedTexture");
    

    Also make sure that your material uses a shader such as Unlit\Texture, since you are going to need to attach the RenderTexture to it, either in the Editor or from code:

    newMat.mainTexture = rendertexture;
    

    Finally, your last bit of code stays the same:

    FirstPersonCamera.targetTexture = rendertexture;
    quad.GetComponent<Renderer>().material = newMat;