• 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],");
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    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],");
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    Unity动态存储图片使用方法

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

  • 相关阅读:
    在边缘计算场景中使用Dapr
    其他变量定义、简单四则运算、数组
    海藻酸钠-PEG-马来酰亚胺 MAL-PEG-alginate 马来酰亚胺 修饰海藻酸钠
    IEEE期刊如何查找论文模板
    Hadoop 配置 Kerberos 认证
    七 项目管理
    Redis 5 种基本数据结构(String、List、Hash、Set、Sorted Set)详解 | JavaGuide
    CDH大数据平台 ERROR Heartbeating to 192.168.0.200:7182 failed
    Kotlin作用域函数引发的遮蔽问题
    FPGA_边沿检测电路设计
  • 原文地址:https://blog.csdn.net/weixin_42746271/article/details/125893674