Last active
April 20, 2020 10:12
-
-
Save openroomxyz/654332269cf673c3483c37e605f3a799 to your computer and use it in GitHub Desktop.
Unity : How to record Spherical 360 Stereoscopic 3d Images ( snapshots, video frame ) ?
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 System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class TakeSpherical360 : MonoBehaviour | |
| { | |
| public Camera cam; | |
| private RenderTexture renderTexture_left; | |
| private RenderTexture renderTexture_right; | |
| private RenderTexture renderTexturteEquirect; | |
| // Start is called before the first frame update | |
| void TakeSnap(string path_EQUI, int resolution_multiple = 8) | |
| { | |
| if(renderTexture_left == null) | |
| { | |
| renderTexture_left = new RenderTexture(1024 * resolution_multiple, 1024 * resolution_multiple, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); | |
| renderTexture_right = new RenderTexture(1024 * resolution_multiple, 1024 * resolution_multiple, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); | |
| renderTexturteEquirect = new RenderTexture(1024 * resolution_multiple, 1024 * resolution_multiple * 2, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); | |
| } | |
| renderTexture_left.dimension = UnityEngine.Rendering.TextureDimension.Cube; | |
| renderTexture_right.dimension = UnityEngine.Rendering.TextureDimension.Cube; | |
| cam.stereoSeparation = 0.064f; | |
| cam.RenderToCubemap(renderTexture_left, 63, Camera.MonoOrStereoscopicEye.Left); //Save( @"C:\Users\..", renderTexture_left); | |
| cam.RenderToCubemap(renderTexture_right, 63, Camera.MonoOrStereoscopicEye.Right); //Save( @"C:\Users\..", renderTexture_right); | |
| renderTexture_left.ConvertToEquirect(renderTexturteEquirect, Camera.MonoOrStereoscopicEye.Left); | |
| renderTexture_right.ConvertToEquirect(renderTexturteEquirect, Camera.MonoOrStereoscopicEye.Right); | |
| Save(path_EQUI, renderTexturteEquirect); | |
| UnityEngine.Resources.UnloadUnusedAssets(); | |
| } | |
| void Save(string path, RenderTexture renderTexture) | |
| { | |
| Texture2D tex = toTexture2D(renderTexture); | |
| byte[] bytes = tex.EncodeToPNG(); | |
| System.IO.File.WriteAllBytes(path, bytes); | |
| } | |
| Texture2D toTexture2D(RenderTexture rTex) | |
| { | |
| int widht = rTex.width; | |
| int height = rTex.height; | |
| Texture2D tex = new Texture2D(widht, height, TextureFormat.RGB24, false); | |
| RenderTexture.active = rTex; | |
| tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0); | |
| tex.Apply(); | |
| RenderTexture.active = null; | |
| return tex; | |
| } | |
| int cycle = 0; | |
| bool record = false; | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (Input.GetKeyDown(KeyCode.T)) | |
| { | |
| record = true; | |
| } | |
| if(record) | |
| { | |
| TakeSnap(@"C:\Users\x\Desktop\x\"+cycle+".png"); | |
| cycle += 1; | |
| if(cycle == 10) | |
| { | |
| cycle = 0; | |
| record = false; | |
| } | |
| } | |
| else | |
| { | |
| if(renderTexture_left != null) | |
| { | |
| DestroyImmediate(renderTexture_left); | |
| DestroyImmediate(renderTexture_right); | |
| DestroyImmediate(renderTexturteEquirect); | |
| UnityEngine.Resources.UnloadAsset(renderTexture_left); | |
| UnityEngine.Resources.UnloadAsset(renderTexture_right); | |
| UnityEngine.Resources.UnloadAsset(renderTexturteEquirect); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment