Skip to content

Instantly share code, notes, and snippets.

@kaiware007
Last active June 22, 2017 04:26
Show Gist options
  • Save kaiware007/3d4e7599209aa384461560d06081cc44 to your computer and use it in GitHub Desktop.
Save kaiware007/3d4e7599209aa384461560d06081cc44 to your computer and use it in GitHub Desktop.
任意の解像度でCubemapを作って保存するEditorスクリプト
// http://docs.unity3d.com/ja/current/ScriptReference/Camera.RenderToCubemap.html
// http://stackoverflow.com/questions/34458622/unity-save-cubemap-to-one-circle-image
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;
public class RenderCubemapWizard : ScriptableWizard
{
public Camera camera;
public int cubeMapSize = 2048;
void OnWizardUpdate()
{
bool isValid = (camera != null);
}
void OnWizardCreate()
{
if (camera == null)
{
camera = Camera.main;
}
Cubemap cu = new Cubemap(cubeMapSize, TextureFormat.RGB24, false);
camera.RenderToCubemap(cu);
cu.Apply();
var path = EditorUtility.SaveFilePanel("CubeMap", "", "", "cubemap");
if (!string.IsNullOrEmpty(path))
{
Debug.Log("path " + path);
ConvertToPng(cu, path);
}
}
[MenuItem("GameObject/Render into Cubemap")]
static void RenderCubemap()
{
ScriptableWizard.DisplayWizard<RenderCubemapWizard>(
"Render cubemap", "Render!");
}
void ConvertToPng(Cubemap cube, string path)
{
string dir = Path.GetDirectoryName(path);
string filename = Path.GetFileNameWithoutExtension(path);
string dir2 = dir.Replace(Application.dataPath, "");
dir2 = "Assets" + dir2;
Debug.Log("dir " + dir + " filename " + filename);
Debug.Log("Application.dataPath " + Application.dataPath);
Debug.Log("dir2 " + dir2);
// Save Cubemap
string cubeMapPath = dir2 + "/" + filename + ".cubemap";
var asset = AssetDatabase.LoadAssetAtPath(cubeMapPath, typeof(Cubemap));
if (asset == null)
{
AssetDatabase.CreateAsset(cube, cubeMapPath);
}
else
{
EditorUtility.CopySerialized(cube, asset);
AssetDatabase.SaveAssets();
}
// Save Plane Texture
var tex = new Texture2D(cube.width, cube.height, TextureFormat.RGB24, false);
var bytes = getPlanePixels(tex, CubemapFace.PositiveX, cube);
File.WriteAllBytes(dir + "/" + filename + "_PositiveX.png", bytes);
bytes = getPlanePixels(tex, CubemapFace.NegativeX, cube);
File.WriteAllBytes(dir + "/" + filename + "_NegativeX.png", bytes);
bytes = getPlanePixels(tex, CubemapFace.PositiveY, cube);
File.WriteAllBytes(dir + "/" + filename + "_PositiveY.png", bytes);
bytes = getPlanePixels(tex, CubemapFace.NegativeY, cube);
File.WriteAllBytes(dir + "/" + filename + "_NegativeY.png", bytes);
bytes = getPlanePixels(tex, CubemapFace.PositiveZ, cube);
File.WriteAllBytes(dir + "/" + filename + "_PositiveZ.png", bytes);
bytes = getPlanePixels(tex, CubemapFace.NegativeZ, cube);
File.WriteAllBytes(dir + "/" + filename + "_NegativeZ.png", bytes);
DestroyImmediate(tex);
AssetDatabase.Refresh();
}
byte[] getPlanePixels(Texture2D _tex, CubemapFace _face, Cubemap cube)
{
Texture2D tmpTex = new Texture2D(cube.width, cube.height, TextureFormat.RGB24, false);
tmpTex.SetPixels(cube.GetPixels(_face));
Color[] vline;
for (int x = 0; x < cube.width; ++x)
{
vline = tmpTex.GetPixels(x, 0, 1, cube.height);
_tex.SetPixels(x, 0, 1, cube.height, vline.Reverse().ToArray());
}
return _tex.EncodeToPNG();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment