Search Unity

Generate Cylindrical Cubemap at runtime for 360 VR Image

Discussion in 'General Graphics' started by inancatil, May 25, 2018.

  1. inancatil

    inancatil

    Joined:
    Dec 9, 2016
    Posts:
    13
    Hi all,

    I am trying 360 degree photoviewer. There are many topics but they all use inverted sphere with a shader. I tried 3-4 different shader but all of them decreased the resolution of image. Therefore I am trying to create Cylindrical cubemap at runtime to use it as skybox material.

    First of all, it is an android application. User choose image from gallery. Using this asset. What I tried is;

    Code (CSharp):
    1.     void ConvertTextureToCubeMap(Texture2D tex)
    2.     {
    3.         string path = AssetDatabase.GetAssetPath(tex);
    4.  
    5.         TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
    6.         if (tex.dimension != UnityEngine.Rendering.TextureDimension.Cube)
    7.         {
    8.             importer.textureShape = TextureImporterShape.TextureCube;
    9.             importer.generateCubemap = TextureImporterGenerateCubemap.Cylindrical;
    10.             importer.SaveAndReimport();
    11.  
    12.         }
    13.  
    14.     }
    It works if I assign Texture as public variable. But I can't pass Texture from script. As it needs texture to be inside Assets folder (if i understood correctly), I first copy the image that user selected from gallery using this function:

    Code (CSharp):
    1.  void CopyImage(string path)
    2.     {
    3.         byte[] imageBytes = File.ReadAllBytes(path);
    4.         string fileName = Path.GetFileName(path);
    5.         File.WriteAllBytes(Path.Combine(CustomSkyboxDir, fileName), imageBytes); //this copies image to "Assets/CustomSkybox" folder with its name.
    6.  
    7.         AssetDatabase.Refresh();
    8.      
    9.         //Here I tried to call "ConvertTextureToCubeMap" function, but "AssetDatabase.GetAssetPath(tex);" returns null/empty.
    10.     }
    To be honest, i am not even sure if it is possible. Any help appreciated.

    Thanks in advance.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    What resolution are your textures, and what device are you targeting?

    Generally speaking shaders won't decrease a texture's resolution. If thats something you're seeing, the issue is elsewhere. Could be the device you're using doesn't support higher resolution textures, or the resolution you are running the game at is too low, or you've left the texture importer settings at the default Max of 2048.

    The texture importer is an editor only class that can't be used at runtime. If you want to covert a panoramic 360 image into a cube map the easiest solution is to import the texture, apply it to a sphere, and render out the six faces to a cubemap render texture. At that point though there's not a great reason not to just use that sphere with the original texture. Really you can use the panoramic sky box shader that ships with Unity and apply it to a box and it should work.
     
    inancatil likes this.
  3. inancatil

    inancatil

    Joined:
    Dec 9, 2016
    Posts:
    13
    I can't believe I couldn't come up with panoramic shader any of my searches. It is just 2 lines of code to set the image. Thinking about what I have been trying for 3 days, I feel stupid.
    Thanks a lot
     
  4. abibarte

    abibarte

    Joined:
    Feb 22, 2019
    Posts:
    3
    I have the same problem, try use a cicle but dosn't work , some one have the good way to use.