Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Created August 21, 2015 12:29
Show Gist options
  • Save reinsteam/b3a6b1034edd1332fb85 to your computer and use it in GitHub Desktop.
Save reinsteam/b3a6b1034edd1332fb85 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.IO;
public class BakeCubemap : ScriptableWizard
{
public Transform CapturePosition;
public int Antialiasing = 4;
public int CubemapResolution = 128;
public const string OutputDirectory = "Resources\\Generated\\Cubemaps";
public void OnWizardCreate()
{
if (CapturePosition == null)
{
Debug.LogWarning("Setup render position");
return;
}
var obj = new GameObject("BakeCubema_Camera");
var cam = obj.AddComponent<Camera>();
cam.gameObject.transform.position = CapturePosition.position;
cam.backgroundColor = Color.black;
cam.clearFlags = CameraClearFlags.Skybox;
cam.fieldOfView = 90.0f;
cam.aspect = 1.0f;
var FullPath = Path.Combine(Application.dataPath, OutputDirectory);
if (Directory.Exists(FullPath) == false)
{
Directory.CreateDirectory(FullPath);
}
var tex = new Texture2D(CubemapResolution, 6 * CubemapResolution, TextureFormat.ARGB32, false);
var rct = new Rect(0, 0, CubemapResolution, CubemapResolution);
cam.targetTexture = RenderTexture.GetTemporary(CubemapResolution, CubemapResolution, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 1);
var prevRT = RenderTexture.active;
RenderTexture.active = cam.targetTexture;
cam.gameObject.transform.rotation = Quaternion.Euler(0.0f, -90.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 0 * CubemapResolution, false);
cam.gameObject.transform.rotation = Quaternion.Euler(0.0f, +90.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 1 * CubemapResolution, false);
cam.gameObject.transform.rotation = Quaternion.Euler(+90.0f, +90.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 2 * CubemapResolution, false);
cam.gameObject.transform.rotation = Quaternion.Euler(-90.0f, +90.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 3 * CubemapResolution, false);
cam.gameObject.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 4 * CubemapResolution, false);
cam.gameObject.transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
cam.Render();
tex.ReadPixels(rct, 0, 5 * CubemapResolution, false);
RenderTexture.ReleaseTemporary(cam.targetTexture);
RenderTexture.active = prevRT;
byte[] texData = tex.EncodeToPNG();
var SavePath = Path.Combine(Path.Combine("Assets", OutputDirectory), CapturePosition.gameObject.name + ".png");
File.WriteAllBytes(SavePath, texData);
AssetDatabase.ImportAsset(SavePath, ImportAssetOptions.ForceUpdate);
AssetDatabase.Refresh();
DestroyImmediate(obj);
}
public void OnWizardOtherButton()
{
}
public void OnWizardUpdate()
{
}
[MenuItem ("Texture Generator/Bake Cubemap")]
static void CreateWizard ()
{
ScriptableWizard.DisplayWizard<BakeCubemap>("Bake Cubemap", "Create", "Apply");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment