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 BabylonMaterial DumpPBRMaterial(Material material, Renderer renderer) { if (!materialsDictionary.ContainsKey(material.name)) { var bMat = new BabylonPBRMaterial { name = material.name, id = Guid.NewGuid().ToString(), albedoColor = new float[4] }; if (material.HasProperty("_Color")) { bMat.albedoColor = material.color.ToFloat(); } bMat.albedoTexture = DumpTextureFromMaterial(material, "_MainTex"); if (material.HasProperty("_Glossiness")) { bMat.microSurface = material.GetFloat("_Glossiness"); } if (material.HasProperty("_Metallic")) { var metallic = material.GetFloat("_Metallic"); bMat.reflectivityColor = new float[] { metallic, metallic, metallic }; } bMat.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap"); materialsDictionary.Add(bMat.name, bMat); return bMat; } return materialsDictionary[material.name]; }
private static void DumpTransparency(Material material, BabylonPBRMaterial babylonPbrMaterial) { if (material.HasProperty("_Mode")) { var mode = material.GetFloat("_Mode"); if (mode == 2.0f) { // Transparent Albedo if (babylonPbrMaterial.albedoTexture != null && babylonPbrMaterial.albedoTexture.hasAlpha) { babylonPbrMaterial.useAlphaFromAlbedoTexture = true; } // Material Alpha else { babylonPbrMaterial.alpha = babylonPbrMaterial.albedo[3]; } } else if (mode == 1.0f) { // Cutout // Follow the texture hasAlpha property. } else { // Opaque if (babylonPbrMaterial.albedoTexture != null) { babylonPbrMaterial.albedoTexture.hasAlpha = false; } babylonPbrMaterial.alpha = 1.0f; } } }
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 BabylonMaterial DumpPBRMaterial(Material material, Renderer renderer, bool metallic) { if (materialsDictionary.ContainsKey(material.name)) { return materialsDictionary[material.name]; } var babylonPbrMaterial = new BabylonPBRMaterial { name = material.name, id = Guid.NewGuid().ToString(), albedo = new float[4], useEmissiveAsIllumination = true, useSpecularOverAlpha = true, useRadianceOverAlpha = true, }; babylonPbrMaterial.environmentIntensity = RenderSettings.ambientIntensity; // Albedo if (material.HasProperty("_Color")) { babylonPbrMaterial.albedo = material.color.ToFloat(); } babylonPbrMaterial.albedoTexture = DumpTextureFromMaterial(material, "_MainTex"); // Transparency DumpTransparency(material, babylonPbrMaterial); // Glossiess/Reflectivity DumpGlossinessReflectivity(material, metallic, babylonPbrMaterial); // Occlusion babylonPbrMaterial.ambientTexture = DumpTextureFromMaterial(material, "_OcclusionMap"); if (babylonPbrMaterial.ambientTexture != null && material.HasProperty("_OcclusionStrength")) { babylonPbrMaterial.ambientTexture.level = material.GetFloat("_OcclusionStrength"); } // Emissive if (material.HasProperty("_EmissionColor")) { babylonPbrMaterial.emissive = material.GetColor("_EmissionColor").ToFloat(); } babylonPbrMaterial.emissiveTexture = DumpTextureFromMaterial(material, "_EmissionMap"); // Normal babylonPbrMaterial.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap"); if (babylonPbrMaterial.bumpTexture != null && material.HasProperty("_BumpMapScale")) { babylonPbrMaterial.bumpTexture.level = material.GetFloat("_BumpMapScale"); } // Reflection babylonPbrMaterial.reflectionTexture = DumpReflectionTexture(); materialsDictionary.Add(babylonPbrMaterial.name, babylonPbrMaterial); return babylonPbrMaterial; }
private BabylonMaterial DumpPBRMaterial(Material material, int lightmapIndex = -1, Vector4 lightmapScaleOffset = default(Vector4), bool metallic = true, int lightmapCoordIndex = -1) { var materialNotSupported = false; if (materialsDictionary.ContainsKey(material.name)) { return materialsDictionary[material.name]; } var babylonPbrMaterial = new BabylonPBRMaterial { name = material.name, id = Guid.NewGuid().ToString(), albedo = new float[4], useEmissiveAsIllumination = true, useSpecularOverAlpha = true, useRadianceOverAlpha = true, }; ExporterWindow.ReportProgress(1, "Exporting physical material: " + material.name); babylonPbrMaterial.environmentIntensity = RenderSettings.ambientIntensity; if (exportationOptions.DefaultLightmapMode == (int)BabylonLightmapMode.FullLightBaking) { babylonPbrMaterial.disableLighting = true; babylonPbrMaterial.useEmissiveAsIllumination = true; } if (material.mainTexture && material.mainTexture.GetType().FullName == "UnityEngine.ProceduralTexture") { materialNotSupported = true; Debug.LogWarning("ProceduralTexture: " + material.mainTexture.name + " not supported by Babylon.js"); } // Albedo if (material.HasProperty("_Color")) { babylonPbrMaterial.albedo = material.color.ToFloat(); } babylonPbrMaterial.albedoTexture = DumpTextureFromMaterial(material, "_MainTex"); if (material.mainTexture != null && !materialNotSupported) { var textureScale = material.mainTextureScale; babylonPbrMaterial.albedoTexture.uScale = textureScale.x; babylonPbrMaterial.albedoTexture.vScale = textureScale.y; var textureOffset = material.mainTextureOffset; babylonPbrMaterial.albedoTexture.uOffset = textureOffset.x; babylonPbrMaterial.albedoTexture.vOffset = textureOffset.y; } // Emissive if (material.HasProperty("_EmissionColor")) { babylonPbrMaterial.emissive = material.GetColor("_EmissionColor").ToFloat(); } babylonPbrMaterial.emissiveTexture = DumpTextureFromMaterial(material, "_EmissionMap"); // Transparency DumpTransparency(material, babylonPbrMaterial); // Glossiess/Reflectivity DumpGlossinessReflectivity(material, metallic, babylonPbrMaterial); // Occlusion babylonPbrMaterial.ambientTexture = DumpTextureFromMaterial(material, "_OcclusionMap"); if (babylonPbrMaterial.ambientTexture != null && material.HasProperty("_OcclusionStrength")) { babylonPbrMaterial.ambientTexture.level = material.GetFloat("_OcclusionStrength"); } // Normal babylonPbrMaterial.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap"); if (babylonPbrMaterial.bumpTexture != null && material.HasProperty("_BumpScale")) { babylonPbrMaterial.bumpTexture.level = material.GetFloat("_BumpScale"); } // Reflection babylonPbrMaterial.reflectionTexture = DumpReflectionTexture(); // Lightmapping bool hasLightmap = (exportationOptions.ExportLightmaps && lightmapIndex >= 0 && lightmapIndex != 65535 && LightmapSettings.lightmaps.Length > lightmapIndex); if (hasLightmap && babylonPbrMaterial.ambientTexture == null) { var lightmap = LightmapSettings.lightmaps[lightmapIndex].lightmapLight; var texturePath = AssetDatabase.GetAssetPath(lightmap); if (!String.IsNullOrEmpty(texturePath)) { ExporterWindow.ReportProgress(1, "Dumping pbr material lightmap: " + lightmap.name); babylonPbrMaterial.lightmapTexture = DumpTexture(lightmap, isLightmap: true); babylonPbrMaterial.lightmapTexture.coordinatesIndex = (lightmapCoordIndex >= 0) ? lightmapCoordIndex : exportationOptions.DefaultCoordinatesIndex; babylonPbrMaterial.useLightmapAsShadowmap = true; babylonPbrMaterial.lightmapTexture.uScale = lightmapScaleOffset.x; babylonPbrMaterial.lightmapTexture.vScale = lightmapScaleOffset.y; babylonPbrMaterial.lightmapTexture.uOffset = lightmapScaleOffset.z; babylonPbrMaterial.lightmapTexture.vOffset = lightmapScaleOffset.w; } } materialsDictionary.Add(babylonPbrMaterial.name, babylonPbrMaterial); return babylonPbrMaterial; }
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"); } } } } }