Search Unity

Modular Level Editor texturing - ideas

Discussion in 'Editor & General Support' started by delzhand, Aug 16, 2012.

  1. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    I've tried a number of solutions to my issue, but none of them seem to really work, so I'm seeking advice on how to proceed.

    The short version is that my stages are composed of hexagonal tiles. In prior versions, I've used these like legos, stacking them up to achieve the desired terrain, but this has a few issues:
    • Textures repeat in too small increments
    • A good number of the hexes are "buried" anyway
    • The number of combinations of top texture and side texture is prohibitive, requiring a different hex model that differs only in UV coordinates

    My current attempt is to use one hex where before there would be a vertical stack. To make it taller, I just scale the hex (well, its parent, really). But as I worked on it, I realized there's no way to handle textures the way I want. Each hex is 1 unit tall, and the default UV looks like the red section of this:



    What I'd really like is for the texture coverage to be flexible - for example, when the scale is 1, the sides go down the the end of the red, when scale is 2 it goes to the end of the blue, etc.

    One idea I had was to just make the hex model really tall, texture it using the entire image, and then use Y position instead of scale to get the top where I want it, but I don't want anything to extend down below Y = 0. If there's a way to set a clipping plane that isn't attached to a camera, that would work perfectly I think, but I don't know if that's possible.
     
  2. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    I found something that I think will work, and hopefully it will help anyone else who finds this.
    Code (csharp):
    1.  
    2.  
    3. public class ScaleTexture : MonoBehaviour {
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.                 // This should be specific to however you find the scale you want.
    8.         GameObject g = TileStack.FindTerrainParent(this.gameObject);
    9.         float scale = g.transform.localScale.y;
    10.        
    11.                 // Grab the shared mesh (as a "master" copy) and this instance of the mesh
    12.         MeshFilter meshFilter = GetComponent("MeshFilter") as MeshFilter;
    13.         Mesh sMesh = meshFilter.sharedMesh;
    14.         Mesh mesh = meshFilter.mesh;
    15.  
    16.         // This will be our new UV list
    17.         Vector2[] newUVs = new Vector2[sMesh.uv.Length];
    18.        
    19.         // First we need to get all the Y values (our highest 2 will be important later)
    20.         List<float> l = new List<float>();
    21.         for (int i = 0; i<sMesh.uv.Length; i++) {
    22.             if (!l.Contains(sMesh.uv[i].y)){
    23.                 l.Add(sMesh.uv[i].y);
    24.             }
    25.                         // while we're at it, populate newUVs by cloning from the sharedMesh
    26.             newUVs[i] = sMesh.uv[i];
    27.         }
    28.  
    29.                 // this part only works if your texture is laid out vertically, with the UVs you want to move at the lowest point
    30.         l.Sort();
    31.        
    32.         // We can get the height of a "unit" by subtracting
    33.         float unitHeight = l[0] - l[1];
    34.        
    35.         for(int i=0; i<newUVs.Length; i++) {
    36.           if (newUVs[i].y == l[0]) newUVs[i].y += unitHeight * (scale - 1);
    37.         }
    38.        
    39.         mesh.uv = newUVs;
    40.     }
    41.    
    42.     // Update is called once per frame
    43.     void Update () {
    44.    
    45.     }
    46. }
    47.