Search Unity

Outline shader

Discussion in 'Shaders' started by Friend123, Jan 12, 2017.

  1. Friend123

    Friend123

    Joined:
    Sep 16, 2012
    Posts:
    19
    Hi.
    I have a problem with outline shader. The task is to do borders between territories like in Civilization or in Europa Universalis.
    I'm using Unity 5.5.0p3 and in this version objects are in editor selecting by orange color.
    I found thread where explained how it works (https://forum.unity3d.com/threads/selection-outline.429292/#post-2776318) - i create these two shader files, put them in Assets folder, create a new material (without texture), apply this shader from thread, apply the material to the object, but nothing happened - no borders, nothing.
    Where's i'm wrong?

    Code of hexagons generating:
    Code (CSharp):
    1.  
    2. public class FX_HexGen : MonoBehaviour {
    3.     public Material material;
    4.  
    5.     public GameObject MakeHex (int offset, float scale) {
    6.  
    7.         Mesh mesh = new Mesh();
    8.  
    9.         Vector3[] vertices = new Vector3[7];
    10.  
    11.         //Center
    12.         vertices[0] = Vector3.zero;
    13.         vertices[1] = GetVertexPos(0 + offset) * scale;
    14.         vertices[2] = GetVertexPos(60 + offset) * scale;
    15.         vertices[3] = GetVertexPos(120 + offset) * scale;
    16.         vertices[4] =  GetVertexPos(180 + offset) * scale;
    17.         vertices[5] = GetVertexPos(240 + offset) * scale;
    18.         vertices[6] = GetVertexPos(300 + offset) * scale;
    19.  
    20.         int[] triangles = new int[18];
    21.      
    22.         //Top
    23.         triangles[0] = 0;
    24.         triangles[1] = 2;
    25.         triangles[2] = 1;
    26.      
    27.         //Bottom
    28.         triangles[3] = 0;
    29.         triangles[4] = 3;
    30.         triangles[5] = 2;
    31.      
    32.         //Bottom Left
    33.         triangles[6] = 0;
    34.         triangles[7] = 4;
    35.         triangles[8] = 3;
    36.      
    37.         //Bottom Right
    38.         triangles[9] = 0;
    39.         triangles[10] = 5;
    40.         triangles[11] = 4;
    41.      
    42.         //Top Right
    43.         triangles[12] = 0;
    44.         triangles[13] = 6;
    45.         triangles[14] = 5;
    46.      
    47.         //Top Left
    48.         triangles[15] = 0;
    49.         triangles[16] = 1;
    50.         triangles[17] = 6;
    51.  
    52.         Vector2[] uv = new Vector2[7];
    53.      
    54.         // Center
    55.         uv[0] = new Vector2(.5f, .5f);
    56.         uv[1] = new Vector2(1f, .5f); // Top Left
    57.         uv[2] = new Vector2(.75f, .935f); // Top Right
    58.         uv[3] = new Vector2(.25f, 0.935f); // Bottom Right
    59.         uv[4] = new Vector2(0, 0.5f); // Bottom Left
    60.         uv[5] = new Vector2(.25f, .065f);  // Center Bottom Left
    61.         uv[6] = new Vector2(.75f, .065f); // Bottom Left
    62.  
    63.         mesh.vertices = vertices;
    64.         mesh.triangles = triangles;
    65.         mesh.uv = uv;
    66.         mesh.RecalculateNormals();
    67.      
    68.         GameObject o = new GameObject("New Hex");
    69.  
    70.         o.AddComponent<MeshRenderer>();
    71.         o.AddComponent<MeshFilter>();
    72.         o.GetComponent<MeshFilter>().mesh = mesh;
    73.         mesh.RecalculateNormals();
    74.         mesh.RecalculateBounds();
    75.  
    76.         o.AddComponent<MeshCollider>();
    77.  
    78.         //o.GetComponent<Renderer>().material = material;
    79.  
    80.         return o;
    81.     }
    82. }
    83.  
     

    Attached Files:

  2. Friend123

    Friend123

    Joined:
    Sep 16, 2012
    Posts:
    19
    Can anyone help?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The post you originally linked to has some basic instructions for how to use those two shaders, and it's far more than just slapping them on an object. There have been a few threads since that post with people putting up github repos with the effect working.

    However if what you're looking for is to have all of your regions individually outlined all of the time, that is not the way to do it. The effect is relatively expensive, so it's appropriate if you're looking to highlight one, or maybe a small handful of individual regions. If you want to give them all an outline that is something you'll want to do via C# to build out additional geometry and/or encoding data in the existing geometry that the shader being used to draw the regions normally can use to know where the edges are.
     
  4. Friend123

    Friend123

    Joined:
    Sep 16, 2012
    Posts:
    19
    Thanks for the answer! Additional geometry, I did not think about it! That's what I need.