Exemplo n.º 1
0
 private void holdCubemap()
 {
     cubemapColors.Clear();
     for (int a = 0; a < 6; a++)
     {
         Color[] col = cubemap.GetPixels(faceArray[a], 0); // Get previous face colors
         cubemapColors.Add(col);
     }
 }
Exemplo n.º 2
0
    void CreateSphere(Cubemap _texture, Vector3 pos)
    {
        GameObject go   = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        GameObject temp = new GameObject(_texture.ToString());

        spheres.Add(go.transform);
        go.transform.SetParent(temp.transform);
        Transform t = temp.GetComponent <Transform> ();

        t.localScale = Vector3.one * scale;
        MeshRenderer mr  = go.GetComponent <MeshRenderer> ();
        Material     mat = new Material(_shader);

        Cubemap tex = null;

        tex = new Cubemap(_texture.width, TextureFormat.ARGB32, false);
        Color[][] pixels = new Color[6][];
        if (!getPixel)
        {
            for (int i = 0; i < 6; i++)
            {
                pixels [i] = new Color[_texture.width * _texture.width];
                pixels [i] = _texture.GetPixels(cubemapFaces [i]);
                tex.SetPixels(pixels [i], cubemapFaces [i]);
            }
        }
        else
        {
            for (int ii = 0; ii < 6; ii++)
            {
                pixels [ii] = new Color[_texture.width * _texture.width];
                int i = 0;
                for (int y = 0; y < _texture.width; y++)
                {
                    for (int x = 0; x < _texture.width; x++)
                    {
                        pixels [ii] [i] = _texture.GetPixel(cubemapFaces [ii], x, y);
                        i++;
                    }
                }
                tex.SetPixels(pixels [ii], cubemapFaces [ii]);
            }
        }
        //tex.SetPixels (pixels);
        tex.Apply();
        LabelMaker.MakeLabel(_texture.name.ToString().Remove(0, 5), Color.white, t);
        tex.filterMode  = FilterMode.Point;
        mat.mainTexture = tex;
        mat.name        = _texture.name;
        go.name         = _texture.name;
        mr.material     = mat;
        tex.wrapMode    = TextureWrapMode.Clamp;
        tex.Apply();
        t.SetParent(transform);
        t.position = pos;
    }
Exemplo n.º 3
0
 /// <summary>
 /// Capture frame Sync impl.
 /// </summary>
 private void CaptureCubemapFrameSync()
 {
     // TODO, make this into shader for GPU fast processing.
     if (panoramaProjection == PanoramaProjectionType.CUBEMAP)
     {
         captureCamera.RenderToCubemap(frameCubemap);
         captureCamera.Render();
         frameTexture.SetPixels(0, cubemapSize, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.PositiveX));
         frameTexture.SetPixels(cubemapSize, cubemapSize, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.NegativeX));
         frameTexture.SetPixels(cubemapSize * 2, cubemapSize, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.PositiveY));
         frameTexture.SetPixels(0, 0, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.NegativeY));
         frameTexture.SetPixels(cubemapSize, 0, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.PositiveZ));
         frameTexture.SetPixels(cubemapSize * 2, 0, cubemapSize, cubemapSize, frameCubemap.GetPixels(CubemapFace.NegativeZ));
         frameTexture.Apply();
     }
     else if (panoramaProjection == PanoramaProjectionType.EQUIRECTANGULAR)
     {
         if (!captureGUI)
         {
             captureCamera.farClipPlane = 100;
             captureCamera.Render();
             captureCamera.RenderToCubemap(panoramaTempRenderTexture);
             // Convert to equirectangular projection.
             Graphics.Blit(panoramaTempRenderTexture, frameRenderTexture, cubemap2Equirectangular);
         }
         else
         {
             RenderToCubemapWithGUI(captureCamera, panoramaTempRenderTexture);
             // Convert to equirectangular projection.
             frameRenderTexture.DiscardContents();
             Graphics.Blit(panoramaTempRenderTexture, frameRenderTexture, cubemap2Equirectangular);
             copyReverseMaterial.DisableKeyword("REVERSE_TOP_BOTTOM");
             copyReverseMaterial.EnableKeyword("REVERSE_LEFT_RIGHT");
             Graphics.Blit(frameRenderTexture, finalTargetTexture, copyReverseMaterial);
             frameRenderTexture.DiscardContents();
             Graphics.Blit(finalTargetTexture, frameRenderTexture, blitMaterial);
         }
         // From frameRenderTexture to frameTexture.
         CopyFrameTexture();
     }
     // Send for encoding.
     EnqueueFrameTexture();
 }
