直接上代码
[MenuItem("生成图片/CreatPic")]
public static void CreatPic()
{
Camera cam = Camera.main;
//可修改数值 new RenderTexture(4096, 4096, 32);
RenderTexture cubemap = new RenderTexture(2048, 2048, 16);
cubemap.dimension = TextureDimension.Cube;
cam.RenderToCubemap(cubemap, 63, Camera.MonoOrStereoscopicEye.Mono);
//可修改数值 new RenderTexture(4096, 4096, 32);
RenderTexture equirect = new RenderTexture(1920, 1080, 8);
cubemap.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono);
RenderTexture.active = equirect;
Texture2D tex = new Texture2D(equirect.width, equirect.height, TextureFormat.ARGB32, false, true);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
RenderTexture.active = null;
GL.Clear(true, true, Color.black);
tex.Apply();
byte[] bytes = tex.EncodeToTGA();
CreateDirectroryOfFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\全景图\\)");
System.IO.File.WriteAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
+ "\\全景图\\" + System.DateTime.Now.Ticks + ".tga", bytes);
}
public static void CreateDirectroryOfFile(string filePath)
{
Debug.Log($"CreateDirectrory {filePath}[folder_path],");
if (!string.IsNullOrEmpty(filePath))
{
string dir_name = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dir_name))
{
Debug.Log($"No Exists {dir_name}[dir_name],");
Directory.CreateDirectory(dir_name);
}
else
{
Debug.Log($"Exists {dir_name}[dir_name],");
}
}
}
在桌面生成TGA格式的全景图,如果想要别的格式用格式工厂轻松搞定