Search Unity

Skybox layout

Discussion in 'Scripting' started by nullstar, Sep 18, 2011.

  1. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Heya folks.

    I'm creating a procedural skybox texture generator and currently have it configured so that it generates the 6 skybox face textures to correctly align when applied to the built-in Unity RenderFX/Skybox shader. Now however I wish to use my own shader along with my own skybox geometry which is generated in script. Does anyone know the correct vertex ordering and uv asignment for such a skybox such that the faces all line up the same way as they would using the built-in Skybox shader?

    I'm messing around with different ordering at the moment but there's a whole lot of possible different combinations, I'll be here all night ;).

    Cheers
     
  2. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    No worries, I got it sorted. It's a strange mapping though, I had to swap two of the face in the end to get it to fit, ie the top and bottom faces switched places. Anyway, in case anyone else struggles with this and finds this post in a forum search, here's the code:

    Code (csharp):
    1.  
    2.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    3.         mesh.Clear();
    4.  
    5.         // create verts and uvs
    6.         Vector3[] verts = new Vector3[24];
    7.         Vector2[] uvs = new Vector2[24];
    8.        
    9.         // front face
    10.         verts[0] = new Vector3(1.0f, 1.0f, 1.0f);      // UR
    11.         verts[1] = new Vector3(-1.0f, 1.0f, 1.0f);       // UL
    12.         verts[2] = new Vector3(1.0f, -1.0f, 1.0f);     // DR      
    13.         verts[3] = new Vector3(-1.0f, -1.0f, 1.0f);      // DL
    14.  
    15.         // back face
    16.         verts[4] = new Vector3(-1.0f, 1.0f, -1.0f);      // UL
    17.         verts[5] = new Vector3(1.0f, 1.0f, -1.0f);       // UR
    18.         verts[6] = new Vector3(-1.0f, -1.0f, -1.0f);     // DL      
    19.         verts[7] = new Vector3(1.0f, -1.0f, -1.0f);      // DR
    20.  
    21.         // left face
    22.         verts[8] = new Vector3(-1.0f, 1.0f, 1.0f);      // UF    
    23.         verts[9] = new Vector3(-1.0f, 1.0f, -1.0f);     // UB
    24.         verts[10] = new Vector3(-1.0f, -1.0f, 1.0f);    // DF          
    25.         verts[11] = new Vector3(-1.0f, -1.0f, -1.0f);   // DB
    26.  
    27.         // right face
    28.         verts[12] = new Vector3(1.0f, 1.0f, -1.0f);      // UB    
    29.         verts[13] = new Vector3(1.0f, 1.0f, 1.0f);     // UF
    30.         verts[14] = new Vector3(1.0f, -1.0f, -1.0f);     // DB          
    31.         verts[15] = new Vector3(1.0f, -1.0f, 1.0f);    // DF
    32.  
    33.         // up face
    34.         verts[16] = new Vector3(1.0f, -1.0f, 1.0f);      // RF    
    35.         verts[17] = new Vector3(-1.0f, -1.0f, 1.0f);     // LF
    36.         verts[18] = new Vector3(1.0f, -1.0f, -1.0f);     // RB          
    37.         verts[19] = new Vector3(-1.0f, -1.0f, -1.0f);    // LB
    38.  
    39.         // down face
    40.         verts[20] = new Vector3(1.0f, 1.0f, -1.0f);      // RB    
    41.         verts[21] = new Vector3(-1.0f, 1.0f, -1.0f);     // LB
    42.         verts[22] = new Vector3(1.0f, 1.0f, 1.0f);     // RF          
    43.         verts[23] = new Vector3(-1.0f, 1.0f, 1.0f);    // LF
    44.  
    45.         for (int i = 0; i < 24; i += 4)
    46.         {
    47.             uvs[i] = new Vector2(0.0f, 0.0f);
    48.             uvs[i + 1] = new Vector2(1.0f, 0.0f);
    49.             uvs[i + 2] = new Vector2(0.0f, 1.0f);
    50.             uvs[i + 3] = new Vector2(1.0f, 1.0f);
    51.         }
    52.  
    53.         // assign verts and uvs
    54.         mesh.vertices = verts;
    55.         mesh.uv = uvs;
    56.  
    57.         // create and assign tris
    58.         mesh.subMeshCount = 6;
    59.         for (int i = 0; i < 6; ++i)
    60.         {
    61.             int startIndex = 4 * i;
    62.  
    63.             int[] tris = new int[6];
    64.             tris[0] = startIndex + 0;
    65.             tris[1] = startIndex + 1;
    66.             tris[2] = startIndex + 2;
    67.             tris[3] = startIndex + 1;
    68.             tris[4] = startIndex + 3;
    69.             tris[5] = startIndex + 2;
    70.  
    71.             mesh.SetTriangles(tris, i);
    72.         }
    73.  
    And here's a sneak peek at what I'm working on ;)