Search Unity

How to get modified texture using shader, from material

Discussion in 'Shaders' started by roadrage, Aug 1, 2019.

  1. roadrage

    roadrage

    Joined:
    Jul 24, 2018
    Posts:
    1
    Hi,

    I am creating a scribbling tool. I have created all the functionality and written code in c#. Now I want to use the shader for that. I am having a problem with fetching modified texture from the material. It is the same I gave to the material.

    void Start()
    {
    tex = new Texture2D(width,height,DefaultFormat.LDR,TextureCreationFlags.None);
    Sprite sprite = Sprite.Create(tex,new Rect(0,0,width,height),new Vector2(0.5f,0.5f));
    this.transform.GetChild(0).GetComponent<Image>().sprite = sprite;
    scribbleMat = this.transform.GetChild(0).GetComponent<Image>().material;
    scribbleMat.SetTexture("_MainTex",tex);

    }

    void Update()
    {
    if (Input.GetMouseButton(0))
    {
    Vector2 vec = getPoints(v,Input.mousePosition);
    scribbleMat.SetVector("_PenPos",new Vector4(vec.x,vec.y,0,0));
    Sprite s = this.GetComponent<Image>().sprite;
    Texture2D texture = (Texture2D)scribbleMat.GetTexture("_MainTex");
    s = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),s.pivot);
    this.GetComponent<Image>().sprite = s;
    // Color32[] c = ((Texture2D)scribbleMat.GetTexture("_MainTex")).GetPixels32();
    // tex.SetPixels32(c);
    // tex.Apply();

    }
    }
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    278
    You should use render texture and something like "Blit" or "DrawMeshNow" to make rendering happen.