コード例 #1
0
    public void RemakeImageFast()
    {
        GameObject     obj    = GameObject.Find("Button3");
        OpenFileScript filesc = obj.GetComponent <OpenFileScript>();

        _resolutionWidth = filesc.ReadImageWidth;

        RenderTexture tex    = new RenderTexture(_resolutionWidth / 4, _resolutionWidth / 4, dispCamera.targetTexture.depth, dispCamera.targetTexture.format);
        GameObject    camobj = GameObject.Find("BGCamera");
        Camera        cam    = camobj.GetComponent <Camera>();

        cam.targetTexture = tex;

        CreateEquirectangular(_resolutionWidth);
        //_finalTex.Apply ();
        //GetComponent<Image>().material.mainTexture = _finalTex;
    }
コード例 #2
0
    //------------------------------------------------------------------------------
    // 現在視点から全方位パノラマ画像を作成する
    Texture2D CreateEquirectangular(int width)
    {
        Texture2D imageCube = CreateCubeMap(width);

        _finalTex = imageCube;
        //debug
        {
            byte[] data = imageCube.EncodeToPNG();
            System.IO.File.WriteAllBytes("C:/temp/imagecube.jpg", data);
        }

        /*
         *      //StartCoroutine(sub111());
         *      var task1 = await sub111();
         *      Debug.Log(task1);
         *     // return imageCube;
         *  }
         *
         *  async Task<string> sub111()
         *  {
         *      return await Task.Run<string>(() =>
         */

        {
            //Texture2D imageCube = _finalTex;
            Texture2D equiImage = new Texture2D(imageCube.width, imageCube.width / 2);
            //equiImage->allocateImage(imageCube->s(), imageCube->s()/2, 1, GL_RGBA, GL_UNSIGNED_BYTE);
            float u, v;         // Normalised texture coordinates, from 0 to 1, starting at lower left corner
            float phi, theta;   // Polar coordinates
            int   cubeFaceWidth, cubeFaceHeight;

            cubeFaceWidth  = imageCube.width / 4;   // 4 horizontal faces
            cubeFaceHeight = imageCube.height / 3;  // 3 vertical faces

            int equiW = equiImage.width;
            int equiH = equiImage.height;


            // スライダ用
            //float posCur = 0f;
            //float posMax = (float)equiH;
            GameObject obj1   = GameObject.Find("SliderProgressBar");
            Slider     slider = obj1.GetComponent <Slider>();

            for (int j = 0; j < equiH; j++)
            {
                // スライダ
                slider.value = (float)j / equiH;

                //Rows start from the bottom
                v     = 1f - ((float)j / (float)equiH);
                theta = v * Mathf.PI;

                for (int i = 0; i < equiW; i++)
                {
                    //Columns start from the left
                    u   = (float)i / (float)equiW;
                    phi = u * 2f * Mathf.PI;

                    float x, y, z; // ベクトル
                    x = Mathf.Sin(phi) * Mathf.Sin(theta) * -1f;
                    y = Mathf.Cos(theta);
                    z = Mathf.Cos(phi) * Mathf.Sin(theta) * -1f;

                    float a;    // Intensity
                    a = Mathf.Max(Mathf.Abs(x), Mathf.Abs(y));
                    a = Mathf.Max(a, Mathf.Abs(z));

                    //Vector Parallel to the unit vector that lies on one of the cube faces
                    float xa, ya, za;    // 単位ベクトル
                    xa = x / a;
                    ya = y / a;
                    za = z / a;

                    //Color color;
                    int xPixel, yPixel;
                    int xOffset, yOffset;

                    if (xa == 1)
                    {//Right
                        xPixel  = (int)((((za + 1f) / 2f) - 1f) * cubeFaceWidth);
                        xOffset = 2 * cubeFaceWidth;
                        yPixel  = (int)((((ya + 1f) / 2f)) * cubeFaceHeight);
                        yOffset = cubeFaceHeight;
                    }
                    else if (xa == -1)
                    {//Left
                        xPixel  = (int)((((za + 1f) / 2f)) * cubeFaceWidth);
                        xOffset = 0;
                        yPixel  = (int)((((ya + 1f) / 2f)) * cubeFaceHeight);
                        yOffset = cubeFaceHeight;
                    }
                    else if (ya == 1)
                    {//Up
                        xPixel  = (int)((((xa + 1f) / 2f)) * cubeFaceWidth);
                        xOffset = cubeFaceWidth;
                        yPixel  = (int)((((za + 1f) / 2f) - 1f) * cubeFaceHeight);
                        yOffset = 2 * cubeFaceHeight;
                    }
                    else if (ya == -1)
                    {//Down
                        xPixel  = (int)((((xa + 1f) / 2f)) * cubeFaceWidth);
                        xOffset = cubeFaceWidth;
                        yPixel  = (int)((((za + 1f) / 2f)) * cubeFaceHeight);
                        yOffset = 0;
                    }
                    else if (za == 1)
                    {//Front
                        xPixel  = (int)((((xa + 1f) / 2f)) * cubeFaceWidth);
                        xOffset = cubeFaceWidth;
                        yPixel  = (int)((((ya + 1f) / 2f)) * cubeFaceHeight);
                        yOffset = cubeFaceHeight;
                    }
                    else if (za == -1)
                    {//Back
                        xPixel  = (int)((((xa + 1f) / 2f) - 1f) * cubeFaceWidth);
                        xOffset = 3 * cubeFaceWidth;
                        yPixel  = (int)((((ya + 1f) / 2f)) * cubeFaceHeight);
                        yOffset = cubeFaceHeight;
                    }
                    else
                    {// ここに来たらおかしいと判断すべき
                        xPixel  = 0;
                        yPixel  = 0;
                        xOffset = 0;
                        yOffset = 0;
                    }

                    xPixel = Mathf.Abs(xPixel);
                    yPixel = Mathf.Abs(yPixel);

                    xPixel += xOffset;
                    yPixel += yOffset;

                    // データ不正チェック
                    if (imageCube.width <= xPixel)
                    {
                        xPixel = imageCube.width - 1;
                    }
                    else if (imageCube.height <= yPixel)
                    {
                        yPixel = imageCube.height - 1;
                    }
                    else if (equiImage.width <= i)
                    {
                        i = equiImage.width - 1;
                    }
                    else if (equiImage.height <= j)
                    {
                        j = equiImage.height - 1;
                    }

                    //unsigned char* data1 = imageCube->data(xPixel, yPixel);
                    //unsigned char* data2 = equiImage->data(i, j);
                    //memcpy(&data2[0], &data1[0], sizeof(unsigned char) * 4);
                    //data2[3] = (unsigned char)255;
                    Color col1 = imageCube.GetPixel(xPixel, yPixel);
                    //Color col2 = equiImage.GetPixel (i, j);
                    equiImage.SetPixel(equiImage.width - 1 - i, j, col1);   // Xは逆にする

                    // デバッグ用(注意:超遅くなります)
                    //CString msg;
                    //msg.Format("x=%4d y=%4d | i=%4d, j=%4d", xPixel, yPixel, i, j);
                    //CAirStatusBar::Instance()->SetTextMessage(msg);
                    //Log::Instance()->Write("x=%4d y=%4d | i=%4d, j=%4d", xPixel, yPixel, i, j);
                }
            }

            //  slider.value = 0f;

            //debug
            {
                GameObject     obj    = GameObject.Find("Button3");
                OpenFileScript filesc = obj.GetComponent <OpenFileScript>();

                string filename = filesc.File.Substring(0, filesc.File.Length - 4); // 拡張子抜き
                string ext      = "_adj.png";
                string path     = filesc.Dir + filename + ext;
                byte[] data     = equiImage.EncodeToPNG();
                System.IO.File.WriteAllBytes(path, data);
            }

            return(equiImage);
            // yield return equiImage;
            //return "完了";

            //});
        }
    }