Exemplo n.º 4
0
    // Use this for initialization
    void Start()
    {
        Texture2D t = new Texture2D(1024, 1024);

        _cam.GetComponent <Camera> ().RenderToCubemap(_c);
        Color[] c = _c.GetPixels(CubemapFace.PositiveX);
        t.SetPixels(c);
        t.Apply();

        _m.SetTexture("Main_Tex", t);
    }
Exemplo n.º 5
0
    public static bool CPU_Project_Uniform_9Coeff(Cubemap input, Vector4[] output)
    {
        if (output.Length != 9)
        {
            Debug.LogWarning("output size must be 9 for 9 coefficients");
            return(false);
        }

        if (input.width != input.height)
        {
            Debug.LogWarning("input cubemap must be square");
            return(false);
        }

        Color[] input_face;
        int     size = input.width;



        //cycle on all 6 faces of the cubemap
        for (int face = 0; face < 6; ++face)
        {
            input_face = input.GetPixels((CubemapFace)face);

            //cycle all the texels
            for (int texel = 0; texel < size * size; ++texel)
            {
                float u = (texel % size) / (float)size;
                float v = ((int)(texel / size)) / (float)size;

                //get the direction vector
                Vector3 dir      = DirectionFromCubemapTexel(face, u, v);
                Color   radiance = input_face[texel];

                //compute the differential solid angle
                float d_omega = DifferentialSolidAngle(size, u, v);

                //cycle for 9 coefficients
                for (int c = 0; c < 9; ++c)
                {
                    //compute shperical harmonic
                    float sh = SphericalHarmonicsBasis.Eval[c](dir);

                    output[c].x += radiance.r * d_omega * sh;
                    output[c].y += radiance.g * d_omega * sh;
                    output[c].z += radiance.b * d_omega * sh;
                    output[c].w += radiance.a * d_omega * sh;
                }
            }
        }

        return(true);
    }
Exemplo n.º 6
0
void ConvertToPng()
{
    Debug.Log(Application.dataPath + "/" +cubemap.name +"_PositiveX.png");
    var tex = new Texture2D (cubemap.width, cubemap.height, TextureFormat.RGB24, false);
    // Read screen contents into the texture        
    tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));        
    // Encode texture into PNG
    var bytes = tex.EncodeToPNG();      
    File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name +"_PositiveX.png", bytes);       

    tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
    bytes = tex.EncodeToPNG();     
      File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name +"_NegativeX.png", bytes);       

      tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
      bytes = tex.EncodeToPNG();     
    File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name +"_PositiveY.png", bytes);       

      tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
      bytes = tex.EncodeToPNG();     
      File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name +"_NegativeY.png", bytes);       

      tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
      bytes = tex.EncodeToPNG();     
      File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name +"_PositiveZ.png", bytes);       

      tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
      bytes = tex.EncodeToPNG();     
      File.WriteAllBytes(Application.dataPath + "/"  + cubemap.name   +"_NegativeZ.png", bytes);       
    DestroyImmediate(tex);

    }
Exemplo n.º 7
0
        private void setTextureData()
        {
            if (obj is Cubemap)
            {
                Cubemap cubemap = obj as Cubemap;

                try
                {
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.PositiveX)), obj.name + "_right"));
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.NegativeX)), obj.name + "_left"));
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.NegativeY)), obj.name + "_down"));
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.PositiveY)), obj.name + "_up"));
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.PositiveZ)), obj.name + "_forward"));
                    textureData.Add(imageData(createTexture(obj.width, obj.height, cubemap.GetPixels(CubemapFace.NegativeZ)), obj.name + "_back"));
                }
                catch (Exception e)
                {
                    Debug.LogError("IS " + AssetDatabase.GetAssetPath(obj) + " Cubemap set to readable? " + e.Message);
                }
            }
            if (obj is Texture2D)
            {
                try
                {
                    textureData.Add(imageData(createTexture(obj.width, obj.height, (obj as Texture2D).GetPixels()), obj.name));
                }
                catch (Exception e)
                {
                    Debug.LogError("IS " + AssetDatabase.GetAssetPath(obj) + " Sprite? " + e.Message);
                }
            }
        }
