Last active
December 29, 2018 18:26
-
-
Save vertis/9cf2dfe42a4c1e0e2369411b014a6d41 to your computer and use it in GitHub Desktop.
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
// From https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Camera.RenderToCubemap.html | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.Rendering; | |
using System.Collections; | |
public class RenderEquirectWizard : ScriptableWizard | |
{ | |
public Transform renderFromPosition; | |
RenderTexture cubemap; | |
public RenderTexture equirect; | |
private void OnEnable() | |
{ | |
cubemap = new RenderTexture(8192, 8192, 24, RenderTextureFormat.ARGB32); | |
cubemap.dimension = UnityEngine.Rendering.TextureDimension.Cube; | |
equirect = new RenderTexture(4096, 2048, 24, RenderTextureFormat.ARGB32); | |
cubemap.Create(); | |
equirect.Create(); | |
} | |
void OnWizardUpdate() | |
{ | |
string helpString = "Select transform to render from and cubemap to render into"; | |
bool isValid = (renderFromPosition != null) && (cubemap != null); | |
} | |
void OnWizardCreate() | |
{ | |
// create temporary camera for rendering | |
GameObject go = new GameObject("CubemapCamera"); | |
go.AddComponent<Camera>(); | |
// place it on the object | |
go.transform.position = renderFromPosition.position; | |
go.transform.rotation = Quaternion.identity; | |
// render into cubemap | |
go.GetComponent<Camera>().RenderToCubemap(cubemap); | |
cubemap.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono); | |
byte[] bytes = toTexture2D(equirect).EncodeToPNG(); | |
System.IO.File.WriteAllBytes(Application.dataPath + "/equirect.png", bytes); | |
//Output the Game data path to the console | |
Debug.Log("Path : " + Application.dataPath); | |
// destroy temporary camera | |
DestroyImmediate(go); | |
} | |
[MenuItem("GameObject/Render Equirect")] | |
static void RenderCubemap() | |
{ | |
ScriptableWizard.DisplayWizard<RenderEquirectWizard>( | |
"Render Equirect", "Render!"); | |
} | |
Texture2D toTexture2D(RenderTexture rTex) | |
{ | |
Texture2D tex = new Texture2D(4096, 2048, TextureFormat.RGB24, false); | |
RenderTexture.active = rTex; | |
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0); | |
tex.Apply(); | |
return tex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment