public void registerAsset(JsonCubemap asset)
 {
     if (collectExternAssets)
     {
         externalAssets.registerAsset(asset);
     }
 }
Beispiel #2
0
        public static Color32[] getCubemapFace32(CubemapFace cubeFace, JsonCubemap jsonCube)
        {
            Color32[] result = null;
            processCubemapFace(cubeFace, jsonCube, (tex, isHdr) => {
                result = tex.GetPixels32();
            });

            return(result);
        }
Beispiel #3
0
        public static byte[] getCubemapFaceRawData(CubemapFace cubeFace, JsonCubemap jsonCube)
        {
            byte[] rawData = null;
            processCubemapFace(cubeFace, jsonCube, (tex, isHdr) => {
                rawData = tex.GetRawTextureData();
                //result = tex.GetPixels();
            });

            return(rawData);
        }
 public static void saveRawCubemap(JsonCubemap jsonCube, string filename, bool compress, Logger logger = null)
 {
     if (!compress)
     {
         saveRawCubemap(jsonCube, filename, logger);
     }
     else
     {
         saveRawCompressedCubemap(jsonCube, filename, logger);
     }
 }
Beispiel #5
0
        public static void processCubemapFace(CubemapFace cubeFace, JsonCubemap jsonCube, System.Action <Texture2D, bool> texCallback)
        {
            if (texCallback == null)
            {
                throw new System.ArgumentNullException("texCallback");
            }
            Texture2D     tmpTex        = null;
            Texture2D     rgbTex        = null;
            RenderTexture rendTex       = null;
            RenderTexture prevRenderTex = RenderTexture.active;

            try{
                var  cubeSize = jsonCube.texParams.width;
                bool linear   = !jsonCube.texImportParams.importer.sRGBTexture;
                bool hasMips  = jsonCube.cubemap.mipmapCount > 1;
                tmpTex = new Texture2D(cubeSize, cubeSize, jsonCube.cubemap.format, hasMips, linear);
                Graphics.CopyTexture(jsonCube.cubemap, (int)cubeFace, tmpTex, 0);
                tmpTex.Apply();

                bool hdr = jsonCube.isHdr;

                var texFmt = hdr ? TextureFormat.RGBAFloat: TextureFormat.RGBA32;
                rgbTex = new Texture2D(cubeSize, cubeSize, texFmt, false, linear);

                rendTex = RenderTexture.GetTemporary(cubeSize, cubeSize, 0,
                                                     hdr ? RenderTextureFormat.ARGBFloat: RenderTextureFormat.ARGB32,
                                                     linear ? RenderTextureReadWrite.Linear: RenderTextureReadWrite.sRGB);

                Graphics.Blit(tmpTex, rendTex);
                rgbTex.ReadPixels(new Rect(0, 0, rendTex.width, rendTex.height), 0, 0);
                rgbTex.Apply();

                texCallback(rgbTex, hdr);

                /*
                 * var result = rgbTex.GetPixels();
                 * flipY(cubeSize, cubeSize, result);
                 * return result;
                 */
            }
            finally{
                if (rendTex)
                {
                    RenderTexture.active = prevRenderTex;
                    RenderTexture.ReleaseTemporary(rendTex);
                }
                destroyEditorObject(tmpTex);
                destroyEditorObject(rgbTex);
            }
        }
Beispiel #6
0
        public static Color[] getCubemapFace(CubemapFace cubeFace, JsonCubemap jsonCube)
        {
            if (jsonCube.cubemap && jsonCube.texImportParams.initialized && jsonCube.texImportParams.importer.isReadable)
            {
                return(jsonCube.cubemap.GetPixels(cubeFace));
            }

            Color[] result = null;
            processCubemapFace(cubeFace, jsonCube, (tex, isHdr) => {
                result = tex.GetPixels();
            });

            return(result);
        }
        public static void copyCubemap(JsonCubemap jsonCube, string targetDir, string projectDir, Logger logger = null)
        {
            Logger.makeValid(ref logger);
            logger = Logger.getValid(logger);

            if (!jsonCube.texParams.initialized)
            {
                logger.logErrorFormat("Cubemap {0} is not initialzied", jsonCube.name);
                return;
            }

            var targetPath      = System.IO.Path.Combine(targetDir, jsonCube.exportPath);
            var unsupportedDir  = System.IO.Path.Combine(targetDir, unsupportedTexFolder);
            var unsupportedPath = System.IO.Path.Combine(unsupportedDir, jsonCube.assetPath);

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(targetPath));
            var srcPath = System.IO.Path.Combine(projectDir, jsonCube.assetPath);

            if (!jsonCube.needConversion)
            {
                System.IO.File.Copy(srcPath, targetPath, true);
                return;
            }

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(unsupportedPath));
            System.IO.File.Copy(srcPath, unsupportedPath, true);

            logger.logWarningFormat("Cubemap \"{0}\"({1}) requires conversion. Data loss possible", jsonCube.name, jsonCube.id);

            saveHorizontalStripCubemap(jsonCube, targetPath);

            if (!string.IsNullOrEmpty(jsonCube.rawPath))
            {
                var targetRaw = System.IO.Path.Combine(targetDir, jsonCube.rawPath);

                /*
                 * Compression disabled, due to compression format issues.
                 * Basically, DeflateStream in C#/Unity produces format that cannot be decompressed by ZLib on unreal side.
                 * Meanwhile, Due to deprecation of FUncompressMemory API, GZip decompression simply doesn't work, and there's a "plug"
                 *
                 * Sigh.
                 */
                saveRawCubemap(jsonCube, targetRaw, false);
            }

            /*
             * jsonCube.cubemap.GetPixels(CubemapFace.
             *
             * Graphics.CopyTexture(*/
        }
Beispiel #8
0
        public static void saveRawCubemap(System.IO.Stream outStream, JsonCubemap jsonCube, Logger logger = null)
        {
            Logger.makeValid(ref logger);
            var cubeSize = jsonCube.texParams.width;

            var hdr = jsonCube.isHdr;

            using (var writer = new System.IO.BinaryWriter(outStream)){
                var faces = new List <CubemapFace>()
                {
                    CubemapFace.PositiveZ,
                    CubemapFace.NegativeZ,
                    CubemapFace.PositiveX,
                    CubemapFace.NegativeX,
                    CubemapFace.PositiveY,
                    CubemapFace.NegativeY
                };

                var rotations = new List <SquareTransformDesc>()
                {
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1)),
                    new SquareTransformDesc(new Vector2Int(0, 1), new Vector2Int(1, 1), new Vector2Int(0, 0)),
                    new SquareTransformDesc(new Vector2Int(1, 1), new Vector2Int(1, 0), new Vector2Int(0, 1)),
                    new SquareTransformDesc(new Vector2Int(0, 0), new Vector2Int(0, 1), new Vector2Int(1, 0)),
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1)),
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1))
                };

                for (int i = 0; i < faces.Count; i++)
                {
                    var curFace     = faces[i];
                    var curRotation = rotations[i];
                    if (hdr)
                    {
                        var data = getCubemapFace(curFace, jsonCube);
                        data = transformSquareArray(data, cubeSize, curRotation);
                        writeColorArray(writer, data);
                    }
                    else
                    {
                        var data = getCubemapFace32(curFace, jsonCube);
                        data = transformSquareArray(data, cubeSize, curRotation);
                        writeColorArray(writer, data);
                    }
                }
            }
        }
Beispiel #9
0
        public static void saveRawCompressedCubemap(JsonCubemap jsonCube, string filename, Logger logger = null)
        {
            byte[] mem        = null;
            byte[] compressed = null;
            using (var memData = new System.IO.MemoryStream()){
                saveRawCubemap(memData, jsonCube, logger);
                mem = memData.ToArray();
            }
            var dataSize = mem.Length;

            compressed = compressBytesGzip(mem);

            using (var writer = new System.IO.BinaryWriter(
                       System.IO.File.Open(filename, System.IO.FileMode.Create))){
                writer.Write(mem.Length);
                writer.Write(compressed, 0, compressed.Length);
            }
        }
Beispiel #10
0
        public static void copyCubemap(JsonCubemap jsonCube, string targetDir, string projectDir, Logger logger = null)
        {
            Logger.makeValid(ref logger);
            logger = Logger.getValid(logger);

            if (!jsonCube.texParams.initialized)
            {
                logger.logErrorFormat("Cubemap {0} is not initialzied", jsonCube.name);
                return;
            }

            var targetPath      = System.IO.Path.Combine(targetDir, jsonCube.exportPath);
            var unsupportedDir  = System.IO.Path.Combine(targetDir, unsupportedTexFolder);
            var unsupportedPath = System.IO.Path.Combine(unsupportedDir, jsonCube.assetPath);

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(targetPath));
            var srcPath = System.IO.Path.Combine(projectDir, jsonCube.assetPath);

            if (!jsonCube.needConversion)
            {
                System.IO.File.Copy(srcPath, targetPath, true);
                return;
            }

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(unsupportedPath));
            System.IO.File.Copy(srcPath, unsupportedPath, true);

            logger.logWarningFormat("Cubemap \"{0}\"({1}) requires conversion. Data loss possible", jsonCube.name, jsonCube.id);

            saveHorizontalStripCubemap(jsonCube, targetPath);

            if (!string.IsNullOrEmpty(jsonCube.rawPath))
            {
                var targetRaw = System.IO.Path.Combine(targetDir, jsonCube.rawPath);
                saveRawCompressedCubemap(jsonCube, targetRaw);
                //saveRawCubemap(jsonCube, targetRaw);
            }

            /*
             * jsonCube.cubemap.GetPixels(CubemapFace.
             *
             * Graphics.CopyTexture(*/
        }
