Search Unity

Graphics.Blit is not working

Discussion in 'Scripting' started by namba02250, May 28, 2015.

  1. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    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):
    1.  
    2. deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture;
    3.  
    4.         Graphics.Blit (deTex, render, croma);
    5.  
    6.         RenderTexture.active = render;
    7.         paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0);
    8.  
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Where and how are you defining the "render" variable?
     
  3. namba02250

    namba02250

    Joined:
    Feb 6, 2015
    Posts:
    19
    Code (csharp):
    1.  
    2. Texture2D paraSprite;
    3.     public GameObject tex;
    4.     RenderTexture render;
    5.     Texture2D deTex;
    6.     Sprite fotoTomada ;
    7.     public Material croma;
    8.     public GameObject fotico;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         fotoTomada = new Sprite ();
    14.  
    15.         paraSprite = new Texture2D(Screen.width, Screen.height);
    16.  
    17.         deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture;
    18.  
    19.         Graphics.Blit (deTex, render, croma);
    20.  
    21.         RenderTexture.active = render;
    22.         paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0);
    23.  
    24.         paraSprite.Apply();
    25.  
    26.         fotoTomada = Sprite.Create (paraSprite, new Rect (0, 0, paraSprite.width, paraSprite.height),new Vector2(0.5f,0.5f));
    27.  
    28.         fotico.AddComponent("SpriteRenderer");
    29.         fotico.GetComponent<SpriteRenderer>().sprite = fotoTomada;
    30.  
    31.     }
    32.  
    i think that Graphics.Blit is not changing my RenderTexture, im using a surface shader
     
    Last edited: May 28, 2015
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BlitTest2 : MonoBehaviour {
    5.  
    6.     Texture2D paraSprite;
    7.     public GameObject tex;
    8.     RenderTexture render;
    9.     Texture2D deTex;
    10.     Sprite fotoTomada ;
    11.     public Material croma;
    12.     public GameObject fotico;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.      
    18.         fotoTomada = new Sprite ();
    19.      
    20.         paraSprite = new Texture2D(Screen.width, Screen.height);
    21.      
    22.         deTex = tex.GetComponent<SpriteRenderer> ().sprite.texture;
    23.      
    24.         Graphics.Blit (deTex, render, croma);
    25.         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.
    26.  
    27.         RenderTexture.active = render;
    28.         paraSprite.ReadPixels(new Rect(0, 0, render.width, render.height), 0, 0);
    29.      
    30.         paraSprite.Apply();
    31.      
    32.         fotoTomada = Sprite.Create (paraSprite, new Rect (0, 0, paraSprite.width, paraSprite.height),new Vector2(0.5f,0.5f));
    33.      
    34.         fotico.AddComponent<SpriteRenderer>();
    35.         fotico.GetComponent<SpriteRenderer>().sprite = fotoTomada;
    36.      
    37.     }
    38. }
     
    namba02250 likes this.
  5. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    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