Search Unity

Cube Map Seperator Here!

Discussion in 'Scripting' started by cherub, Apr 8, 2014.

  1. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    Someone asked if i could add a the ability to separate or extract the images from a LightShape cube map. I decided I would just write up a quick and dirty utility separately.

    Instructions:
    1. Put this in a folder called Editor in your assets
    2. drag the cubemap you want to separate into the SplitCube spot
    3. set the resolution to the resolution of the cube map you are splitting.(too lazy to auto detect)
    4. wait for unity to update the project to reveal the new .png files.

    here you go! feel free to make it better.
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.IO;
    4. using System.Collections;  
    5. using System.Collections.Generic;
    6.  
    7. public class CubeSplitter : EditorWindow
    8. {
    9.     Cubemap splitCube;
    10.     Color[] CubeMapColors;
    11.     int splitSize;
    12.  
    13.     [MenuItem ("Window/CubeSplitter")]
    14.    
    15.     static void Init ()
    16.     {
    17.         CubeSplitter window = (CubeSplitter)EditorWindow.GetWindow (typeof (CubeSplitter), false);
    18.        
    19.         window.maxSize = new Vector2(512, 155);
    20.         window.minSize = window.maxSize;
    21.         window.title  = ("Cube Splitter!");
    22.         window.Show();
    23.  
    24.     }
    25.    
    26.  
    27.    
    28.     void OnGUI ()
    29.     {
    30.  
    31.         GUILayout.Label("Choose the Cube Map you want to save as 6 images and click EXPORT!", EditorStyles.boldLabel);
    32.         splitCube = EditorGUILayout.ObjectField("My Cubemap:", splitCube, typeof(Cubemap), false) as Cubemap;
    33.         GUILayout.Label("Make sure to set the Size to the same as the Cubemap you are using", EditorStyles.boldLabel);
    34.         splitSize = EditorGUILayout.IntField("CubeMap Size: ", splitSize);
    35.  
    36.         if(GUILayout.Button("EXPORT!"))
    37.         {
    38.             if(splitCube)
    39.             {
    40.                 Export();
    41.             }
    42.        
    43.             if(!splitCube)
    44.             {
    45.                 Debug.Log ("Forget Something?");
    46.             }
    47.         }
    48.     }
    49.  
    50.     void Export()
    51.     {
    52.         var filePath = AssetDatabase.GetAssetPath(splitCube);
    53.  
    54.         Texture2D tex = new Texture2D (splitSize, splitSize, TextureFormat.RGB24, false);
    55.         CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveY);
    56.         tex.SetPixels(CubeMapColors, 0);
    57.  
    58.         tex.Apply ();
    59.  
    60.         byte[] bytes = tex.EncodeToPNG();
    61.         File.WriteAllBytes(filePath + "_Bot.png", bytes);
    62.  
    63.         CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeY);
    64.         tex.SetPixels(CubeMapColors, 0);
    65.        
    66.         tex.Apply ();
    67.        
    68.         bytes = tex.EncodeToPNG();
    69.         File.WriteAllBytes(filePath + "_Top.png", bytes);
    70.  
    71.  
    72.         CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveX);
    73.         tex.SetPixels(CubeMapColors, 0);
    74.        
    75.         tex.Apply ();
    76.        
    77.         bytes = tex.EncodeToPNG();
    78.         File.WriteAllBytes(filePath + "_Lef.png", bytes);
    79.  
    80.  
    81.         CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeX);
    82.         tex.SetPixels(CubeMapColors, 0);
    83.        
    84.         tex.Apply ();
    85.        
    86.         bytes = tex.EncodeToPNG();
    87.         File.WriteAllBytes(filePath + "_Rig.png", bytes);
    88.  
    89.         CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveZ);
    90.         tex.SetPixels(CubeMapColors, 0);
    91.        
    92.         tex.Apply ();
    93.        
    94.         bytes = tex.EncodeToPNG();
    95.         File.WriteAllBytes(filePath + "_Fro.png", bytes);
    96.  
    97.         CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeZ);
    98.         tex.SetPixels(CubeMapColors, 0);
    99.        
    100.         tex.Apply ();
    101.        
    102.         bytes = tex.EncodeToPNG();
    103.         File.WriteAllBytes(filePath + "_Bak.png", bytes);
    104.  
    105.         this.Close();
    106.     }
    107.  
    108. }
    109.  
    110.  
    111.  
    112.  
    Haha, after I wrote this i was able to also find this...at least I learned something new!

    http://answers.unity3d.com/questions/9189/how-can-i-extract-the-individual-faces-from-a-gene.html
     
    Last edited: Apr 9, 2014
  2. kryptopath2

    kryptopath2

    Joined:
    Jul 19, 2013
    Posts:
    104
    thanks a lot!!!!!
     
  3. Jumeuan

    Jumeuan

    Joined:
    Mar 14, 2017
    Posts:
    39
    @cherub Thank you! But your script export have reverse way, please check it out, thank!