123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using System;
- using System.IO;
- using BabylonExport.Entities;
- using UnityEditor;
- using UnityEngine;
- namespace Unity3D2Babylon
- {
- partial class SceneBuilder
- {
- 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;
- }
- var tempTexture = new Texture2D(cubemap.width, cubemap.height, cubemap.format, false);
- tempTexture.SetPixels(cubemap.GetPixels(face));
- tempTexture.Apply();
- 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;
- }
- 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 BabylonMaterial DumpMaterial(Material material, Renderer renderer)
- {
- if (!materialsDictionary.ContainsKey(material.name))
- {
- var bMat = new BabylonMaterial
- {
- name = material.name,
- id = Guid.NewGuid().ToString(),
- diffuse = new float[4],
- specular = new float[4]
- };
- bMat.diffuse[0] = 1.0f;
- bMat.diffuse[1] = 1.0f;
- bMat.diffuse[2] = 1.0f;
- bMat.diffuse[3] = 1.0f;
- if (material.HasProperty("_Color"))
- {
- bMat.diffuse = material.color.ToFloat();
- }
- if (material.HasProperty("_SpecColor"))
- {
- var specColor = material.GetColor("_SpecColor");
- bMat.specular = specColor.ToFloat();
- }
- if (material.HasProperty("_Shininess"))
- {
- var specShininess = material.GetFloat("_Shininess");
- bMat.specularPower = specShininess * 128;
- }
- if (material.HasProperty("_Emission"))
- {
- var emissiveColor = material.GetColor("_Emission");
- bMat.emissive = emissiveColor.ToFloat();
- }
- if (material.mainTexture)
- {
- var mainTexturePath = AssetDatabase.GetAssetPath(material.mainTexture);
- bMat.diffuseTexture = new BabylonTexture
- {
- uScale = material.mainTextureScale.x,
- vScale = material.mainTextureScale.y,
- uOffset = material.mainTextureOffset.x,
- vOffset = material.mainTextureOffset.y
- };
- var mainTexture2D = material.mainTexture as Texture2D;
- CopyTexture(mainTexturePath, mainTexture2D, bMat.diffuseTexture);
- var alphaCuttOff = 0f;
- if (material.HasProperty("_Cutoff"))
- {
- alphaCuttOff = material.GetFloat("_Cutoff");
- }
- if ((mainTexture2D && mainTexture2D.alphaIsTransparency) || alphaCuttOff > 0)
- {
- bMat.diffuseTexture.hasAlpha = true;
- bMat.backFaceCulling = false;
- }
- bMat.diffuse[0] = 1.0f;
- bMat.diffuse[1] = 1.0f;
- bMat.diffuse[2] = 1.0f;
- bMat.diffuse[3] = 1.0f;
- }
- bMat.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap");
- bMat.emissiveTexture = DumpTextureFromMaterial(material, "_Illum");
- bMat.ambientTexture = DumpTextureFromMaterial(material, "_LightMap");
- bMat.reflectionTexture = DumpTextureFromMaterial(material, "_Cube");
- //if (bMat.ambientTexture == null && renderer.lightmapIndex >= 0 && renderer.lightmapIndex != 255 && LightmapSettings.lightmaps.Length > renderer.lightmapIndex)
- //{
- // var lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapFar;
- // bMat.ambientTexture = DumpTexture(lightmap);
- // bMat.ambientTexture.coordinatesIndex = 1;
- // bMat.ambientTexture.uScale = renderer.lightmapTilingOffset.x;
- // bMat.ambientTexture.vScale = renderer.lightmapTilingOffset.y;
- // bMat.ambientTexture.uOffset = renderer.lightmapTilingOffset.z;
- // bMat.ambientTexture.vOffset = renderer.lightmapTilingOffset.w;
- //}
- materialsDictionary.Add(bMat.name, bMat);
- return bMat;
- }
- return materialsDictionary[material.name];
- }
- private BabylonTexture DumpTextureFromMaterial(Material material, string name)
- {
- if (!material.HasProperty(name))
- {
- return null;
- }
- var texture = material.GetTexture(name);
- return DumpTexture(texture, material, name);
- }
- 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;
- }
- }
- }
|