Exemplo n.º 8
0
        public static void exportCubemap(Cubemap envMap, string name)
        {
            Directory.CreateDirectory("cubeExport");
            Texture2D tex = new Texture2D(envMap.width, envMap.height, TextureFormat.ARGB32, false);

            for (int i = 0; i < 6; i++)
            {
                tex.SetPixels(envMap.GetPixels((CubemapFace)i));
                byte[] bytes = tex.EncodeToPNG();
                File.WriteAllBytes("cubeExport/" + name + "-" + i + ".png", bytes);
            }
            GameObject.Destroy(tex);
        }
Exemplo n.º 9
0
    byte[] getPlanePixels(Texture2D _tex, CubemapFace _face)
    {
        Texture2D tmpTex = new Texture2D(cubemap.width, cubemap.height, TextureFormat.RGB24, false);

        tmpTex.SetPixels(cubemap.GetPixels(_face));
        Color[] vline;
        for (int x = 0; x < cubemap.width; ++x)
        {
            vline = tmpTex.GetPixels(x, 0, 1, cubemap.height);
            _tex.SetPixels(x, 0, 1, cubemap.height, vline.Reverse().ToArray());
        }
        return(_tex.EncodeToPNG());
    }
Exemplo n.º 10
0
        /// <summary>Copies a cubemap face to a raw array of colors.</summary>
        /// <param name="data">Raw array to store the colors.</param>
        /// <param name="map">The cubemap to pull from.</param>
        /// <param name="face">The cubemap face to pull from.</param>
        /// <param name="startX">The pixel column to start writing data to.</param>
        private static void CopyFace(ref Color[] data, Cubemap map, CubemapFace face, int startX)
        {
            Color[] faceData = map.GetPixels(face);

            for (int y = 0; y < map.height; y++)
            {
                int yStride = y * map.width * 6;
                for (int x = 0; x < map.width; x++)
                {
                    int i = startX + x + yStride;
                    data[i] = faceData[x + ((map.height - 1) - y) * map.width];
                }
            }
        }
Exemplo n.º 11
0
    // Find distance between neighborhood samples
    //float getDistanceBetweenSamples() {
    //    //Debug.Log("Distance: " + (transform.position - mainCam.transform.position).magnitude);
    //    return (transform.position - mainCam.transform.position).magnitude * clientLOS.SMOOTHING / faceSideLength;
    //}

    // Apply cubemap texture to skyMat material
    void applyCubemap()
    {
        setCubemap();
        cubemap.Apply(); // Save changes from Setting Cubemap faces to our stored/sampled cubemap.


        // Assign lighting cubemap to skybox so objects adhere to lighting.
        skyMaterial.SetTexture("_Tex", cubemap);



        Texture red_tex = new Texture2D(probeSampleSize, probeSampleSize);

        for (int i = 0; i < probeSampleSize; i++)
        {
            for (int j = 0; j < probeSampleSize; j++)
            {
            }
        }



        Texture2D debugtex = new Texture2D(probeSampleSize, probeSampleSize);

        debugtex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
        debugViewer.texture = debugtex;

        // debug cubemap texture
        string properties = "";

        string[] y = skyMaterial.GetTexturePropertyNames();
        foreach (string p in y)
        {
            properties += p + " ";
        }
        Debug.Log("PROPERTIES: " + properties);
    }