Beispiel #11
0
        public static void saveHorizontalStripCubemap(JsonCubemap jsonCube, string targetPath)
        {
            var cubeSize = jsonCube.texParams.width;

            var texWidth  = cubeSize;
            var texHeight = cubeSize;

            texWidth = texWidth * 6;

            var xPos = getCubemapFace(CubemapFace.PositiveX, jsonCube);
            var xNeg = getCubemapFace(CubemapFace.NegativeX, jsonCube);
            var yPos = getCubemapFace(CubemapFace.PositiveY, jsonCube);
            var yNeg = getCubemapFace(CubemapFace.NegativeY, jsonCube);
            var zPos = getCubemapFace(CubemapFace.PositiveZ, jsonCube);
            var zNeg = getCubemapFace(CubemapFace.NegativeZ, jsonCube);

            var texFmt = TextureFormat.RGBA32;

            var newTex = new Texture2D(texWidth, texHeight, texFmt, false);

            newTex.SetPixels(0, 0, cubeSize, cubeSize, xPos, 0);
            newTex.SetPixels(cubeSize * 1, 0, cubeSize, cubeSize, xNeg, 0);
            newTex.SetPixels(cubeSize * 2, 0, cubeSize, cubeSize, yPos, 0);
            newTex.SetPixels(cubeSize * 3, 0, cubeSize, cubeSize, yNeg, 0);
            newTex.SetPixels(cubeSize * 4, 0, cubeSize, cubeSize, zPos, 0);
            newTex.SetPixels(cubeSize * 5, 0, cubeSize, cubeSize, zNeg, 0);
            newTex.Apply();

            var pngBytes = newTex.EncodeToPNG();


            destroyEditorObject(newTex);

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(targetPath));
            Utility.saveBytesToFile(targetPath, pngBytes);
        }
Beispiel #12
0
        public static void saveRawCubemap(JsonCubemap jsonCube, string filename, Logger logger = null)
        {
            Logger.makeValid(ref logger);
            var cubeSize = jsonCube.texParams.width;
            //bool rgb = jsonCube.

            var hdr = jsonCube.isHdr;

            //compression?
            //byte[] memData = null;
            //using(var outData = new System.IO.MemoryStream())
            using (var writer = new System.IO.BinaryWriter(
                       //outData
                       System.IO.File.Open(filename, System.IO.FileMode.Create)
                       )){
                var faces = new List <CubemapFace>()
                {
                    CubemapFace.PositiveZ,
                    CubemapFace.NegativeZ,
                    CubemapFace.PositiveX,
                    CubemapFace.NegativeX,
                    CubemapFace.PositiveY,
                    CubemapFace.NegativeY
                };

                var rotations = new List <SquareTransformDesc>()
                {
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1)),
                    new SquareTransformDesc(new Vector2Int(0, 1), new Vector2Int(1, 1), new Vector2Int(0, 0)),
                    new SquareTransformDesc(new Vector2Int(1, 1), new Vector2Int(1, 0), new Vector2Int(0, 1)),
                    new SquareTransformDesc(new Vector2Int(0, 0), new Vector2Int(0, 1), new Vector2Int(1, 0)),
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1)),
                    new SquareTransformDesc(new Vector2Int(1, 0), new Vector2Int(0, 0), new Vector2Int(1, 1))
                };

                for (int i = 0; i < faces.Count; i++)
                {
                    var curFace     = faces[i];
                    var curRotation = rotations[i];
                    if (hdr)
                    {
                        var data = getCubemapFace(curFace, jsonCube);
                        data = transformSquareArray(data, cubeSize, curRotation);
                        writeColorArray(writer, data);
                    }
                    else
                    {
                        var data = getCubemapFace32(curFace, jsonCube);
                        data = transformSquareArray(data, cubeSize, curRotation);
                        writeColorArray(writer, data);
                    }
                }

                //memData = outData.ToArray();
            }

            /*
             * var memDataSize = memData.Length;
             * var compressedData = compressBytesZlib(memData);
             *
             * using(var writer = new System.IO.BinaryWriter(System.IO.File.Open(filename, System.IO.FileMode.Create))){
             *      writer.Write(memDataSize);
             *      writer.Write(compressedData, 0, compressedData.Length);
             * }
             */
        }
Beispiel #13
0
 public static void saveRawCubemap(JsonCubemap jsonCube, string filename, Logger logger = null)
 {
     using (var file = System.IO.File.Open(filename, System.IO.FileMode.Create)){
         saveRawCubemap(file, jsonCube, logger);
     }
 }
 public void registerAsset(JsonCubemap newObj)
 {
     cubemaps.Add(newObj);
 }