Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tiling RenderTexture on Material Created From Script

Discussion in 'Scripting' started by ShadyGuy, Nov 16, 2015.

  1. ShadyGuy

    ShadyGuy

    Joined:
    Oct 18, 2015
    Posts:
    13
    Code (csharp):
    1. RenderTexture rt = new RenderTexture(500, 500, 24);
    2. rt.filterMode = FilterMode.Trilinear;
    3. rt.anisoLevel = 9;
    4. rt.antiAliasing = 8;
    5. rt.wrapMode = TextureWrapMode.Repeat;
    6.  
    7. cam.targetTexture = rt;
    8.  
    9. Material mat = new Material(Shader.Find("Sprites/Default"));
    10. mat.mainTextureScale = new Vector2(0.5f, 0.5f);
    11. mat.mainTexture = rt;
    12.  
    13. go.GetComponent<MeshRenderer>().sharedMaterial = mat;
    cam is a Camera that is rendering to the RenderTexture.
    go is a blank Quad GameObject created from within the Unity Editor (create > 3d object > quad).

    My problem is that setting mainTextureScale does absolutely nothing no matter what values I put into the Vector2. The texture rendered via the Camera appears like it should on the Quad but it always fills the entire Quad.

    What am I doing wrong?
     
  2. ShadyGuy

    ShadyGuy

    Joined:
    Oct 18, 2015
    Posts:
    13
    Figured it out, the "Sprites/Default" shader simply doesn't support tiling (what tricked me is that the tiling/offset options are shown in the Unity Editor, but if you try to modify them they simply reset to 1 or 0).

    Edit: Seems like you can't even select a Texture for the "Sprites/Default" or "Sprites/Diffuse" shaders inside the Unity Editor. I guess those are only intended to be used with SpriteRenderer? Might be a good idea for me to use "Unlit/Texture" instead.
     
    Last edited: Nov 16, 2015