public void Split() { ExporterWindow.ReportProgress(1, "Splitting cubemap textures... This may take a while."); var filePath = AssetDatabase.GetAssetPath(splitCube); int splitSize = splitCube.width; Color[] CubeMapColors; var faceTexturePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)); bool png = (ExporterWindow.exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG); string fext = (png == true) ? ".png" : ".jpg"; string filename = ""; string preview = ""; var importTool = new BabylonTextureImporter(filePath); bool isReadable = importTool.IsReadable(); if (!isReadable) { importTool.SetReadable(); } try { filename = (faceTexturePath + "_py" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); Texture2D tex = new Texture2D(splitSize, splitSize, TextureFormat.RGB24, false); CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveY); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); filename = (faceTexturePath + "_ny" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeY); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); filename = (faceTexturePath + "_px" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveX); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); filename = (faceTexturePath + "_nx" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeX); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); filename = (faceTexturePath + "_pz" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveZ); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); filename = (faceTexturePath + "_nz" + fext); preview = Path.GetFileName(filename); ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while."); CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeZ); tex.SetPixels(CubeMapColors, 0); tex.Apply(); WriteTextureFile(filename, tex, png); } catch (Exception ex) { UnityEngine.Debug.LogException(ex); } finally { if (!isReadable) { ExporterWindow.ReportProgress(1, "Finalizing assets database..."); importTool.ForceUpdate(); } } ExporterWindow.ReportProgress(1, "Refresing assets database..."); AssetDatabase.Refresh(); ExporterWindow.ReportProgress(1, "Cubemap split complete."); EditorUtility.ClearProgressBar(); this.Close(); }
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 static Color[] GetPixels(Texture2D texture) { Color[] pixels = null; string srcTexturePath = AssetDatabase.GetAssetPath(texture); var importTool = new BabylonTextureImporter(srcTexturePath); bool isReadable = importTool.IsReadable(); if (!isReadable) importTool.SetReadable(); try { pixels = texture.GetPixels(); } catch (Exception ex) { Debug.LogException(ex); } finally { if (!isReadable) importTool.ForceUpdate(); } return pixels; }
private void CopyTextureFace(string texturePath, string textureName, Texture2D textureFace) { if (!babylonScene.AddTextureCube(textureName)) { return; } bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG); bool textureNotExistsMode = (SceneController != null && SceneController.skyboxOptions.textureFile == BabylonTextureExport.IfNotExists); bool writeTextureFile = true; if (textureNotExistsMode && File.Exists(texturePath)) { writeTextureFile = false; ExporterWindow.ReportProgress(1, "Texture cube item exists: " + textureName); } if (writeTextureFile) { var srcTexturePath = AssetDatabase.GetAssetPath(textureFace); if ((png && srcTexturePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) || (!png && (srcTexturePath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || srcTexturePath.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)))) { ExporterWindow.ReportProgress(1, "Copying texture cube item: " + textureName); File.Copy(srcTexturePath, texturePath, true); } else { if (textureFace != null) { var importTool = new BabylonTextureImporter(srcTexturePath); bool isReadable = importTool.IsReadable(); if (!isReadable) importTool.SetReadable(); try { ExporterWindow.ReportProgress(1, "Exporting texture cube item: " + textureName); var tempTexture = new Texture2D(textureFace.width, textureFace.height, TextureFormat.ARGB32, false); Color32[] pixels = textureFace.GetPixels32(); tempTexture.SetPixels32(pixels); tempTexture.Apply(); if (png) { File.WriteAllBytes(texturePath, tempTexture.EncodeToPNG()); } else { File.WriteAllBytes(texturePath, tempTexture.EncodeToJPG(exportationOptions.DefaultQualityLevel)); } } catch (Exception ex) { Debug.LogException(ex); } finally { if (!isReadable) importTool.ForceUpdate(); } } } } }
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; } }
private void ConvertUnitySkyboxToBabylon(Camera camera, float progress) { // Note: Only Support Main Camera Skyboxes if (Camera.main == camera && (camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox) { // Note: Only Support Tone Mapped Skyboxes 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 cubeTextureFile = AssetDatabase.GetAssetPath(cubeMap); var cubeTextureExt = Path.GetExtension(cubeTextureFile); if (!cubeTextureExt.Equals(".dds", StringComparison.OrdinalIgnoreCase)) { ExporterWindow.ReportProgress(progress, "Baking skybox environment textures... This may take a while."); var faceTextureExt = ".jpg"; var faceTextureFormat = BabylonImageFormat.JPEG; if (exportationOptions.ImageEncodingOptions == (int)BabylonImageFormat.PNG) { faceTextureExt = ".png"; faceTextureFormat = BabylonImageFormat.PNG; } string frontTextureExt = "_pz" + faceTextureExt; string backTextureExt = "_nz" + faceTextureExt; string leftTextureExt = "_px" + faceTextureExt; string rightTextureExt = "_nx" + faceTextureExt; string upTextureExt = "_py" + faceTextureExt; string downTextureExt = "_ny" + faceTextureExt; skytex = new BabylonTexture(); skytex.name = String.Format("{0}_Skybox", SceneName); skytex.isCube = true; skytex.coordinatesMode = 5; skytex.extensions = new string[] { leftTextureExt, upTextureExt, frontTextureExt, rightTextureExt, downTextureExt, backTextureExt }; Tools.SetTextureWrapMode(skytex, cubeMap); var outputFile = Path.Combine(babylonScene.OutputPath, skytex.name + faceTextureExt); var splitterOpts = new BabylonSplitterOptions(); this.skyboxTextures = Tools.ExportCubemap(cubeMap, outputFile, faceTextureFormat, splitterOpts); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cubemap texture type of " + cubeTextureExt + " for " + Path.GetFileName(cubeTextureFile)); return; } } } else if (RenderSettings.skybox.shader.name == "Skybox/6 Sided" || RenderSettings.skybox.shader.name == "Mobile/Skybox") { // 6-Sided Skybox Textures (Tone Mapped Image Formats Only) 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) { ExporterWindow.ReportProgress(progress, "Exporting skybox environment textures... This may take a while."); string frontTextureExt = "_pz.jpg"; string backTextureExt = "_nz.jpg"; string leftTextureExt = "_px.jpg"; string rightTextureExt = "_nx.jpg"; string upTextureExt = "_py.jpg"; string downTextureExt = "_ny.jpg"; skytex = new BabylonTexture(); skytex.name = String.Format("{0}_Skybox", SceneName); skytex.isCube = true; skytex.coordinatesMode = 5; Tools.SetTextureWrapMode(skytex, frontTexture); List <Tools.TextureInfo> faces = new List <Tools.TextureInfo>(); var faceTextureFile = AssetDatabase.GetAssetPath(frontTexture); var faceTextureExt = Path.GetExtension(faceTextureFile); var faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { frontTextureExt = "_pz" + faceTextureExt; var frontTextureName = String.Format("{0}_pz{1}", skytex.name, faceTextureExt); var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName); faceImportTool.SetReadable(); CopyTextureFace(frontTexturePath, frontTextureName, frontTexture); faces.Add(new Tools.TextureInfo { filename = frontTexturePath, texture = frontTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(backTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { backTextureExt = "_nz" + faceTextureExt; var backTextureName = String.Format("{0}_nz{1}", skytex.name, faceTextureExt); var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName); faceImportTool.SetReadable(); CopyTextureFace(backTexturePath, backTextureName, backTexture); faces.Add(new Tools.TextureInfo { filename = backTexturePath, texture = backTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(leftTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { leftTextureExt = "_px" + faceTextureExt; var leftTextureName = String.Format("{0}_px{1}", skytex.name, faceTextureExt); var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName); faceImportTool.SetReadable(); CopyTextureFace(leftTexturePath, leftTextureName, leftTexture); faces.Add(new Tools.TextureInfo { filename = leftTexturePath, texture = leftTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(rightTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { rightTextureExt = "_nx" + faceTextureExt; var rightTextureName = String.Format("{0}_nx{1}", skytex.name, faceTextureExt); var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName); faceImportTool.SetReadable(); CopyTextureFace(rightTexturePath, rightTextureName, rightTexture); faces.Add(new Tools.TextureInfo { filename = rightTexturePath, texture = rightTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(upTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { upTextureExt = "_py" + faceTextureExt; var upTextureName = String.Format("{0}_py{1}", skytex.name, faceTextureExt); var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName); faceImportTool.SetReadable(); CopyTextureFace(upTexturePath, upTextureName, upTexture); faces.Add(new Tools.TextureInfo { filename = upTexturePath, texture = upTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(downTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { downTextureExt = "_ny" + faceTextureExt; var downTextureName = String.Format("{0}_ny{1}", skytex.name, faceTextureExt); var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName); faceImportTool.SetReadable(); CopyTextureFace(downTexturePath, downTexturePath, downTexture); faces.Add(new Tools.TextureInfo { filename = downTexturePath, texture = downTexture.Copy() }); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } skytex.extensions = new string[] { leftTextureExt, upTextureExt, frontTextureExt, rightTextureExt, downTextureExt, backTextureExt }; this.skyboxTextures = (faces.Count > 0) ? faces.ToArray() : null; } } if (skytex != null) { skytex.level = (SceneController != null) ? SceneController.skyboxOptions.skyTextureLevel : 1.0f; string meshTags = (SceneController != null) ? SceneController.skyboxOptions.meshTags : String.Empty; float skyboxSize = (SceneController != null) ? SceneController.skyboxOptions.skyMeshSize : 1000; bool skyboxSphere = (SceneController != null) ? (SceneController.skyboxOptions.meshType == BabylonSkyboxType.Sphere) : false; // Babylon Skybox Mesh var skybox = new BabylonMesh(); skybox.id = Guid.NewGuid().ToString(); skybox.infiniteDistance = true; skybox.numBoneInfluencers = Tools.GetMaxBoneInfluencers(); if (!String.IsNullOrEmpty(meshTags)) { skybox.tags = meshTags; } if (skyboxSphere) { skybox.name = "sceneSkyboxSphere"; Mesh sphereMesh = Tools.CreateSphereMesh(skyboxSize * 0.5f, 48, 48); Tools.GenerateBabylonMeshData(sphereMesh, skybox); } else { skybox.name = "sceneSkyboxCube"; Mesh boxMesh = Tools.CreateBoxMesh(skyboxSize, skyboxSize, skyboxSize); Tools.GenerateBabylonMeshData(boxMesh, skybox); } // Babylon Default Skybox var skyboxMaterial = new BabylonDefaultMaterial(); skyboxMaterial.name = "sceneSkyboxMaterial"; skyboxMaterial.id = Guid.NewGuid().ToString(); skyboxMaterial.backFaceCulling = false; skyboxMaterial.disableLighting = true; skyboxMaterial.diffuse = Color.black.ToFloat(); skyboxMaterial.specular = Color.black.ToFloat(); skyboxMaterial.ambient = Color.clear.ToFloat(); skyboxMaterial.reflectionTexture = skytex; // Babylon Skybox Material skybox.materialId = skyboxMaterial.id; babylonScene.MeshesList.Add(skybox); babylonScene.MaterialsList.Add(skyboxMaterial); babylonScene.AddTextureCube("sceneSkyboxMaterial"); } } } }
private void DumpSkyboxTextures(ref BabylonTexture skytex, ref Texture2D frontTexture, ref Texture2D backTexture, ref Texture2D leftTexture, ref Texture2D rightTexture, ref Texture2D upTexture, ref Texture2D downTexture) { if (frontTexture != null && backTexture != null && leftTexture != null && rightTexture != null && upTexture != null && downTexture != null) { ExporterWindow.ReportProgress(1, "Exporting skybox environment textures... This may take a while."); string frontTextureExt = "_pz.jpg"; string backTextureExt = "_nz.jpg"; string leftTextureExt = "_px.jpg"; string rightTextureExt = "_nx.jpg"; string upTextureExt = "_py.jpg"; string downTextureExt = "_ny.jpg"; Tools.SetTextureWrapMode(skytex, frontTexture); var faceTextureFile = AssetDatabase.GetAssetPath(frontTexture); var faceTextureExt = Path.GetExtension(faceTextureFile); var faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { frontTextureExt = "_pz" + faceTextureExt; var frontTextureName = String.Format("{0}_pz{1}", skytex.name, faceTextureExt); var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName); faceImportTool.SetReadable(); CopyTextureFace(frontTexturePath, frontTextureName, frontTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(backTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { backTextureExt = "_nz" + faceTextureExt; var backTextureName = String.Format("{0}_nz{1}", skytex.name, faceTextureExt); var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName); faceImportTool.SetReadable(); CopyTextureFace(backTexturePath, backTextureName, backTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(leftTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { leftTextureExt = "_px" + faceTextureExt; var leftTextureName = String.Format("{0}_px{1}", skytex.name, faceTextureExt); var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName); faceImportTool.SetReadable(); CopyTextureFace(leftTexturePath, leftTextureName, leftTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(rightTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { rightTextureExt = "_nx" + faceTextureExt; var rightTextureName = String.Format("{0}_nx{1}", skytex.name, faceTextureExt); var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName); faceImportTool.SetReadable(); CopyTextureFace(rightTexturePath, rightTextureName, rightTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(upTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { upTextureExt = "_py" + faceTextureExt; var upTextureName = String.Format("{0}_py{1}", skytex.name, faceTextureExt); var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName); faceImportTool.SetReadable(); CopyTextureFace(upTexturePath, upTextureName, upTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } faceTextureFile = AssetDatabase.GetAssetPath(downTexture); faceTextureExt = Path.GetExtension(faceTextureFile); faceImportTool = new BabylonTextureImporter(faceTextureFile); if (faceTextureExt.Equals(".png", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || faceTextureExt.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { downTextureExt = "_ny" + faceTextureExt; var downTextureName = String.Format("{0}_ny{1}", skytex.name, faceTextureExt); var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName); faceImportTool.SetReadable(); CopyTextureFace(downTexturePath, downTexturePath, downTexture); } else { UnityEngine.Debug.LogWarning("SKYBOX: Unsupported cube face texture type of " + faceTextureExt + " for " + Path.GetFileName(faceTextureFile)); } skytex.extensions = new string[] { leftTextureExt, upTextureExt, frontTextureExt, rightTextureExt, downTextureExt, backTextureExt }; } }