I’ve been trying for ages but I can’t seem to get Camera.Render() to work.
All I get is a completely grey texture :(((
RGBA(0.804, 0.804, 0.804, 0.804)
So the setup is super simple, I have a second camera (disabled as it is suggested in the documentation)
which looks at a sphere and has a render texture assigned. There is a plane which should now get the
rendered image as a texture.
The script is the following:
using UnityEngine;
using System.Collections;
public class RenderTest : MonoBehaviour {
public Camera textureRenderCamera;
public GameObject plane;
public Texture2D CreateTexture() {
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = textureRenderCamera.targetTexture;
Debug.Log("Rendertexture: "+ textureRenderCamera.targetTexture);
Debug.Log("width/height:" + textureRenderCamera.targetTexture.width + " " + textureRenderCamera.targetTexture.height);
textureRenderCamera.Render();
Texture2D image = new Texture2D(textureRenderCamera.targetTexture.width, textureRenderCamera.targetTexture.height);
image.ReadPixels(new Rect(0, 0, textureRenderCamera.targetTexture.width, textureRenderCamera.targetTexture.height), 0, 0);
//for (int i=0;i<textureRenderCamera.targetTexture.width;i++)
// for (int j=0; j<textureRenderCamera.targetTexture.height; j++)
// image.SetPixel(i,j, Color.green);
//image.Apply(true);
Debug.Log (image.GetPixel(128,128));
RenderTexture.active = currentRT;
return image;
}
public void UpdateTexture()
{
Texture2D tex = CreateTexture();
plane.renderer.material.mainTexture = tex;
}
// Update is called once per frame
void Update () {
if(Input.anyKey)
{
UpdateTexture();
}
}
}
If I assign the render texture to my plane again manually, it works fine.
If I set all the pixels to green in the script it also works (I feared the texture data might be getting lost somewhere…)
Maybe it’s a silly mistake but I just can’t find it. I would at least suspect the texture to become the clear color … but all i get is perfect grey.
Any ideas? Please…help
Thanks in advance,
seb