class BakeSkybox

{
    static var faceSize = 1024;
    static var directory = "Assets/Skyboxes";
    static var skyboxShader = "RenderFX/Skybox";
   
   
    static var skyBoxImage = new Array ("front", "right", "back", "left", "up", "down");
    static var skyBoxProps = new Array ("_FrontTex", "_RightTex", "_BackTex", "_LeftTex", "_UpTex", "_DownTex");
   
    static var skyDirection = new Array (Vector3 (0,0,0), Vector3 (0,-90,0), Vector3 (0,180,0), Vector3 (0,90,0), Vector3 (-90,0,0), Vector3 (90,0,0));
   
    @MenuItem("Custom/Bake Skybox", false, 4)
    static function Bake()
    {
        if (Selection.transforms.Length == 0)
        {
            Debug.LogWarning ("Select at least one scene object as a skybox center!");
            return;
        }
               
        if (!System.IO.Directory.Exists(directory))
            System.IO.Directory.CreateDirectory(directory);
       
        for (var t in Selection.transforms)
            RenderSkyboxTo6PNG(t);
    }
   
    static function RenderSkyboxTo6PNG(t : Transform)
    {
        var go = new GameObject ("SkyboxCamera", Camera);
       
        go.camera.backgroundColor = Color.black;
        go.camera.clearFlags = CameraClearFlags.Skybox;
        go.camera.fieldOfView = 90;   
        go.camera.aspect = 1.0;
       
        go.transform.position = t.position;
        go.transform.rotation = Quaternion.identity;
       
        // render skybox       
        for (var orientation = 0; orientation < skyDirection.length ; orientation++)
        {
            var assetPath = System.IO.Path.Combine(directory, t.name + "_" + skyBoxImage[orientation] + ".png");
            RenderSkyBoxFaceToPNG(orientation, go.camera, assetPath);
        }
        GameObject.DestroyImmediate (go);
       
        // wire skybox material
        AssetDatabase.Refresh();
       
        var skyboxMaterial = new Material (Shader.Find(skyboxShader));       
        for (orientation = 0; orientation < skyDirection.length ; orientation++)
        {
            var texPath = System.IO.Path.Combine(directory, t.name + "_" + skyBoxImage[orientation] + ".png");
            var tex : Texture2D = AssetDatabase.LoadAssetAtPath(texPath, Texture2D) as Texture2D;
            tex.wrapMode = TextureWrapMode.Clamp;
            skyboxMaterial.SetTexture(skyBoxProps[orientation] as String, tex);
        }
       
        // save material
        var matPath = System.IO.Path.Combine(directory, t.name + "_skybox" + ".mat");
        AssetDatabase.CreateAsset(skyboxMaterial, matPath);
    }

    static function RenderSkyBoxFaceToPNG(orientation : int, cam : Camera, assetPath : String)
    {
        cam.transform.eulerAngles = skyDirection[orientation];
        var rt = new RenderTexture (faceSize, faceSize, 24);
        cam.camera.targetTexture = rt;
        cam.camera.Render();
        RenderTexture.active = rt;
       
        var screenShot = new Texture2D (faceSize, faceSize, TextureFormat.RGB24, false);
        screenShot.ReadPixels (Rect (0, 0, faceSize, faceSize), 0, 0);
       
        RenderTexture.active = null;
        GameObject.DestroyImmediate (rt);
       
        var bytes = screenShot.EncodeToPNG();
        System.IO.File.WriteAllBytes (assetPath, bytes);
       
        AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
    }
}