Created
June 8, 2015 10:16
-
-
Save ddutchie/2b167f2eec935843420d to your computer and use it in GitHub Desktop.
Initial Tests for Stereo Cubemap Generator.
This file contains hidden or 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
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
class NewSkyBoxGenerator : MonoBehaviour | |
{ | |
static int faceSize = 2046; | |
static string directory = "Assets/Skyboxes"; | |
static string skyboxShader = "RenderFX/Skybox"; | |
static string[] skyBoxImageFBL = new string[]{"frontR", "backL", "upL","downL" }; | |
static string[] skyBoxImageFBR = new string[]{"frontL", "backR", "upR","downR"}; | |
static string[] skyBoxPropsFBL = new string[]{"_FrontTex", "_BackTex", "_UpTex", "_DownTex"}; | |
static string[] skyBoxPropsFBR = new string[]{"_FrontTex", "_BackTex", "_UpTex", "_DownTex"}; | |
static Vector3[] skyDirectionFB = new Vector3[]{new Vector3(0,0,0), new Vector3(0,180,0), new Vector3(-90,0,0), new Vector3(90,0,0)}; | |
// static Vector3[] skyDirectionFBR = new Vector3[]{new Vector3(0,0,0), new Vector3(0,180,0)}; | |
static string[] skyBoxImageSDL = new string[]{"SideFR", "SideBL"}; | |
static string[] skyBoxImageSDR = new string[]{"SideFL", "SideBR"}; | |
static string[] skyBoxPropsSDL = new string[]{"_RightTex", "_LeftTex"}; | |
static string[] skyBoxPropsSDR = new string[]{"_RightTex", "_LeftTex"}; | |
static Vector3[] skyDirectionSD = new Vector3[]{new Vector3(0,-90,0), new Vector3(0,90,0)}; | |
static string[] skyBoxImageTB = new string[]{"topL", "bottomR"}; | |
// static Vector3[] skyDirectionSDR = new Vector3[]{new Vector3(0,-90,0), new Vector3(0,90,0)}; | |
static string[] skyBoxImage = new string[]{"front", "right", "back", "left", "up", "down"}; | |
static string[] skyBoxProps = new string[]{"_FrontTex", "_RightTex", "_BackTex", "_LeftTex", "_UpTex", "_DownTex"}; | |
static Vector3[] skyDirection = new Vector3[]{new Vector3(0,0,0), new Vector3(0,-90,0), new Vector3(0,180,0), new Vector3(0,90,0), new Vector3(-90,0,0), new Vector3(90,0,0)}; | |
// static Material skyboxMaterialL; | |
//static Material skyboxMaterialR; | |
[MenuItem("Slee/Bake Skybox", false, 4)] | |
static void Bake() | |
{ | |
if (Selection.transforms.Length == 0) | |
{ | |
Debug.LogWarning ("Select at least one scene object as a skybox center!"); | |
return; | |
} | |
if (!Directory.Exists(directory)) | |
Directory.CreateDirectory(directory); | |
foreach(Transform t in Selection.transforms) | |
RenderSkyboxTo6PNG(t); | |
} | |
[MenuItem("Slee/Bake VR SKybox FB", false, 4)] | |
static void Bake2() | |
{ | |
if (Selection.transforms.Length == 0) | |
{ | |
Debug.LogWarning ("Select at least one scene object as a skybox center!"); | |
return; | |
} | |
if (!Directory.Exists(directory)) | |
Directory.CreateDirectory(directory); | |
RenderSkyboxToFBLPNG(Selection.transforms[0]); | |
RenderSkyboxToFBRPNG(Selection.transforms[1]); | |
RenderSkyboxToSDLPNG(Selection.transforms[2]); | |
RenderSkyboxToSDRPNG(Selection.transforms[3]); | |
// foreach(Transform t in Selection.transforms) | |
// RenderSkyboxToLR1PNG(Selection.transforms[0]); | |
// foreach(Transform t in Selection.transforms) | |
// RenderSkyboxToLR2PNG(t); | |
// | |
} | |
[MenuItem("Slee/Bake VR SKybox SD", false, 4)] | |
static void Bake3() | |
{ | |
if (Selection.transforms.Length == 0) | |
{ | |
Debug.LogWarning ("Select at least one scene object as a skybox center!"); | |
return; | |
} | |
if (!Directory.Exists(directory)) | |
Directory.CreateDirectory(directory); | |
// RenderSkyboxToFBLPNG(Selection.transforms[0]); | |
// RenderSkyboxToFBRPNG(Selection.transforms[1]); | |
RenderSkyboxToSDLPNG(Selection.transforms[0]); | |
RenderSkyboxToSDRPNG(Selection.transforms[1]); | |
// foreach(Transform t in Selection.transforms) | |
// RenderSkyboxToLR1PNG(Selection.transforms[0]); | |
// foreach(Transform t in Selection.transforms) | |
// RenderSkyboxToLR2PNG(t); | |
// | |
} | |
static void RenderSkyboxToSDLPNG(Transform t) | |
{ | |
GameObject go = new GameObject("SkyboxCamera", typeof(Camera)); | |
go.GetComponent<Camera>().useOcclusionCulling = false; | |
go.GetComponent<Camera>().hdr = true; | |
go.GetComponent<Camera>().renderingPath = RenderingPath.DeferredLighting; | |
go.GetComponent<Camera>().clearFlags = CameraClearFlags.Skybox; | |
go.GetComponent<Camera>().fieldOfView = 90; | |
go.GetComponent<Camera>().aspect = 1.0f; | |
go.transform.position = t.position; | |
go.transform.rotation = Quaternion.identity; | |
//Render skybox | |
for (int orientation = 0; orientation < skyDirectionSD.Length ; orientation++) | |
{ | |
string assetPath = Path.Combine(directory, t.name + "_" + skyBoxImageSDL[orientation] + ".png"); | |
RenderSkyBoxFaceToSDPNG(orientation, go.GetComponent<Camera>(), assetPath); | |
} | |
GameObject.DestroyImmediate(go); | |
//Wire skybox material | |
AssetDatabase.Refresh(); | |
Material skyboxMaterial = new Material(Shader.Find(skyboxShader)); | |
for (int orientation = 0; orientation < skyDirectionSD.Length ; orientation++) | |
{ | |
string texPath = Path.Combine(directory, t.name + "_" + skyBoxImageSDL[orientation] + ".png"); | |
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)); | |
tex.wrapMode = TextureWrapMode.Clamp; | |
skyboxMaterial.SetTexture(skyBoxPropsSDL[orientation], tex); | |
} | |
//Save material | |
string matPath = Path.Combine(directory, t.name + "_skybox" + ".mat"); | |
AssetDatabase.CreateAsset(skyboxMaterial, matPath); | |
} | |
static void RenderSkyboxToSDRPNG(Transform t) | |
{ | |
GameObject go = new GameObject("SkyboxCamera", typeof(Camera)); | |
go.GetComponent<Camera>().useOcclusionCulling = false; | |
go.GetComponent<Camera>().hdr = true; | |
go.GetComponent<Camera>().renderingPath = RenderingPath.DeferredLighting; | |
go.GetComponent<Camera>().clearFlags = CameraClearFlags.Skybox; | |
go.GetComponent<Camera>().fieldOfView = 90; | |
go.GetComponent<Camera>().aspect = 1.0f; | |
go.transform.position = t.position; | |
go.transform.rotation = Quaternion.identity; | |
//Render skybox | |
for (int orientation = 0; orientation < skyDirectionSD.Length ; orientation++) | |
{ | |
string assetPath = Path.Combine(directory, t.name + "_" + skyBoxImageSDR[orientation] + ".png"); | |
RenderSkyBoxFaceToSDPNG(orientation, go.GetComponent<Camera>(), assetPath); | |
} | |
GameObject.DestroyImmediate(go); | |
//Wire skybox material | |
AssetDatabase.Refresh(); | |
Material skyboxMaterial = new Material(Shader.Find(skyboxShader)); | |
for (int orientation = 0; orientation < skyDirectionSD.Length ; orientation++) | |
{ | |
string texPath = Path.Combine(directory, t.name + "_" + skyBoxImageSDR[orientation] + ".png"); | |
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)); | |
tex.wrapMode = TextureWrapMode.Clamp; | |
skyboxMaterial.SetTexture(skyBoxPropsSDR[orientation], tex); | |
} | |
//Save material | |
string matPath = Path.Combine(directory, t.name + "_skybox" + ".mat"); | |
AssetDatabase.CreateAsset(skyboxMaterial, matPath); | |
} | |
static void RenderSkyboxToFBLPNG(Transform t) | |
{ | |
GameObject go = new GameObject("SkyboxCamera", typeof(Camera)); | |
go.GetComponent<Camera>().useOcclusionCulling = false; | |
go.GetComponent<Camera>().hdr = true; | |
go.GetComponent<Camera>().renderingPath = RenderingPath.DeferredLighting; | |
go.GetComponent<Camera>().clearFlags = CameraClearFlags.Skybox; | |
go.GetComponent<Camera>().fieldOfView = 90; | |
go.GetComponent<Camera>().aspect = 1.0f; | |
go.transform.position = t.position; | |
go.transform.rotation = Quaternion.identity; | |
//Render skybox | |
for (int orientation = 0; orientation < skyDirectionFB.Length ; orientation++) | |
{ | |
string assetPath = Path.Combine(directory, t.name + "_" + skyBoxImageFBL[orientation] + ".png"); | |
RenderSkyBoxFaceToLRPNG(orientation, go.GetComponent<Camera>(), assetPath); | |
} | |
GameObject.DestroyImmediate(go); | |
//Wire skybox material | |
AssetDatabase.Refresh(); | |
Material skyboxMaterial = new Material(Shader.Find(skyboxShader)); | |
for (int orientation = 0; orientation < skyDirectionFB.Length ; orientation++) | |
{ | |
string texPath = Path.Combine(directory, t.name + "_" + skyBoxImageFBL[orientation] + ".png"); | |
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)); | |
tex.wrapMode = TextureWrapMode.Clamp; | |
skyboxMaterial.SetTexture(skyBoxPropsFBL[orientation], tex); | |
} | |
//Save material | |
string matPath = Path.Combine(directory, t.name + "_skybox" + ".mat"); | |
AssetDatabase.CreateAsset(skyboxMaterial, matPath); | |
} | |
static void RenderSkyboxToFBRPNG(Transform t) | |
{ | |
GameObject go = new GameObject("SkyboxCamera", typeof(Camera)); | |
go.GetComponent<Camera>().useOcclusionCulling = false; | |
go.GetComponent<Camera>().hdr = true; | |
go.GetComponent<Camera>().renderingPath = RenderingPath.DeferredLighting; | |
go.GetComponent<Camera>().clearFlags = CameraClearFlags.Skybox; | |
go.GetComponent<Camera>().fieldOfView = 90; | |
go.GetComponent<Camera>().aspect = 1.0f; | |
go.transform.position = t.position; | |
go.transform.rotation = Quaternion.identity; | |
//Render skybox | |
for (int orientation = 0; orientation < skyDirectionFB.Length ; orientation++) | |
{ | |
string assetPath = Path.Combine(directory, t.name + "_" + skyBoxImageFBR[orientation] + ".png"); | |
RenderSkyBoxFaceToLRPNG(orientation, go.GetComponent<Camera>(), assetPath); | |
} | |
GameObject.DestroyImmediate(go); | |
//Wire skybox material | |
AssetDatabase.Refresh(); | |
Material skyboxMaterial = new Material(Shader.Find(skyboxShader)); | |
for (int orientation = 0; orientation < skyDirectionFB.Length ; orientation++) | |
{ | |
string texPath = Path.Combine(directory, t.name + "_" + skyBoxImageFBR[orientation] + ".png"); | |
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)); | |
tex.wrapMode = TextureWrapMode.Clamp; | |
skyboxMaterial.SetTexture(skyBoxPropsFBR[orientation], tex); | |
} | |
//Save material | |
string matPath = Path.Combine(directory, t.name + "_skybox" + ".mat"); | |
AssetDatabase.CreateAsset(skyboxMaterial, matPath); | |
} | |
static void RenderSkyboxTo6PNG(Transform t) | |
{ | |
GameObject go = new GameObject("SkyboxCamera", typeof(Camera)); | |
go.GetComponent<Camera>().useOcclusionCulling = false; | |
go.GetComponent<Camera>().hdr = true; | |
go.GetComponent<Camera>().renderingPath = RenderingPath.DeferredLighting; | |
go.GetComponent<Camera>().clearFlags = CameraClearFlags.Skybox; | |
go.GetComponent<Camera>().fieldOfView = 90; | |
go.GetComponent<Camera>().aspect = 1.0f; | |
go.transform.position = t.position; | |
go.transform.rotation = Quaternion.identity; | |
//Render skybox | |
for (int orientation = 0; orientation < skyDirection.Length ; orientation++) | |
{ | |
string assetPath = Path.Combine(directory, t.name + "_" + skyBoxImage[orientation] + ".png"); | |
RenderSkyBoxFaceToPNG(orientation, go.GetComponent<Camera>(), assetPath); | |
} | |
GameObject.DestroyImmediate(go); | |
//Wire skybox material | |
AssetDatabase.Refresh(); | |
Material skyboxMaterial = new Material(Shader.Find(skyboxShader)); | |
for (int orientation = 0; orientation < skyDirection.Length ; orientation++) | |
{ | |
string texPath = Path.Combine(directory, t.name + "_" + skyBoxImage[orientation] + ".png"); | |
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)); | |
tex.wrapMode = TextureWrapMode.Clamp; | |
skyboxMaterial.SetTexture(skyBoxProps[orientation], tex); | |
} | |
//Save material | |
string matPath = Path.Combine(directory, t.name + "_skybox" + ".mat"); | |
AssetDatabase.CreateAsset(skyboxMaterial, matPath); | |
} | |
static void RenderSkyBoxFaceToLRPNG(int orientation, Camera cam, string assetPath) | |
{ | |
cam.transform.eulerAngles = skyDirectionFB[orientation]; | |
RenderTexture rt = new RenderTexture(faceSize, faceSize, 24); | |
cam.GetComponent<Camera>().targetTexture = rt; | |
cam.GetComponent<Camera>().Render(); | |
RenderTexture.active = rt; | |
Texture2D screenShot = new Texture2D(faceSize, faceSize, TextureFormat.RGB24, false); | |
screenShot.ReadPixels(new Rect(0, 0, faceSize, faceSize), 0, 0); | |
RenderTexture.active = null; | |
GameObject.DestroyImmediate(rt); | |
byte[] bytes = screenShot.EncodeToPNG(); | |
File.WriteAllBytes(assetPath, bytes); | |
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); | |
} | |
static void RenderSkyBoxFaceToSDPNG(int orientation, Camera cam, string assetPath) | |
{ | |
cam.transform.eulerAngles = skyDirectionSD[orientation]; | |
RenderTexture rt = new RenderTexture(faceSize, faceSize, 24); | |
cam.GetComponent<Camera>().targetTexture = rt; | |
cam.GetComponent<Camera>().Render(); | |
RenderTexture.active = rt; | |
Texture2D screenShot = new Texture2D(faceSize, faceSize, TextureFormat.RGB24, false); | |
screenShot.ReadPixels(new Rect(0, 0, faceSize, faceSize), 0, 0); | |
RenderTexture.active = null; | |
GameObject.DestroyImmediate(rt); | |
byte[] bytes = screenShot.EncodeToPNG(); | |
File.WriteAllBytes(assetPath, bytes); | |
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); | |
} | |
static void RenderSkyBoxFaceToPNG(int orientation, Camera cam, string assetPath) | |
{ | |
cam.transform.eulerAngles = skyDirection[orientation]; | |
RenderTexture rt = new RenderTexture(faceSize, faceSize, 24); | |
cam.GetComponent<Camera>().targetTexture = rt; | |
cam.GetComponent<Camera>().Render(); | |
RenderTexture.active = rt; | |
Texture2D screenShot = new Texture2D(faceSize, faceSize, TextureFormat.RGB24, false); | |
screenShot.ReadPixels(new Rect(0, 0, faceSize, faceSize), 0, 0); | |
RenderTexture.active = null; | |
GameObject.DestroyImmediate(rt); | |
byte[] bytes = screenShot.EncodeToPNG(); | |
File.WriteAllBytes(assetPath, bytes); | |
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment