private void CopyTextureCube(string texturePath, Cubemap cubemap, BabylonTexture babylonTexture)
        {
            if (!babylonScene.AddTextureCube(texturePath))
            {
                return;
            }

            try
            {
                foreach (CubemapFace face in Enum.GetValues(typeof(CubemapFace)))
                {
                    var faceTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileNameWithoutExtension(texturePath));

                    switch (face)
                    {
                        case CubemapFace.PositiveX:
                            faceTexturePath += "_px.jpg";
                            break;
                        case CubemapFace.NegativeX:
                            faceTexturePath += "_nx.jpg";
                            break;
                        case CubemapFace.PositiveY:
                            faceTexturePath += "_py.jpg";
                            break;
                        case CubemapFace.NegativeY:
                            faceTexturePath += "_ny.jpg";
                            break;
                        case CubemapFace.PositiveZ:
                            faceTexturePath += "_pz.jpg";
                            break;
                        case CubemapFace.NegativeZ:
                            faceTexturePath += "_nz.jpg";
                            break;
                        default:
                            continue;
                    }

                    var tempTexture = new Texture2D(cubemap.width, cubemap.height, TextureFormat.RGB24, false);

                    tempTexture.SetPixels(cubemap.GetPixels(face));
                    tempTexture.Apply();

                    // Flip faces in cube texture.
                    tempTexture = FlipTexture(tempTexture);

                    File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToJPG());
                }

            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }

            var textureName = Path.GetFileNameWithoutExtension(texturePath);
            babylonTexture.name = textureName;
            babylonTexture.isCube = true;
            babylonTexture.level = exportationOptions.ReflectionDefaultLevel;
            babylonTexture.coordinatesMode = 3;
        }
Exemple #2
0
 public BabylonTexture(BabylonTexture original)
 {
     Id               = Guid.NewGuid().ToString();
     name             = original.name;
     level            = original.level;
     hasAlpha         = original.hasAlpha;
     getAlphaFromRGB  = original.getAlphaFromRGB;
     coordinatesMode  = original.coordinatesMode;
     isCube           = original.isCube;
     uOffset          = original.uOffset;
     vOffset          = original.vOffset;
     uScale           = original.uScale;
     vScale           = original.vScale;
     uRotationCenter  = original.uRotationCenter;
     vRotationCenter  = original.vRotationCenter;
     wRotationCenter  = original.wRotationCenter;
     invertY          = original.invertY;
     uAng             = original.uAng;
     vAng             = original.vAng;
     wAng             = original.wAng;
     wrapU            = original.wrapU;
     wrapV            = original.wrapV;
     coordinatesIndex = original.coordinatesIndex;
     isRenderTarget   = original.isRenderTarget;
     renderTargetSize = original.renderTargetSize;
     mirrorPlane      = original.mirrorPlane;
     renderList       = original.renderList;
     animations       = original.animations;
     extensions       = original.extensions;
     samplingMode     = original.samplingMode;
     originalPath     = original.originalPath;
     bitmap           = original.bitmap;
     baseColorPath    = original.baseColorPath;
     alphaPath        = original.alphaPath;
 }
 public BabylonTexture(BabylonTexture original)
 {
     name             = original.name;
     level            = original.level;
     hasAlpha         = original.hasAlpha;
     getAlphaFromRGB  = original.getAlphaFromRGB;
     coordinatesMode  = original.coordinatesMode;
     isCube           = original.isCube;
     uOffset          = original.uOffset;
     vOffset          = original.vOffset;
     uScale           = original.uScale;
     vScale           = original.vScale;
     uAng             = original.uAng;
     vAng             = original.vAng;
     wAng             = original.wAng;
     wrapU            = original.wrapU;
     wrapV            = original.wrapV;
     coordinatesIndex = original.coordinatesIndex;
     isRenderTarget   = original.isRenderTarget;
     renderTargetSize = original.renderTargetSize;
     mirrorPlane      = original.mirrorPlane;
     renderList       = original.renderList;
     animations       = original.animations;
     extensions       = original.extensions;
     samplingMode     = original.samplingMode;
     originalPath     = original.originalPath;
     bitmap           = original.bitmap;
 }
        private void ConvertUnitySkyboxToBabylon(Camera camera, float progress)
        {
            // Skybox
            if ((camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox)
            {
                if (RenderSettings.skybox != null)
                {
                    if (RenderSettings.skybox.shader.name == "Skybox/Cubemap")
                    {
                        var cubeMap = RenderSettings.skybox.GetTexture("_Tex") as Cubemap;
                        if (cubeMap != null)
                        {
                            var skytex = new BabylonTexture();
                            CopyTextureCube("sceneSkybox.hdr", cubeMap, skytex);
                            skytex.coordinatesMode = 5;
                            skytex.level = RenderSettings.reflectionIntensity;

                            BabylonMesh skybox = new BabylonMesh();
                            skybox.indices = new[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 };
                            skybox.positions = new[] { 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f };
                            skybox.uvs = new[] { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
                            skybox.normals = new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0,0f };

                            var skyboxMaterial = new BabylonPBRMaterial()
                            {
                                name = "sceneSkyboxMaterial",
                                id = Guid.NewGuid().ToString(),
                                albedo = new[] { 1.0f, 1.0f, 1.0f, 1.0f },
                                reflectivity = new[] { 0.0f, 0.0f, 0.0f },
                                microSurface = 1.0f,
                                directIntensity = 0.0f,
                                specularIntensity = 0.0f,
                                environmentIntensity = 1.0f
                            };
                            skyboxMaterial.backFaceCulling = false;
                            skybox.materialId = skyboxMaterial.id;
                            skybox.infiniteDistance = true;
                            skyboxMaterial.reflectionTexture = skytex;
                            
                            babylonScene.MeshesList.Add(skybox);
                            babylonScene.MaterialsList.Add(skyboxMaterial);
                            babylonScene.AddTextureCube("sceneSkyboxMaterial");
                        }
                    }
                }
            }
        }
        private BabylonTexture DumpReflectionTexture()
        {
            if (sceneReflectionTexture != null)
            {
                return sceneReflectionTexture;
            }

            // Take only reflection source currently and not the RenderSettings.ambientMode
            if (RenderSettings.defaultReflectionMode == UnityEngine.Rendering.DefaultReflectionMode.Skybox)
            {
                var skybox = RenderSettings.skybox;
                if (skybox != null)
                {
                    if (skybox.shader.name == "Skybox/Cubemap")
                    {
                        var cubeMap = skybox.GetTexture("_Tex") as Cubemap;
                        if (cubeMap != null)
                        {
                            sceneReflectionTexture = new BabylonTexture();
                            CopyTextureCube("sceneReflectionTexture.hdr", cubeMap, sceneReflectionTexture);
                            sceneReflectionTexture.level = RenderSettings.reflectionIntensity;
                        }
                    }
                    //else if (skybox.shader.name == "Skybox/6 Sided")
                    //{
                    //    // TODO. HDR faces.
                    //}
                }
            }
            else if (RenderSettings.customReflection != null)
            {
                var cubeMap = RenderSettings.customReflection;
                sceneReflectionTexture = new BabylonTexture();
                CopyTextureCube("sceneReflectionTexture.hdr", cubeMap, sceneReflectionTexture);
                sceneReflectionTexture.level = RenderSettings.reflectionIntensity;
            }

            return sceneReflectionTexture;
        }
        private void DumpGlossinessReflectivity(Material material, bool metallic, BabylonPBRMaterial babylonPbrMaterial)
        {
            if (material.HasProperty("_Glossiness"))
            {
                babylonPbrMaterial.microSurface = material.GetFloat("_Glossiness");
            }

            if (metallic)
            {
                if (material.HasProperty("_Metallic"))
                {
                    var metalness = material.GetFloat("_Metallic");
                    babylonPbrMaterial.reflectivity = new float[] { metalness * babylonPbrMaterial.albedo[0],
                        metalness * babylonPbrMaterial.albedo[1],
                        metalness * babylonPbrMaterial.albedo[2] };

                    if (babylonPbrMaterial.albedoTexture != null)
                    {
                        var albedoTexture = material.GetTexture("_MainTex") as Texture2D;
                        if (albedoTexture != null)
                        {
                            var albedoPixels = GetPixels(albedoTexture);
                            var reflectivityTexture = new Texture2D(albedoTexture.width, albedoTexture.height, TextureFormat.RGBA32, false);
                            reflectivityTexture.alphaIsTransparency = true;
                            babylonPbrMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;

                            var metallicTexture = material.GetTexture("_MetallicGlossMap") as Texture2D;
                            if (metallicTexture == null)
                            {
                                for (var i = 0; i < albedoTexture.width; i++)
                                {
                                    for (var j = 0; j < albedoTexture.height; j++)
                                    {
                                        albedoPixels[j * albedoTexture.width + i].r *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].g *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].b *= metalness;
                                        albedoPixels[j * albedoTexture.width + i].a = babylonPbrMaterial.microSurface;
                                    }
                                }
                            }
                            else
                            {
                                var metallicPixels = GetPixels(metallicTexture);
                                for (var i = 0; i < albedoTexture.width; i++)
                                {
                                    for (var j = 0; j < albedoTexture.height; j++)
                                    {
                                        var pixel = albedoPixels[j * albedoTexture.width + i];
                                        var metallicPixel = metallicPixels[j * albedoTexture.width + i];
                                        albedoPixels[j * albedoTexture.width + i].r *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].g *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].b *= metallicPixel.r;
                                        albedoPixels[j * albedoTexture.width + i].a = metallicPixel.a;
                                    }
                                }
                            }

                            reflectivityTexture.SetPixels(albedoPixels);
                            reflectivityTexture.Apply();

                            var textureName = albedoTexture.name + "_MetallicGlossMap.png";
                            var babylonTexture = new BabylonTexture { name = textureName };
                            var textureScale = material.GetTextureScale("_MainTex");
                            babylonTexture.uScale = textureScale.x;
                            babylonTexture.vScale = textureScale.y;

                            var textureOffset = material.GetTextureOffset("_MainTex");
                            babylonTexture.uOffset = textureOffset.x;
                            babylonTexture.vOffset = textureOffset.y;

                            var reflectivityTexturePath = Path.Combine(Path.GetTempPath(), textureName);
                            File.WriteAllBytes(reflectivityTexturePath, reflectivityTexture.EncodeToPNG());
                            babylonScene.AddTexture(reflectivityTexturePath);
                            if (File.Exists(reflectivityTexturePath))
                            {
                                File.Delete(reflectivityTexturePath);
                            }

                            babylonPbrMaterial.reflectivityTexture = babylonTexture;
                        }
                    }
                    //else
                    //{
                    //      TODO. Manage Albedo Cube Texture.
                    //}
                }
            }
            else
            {

                if (material.HasProperty("_SpecColor"))
                {
                    babylonPbrMaterial.reflectivity = material.GetColor("_SpecColor").ToFloat();
                }
                babylonPbrMaterial.reflectivityTexture = DumpTextureFromMaterial(material, "_SpecGlossMap");
                if (babylonPbrMaterial.reflectivityTexture != null && babylonPbrMaterial.reflectivityTexture.hasAlpha)
                {
                    babylonPbrMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
                }
            }
        }
        private BabylonTexture DumpReflectionTexture()
        {
            if (sceneReflectionTexture != null)
            {
                return sceneReflectionTexture;
            }

            bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
            string fext = (png == true) ? ".png" : ".jpg";

            // Take only reflection source currently and not the RenderSettings.ambientMode
            if (RenderSettings.defaultReflectionMode == UnityEngine.Rendering.DefaultReflectionMode.Skybox)
            {
                var skybox = RenderSettings.skybox;
                if (SceneController != null && skybox != null)
                {
                    if (skybox.shader.name == "Skybox/Cubemap")
                    {
                        var cubeMap = skybox.GetTexture("_Tex") as Cubemap;
                        if (cubeMap != null)
                        {
                            var srcTexturePath = AssetDatabase.GetAssetPath(cubeMap);
                            if (srcTexturePath.EndsWith(".hdr", StringComparison.OrdinalIgnoreCase))
                            {
                                var hdr = new BabylonHDRCubeTexture();
                                hdr.size = cubeMap.width;
                                sceneReflectionTexture = hdr;
                                sceneReflectionTexture.isCube = true;
                                CopyTextureCube(String.Format("{0}Reflection.hdr", SceneName), cubeMap, sceneReflectionTexture, true);
                            }
                            else
                            {
                                sceneReflectionTexture = new BabylonTexture();
                                sceneReflectionTexture.isCube = true;
                                CopyTextureCube(String.Format("{0}Reflection.hdr", SceneName), cubeMap, sceneReflectionTexture, false);
                                if (png) (sceneReflectionTexture as BabylonTexture).extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
                            }
                            sceneReflectionTexture.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;
                        }
                    }
                    else if (skybox.shader.name == "Skybox/6 Sided")
                    {
                        var frontTexture = skybox.GetTexture("_FrontTex") as Texture2D;
                        var backTexture = skybox.GetTexture("_BackTex") as Texture2D;
                        var leftTexture = skybox.GetTexture("_LeftTex") as Texture2D;
                        var rightTexture = skybox.GetTexture("_RightTex") as Texture2D;
                        var upTexture = skybox.GetTexture("_UpTex") as Texture2D;
                        var downTexture = skybox.GetTexture("_DownTex") as Texture2D;
                        if (frontTexture != null && backTexture != null && leftTexture != null && rightTexture != null && upTexture != null && downTexture != null)
                        {
                            sceneReflectionTexture = new BabylonTexture();
                            sceneReflectionTexture.name = String.Format("{0}Reflection", SceneName);
                            sceneReflectionTexture.isCube = true;

                            var frontTextureName = String.Format("{0}_pz{1}", sceneReflectionTexture.name, fext);
                            var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName);
                            CopyTextureFace(frontTexturePath, frontTextureName, frontTexture);

                            var backTextureName = String.Format("{0}_nz{1}", sceneReflectionTexture.name, fext);
                            var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName);
                            CopyTextureFace(backTexturePath, backTextureName, backTexture);

                            var leftTextureName = String.Format("{0}_px{1}", sceneReflectionTexture.name, fext);
                            var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName);
                            CopyTextureFace(leftTexturePath, leftTextureName, leftTexture);

                            var rightTextureName = String.Format("{0}_nx{1}", sceneReflectionTexture.name, fext);
                            var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName);
                            CopyTextureFace(rightTexturePath, rightTextureName, rightTexture);

                            var upTextureName = String.Format("{0}_py{1}", sceneReflectionTexture.name, fext);
                            var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName);
                            CopyTextureFace(upTexturePath, upTextureName, upTexture);

                            var downTextureName = String.Format("{0}_ny{1}", sceneReflectionTexture.name, fext);
                            var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName);
                            CopyTextureFace(downTexturePath, downTexturePath, downTexture);

                            if (png) (sceneReflectionTexture as BabylonTexture).extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
                            sceneReflectionTexture.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;
                        }
                    }
                }
            }
            else if (RenderSettings.customReflection != null)
            {
                var cubeMap = RenderSettings.customReflection;
                var srcTexturePath = AssetDatabase.GetAssetPath(cubeMap);
                if (srcTexturePath.EndsWith(".hdr", StringComparison.OrdinalIgnoreCase))
                {
                    var hdr = new BabylonHDRCubeTexture();
                    hdr.size = cubeMap.width;
                    sceneReflectionTexture = hdr;
                    sceneReflectionTexture.isCube = true;
                    CopyTextureCube(String.Format("{0}Reflection.hdr", SceneName), cubeMap, sceneReflectionTexture);
                }
                else
                {
                    sceneReflectionTexture = new BabylonTexture();
                    sceneReflectionTexture.isCube = true;
                    CopyTextureCube(String.Format("{0}Reflection.hdr", SceneName), cubeMap, sceneReflectionTexture);
                    if (png) (sceneReflectionTexture as BabylonTexture).extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
                }
                sceneReflectionTexture.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;
            }
            return sceneReflectionTexture;
        }
        private void CopyTexture(string texturePath, Texture2D texture2D, BabylonTexture babylonTexture, bool isLightmap = false)
        {
            string rename = null;
            bool needToDelete = false;
            // Convert unsupported file extensions
            if (texturePath.EndsWith(".psd") || texturePath.EndsWith(".tif") || texturePath.EndsWith(".exr"))
            {
                string srcTexturePath = AssetDatabase.GetAssetPath(texture2D);
                var importTool = new BabylonTextureImporter(srcTexturePath);
                var previousConvertToNormalmap = importTool.textureImporter.convertToNormalmap;
                var previousAlphaSource = importTool.textureImporter.alphaSource;
                var previousTextureType = importTool.textureImporter.textureType;
                importTool.SetReadable();
                importTool.textureImporter.textureType = (isLightmap) ? TextureImporterType.Lightmap : TextureImporterType.Default;
                importTool.textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;
                importTool.textureImporter.convertToNormalmap = false;
                AssetDatabase.ImportAsset(texturePath);

                try
                {
                    var usePNG = texture2D.alphaIsTransparency;
                    var tempTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, false);
                    if (isLightmap)
                    {
                        rename = SceneName + Path.GetFileName(texturePath);
                        Color[] pixels = texture2D.GetPixels(0, 0, texture2D.width, texture2D.height);
                        for (int index = 0; index < pixels.Length; index++)
                        {
                            pixels[index].r = pixels[index].r * pixels[index].a * 5;
                            pixels[index].g = pixels[index].g * pixels[index].a * 5;
                            pixels[index].b = pixels[index].b * pixels[index].a * 5;
                        }
                        tempTexture.SetPixels(pixels);
                    }
                    else
                    {
                        Color[] pixels = texture2D.GetPixels(0, 0, texture2D.width, texture2D.height);
                        for (int index = 0; index < pixels.Length; index++)
                        {
                            usePNG |= pixels[index].a <= 0.99999f;
                        }
                        tempTexture.SetPixels32(texture2D.GetPixels32());
                    }
                    tempTexture.Apply();
                    string outputfile = (!String.IsNullOrEmpty(rename)) ? rename : texturePath;
                    texturePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(outputfile));
                    var extension = (usePNG || exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG) ? ".png" : ".jpg";
                    texturePath = texturePath.Replace(".psd", extension).Replace(".tif", extension).Replace(".exr", extension);
                    File.WriteAllBytes(texturePath, (usePNG || exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG) ? tempTexture.EncodeToPNG() : tempTexture.EncodeToJPG(exportationOptions.DefaultQualityLevel));
                    needToDelete = true;
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                }
                finally
                {
                    importTool.textureImporter.textureType = previousTextureType;
                    importTool.textureImporter.alphaSource = previousAlphaSource;
                    importTool.textureImporter.convertToNormalmap = previousConvertToNormalmap;
                    importTool.ForceUpdate();
                }
            }
            else if (texture2D.alphaIsTransparency || texturePath.EndsWith(".png"))
            {
                babylonTexture.hasAlpha = true;
            }
            else
            {
                babylonTexture.hasAlpha = false;
            }
            var textureName = Path.GetFileName(texturePath);
            babylonTexture.name = textureName;
            babylonScene.AddTexture(texturePath);
            if (needToDelete) File.Delete(texturePath);
        }
        private BabylonTexture ExportTexture(IStdMat2 stdMat, int index, BabylonScene babylonScene, Boolean allowCube = false)
        {
            if (!stdMat.MapEnabled(index))
            {
                return null;
            }
            var babylonTexture = new BabylonTexture();

            var texMap = stdMat.GetSubTexmap(index);
            var texture = texMap.GetParamBlock(0).Owner as IBitmapTex;

            if (texture == null)
            {
                return null;
            }

            babylonTexture.hasAlpha = (texture.AlphaSource != 3);
            babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2);
            babylonTexture.level = stdMat.GetTexmapAmt(index, 0);

            var uvGen = texture.UVGen;

            switch (uvGen.GetCoordMapping(0))
            {
                case 1: //MAP_SPHERICAL
                    babylonTexture.coordinatesMode = 1;
                    break;
                case 2: //MAP_PLANAR
                    babylonTexture.coordinatesMode = 2;
                    break;
                default:
                    babylonTexture.coordinatesMode = 0;
                    break;
            }

            babylonTexture.coordinatesIndex = uvGen.MapChannel - 1;
            if (uvGen.MapChannel > 2)
            {
                RaiseWarning(string.Format("Unsupported map channel, Only channel 1 and 2 are supported."), 2);
            }

            babylonTexture.uOffset = uvGen.GetUOffs(0);
            babylonTexture.vOffset = uvGen.GetVOffs(0);

            babylonTexture.uScale = uvGen.GetUScl(0);
            babylonTexture.vScale = uvGen.GetVScl(0);

            if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
            {
                babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
            }

            babylonTexture.uAng = uvGen.GetUAng(0);
            babylonTexture.vAng = uvGen.GetVAng(0);
            babylonTexture.wAng = uvGen.GetWAng(0);

            babylonTexture.wrapU = 0; // CLAMP
            if ((uvGen.TextureTiling & 1) != 0) // WRAP
            {
                babylonTexture.wrapU = 1;
            }
            else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
            {
                babylonTexture.wrapU = 2;
            }

            babylonTexture.wrapV = 0; // CLAMP
            if ((uvGen.TextureTiling & 2) != 0) // WRAP
            {
                babylonTexture.wrapV = 1;
            }
            else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
            {
                babylonTexture.wrapV = 2;
            }

            babylonTexture.name = Path.GetFileName(texture.MapName);

            // Animations
            var animations = new List<BabylonAnimation>();
            ExportFloatAnimation("uOffset", animations, key => new[] { uvGen.GetUOffs(key) });
            ExportFloatAnimation("vOffset", animations, key => new[] { uvGen.GetVOffs(key) });
            ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) });
            ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) });
            ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) });
            ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) });
            ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) });

            babylonTexture.animations = animations.ToArray();

            // Copy texture to output
            try
            {
                if (File.Exists(texture.MapName))
                {
                    if (CopyTexturesToOutput)
                    {
                        File.Copy(texture.MapName, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
                    }
                    babylonTexture.isCube = IsTextureCube(texture.MapName);
                }
                else
                {
                    var texturepath = Path.Combine(Path.GetDirectoryName(Loader.Core.CurFilePath), babylonTexture.name);
                    if (File.Exists(texturepath))
                    {
                        if (CopyTexturesToOutput)
                        {
                            File.Copy(texturepath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
                        }
                        babylonTexture.isCube = IsTextureCube(texturepath);
                    }
                    else
                    {
                        RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), 2);
                    }
                }
            }
            catch
            {
                // silently fails
            }

            if (babylonTexture.isCube && !allowCube)
            {
                RaiseWarning(string.Format("Cube texture are only supported for reflection channel"), 2);
            }

            return babylonTexture;
        }
Exemple #10
0
 private void ParseParticleSystems(GameObject gameObject, string emitterId, ref List<BabylonExport.Entities.BabylonParticleSystem> particles)
 {
     var particle = gameObject.GetComponent<UnityEditor.BabylonParticleSystem>();
     if (particle != null && particle.exportParticle)
     {
         var particleSystem = new BabylonExport.Entities.BabylonParticleSystem();
         particleSystem.name = (!String.IsNullOrEmpty(particle.particleName)) ? particle.particleName : String.Format("particalSystem#" + Guid.NewGuid().ToString());
         particleSystem.emitterId = emitterId;
         particleSystem.linkToEmitter = true;
         particleSystem.preventAutoStart = !particle.autoStart;
         particleSystem.textureMask = particle.textureMask.ToFloat();
         particleSystem.updateSpeed = particle.updateSpeed;
         particleSystem.emitRate = particle.emitRate;
         particleSystem.gravity = particle.gravity.ToFloat();
         particleSystem.blendMode = (int)particle.blendMode;
         particleSystem.capacity = particle.capacity;
         particleSystem.color1 = particle.color1.ToFloat();
         particleSystem.color2 = particle.color2.ToFloat();
         particleSystem.colorDead = particle.colorDead.ToFloat();
         particleSystem.direction1 = particle.direction1.ToFloat();
         particleSystem.direction2 = particle.direction2.ToFloat();
         particleSystem.minEmitBox = particle.minEmitBox.ToFloat();
         particleSystem.maxEmitBox = particle.maxEmitBox.ToFloat();
         particleSystem.minEmitPower = particle.minEmitPower;
         particleSystem.maxEmitPower = particle.maxEmitPower;
         particleSystem.minLifeTime = particle.minLifeTime;
         particleSystem.maxLifeTime = particle.maxLifeTime;
         particleSystem.minSize = particle.minSize;
         particleSystem.maxSize = particle.maxSize;
         particleSystem.minAngularSpeed = particle.minAngularSpeed;
         particleSystem.maxAngularSpeed = particle.maxAngularSpeed;
         particleSystem.targetStopFrame = particle.targetStopFrame;
         particleSystem.deadAlpha = particle.deadAlpha;
         if (particle.texture != null)
         {
             var babylonTexture = new BabylonTexture();
             var texturePath = AssetDatabase.GetAssetPath(particle.texture);
             CopyTexture(texturePath, particle.texture, babylonTexture);
             particleSystem.textureName = Path.GetFileName(texturePath);
         }
         particles.Add(particleSystem);
     }
 }
Exemple #11
0
 private void ParseLensFlares(GameObject gameObject, string emitterId, ref List<UnityFlareSystem> lens)
 {
     var flare = gameObject.GetComponent<BabylonFlareSystem>();
     if (flare != null && flare.exportFlare && flare.lensFlares != null && flare.lensFlares.Length > 0)
     {
         var flareSystem = new UnityFlareSystem();
         flareSystem.name = (!String.IsNullOrEmpty(flare.flareName)) ? flare.flareName : String.Format("lensFlareSystem#" + Guid.NewGuid().ToString());
         flareSystem.emitterId = emitterId;
         flareSystem.borderLimit = flare.borderLimit;
         var flareBuffer = new List<UnityFlareItem>();
         foreach (var flareItem in flare.lensFlares)
         {
             var item = new UnityFlareItem();
             item.size = flareItem.size;
             item.position = flareItem.position;
             item.color = flareItem.color.ToFloat();
             if (flareItem.texture != null)
             {
                 var babylonTexture = new BabylonTexture();
                 var texturePath = AssetDatabase.GetAssetPath(flareItem.texture);
                 CopyTexture(texturePath, flareItem.texture, babylonTexture);
                 item.textureName = Path.GetFileName(texturePath);
             }
             flareBuffer.Add(item);
         }
         flareSystem.lensFlares = flareBuffer.ToArray();
         lens.Add(flareSystem);
     }
 }
Exemple #12
0
        private object GetTexturePropertyValue(Texture2D texture)
        {
            if (texture == null) return null;
            var texturePath = AssetDatabase.GetAssetPath(texture);
            if (String.IsNullOrEmpty(texturePath)) return null;

            var babylonTexture = new BabylonTexture();
            CopyTexture(texturePath, texture, babylonTexture);
            Dictionary<string, object> textureInfo = new Dictionary<string, object>();
            textureInfo.Add("type", texture.GetType().FullName);
            textureInfo.Add("name", babylonTexture.name);
            textureInfo.Add("level", babylonTexture.level);
            textureInfo.Add("isCube", babylonTexture.isCube);
            textureInfo.Add("hasAlpha", babylonTexture.hasAlpha);
            textureInfo.Add("coordinatesMode", babylonTexture.coordinatesMode);
            textureInfo.Add("coordinatesIndex", babylonTexture.coordinatesIndex);
            return textureInfo;
        }
        private void ConvertUnitySkyboxToBabylon(Camera camera, float progress)
        {
            // Skybox
            bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
            string fext = (png == true) ? ".png" : ".jpg";

            if ((camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox)
            {
                if (RenderSettings.skybox != null)
                {
                    BabylonTexture skytex = null;
                    if (RenderSettings.skybox.shader.name == "Skybox/Cubemap")
                    {
                        var cubeMap = RenderSettings.skybox.GetTexture("_Tex") as Cubemap;
                        if (cubeMap != null)
                        {
                            var srcTexturePath = AssetDatabase.GetAssetPath(cubeMap);
                            if (srcTexturePath.EndsWith(".hdr", StringComparison.OrdinalIgnoreCase))
                            {
                                var hdr = new BabylonHDRCubeTexture();
                                hdr.size = cubeMap.width;
                                skytex = hdr;
                                CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, true);
                            }
                            else
                            {
                                skytex = new BabylonTexture();
                                CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, false);
                                if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
                            }
                        }
                    }
                    else if (RenderSettings.skybox.shader.name == "Skybox/6 Sided")
                    {
                        var frontTexture = RenderSettings.skybox.GetTexture("_FrontTex") as Texture2D;
                        var backTexture = RenderSettings.skybox.GetTexture("_BackTex") as Texture2D;
                        var leftTexture = RenderSettings.skybox.GetTexture("_LeftTex") as Texture2D;
                        var rightTexture = RenderSettings.skybox.GetTexture("_RightTex") as Texture2D;
                        var upTexture = RenderSettings.skybox.GetTexture("_UpTex") as Texture2D;
                        var downTexture = RenderSettings.skybox.GetTexture("_DownTex") as Texture2D;
                        if (frontTexture != null && backTexture != null && leftTexture != null && rightTexture != null && upTexture != null && downTexture != null)
                        {
                            skytex = new BabylonTexture();
                            skytex.name = String.Format("{0}Skybox", SceneName);

                            var frontTextureName = String.Format("{0}_pz{1}", skytex.name, fext);
                            var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName);
                            CopyTextureFace(frontTexturePath, frontTextureName, frontTexture);

                            var backTextureName = String.Format("{0}_nz{1}", skytex.name, fext);
                            var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName);
                            CopyTextureFace(backTexturePath, backTextureName, backTexture);

                            var leftTextureName = String.Format("{0}_px{1}", skytex.name, fext);
                            var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName);
                            CopyTextureFace(leftTexturePath, leftTextureName, leftTexture);

                            var rightTextureName = String.Format("{0}_nx{1}", skytex.name, fext);
                            var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName);
                            CopyTextureFace(rightTexturePath, rightTextureName, rightTexture);

                            var upTextureName = String.Format("{0}_py{1}", skytex.name, fext);
                            var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName);
                            CopyTextureFace(upTexturePath, upTextureName, upTexture);

                            var downTextureName = String.Format("{0}_ny{1}", skytex.name, fext);
                            var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName);
                            CopyTextureFace(downTexturePath, downTexturePath, downTexture);
                            if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
                        }
                    }
                    if (skytex != null)
                    {
                        skytex.isCube = true;
                        skytex.coordinatesMode = 5;
                        skytex.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;

                        var skybox = new BabylonMesh();
                        skybox.name = "sceneSkyboxMesh";
                        skybox.id = Guid.NewGuid().ToString();
                        skybox.indices = new[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 };
                        skybox.positions = new[] { 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f };
                        skybox.uvs = new[] { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
                        skybox.normals = new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0, 0f };

                        if (SceneController != null)
                        {
                            Color skycolor = SceneController.skyboxOptions.albedoColor;
                            Color skyreflect = SceneController.skyboxOptions.reflectivityColor;
                            var skyboxMaterial = new BabylonPBRMaterial()
                            {
                                name = "sceneSkyboxMaterial",
                                id = Guid.NewGuid().ToString(),
                                albedo = skycolor.ToFloat(),
                                reflectivity = skyreflect.ToFloat(),
                                microSurface = SceneController.skyboxOptions.microSurface,
                                cameraContrast = SceneController.skyboxOptions.cameraContrast,
                                cameraExposure = SceneController.skyboxOptions.cameraExposure,
                                directIntensity = SceneController.skyboxOptions.directIntensity,
                                emissiveIntensity = SceneController.skyboxOptions.emissiveIntensity,
                                specularIntensity = SceneController.skyboxOptions.specularIntensity,
                                environmentIntensity = SceneController.skyboxOptions.environmentIntensity
                            };

                            skyboxMaterial.reflectionTexture = skytex;
                            skyboxMaterial.backFaceCulling = false;
                            skyboxMaterial.disableLighting = true;
                            skybox.materialId = skyboxMaterial.id;
                            skybox.infiniteDistance = true;

                            babylonScene.MeshesList.Add(skybox);
                            babylonScene.MaterialsList.Add(skyboxMaterial);
                            babylonScene.AddTextureCube("sceneSkyboxMaterial");
                        }
                    }
                }
            }
        }
        private BabylonTexture ExportTexture(IStdMat2 stdMat, int index, out BabylonFresnelParameters fresnelParameters, BabylonScene babylonScene, bool allowCube = false, bool forceAlpha = false)
        {
            fresnelParameters = null;

            if (!stdMat.MapEnabled(index))
            {
                return null;
            }
            var babylonTexture = new BabylonTexture();

            var texMap = stdMat.GetSubTexmap(index);

            // Fallout
            if (texMap.ClassName == "Falloff") // This is the only way I found to detect it. This is crappy but it works
            {
                fresnelParameters = new BabylonFresnelParameters();

                var paramBlock = texMap.GetParamBlock(0);
                var color1 = paramBlock.GetColor(0, 0, 0);
                var color2 = paramBlock.GetColor(4, 0, 0);

                fresnelParameters.isEnabled = true;
                fresnelParameters.leftColor = color2.ToArray();
                fresnelParameters.rightColor = color1.ToArray();

                if (paramBlock.GetInt(8, 0, 0) == 2)
                {
                    fresnelParameters.power = paramBlock.GetFloat(12, 0, 0);
                }
                else
                {
                    fresnelParameters.power = 1;
                }
                var texMap1 = paramBlock.GetTexmap(2, 0, 0);
                var texMap1On = paramBlock.GetInt(3, 0, 0);

                var texMap2 = paramBlock.GetTexmap(6, 0, 0);
                var texMap2On = paramBlock.GetInt(7, 0, 0);

                if (texMap1 != null && texMap1On != 0)
                {
                    texMap = texMap1;
                    fresnelParameters.rightColor = new float[] { 1, 1, 1 };

                    if (texMap2 != null && texMap2On != 0)
                    {
                        RaiseWarning(string.Format("You cannot specify two textures for falloff. Only one is supported"), 2);
                    }
                }
                else if (texMap2 != null && texMap2On != 0)
                {
                    fresnelParameters.leftColor = new float[] { 1, 1, 1 };
                    texMap = texMap2;
                }
                else
                {
                    return null;
                }
            }

            // Bitmap
            var texture = texMap.GetParamBlock(0).Owner as IBitmapTex;

            if (texture == null)
            {
                return null;
            }

            if (forceAlpha)
            {
                babylonTexture.hasAlpha = true;
                babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2) || (texture.AlphaSource == 3);
            }
            else
            {
                babylonTexture.hasAlpha = (texture.AlphaSource != 3);
                babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2);
            }


            babylonTexture.level = stdMat.GetTexmapAmt(index, 0);

            var uvGen = texture.UVGen;

            switch (uvGen.GetCoordMapping(0))
            {
                case 1: //MAP_SPHERICAL
                    babylonTexture.coordinatesMode = 1;
                    break;
                case 2: //MAP_PLANAR
                    babylonTexture.coordinatesMode = 2;
                    break;
                default:
                    babylonTexture.coordinatesMode = 0;
                    break;
            }

            babylonTexture.coordinatesIndex = uvGen.MapChannel - 1;
            if (uvGen.MapChannel > 2)
            {
                RaiseWarning(string.Format("Unsupported map channel, Only channel 1 and 2 are supported."), 2);
            }

            babylonTexture.uOffset = uvGen.GetUOffs(0);
            babylonTexture.vOffset = uvGen.GetVOffs(0);

            babylonTexture.uScale = uvGen.GetUScl(0);
            babylonTexture.vScale = uvGen.GetVScl(0);

            if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
            {
                babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
            }

            babylonTexture.uAng = uvGen.GetUAng(0);
            babylonTexture.vAng = uvGen.GetVAng(0);
            babylonTexture.wAng = uvGen.GetWAng(0);

            babylonTexture.wrapU = 0; // CLAMP
            if ((uvGen.TextureTiling & 1) != 0) // WRAP
            {
                babylonTexture.wrapU = 1;
            }
            else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
            {
                babylonTexture.wrapU = 2;
            }

            babylonTexture.wrapV = 0; // CLAMP
            if ((uvGen.TextureTiling & 2) != 0) // WRAP
            {
                babylonTexture.wrapV = 1;
            }
            else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
            {
                babylonTexture.wrapV = 2;
            }

            babylonTexture.name = Path.GetFileName(texture.MapName);

            // Animations
            var animations = new List<BabylonAnimation>();
            ExportFloatAnimation("uOffset", animations, key => new[] { uvGen.GetUOffs(key) });
            ExportFloatAnimation("vOffset", animations, key => new[] { uvGen.GetVOffs(key) });
            ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) });
            ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) });
            ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) });
            ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) });
            ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) });

            babylonTexture.animations = animations.ToArray();
            var absolutePath = texture.Map.FullFilePath;
            // Copy texture to output
            try
            {
                if (File.Exists(absolutePath))
                {
                    babylonTexture.isCube = IsTextureCube(absolutePath);
                    if (CopyTexturesToOutput)
                    {
                        File.Copy(absolutePath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
                    }
                }
                else
                {
                    RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), 2);
                }

            }
            catch
            {
                // silently fails
            }

            if (babylonTexture.isCube && !allowCube)
            {
                RaiseWarning(string.Format("Cube texture are only supported for reflection channel"), 2);
            }

            return babylonTexture;
        }
        private BabylonTexture DumpTexture(Texture texture, Material material = null, string name = "")
        {
            if (texture == null)
            {
                return null;
            }
            var texturePath = AssetDatabase.GetAssetPath(texture);
            var textureName = Path.GetFileName(texturePath);
            var babylonTexture = new BabylonTexture { name = textureName };

            if (material != null)
            {
                var textureScale = material.GetTextureScale(name);
                babylonTexture.uScale = textureScale.x;
                babylonTexture.vScale = textureScale.y;

                var textureOffset = material.GetTextureOffset(name);
                babylonTexture.uOffset = textureOffset.x;
                babylonTexture.vOffset = textureOffset.y;
            }

            var texture2D = texture as Texture2D;

            if (texture2D)
            {
                babylonTexture.hasAlpha = texture2D.alphaIsTransparency;

                CopyTexture(texturePath, texture2D, babylonTexture);
            }
            else
            {
                var cubemap = texture as Cubemap;
                if (cubemap != null)
                {
                    CopyTextureCube(texturePath, cubemap, babylonTexture);
                }
            }

            return babylonTexture;
        }
        private void CopyTexture(string texturePath, Texture2D texture2D, BabylonTexture babylonTexture)
        {
            bool needToDelete = false;
            var useJPG = !texture2D.alphaIsTransparency;

            // Convert unsupported file extensions
            if (texturePath.EndsWith(".psd") || texturePath.EndsWith(".tif") || texturePath.EndsWith(".exr"))
            {
                try
                {
                    // Change texture import settings to be able to read texture data
                    var textureImporter = AssetImporter.GetAtPath(texturePath) as TextureImporter;
                    var previousIsReadable = textureImporter.isReadable;
                    var previousNormalMap = textureImporter.normalmap;
                    var previousLightmap = textureImporter.lightmap;
                    var previousConvertToNormalmap = textureImporter.convertToNormalmap;
                    var previousTextureType = textureImporter.textureType;
                    var previousGrayscaleToAlpha = textureImporter.grayscaleToAlpha;
                    textureImporter.textureType = TextureImporterType.Advanced;
                    textureImporter.isReadable = true;
                    textureImporter.lightmap = false;
                    textureImporter.normalmap = false;
                    textureImporter.convertToNormalmap = false;
                    textureImporter.grayscaleToAlpha = false;

                    AssetDatabase.ImportAsset(texturePath);

                    texturePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(texturePath));
                    var extension = useJPG ? ".jpg" : ".png";
                    texturePath = texturePath.Replace(".psd", extension).Replace(".tif", extension).Replace(".exr", extension);

                    var tempTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, false);

                    tempTexture.SetPixels32(texture2D.GetPixels32());
                    tempTexture.Apply();

                    File.WriteAllBytes(texturePath, useJPG ? tempTexture.EncodeToJPG() : tempTexture.EncodeToPNG());

                    needToDelete = true;

                    // Restore
                    textureImporter.isReadable = previousIsReadable;
                    textureImporter.normalmap = previousNormalMap;
                    textureImporter.lightmap = previousLightmap;
                    textureImporter.convertToNormalmap = previousConvertToNormalmap;
                    textureImporter.textureType = previousTextureType;
                    textureImporter.grayscaleToAlpha = previousGrayscaleToAlpha;
                   
                    AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                }
            }

            var textureName = Path.GetFileName(texturePath);
            babylonTexture.name = textureName;
            babylonScene.AddTexture(texturePath);

            if (needToDelete)
            {
                File.Delete(texturePath);
            }
        }
        private void CopyTextureCube(string texturePath, Cubemap cubemap, BabylonTexture babylonTexture, bool hdr = false)
        {
            if (!babylonScene.AddTextureCube(texturePath))
            {
                return;
            }
            ExporterWindow.ReportProgress(1, "Parsing texture cube: " + texturePath);
            var srcTexturePath = AssetDatabase.GetAssetPath(cubemap);
            bool textureNotExistsMode = (SceneController != null && SceneController.skyboxOptions.textureFile == BabylonTextureExport.IfNotExists);
            bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
            string fext = (png == true) ? ".png" : ".jpg";
            try
            {
                if (hdr)
                {
                    var hdrTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileName(texturePath));
                    bool writeHdrTexture = true;
                    string textureHdrName = Path.GetFileName(hdrTexturePath);
                    if (textureNotExistsMode && File.Exists(hdrTexturePath))
                    {
                        writeHdrTexture = false;
                        ExporterWindow.ReportProgress(1, "Texture hdr cube item exists: " + textureHdrName);
                    }
                    if (writeHdrTexture)
                    {
                        ExporterWindow.ReportProgress(1, "Copying hdr texture cube item: " + textureHdrName);
                        File.Copy(srcTexturePath, hdrTexturePath, true);
                    }
                }
                else
                {
                    var importTool = new BabylonTextureImporter(srcTexturePath);
                    bool isReadable = importTool.IsReadable();
                    if (!isReadable) importTool.SetReadable();
                    try
                    {
                        foreach (CubemapFace face in Enum.GetValues(typeof(CubemapFace)))
                        {
                            var faceTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileNameWithoutExtension(texturePath));
                            switch (face)
                            {
                                case CubemapFace.PositiveX:
                                    faceTexturePath += ("_px" + fext);
                                    break;
                                case CubemapFace.NegativeX:
                                    faceTexturePath += ("_nx" + fext);
                                    break;
                                case CubemapFace.PositiveY:
                                    faceTexturePath += ("_py" + fext);
                                    break;
                                case CubemapFace.NegativeY:
                                    faceTexturePath += ("_ny" + fext);
                                    break;
                                case CubemapFace.PositiveZ:
                                    faceTexturePath += ("_pz" + fext);
                                    break;
                                case CubemapFace.NegativeZ:
                                    faceTexturePath += ("_nz" + fext);
                                    break;
                                default:
                                    continue;
                            }
                            bool writeFaceTexture = true;
                            string textureFaceName = Path.GetFileName(faceTexturePath);
                            if (textureNotExistsMode && File.Exists(faceTexturePath))
                            {
                                writeFaceTexture = false;
                                ExporterWindow.ReportProgress(1, "Texture cube item exists: " + textureFaceName);
                            }
                            if (writeFaceTexture)
                            {
                                ExporterWindow.ReportProgress(1, "Exporting texture cube item: " + textureFaceName);
                                var tempTexture = new Texture2D(cubemap.width, cubemap.height, TextureFormat.ARGB32, false);

                                Color[] pixels = cubemap.GetPixels(face);
                                tempTexture.SetPixels(pixels);
                                tempTexture.Apply();

                                // Flip faces in cube texture.
                                tempTexture = FlipTexture(tempTexture);

                                // Encode cube face texture
                                if (png)
                                {
                                    File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToPNG());
                                }
                                else
                                {
                                    File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToJPG(exportationOptions.DefaultQualityLevel));
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (!isReadable) importTool.ForceUpdate();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
            if (babylonTexture != null)
            {
                var textureName = Path.GetFileNameWithoutExtension(texturePath);
                if (hdr) textureName += ".hdr";
                babylonTexture.name = textureName;
                babylonTexture.isCube = true;
                babylonTexture.level = exportationOptions.ReflectionDefaultLevel;
                babylonTexture.coordinatesMode = 3;
            }
        }