12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityでカメラのequirectangular画像を作成してみる

Last updated at Posted at 2018-01-12

c++ - Converting a Cubemap into Equirectangular Panorama - Stack Overflow
を参考にしてUnityのカメラの標準機能を使ってCubemapを作成しequirectangularに変換してUIのImageに表示してみた。

index.gif

やったこと 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CubeMapCamera : MonoBehaviour {

    public Camera camera;
    public Cubemap cubemap;
    public Image image;
    public Material convMaterial;

    public int outputWidth = 4096;
    public int outputHeight = 2048;
    public int cubeWidth = 1280;

    private RenderTexture renderTexture;
    private Texture2D equirectangularTexture;

    // Use this for initialization
    void Start () {
		if (camera == null)
        {
            camera = Camera.main;
        }

        if (cubemap == null)
        {
            cubemap = new Cubemap(cubeWidth, TextureFormat.RGBA32, false);
        }

        if (convMaterial == null)
        {
            Shader conversionShader = Shader.Find("Conversion/CubemapToEquirectangular");
            convMaterial = new Material(conversionShader);
        }
        renderTexture = new RenderTexture(outputWidth, outputHeight, 24);
        equirectangularTexture = new Texture2D(outputWidth, outputHeight, TextureFormat.ARGB32, false);
        image.GetComponent<Image>().material.mainTexture = equirectangularTexture;
    }
	
	// Update is called once per frame
	void Update () {
        camera.RenderToCubemap(cubemap);

        Graphics.Blit(cubemap, renderTexture, convMaterial);
        equirectangularTexture.ReadPixels(new Rect(0, 0, outputWidth, outputHeight), 0, 0, false);
        equirectangularTexture.Apply();
    }
}
  • AssetsでMaterialを作成しShaderに上記のConversion/CubemapToEquirectangularを指定する。作成したMaterialを上記スクリプトのconvMaterialに設定する。
  • UIのImageを作成し、上記スクリプトのimageに設定する。

はまったこと

  • 以下のようにSpriteを作成してimageを更新していると処理に時間がかかって動画がコマ送り状態になる。
image.sprite = Sprite.Create(equirectangularTexture, new Rect(0, 0, equirectangularTexture.width, equirectangularTexture.height), Vector2.zero);
  • WebGL出力する際はShader用のmaterialを作成しておく必要がある。以下のようにMaterialを作っているとGraphics.Blitでエラーになって画像を表示されない。
Shader conversionShader = Shader.Find("Conversion/CubemapToEquirectangular");
convMaterial = new Material(conversionShader);
12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?