Unity摄像机画面制作全景图片|截图制作全景图

目录

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

效果展示

unity的场景
在这里插入图片描述
生成的图片
在这里插入图片描述

Unity编辑器中使用脚本部分

[MenuItem("生成图片/CreatPic")]
    public static void A()
    {
    
    
        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],");
            }
        }
    }

Unity编辑器中使用方法

我们的脚本部分写完左上角的菜单栏中会出现生成图片四个字
在这里插入图片描述
打开这个选项我们可以看到CreatPic
然后我们点击后就可以执行完毕
会自动在我们的桌面创建文件夹然后把图片存储进去
在这里插入图片描述
在这里插入图片描述

Unity动态存储图片脚本部分

 Camera cam;
    RenderTexture cubemap;
    RenderTexture equirect;
    [Header("生成次数  true为连续生成")][SerializeField] private bool ison;
    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()
    {
    
    
        if (ison)
        {
    
    
            StartCoroutine(B());
        }
    }

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


        yield return null;
    }


    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();
        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],");
            }
        }
    }

Unity动态存储图片使用方法

动态存储的方法 很简单 随便放置一个空物体然后把我们的脚本放置上去就可以了
在这里插入图片描述
其中注意我们的脚本上会有一个bool值 此值的作用为True的时候会连续存储图片
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42746271/article/details/125893674
今日推荐