private Texture RenderCubemapToTexture(Cubemap cubemap, int faceSize, Color clearColor, TextureFormat textureFormat, bool exportFaces, string assetPathPrefix, bool alphaIsTransparency) { CubeFaceData[] faces = { new CubeFaceData(CubemapFace.NegativeX, new Vector2(0, faceSize)), new CubeFaceData(CubemapFace.PositiveX, new Vector2(faceSize * 2, faceSize)), new CubeFaceData(CubemapFace.PositiveY, new Vector2(faceSize, faceSize * 2)), new CubeFaceData(CubemapFace.NegativeY, new Vector2(faceSize, 0)), new CubeFaceData(CubemapFace.PositiveZ, new Vector2(faceSize, faceSize)), new CubeFaceData(CubemapFace.NegativeZ, new Vector2(faceSize * 3, faceSize)) }; RenderTexture oldRt = RenderTexture.active; Texture2D faceTex = new Texture2D(faceSize, faceSize, TextureFormat.RGBA32, false); RenderTexture faceRenderTex = new RenderTexture(faceSize, faceSize, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); faceRenderTex.Create(); RenderTexture flatCubeTex = new RenderTexture(faceSize * 4, faceSize * 3, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); flatCubeTex.Create(); Material flipMat = new Material(Shader.Find("Funly/Sky Studio/Utility/Flip Image")); Material blitMat = new Material(Shader.Find("Unlit/Transparent")); RenderTexture.active = flatCubeTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, flatCubeTex.width, flatCubeTex.height, 0); GL.Clear(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f)); GL.PopMatrix(); RenderTexture.active = null; for (int i = 0; i < faces.Length; i++) { CubeFaceData data = faces[i]; // Pull a face texture out of the cubemap. Color[] pixels = cubemap.GetPixels(data.face); faceTex.SetPixels(pixels); faceTex.Apply(); // Flip the image. RenderTexture.active = faceRenderTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, faceRenderTex.width, flatCubeTex.height, 0); Graphics.Blit(faceTex, faceRenderTex, flipMat, 0); GL.PopMatrix(); if (exportFaces) { string path = assetPathPrefix + data.face.ToString() + ".png"; SkyEditorUtility.WriteTextureToFile(faceRenderTex, path, textureFormat); RenderTexture.active = null; AssetDatabase.ImportAsset(path); } RenderTexture.active = null; // Target our cubemap, and render the flipped face into it. RenderTexture.active = flatCubeTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, flatCubeTex.width, flatCubeTex.height, 0); Graphics.DrawTexture(new Rect(data.offset.x, data.offset.y, faceSize, faceSize), faceRenderTex, blitMat); // Write the final texture on last face. if (i == faces.Length - 1) { string path = assetPathPrefix + ".png"; Debug.Log("Exporting cubemap to compressed texture at path: " + path); SkyEditorUtility.WriteTextureToFile(flatCubeTex, path, textureFormat); RenderTexture.active = null; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); // Adjust texture settings. TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter; importer.textureShape = TextureImporterShape.TextureCube; importer.alphaIsTransparency = alphaIsTransparency; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); } GL.PopMatrix(); RenderTexture.active = null; } RenderTexture.active = oldRt; return(flatCubeTex); }