Skip to content

Instantly share code, notes, and snippets.

@MarcAlx
Created January 15, 2019 17:03
Show Gist options
  • Save MarcAlx/ee2cdfd6881ccc1faf70eaec3a191490 to your computer and use it in GitHub Desktop.
Save MarcAlx/ee2cdfd6881ccc1faf70eaec3a191490 to your computer and use it in GitHub Desktop.
Unity Scene to Equirectangular png (spherical image)
/*
* This script produces a .png image from an Unity scene.
*
* Result image looks like this : https://fr.wikipedia.org/wiki/Fichier:Paris_s%27éveille_equirectangular_panorama.jpg
*
* inspired from : https://docs.unity3d.com/ScriptReference/RenderTexture.ConvertToEquirect.html
* and https://docs.unity3d.com/ScriptReference/Camera.RenderToCubemap.html
*
* To use it add this script to your scene then run
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
public class Main : MonoBehaviour
{
public RenderTexture cubemapLeftEye;
public RenderTexture equirect;
void Start()
{
cubemapLeftEye = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32);
cubemapLeftEye.dimension = TextureDimension.Cube;
equirect = new RenderTexture(2048, 1024, 24, RenderTextureFormat.ARGB32);
}
void LateUpdate()
{
Camera cam = Camera.main;
cam.RenderToCubemap(cubemapLeftEye, 63, Camera.MonoOrStereoscopicEye.Mono);
if (equirect == null)
{
return;
}
cubemapLeftEye.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono);
//convert to png
Texture2D tex = new Texture2D(equirect.width, equirect.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
var bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes("<ADD PATH HERE>/res.png", bytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment