Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Are shaders working on runtime objects

Discussion in 'General Discussion' started by glingol, May 13, 2020.

  1. glingol

    glingol

    Joined:
    Dec 11, 2019
    Posts:
    2
    Hey guys,

    it's been a couple of years since i've worked with unity and now wanted to come back and create the game i've been fantacing for quite some time.

    Long story short i want to create a battle scene with a hexagonal overlay. This overlay should represent the available movement tiles while underlying a fully beforehand created scene is in the background. The player and the ai would move between the tiles.

    So i created a mesh of a single hexagon in runtime, then i instantiate the object over and over again. looks good so far. (see the orange outlines done by unity? this is basically what i aim for with a shader)



    I then looked through the web and found a shader which seemed sufficiently fine and i got the result i was looking for.



    Now i thought adding the shader in runtime to the MeshRenderer would be sufficient but my mesh is not showing the slightes reaction to the shader. The material can be easily updated but still then the shader is not showing.

    What am i doing wrong? Or is it simply not possible to change the shader on a runtime generated object?

    Code (CSharp):
    1.     public Vector2 FlatTopHex(Vector2 center, int size, int i)
    2.     {
    3.         var angleDeg = 60 * i;
    4.         var angleRad = Math.PI / 180 * angleDeg;
    5.  
    6.         return new Vector2(center.x + size * (float)Math.Cos(angleRad), center.y + size * (float)Math.Sin(angleRad));
    7.     }
    8.  
    9.     public Vector2 PointyTopHex(Vector2 center, int size, int i)
    10.     {
    11.         var angleDeg = 60 * i - 30;
    12.         var angleRad = Math.PI / 180 * angleDeg;
    13.  
    14.         return new Vector2(center.x + size * (float)Math.Cos(angleRad), center.y + size * (float)Math.Sin(angleRad));
    15.     }
    16.  
    17.     private void CreateHexMesh()
    18.     {
    19.         meshHolder = new GameObject();
    20.         MeshRenderer meshRenderer = meshHolder.AddComponent<MeshRenderer>();
    21.         meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
    22.  
    23.         MeshFilter meshFilter = meshHolder.AddComponent<MeshFilter>();
    24.  
    25.         Mesh mesh = new Mesh();
    26.         Vector3[] vertices = new Vector3[7];
    27.         vertices[0] = Vector3.zero;
    28.         for (int i = 1; i <= 6; i++)
    29.         {
    30.             vertices[i] = (flatTopHex) ? FlatTopHex(Vector2.zero, 1, i) : PointyTopHex(Vector2.zero, 1, i);
    31.         }
    32.  
    33.         List<int> trisList = new List<int>();
    34.         for (int i = 0; i < vertices.Length; i++)
    35.         {
    36.             trisList.Add(0);
    37.             trisList.Add(i + 1);
    38.             if (i + 2 > vertices.Length - 1)
    39.             {
    40.                 var val = (i + 2) % vertices.Length + 1;
    41.                 trisList.Add(val);
    42.                 break;
    43.             }
    44.             else
    45.             {
    46.                 trisList.Add(i + 2);
    47.             }
    48.         }
    49.  
    50.         mesh.vertices = vertices;
    51.         mesh.triangles = trisList.ToArray();
    52.  
    53.         List<Vector3> normales = new List<Vector3>();
    54.         foreach (Vector3 v in vertices)
    55.         {
    56.             normales.Add(-Vector3.forward);
    57.         }
    58.         mesh.normals = normales.ToArray(); ;
    59.  
    60.         List<Vector2> uv = new List<Vector2>();
    61.         foreach (Vector3 v in vertices)
    62.         {
    63.             uv.Add(new Vector2(v.x, v.y));
    64.         }
    65.         mesh.uv = uv.ToArray();
    66.  
    67.         meshFilter.mesh = mesh;
    68.         mesh.RecalculateNormals();
    69.  
    70.         Shader xyz = Shader.Find("Outlined/Uniform");
    71.         meshHolder.GetComponent<MeshRenderer>().material.shader = xyz;
    72.     }
    73.  
    74.     // Start is called before the first frame update
    75.     void Start()
    76.     {
    77.         CreateHexMesh();
    78.         GenerateMap();
    79.     }
    80.  
    81.     public void GenerateMap()
    82.     {
    83.         for (int column = 0; column < numColumns; column++)
    84.         {
    85.             for (int row = 0; row < numRows; row++)
    86.             {
    87.                 Hex h = new Hex(column, row);
    88.  
    89.                 Vector3 pos = h.PositionFromCamera(Camera.main.transform.position, numRows, numColumns);
    90.  
    91.                 Instantiate(meshHolder, pos, Quaternion.Euler(0, 180, 0), this.transform);
    92.             }
    93.         }
    94. }