Exemplo n.º 12
0
        // input/output
        public void fromCube(Cubemap cube, int mip, ColorMode cubeColorMode, bool useGamma)
        {
            int mipSize = cube.width >> mip;

            if (pixels == null || faceSize != mipSize)
            {
                resize(mipSize);
            }
            for (int face = 0; face < 6; ++face)
            {
                Color[] src = cube.GetPixels((CubemapFace)face, mip);
                pixelCopy(ref pixels, face * faceSize * faceSize, src, 0, src.Length);
            }
            decode(ref pixels, pixels, cubeColorMode, useGamma);
        }
Exemplo n.º 13
0
    void CannyCubemap(Cubemap cube, Cubemap cannycube)
    {
        Texture2D faceTex = new Texture2D(cannycube.height, cannycube.width);

        foreach (CubemapFace face in System.Enum.GetValues(typeof(CubemapFace)))
        {
            if (cube)
            {
                Color[] cols = cube.GetPixels(face, mip);
                faceTex.SetPixels(cols);
                cols = CannyTex2d(faceTex, s1, s2, f1).GetPixels();
                cannycube.SetPixels(cols, face);
            }
        }
        cannycube.Apply();
    }
        /// <summary>
        /// Texture2Dの向きに合わせてCubemapの指定されたfaceのピクセルにてSetPixelsを実施
        ///
        /// 制限事項
        /// blockWidth, blockHeightはTexture2DおよびCubemapのwidth, height以下の値で指定すること
        /// </summary>
        public static void SetPixels(this Texture2D texture,
                                     int x, int y, int blockWidth, int blockHeight,
                                     Cubemap cubemap, CubemapFace face)
        {
            Color[] pixels = cubemap.GetPixels(face);

            // Cubemap.GetPixelsは右上から左下の順番のため、
            // SetPixelsに左下から右上の順番に合わせて反転させる
            System.Array.Reverse(pixels);
            for (int i = 0; i < blockWidth; ++i)
            {
                System.Array.Reverse(pixels, i * blockHeight, blockWidth);
            }

            texture.SetPixels(x, y, blockWidth, blockHeight, pixels);
        }
Exemplo n.º 15
0
    private Texture2DArray ToTexture2DArray(Cubemap cubemap)
    {
        CubemapFace[] faces = new CubemapFace[] {
            CubemapFace.PositiveX, CubemapFace.NegativeX,
            CubemapFace.PositiveY, CubemapFace.NegativeY,
            CubemapFace.PositiveZ, CubemapFace.NegativeZ
        };

        Texture2DArray array = new Texture2DArray(cubemap.width, cubemap.width, 6, cubemap.format, false);

        for (int i = 0; i < 6; ++i)
        {
            array.SetPixels(cubemap.GetPixels(faces[i]), i, 0);
        }

        return(array);
    }
Exemplo n.º 16
0
    private Texture2D ToTexture2D(Cubemap cubemap)
    {
        CubemapFace[] faces = new CubemapFace[] {
            CubemapFace.PositiveX, CubemapFace.NegativeX,
            CubemapFace.PositiveY, CubemapFace.NegativeY,
            CubemapFace.PositiveZ, CubemapFace.NegativeZ
        };

        Texture2D texture = new Texture2D(6 * cubemap.width, cubemap.width, cubemap.format, false);

        for (int i = 0; i < 6; ++i)
        {
            texture.SetPixels(i * cubemap.width, 0, cubemap.width, cubemap.width, cubemap.GetPixels(faces[i]));
        }

        return(texture);
    }
Exemplo n.º 17
0
        private Texture2D Cubemap2Texture2D(Cubemap cubemap)
        {
            var tex = new Texture2D(cubemap.width * 6, cubemap.height, TextureFormat.RGBAHalf, true);

            // Combine cubemap sides to one Texture2D
            tex.SetPixels(0, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.PositiveX));
            tex.SetPixels(cubemap.width, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.NegativeX));
            tex.SetPixels(cubemap.width * 2, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.PositiveY));
            tex.SetPixels(cubemap.width * 3, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.NegativeY));
            tex.SetPixels(cubemap.width * 4, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.PositiveZ));
            tex.SetPixels(cubemap.width * 5, 0, cubemap.width, cubemap.width, cubemap.GetPixels(CubemapFace.NegativeZ));
            return(tex);
        }
