Search Unity

Question Rotation of a tiled texture

Discussion in 'Shader Graph' started by XenoBits, May 28, 2019.

  1. XenoBits

    XenoBits

    Joined:
    Aug 28, 2014
    Posts:
    27
    Hello,

    I'm using a tileset as a texture and would like to be able to rotate the tile I've setup.

    I've been able to do it directly in shader graph but it does not work at runtime as I couldn't find a way to retrieve the UV offset and tiling.

    Here is my sample tileset texture

    The graph I ended up with

    And a simplified version of the source code I'm using to generate a tile:
    Code (CSharp):
    1.  
    2.     const int TileSetSize = 4;
    3.     const float TileRatio = 1 / (float)TileSetSize;
    4.     private void SetupQuad()
    5.     {
    6.         //Generate quad
    7.         //Set vertices
    8.         var l_quadVertices = new List<Vector3>();
    9.         l_quadVertices.Add(new Vector3(0, 0, 1));
    10.         l_quadVertices.Add(new Vector3(1, 0, 1));
    11.         l_quadVertices.Add(new Vector3(1, 0, 0));
    12.         l_quadVertices.Add(new Vector3(0, 0, 0));
    13.  
    14.         //Define triangles
    15.         var l_quadTriangles = new List<int>();
    16.         l_quadTriangles.Add(0);
    17.         l_quadTriangles.Add(1);
    18.         l_quadTriangles.Add(2);
    19.         l_quadTriangles.Add(0);
    20.         l_quadTriangles.Add(2);
    21.         l_quadTriangles.Add(3);
    22.  
    23.         //Setup uvs
    24.         //Assign a random tile
    25.         var l_textPos = new Vector2(Random.Range(0, TileSetSize), Random.Range(0, TileSetSize));
    26.  
    27.         var l_quadUvs = new List<Vector2>();
    28.         l_quadUvs.Add(new Vector2(TileRatio * l_textPos.x + TileRatio, TileRatio * l_textPos.y));
    29.         l_quadUvs.Add(new Vector2(TileRatio * l_textPos.x + TileRatio, TileRatio * l_textPos.y + TileRatio));
    30.         l_quadUvs.Add(new Vector2(TileRatio * l_textPos.x, TileRatio * l_textPos.y + TileRatio));
    31.         l_quadUvs.Add(new Vector2(TileRatio * l_textPos.x, TileRatio * l_textPos.y));
    32.  
    33.         m_mesh = GetComponent<MeshFilter>().mesh;
    34.         m_mesh.Clear();
    35.         m_mesh.vertices = l_quadVertices.ToArray();
    36.         m_mesh.uv = l_quadUvs.ToArray();
    37.         m_mesh.triangles = l_quadTriangles.ToArray();
    38.         m_mesh.RecalculateNormals();
    39.     }
    40.  
    So my question is: how can I get something like this working using directly the UV and not a custom Vector2 as offset source.

    Bonus question, what would be the best way to pass a value for the rotation (which would be different for each tiles)? I'm currently thinking about using the UV3. Or maybe vertex color?

    Thanks in advance for your help