Created
June 22, 2017 04:17
-
-
Save kaiware007/7a1bd761f9dc8880c3f8c6d192bf95d0 to your computer and use it in GitHub Desktop.
Cubemapを作るEditorスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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