Exemplo n.º 18
0
    public static bool CPU_Project_MonteCarlo_9Coeff(Cubemap input, Vector4[] output, int sample_count)
    {
        if (output.Length != 9)
        {
            Debug.LogWarning("output size must be 9 for 9 coefficients");
            return(false);
        }

        //cache the cubemap faces
        List <Color[]> faces = new List <Color[]>();

        for (int f = 0; f < 6; ++f)
        {
            faces.Add(input.GetPixels((CubemapFace)f, 0));
        }

        for (int c = 0; c < 9; ++c)
        {
            for (int s = 0; s < sample_count; ++s)
            {
                Vector3 dir   = Random.onUnitSphere;
                int     index = GetTexelIndexFromDirection(dir, input.height);
                int     face  = FindFace(dir);

                //read the radiance texel
                Color radiance = faces[face][index];

                //compute shperical harmonic
                float sh = SphericalHarmonicsBasis.Eval[c](dir);

                output[c].x += radiance.r * sh;
                output[c].y += radiance.g * sh;
                output[c].z += radiance.b * sh;
                output[c].w += radiance.a * sh;
            }

            output[c].x = output[c].x * 4.0f * Mathf.PI / (float)sample_count;
            output[c].y = output[c].y * 4.0f * Mathf.PI / (float)sample_count;
            output[c].z = output[c].z * 4.0f * Mathf.PI / (float)sample_count;
            output[c].w = output[c].w * 4.0f * Mathf.PI / (float)sample_count;
        }

        return(true);
    }
Exemplo n.º 19
0
    private static Color[] GetPixels(Cubemap c, CubemapFace f)
    {
        var pixels = c.GetPixels(f);
        var halfH  = c.height / 2;

        for (var y = 0; y < halfH; y++)
        {
            var o = c.width * y;
            var n = c.width * (c.height - y - 1);
            for (var x = 0; x < c.width; x++)
            {
                var a = pixels[o + x];
                var b = pixels[n + x];
                pixels[o + x] = b;
                pixels[n + x] = a;
            }
        }
        return(pixels);
    }
