Unity 生成全景图

桌面创建“全景图”文件夹

1.编辑状态

using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;

public class CreateStereoCubemaps : MonoBehaviour
{

    [MenuItem("生成图片/CreatPic")]
    public static void A()
    {
        Camera cam = Camera.main;
        RenderTexture cubemap = new RenderTexture(4096, 4096, 32);
        cubemap.dimension = TextureDimension.Cube;
        cam.RenderToCubemap(cubemap, 63, Camera.MonoOrStereoscopicEye.Mono);

        RenderTexture equirect = new RenderTexture(4096, 2048, 32);
        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();
        System.IO.File.WriteAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) 
            + "\\全景图\\" + System.DateTime.Now.Ticks + ".tga", bytes);

    }
}

2.运行状态实时生成

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class CreatCubemap : MonoBehaviour
{
    Camera cam;
    RenderTexture cubemap;
    RenderTexture equirect;
    void Start()
    {
        cam = Camera.main;
        cubemap = new RenderTexture(4096, 4096, 32);
        cubemap.dimension = TextureDimension.Cube;
        equirect = new RenderTexture(4096, 2048, 32);
        StartCoroutine(B());
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator B()
    {
        while(true)
        {
            Creat();
            yield return new WaitForSecondsRealtime(0.04F);
        }  
    }


    public void  Creat()
    {
        cam.RenderToCubemap(cubemap, 63, Camera.MonoOrStereoscopicEye.Mono);
        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();
        System.IO.File.WriteAllBytes(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            + "\\全景图\\" + System.DateTime.Now.Ticks + ".tga", bytes);
    }
}

你可能感兴趣的:(unity,unity,c#)