Unity カメラ画面からパノラマ写真を作成する | パノラマを作成するためのスクリーンショット

目次

こんにちは!これは、 Markdown エディターを初めて使用するときに表示されるウェルカム ページです。Markdown エディターの使用方法を学びたい場合は、この記事を注意深く読んで、Markdown の基本構文を理解してください。

結果を示す

団結の情景
ここに画像の説明を挿入
が生み出す映像
ここに画像の説明を挿入

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