Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Dual UVs vs Submeshes

Discussion in 'General Graphics' started by Ralstlin, Mar 10, 2015.

  1. Ralstlin

    Ralstlin

    Joined:
    Jan 12, 2014
    Posts:
    87
    Hello guys, i am creating a map editor project. My maps are based in Walls and Floors (floors like cells and walls on axis) stored in a XML file

    Example:

    Walls and floors on higher levels cast shadows but are hidden by a shader.

    But then, i wanted to add the posibility for a Wall to have a texture on each side (1 side can be a room, and the other side can be the outside the house). I managed to do this modifying my walls UVs in Blender, creating Mesh.uv and Mesh.uv2 (The first one is used when both sides have the same texture and the second one is used for 2 diferent textures Packed into a single one using Texture2D.PackTextures showed on this code:

    Code (CSharp):
    1.     public void setTextures()
    2.     {
    3.  
    4.         if (_DUALTEXTURE)
    5.         {
    6.             if (mainUV == true) // if Mesh.uv is the single texture uvs, swap to dualtexture
    7.                 swapUVS();
    8.  
    9.             // if Pcked texture dont exist, pack it!
    10.             if (dual == null)
    11.             {
    12.                 dual = new Texture2D(512, 256, TextureFormat.ETC_RGB4, true);
    13.  
    14.                 Texture2D[] atlasTextures = new Texture2D[2];
    15.                 atlasTextures[0] = single;
    16.                 atlasTextures[1] = second;
    17.                 dual.PackTextures(atlasTextures, 0);
    18.             }
    19.  
    20.             GetComponent<Renderer>().material.mainTexture = dual;
    21.         }
    22.         else
    23.         {
    24.             if (mainUV == false) // if Mesh.uv is the dual texture uvs, swap to single.
    25.                 swapUVS();
    26.  
    27.             GetComponent<Renderer>().material.mainTexture = single;
    28.         }
    29.  
    30.     }

    And it works fine, but it have a performance hit. Loading a map with 3 walls, first using single texture (no PackTexture) it uses 0.333 Miliseconds to draw each wall



    And this using Mesh.uv2, that need to pack 2 textures into 1:



    As you can see, it needed 5 miliseconds per wall.. this is 15 times more time than the other one!

    This is on my computer, an i5, but when i do it on android.. as you can imagine it go worse. So before to continue i want to try fix this. My posible options are:

    - Plane: Attach a plane to 1 side to cover it and apply the second texture: This option is discarded because some walls meshs have a windows hole, and this plane will block this windows.

    - Submesh: divide each mesh into submeshes and apply a diferent material to each submesh. This seems to be the best option, but i dont have too much knowledge into submeshes and i dont know if this could hit performance too.

    - Prebuild packed textures: Well, all my maps info is on a XML file, so i could read which walls will need a packed texture and build them all together to can use them as resource instead to create the texture everytime needed. This will help when multiple walls need a packed texture, but will not work with too much different packed textures aplied to 1 or 2 walls.

    - Shaders: I dont know if there is an option to do it with shaders, i am not an expert :p


    So because i am not an expert ^^ do you have a good aproximation to do this? is submesh my best option?