im trying to apply a shader affect to a texture and save it in other texture, but when i try to use Graphics.Blit the RenderTexture is null. Code (csharp): deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture; Graphics.Blit (deTex, render, croma); RenderTexture.active = render; paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0);
Code (csharp): Texture2D paraSprite; public GameObject tex; RenderTexture render; Texture2D deTex; Sprite fotoTomada ; public Material croma; public GameObject fotico; // Use this for initialization void Start () { fotoTomada = new Sprite (); paraSprite = new Texture2D(Screen.width, Screen.height); deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture; Graphics.Blit (deTex, render, croma); RenderTexture.active = render; paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0); paraSprite.Apply(); fotoTomada = Sprite.Create (paraSprite, new Rect (0, 0, paraSprite.width, paraSprite.height),new Vector2(0.5f,0.5f)); fotico.AddComponent("SpriteRenderer"); fotico.GetComponent<SpriteRenderer>().sprite = fotoTomada; } i think that Graphics.Blit is not changing my RenderTexture, im using a surface shader
OK, you've create a non-public variable (render) to hold a render texture, but I still don't see anywhere that you're actually defining where this render texture is, so it's not really being defined and is giving you a null reference error... If you've already got the render texture created. make render a public variable and assign it through the Inspector, or you can leave the variable like it is and use GetComponent to access it. If the render texture doesn't yet exist, you could also try something like the following to have it created on the fly at runtime: Code (CSharp): using UnityEngine; using System.Collections; public class BlitTest2 : MonoBehaviour { Texture2D paraSprite; public GameObject tex; RenderTexture render; Texture2D deTex; Sprite fotoTomada ; public Material croma; public GameObject fotico; // Use this for initialization void Start () { fotoTomada = new Sprite (); paraSprite = new Texture2D(Screen.width, Screen.height); deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture; Graphics.Blit (deTex, render, croma); render = new RenderTexture (paraSprite.width, paraSprite.height, 10); // creates a new RenderTexture based on paraSprite's width and height, with a depth of 10. Change these values as necessary to suit your needs. RenderTexture.active = render; paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0); paraSprite.Apply(); fotoTomada = Sprite.Create (paraSprite, new Rect (0, 0, paraSprite.width, paraSprite.height),new Vector2(0.5f,0.5f)); fotico.AddComponent<SpriteRenderer>(); fotico.GetComponent<SpriteRenderer>().sprite = fotoTomada; } }
I tested, and Graphics.Blit only work completely with Unlit and Sprite shaders. With Standard shader work in pass 3, but only color, no apply normals, metallic, etc.. maps. You can see How to use Graphics.Blit: Draw-Modify texture by GPU