Exemplo n.º 20
0
    public void CreateCubeMapSides()
    {
        string folderPath = "";

        if (folderToSaveTextures == null)
        {
            folderPath = Application.dataPath + "/";
        }
        else
        {
            folderPath = AssetDatabase.GetAssetPath(folderToSaveTextures) + "/";
        }

        Cubemap cubemap = new Cubemap(textureSize, TextureFormat.RGB24, false);

        camera.RenderToCubemap(cubemap);

        Texture2D tex = new Texture2D(textureSize, textureSize, TextureFormat.RGB24, false);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
        var bytes = tex.EncodeToPNG();

        File.WriteAllBytes(folderPath + cubemapName + "_PositiveX.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(folderPath + cubemapName + "_NegativeX.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(folderPath + cubemapName + "_PositiveY.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(folderPath + cubemapName + "_NegativeY.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(folderPath + cubemapName + "_PositiveZ.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(folderPath + cubemapName + "_NegativeZ.png", bytes);

        AssetDatabase.Refresh();

        DestroyImmediate(tex);

        Debug.Log("Saved to: " + folderPath, folderToSaveTextures);
    }
Exemplo n.º 21
0
    /// <summary>
    /// Width = 6 * textureSize, height = textureSize
    /// </summary>
    /// <param name="camera"></param>
    /// <param name="textureSize"></param>
    /// <returns></returns>
    public static Color[] CreateCubemapPixels(Camera camera, int textureSize)
    {
        Cubemap cubemap = new Cubemap(textureSize, TextureFormat.RGB24, false);

        camera.RenderToCubemap(cubemap);

        var newPixels = new Color[textureSize * textureSize * 6];

        List <Color[]> pixels = new List <Color[]>
        {
            cubemap.GetPixels(CubemapFace.PositiveX),
            cubemap.GetPixels(CubemapFace.NegativeX),
            cubemap.GetPixels(CubemapFace.PositiveY),
            cubemap.GetPixels(CubemapFace.NegativeY),
            cubemap.GetPixels(CubemapFace.PositiveZ),
            cubemap.GetPixels(CubemapFace.NegativeZ)
        };

        int whidth, height, n;

        for (int i = 0; i < newPixels.Length; i++)
        {
            whidth = i % textureSize;
            height = i / textureSize / 6;
            n      = i / textureSize % 6;

            try
            {
                if (n == 3)
                {
                    newPixels[i] = pixels[n][textureSize * whidth + (textureSize - height - 1)];
                }
                else if (n == 2)
                {
                    newPixels[i] = pixels[2][textureSize * (textureSize - whidth - 1) + height];
                }
                else
                {
                    newPixels[newPixels.Length - i - 1] = pixels[n][textureSize * height + whidth];
                }
            }
            catch
            {
                Debug.LogError(i + " " + whidth + " " + height + " " + n);
                break;
            }
        }

        return(newPixels);
    }
    public static int GetPixels(IntPtr l)
    {
        int result;

        try
        {
            int num = LuaDLL.lua_gettop(l);
            if (num == 2)
            {
                Cubemap     cubemap = (Cubemap)LuaObject.checkSelf(l);
                CubemapFace face;
                LuaObject.checkEnum <CubemapFace>(l, 2, out face);
                Color[] pixels = cubemap.GetPixels(face);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, pixels);
                result = 2;
            }
            else if (num == 3)
            {
                Cubemap     cubemap2 = (Cubemap)LuaObject.checkSelf(l);
                CubemapFace face2;
                LuaObject.checkEnum <CubemapFace>(l, 2, out face2);
                int miplevel;
                LuaObject.checkType(l, 3, out miplevel);
                Color[] pixels2 = cubemap2.GetPixels(face2, miplevel);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, pixels2);
                result = 2;
            }
            else
            {
                LuaObject.pushValue(l, false);
                LuaDLL.lua_pushstring(l, "No matched override function GetPixels to call");
                result = 2;
            }
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Exemplo n.º 23
0
    private void OnWizardCreate()
    {
        if (cubemap == null)
        {
            return;
        }

        int width  = cubemap.width;
        int height = cubemap.height;

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        string getPath = AssetDatabase.GetAssetPath(cubemap.GetInstanceID());

        string[] cubemapTargetPath = getPath.Split(new string[] { "/" }, System.StringSplitOptions.None);
        string   path = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + getPath.Substring(0, cubemapTargetPath[cubemapTargetPath.Length - 1].Length + 1) + cubemap.name;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
        byte[] bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Right.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Left.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Up.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Down.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Forward.png", bytes);

        tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
        bytes = tex.EncodeToPNG();
        File.WriteAllBytes(path + "/Backward.png", bytes);

        DestroyImmediate(tex);
        AssetDatabase.Refresh();
    }
Exemplo n.º 24
0
    void FakeHDR (Cubemap CubeMap, bool hasmipLevels) {
	    int maxMipLevels = 1;
	    if (hasmipLevels) {
		    maxMipLevels = (int)(Mathf.Log((float)CubeMap.width, 2.0f)) + 1;
	    }
	    int base_Size = CubeMap.width;
	    Vector4 rgbmColor = new Vector4();

	    for (int mipLevel = 0; mipLevel < maxMipLevels; mipLevel++)
	    {
		    int a_Size = base_Size >> mipLevel;
		    // As we use a_Size as pointer in our arrays we have to lower it by 1
		    //a_Size -= 1;


		    for (int a_FaceIdx = 0; a_FaceIdx < 6; a_FaceIdx++)
		    {
			    // Get all pixels of the given face
			    Color[] FaceColors = CubeMap.GetPixels((CubemapFace)a_FaceIdx, mipLevel);
			    // Iterate over dst cube map face texel
			    for (int a_V  = 0; a_V < a_Size; a_V++)
			    {
				    for (int a_U = 0; a_U < a_Size; a_U++)
				    {
					    rgbmColor.x = FaceColors[a_V * a_Size + a_U].r;
					    rgbmColor.y = FaceColors[a_V * a_Size + a_U].g;
					    rgbmColor.z = FaceColors[a_V * a_Size + a_U].b;
					    rgbmColor *= 1.0f/6.0f;
					    rgbmColor.w = Mathf.Clamp01(Mathf.Max(Mathf.Max(rgbmColor.x, rgbmColor.y), rgbmColor.z));
					    rgbmColor.w = Mathf.Ceil(rgbmColor.w*255.0f) / 255.0f;
					    rgbmColor.x = rgbmColor.x / rgbmColor.w;
					    rgbmColor.y = rgbmColor.y / rgbmColor.w;
					    rgbmColor.z = rgbmColor.z / rgbmColor.w;
					    FaceColors[a_V * a_Size + a_U] = rgbmColor;
				    }
			    }
			    CubeMap.SetPixels(FaceColors,(CubemapFace)a_FaceIdx, mipLevel);
		    }
	    }
	    CubeMap.Apply(false);
    }
Exemplo n.º 25
0
        public static void SaveAsRgbaDds(Cubemap texture, string fileName, bool convertToSRGB = false)
        {
            using (var fileStream = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                using (var binaryWriter = new BinaryWriter(fileStream))
                {
                    WriteHeader(binaryWriter, texture.width, texture.height, texture.mipmapCount, true);
                    var facesInOrder = new[]
                    {
                        CubemapFace.PositiveX,
                        CubemapFace.NegativeX,
                        CubemapFace.PositiveY,
                        CubemapFace.NegativeY,
                        CubemapFace.PositiveZ,
                        CubemapFace.NegativeZ
                    };
                    foreach (var cubemapFace in facesInOrder)
                    {
                        for (var mipIndex = 0; mipIndex < texture.mipmapCount; ++mipIndex)
                        {
                            var pixels = texture.GetPixels(cubemapFace, mipIndex);
                            var buf    = new Color32[pixels.Length];
                            for (var index = 0; index < pixels.Length; index++)
                            {
                                buf[index] = pixels[index];
                            }

                            if (convertToSRGB)
                            {
                                WriteLinearAsSRGB(binaryWriter, buf, Math.Max(1, texture.width / (1 << mipIndex)));
                            }
                            else
                            {
                                WriteAsIs(binaryWriter, buf, Math.Max(1, texture.width / (1 << mipIndex)));
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 26
0
    public void CaptureAndSaveCubemap(CubemapRenderLayout saveLayout = CubemapRenderLayout.UnrealEngine4)
    {
        // Show the save dialog
        string savePath = StandaloneFileBrowser.SaveFilePanel("Save Cubemap", "", "New Cubemap", "png");

        if (savePath == "")
        {
            return;
        }

        // Sanitise the file path
        savePath = Path.ChangeExtension(savePath, "png");

        Cubemap cubemap = CaptureCubemap();

        if (saveLayout == CubemapRenderLayout.SeparateFaces)
        {
            // Prep a texture buffer before hand, so we don't need to make a new one each loop,
            // we just reuse this one.
            var textureBuffer = new Texture2D(cubemap.width, cubemap.height, TextureFormat.RGB24, false);

            CubemapFace[] faces = new CubemapFace[]
            {
                CubemapFace.PositiveX, CubemapFace.NegativeX,
                CubemapFace.PositiveY, CubemapFace.NegativeY,
                CubemapFace.PositiveZ, CubemapFace.NegativeZ
            };

            foreach (CubemapFace face in faces)
            {
                textureBuffer.SetPixels(cubemap.GetPixels(face));
                File.WriteAllBytes(savePath + "/" + face.ToString() + ".png", textureBuffer.EncodeToPNG());
            }
        }
        else if (saveLayout == CubemapRenderLayout.UnrealEngine4)
        {
            Texture2D finalRender = CubemapToUE4Format(cubemap);
            File.WriteAllBytes(savePath + ".png", finalRender.EncodeToPNG());
        }
    }
Exemplo n.º 27
0
        public static void CreateSubMips(Cubemap cube)
        {
            AssetDatabase.StartAssetEditing();
            int    faceSize = cube.width;
            string cubePath = AssetDatabase.GetAssetPath(cube);

            //load all sub-assets
            UnityEngine.Object[] mips = AssetDatabase.LoadAllAssetRepresentationsAtPath(cubePath);
            if (mips != null)
            {
                for (int i = 0; i < mips.Length; ++i)
                {
                    if (mips[i] != null && AssetDatabase.IsSubAsset(mips[i]))
                    {
                        UnityEngine.Object.DestroyImmediate(mips[i], true);
                    }
                }
            }
            AssetDatabase.Refresh();

            // skip mip level 0, its in the cubemap itself
            faceSize = faceSize >> 1;
            for (int mip = 1; faceSize > 0; ++mip)
            {
                // extract mipmap faces from a cubemap and add them as textures in the sub image
                Texture2D tex = new Texture2D(faceSize, faceSize * 6, TextureFormat.ARGB32, false);
                tex.name = "mip" + mip;
                for (int face = 0; face < 6; ++face)
                {
                    tex.SetPixels(0, face * faceSize, faceSize, faceSize, cube.GetPixels((CubemapFace)face, mip));
                }
                tex.Apply();
                AssetDatabase.AddObjectToAsset(tex, cubePath);
                AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tex));
                faceSize = faceSize >> 1;
            }
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }
            private void CubemapToBitmapLayout()
            {
                cam.RenderToCubemap(tex, 63);
                tex.SmoothEdges(50);
                Unity.Collections.NativeArray <Color32> rawData = OutTex.GetRawTextureData <Color32>();
                int index = 0;

                for (int i = CubemapFaces.Length; i > 0; --i)
                {
                    Color[] face = tex.GetPixels(CubemapFaces[i - 1]);
                    for (int y = 0; y < tex.height; y++)
                    {
                        for (int x = 0; x < tex.width; x++)
                        {
                            // invert the input tex since origin is bottom left corner
                            int faceIndex = ((tex.height - 1 - y) * tex.width + x);
                            rawData[index++] = face[faceIndex];
                        }
                    }
                }
                OutTex.Apply();
            }
Exemplo n.º 29
0
    void genPrefilter()
    {
        RenderTexture old_rt = RenderTexture.active;

        int size = m_cube_map.width;

        var prefilter = new Cubemap(size, texture_format, true);

        prefilter.filterMode = FilterMode.Trilinear;

        int size_mip = size;

        for (int i = 0; i < m_cube_map.mipmapCount; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                var ps  = m_cube_map.GetPixels((CubemapFace)j, i);
                var tex = new Texture2D(size_mip, size_mip, texture_format, false);
                tex.SetPixels(ps);
                tex.Apply();

                var rt  = RenderTexture.GetTemporary(size_mip, size_mip, 0, render_texture_format);
                var mat = new Material(Shader.Find("prefilter"));
                mat.mainTexture = tex;
                Graphics.Blit(tex, rt, mat, 0);
                RenderTexture.active = rt;
                tex.ReadPixels(new Rect(0, 0, size_mip, size_mip), 0, 0);
                RenderTexture.ReleaseTemporary(rt);

                prefilter.SetPixels(tex.GetPixels(), (CubemapFace)j, i);
            }

            size_mip >>= 1;
        }

        AssetDatabase.CreateAsset(prefilter, "Assets/AppPBR/prefilter.asset");

        RenderTexture.active = old_rt;
    }
Exemplo n.º 30
0
        private void ExportFace(Cubemap cube, CubemapFace face)
        {
            Texture2D tex = new Texture2D(cube.width, cube.height);

            Color[] data     = cube.GetPixels(face);
            Color[] flipData = new Color[data.Length];
            for (int y = 0; y < Resolution; ++y)
            {
                for (int x = 0; x < Resolution; ++x)
                {
                    flipData[JUtilities.To1DIndex(x, y, Resolution)] = data[JUtilities.To1DIndex(Resolution - 1 - x, Resolution - 1 - y, Resolution)];
                }
            }
            tex.SetPixels(flipData);
            tex.Apply();

            byte[] bytes    = tex.EncodeToPNG();
            string fileName = Path.Combine(Directory, cube.name + "-" + face.ToString() + ".png");

            File.WriteAllBytes(fileName, bytes);
            JUtilities.DestroyObject(tex);
        }