SceneBuilder.Materials.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.IO;
  3. using BabylonExport.Entities;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Unity3D2Babylon
  7. {
  8. partial class SceneBuilder
  9. {
  10. private void CopyTextureCube(string texturePath, Cubemap cubemap, BabylonTexture babylonTexture)
  11. {
  12. if (!babylonScene.AddTextureCube(texturePath))
  13. {
  14. return;
  15. }
  16. try
  17. {
  18. foreach (CubemapFace face in Enum.GetValues(typeof(CubemapFace)))
  19. {
  20. var faceTexturePath = Path.Combine(babylonScene.OutputPath, Path.GetFileNameWithoutExtension(texturePath));
  21. switch (face)
  22. {
  23. case CubemapFace.PositiveX:
  24. faceTexturePath += "_px.jpg";
  25. break;
  26. case CubemapFace.NegativeX:
  27. faceTexturePath += "_nx.jpg";
  28. break;
  29. case CubemapFace.PositiveY:
  30. faceTexturePath += "_py.jpg";
  31. break;
  32. case CubemapFace.NegativeY:
  33. faceTexturePath += "_ny.jpg";
  34. break;
  35. case CubemapFace.PositiveZ:
  36. faceTexturePath += "_pz.jpg";
  37. break;
  38. case CubemapFace.NegativeZ:
  39. faceTexturePath += "_nz.jpg";
  40. break;
  41. }
  42. var tempTexture = new Texture2D(cubemap.width, cubemap.height, cubemap.format, false);
  43. tempTexture.SetPixels(cubemap.GetPixels(face));
  44. tempTexture.Apply();
  45. File.WriteAllBytes(faceTexturePath, tempTexture.EncodeToJPG());
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. Debug.LogException(ex);
  51. }
  52. var textureName = Path.GetFileNameWithoutExtension(texturePath);
  53. babylonTexture.name = textureName;
  54. babylonTexture.isCube = true;
  55. babylonTexture.level = exportationOptions.ReflectionDefaultLevel;
  56. babylonTexture.coordinatesMode = 3;
  57. }
  58. private void CopyTexture(string texturePath, Texture2D texture2D, BabylonTexture babylonTexture)
  59. {
  60. bool needToDelete = false;
  61. var useJPG = !texture2D.alphaIsTransparency;
  62. // Convert unsupported file extensions
  63. if (texturePath.EndsWith(".psd") || texturePath.EndsWith(".tif") || texturePath.EndsWith(".exr"))
  64. {
  65. try
  66. {
  67. // Change texture import settings to be able to read texture data
  68. var textureImporter = AssetImporter.GetAtPath(texturePath) as TextureImporter;
  69. var previousIsReadable = textureImporter.isReadable;
  70. var previousNormalMap = textureImporter.normalmap;
  71. var previousLightmap = textureImporter.lightmap;
  72. var previousConvertToNormalmap = textureImporter.convertToNormalmap;
  73. var previousTextureType = textureImporter.textureType;
  74. var previousGrayscaleToAlpha = textureImporter.grayscaleToAlpha;
  75. textureImporter.textureType = TextureImporterType.Advanced;
  76. textureImporter.isReadable = true;
  77. textureImporter.lightmap = false;
  78. textureImporter.normalmap = false;
  79. textureImporter.convertToNormalmap = false;
  80. textureImporter.grayscaleToAlpha = false;
  81. AssetDatabase.ImportAsset(texturePath);
  82. texturePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(texturePath));
  83. var extension = useJPG ? ".jpg" : ".png";
  84. texturePath = texturePath.Replace(".psd", extension).Replace(".tif", extension).Replace(".exr", extension);
  85. var tempTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, false);
  86. tempTexture.SetPixels32(texture2D.GetPixels32());
  87. tempTexture.Apply();
  88. File.WriteAllBytes(texturePath, useJPG ? tempTexture.EncodeToJPG() : tempTexture.EncodeToPNG());
  89. needToDelete = true;
  90. // Restore
  91. textureImporter.isReadable = previousIsReadable;
  92. textureImporter.normalmap = previousNormalMap;
  93. textureImporter.lightmap = previousLightmap;
  94. textureImporter.convertToNormalmap = previousConvertToNormalmap;
  95. textureImporter.textureType = previousTextureType;
  96. textureImporter.grayscaleToAlpha = previousGrayscaleToAlpha;
  97. AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
  98. }
  99. catch (Exception ex)
  100. {
  101. Debug.LogException(ex);
  102. }
  103. }
  104. var textureName = Path.GetFileName(texturePath);
  105. babylonTexture.name = textureName;
  106. babylonScene.AddTexture(texturePath);
  107. if (needToDelete)
  108. {
  109. File.Delete(texturePath);
  110. }
  111. }
  112. private BabylonMaterial DumpMaterial(Material material, Renderer renderer)
  113. {
  114. if (!materialsDictionary.ContainsKey(material.name))
  115. {
  116. var bMat = new BabylonMaterial
  117. {
  118. name = material.name,
  119. id = Guid.NewGuid().ToString(),
  120. diffuse = new float[4],
  121. specular = new float[4]
  122. };
  123. bMat.diffuse[0] = 1.0f;
  124. bMat.diffuse[1] = 1.0f;
  125. bMat.diffuse[2] = 1.0f;
  126. bMat.diffuse[3] = 1.0f;
  127. if (material.HasProperty("_Color"))
  128. {
  129. bMat.diffuse = material.color.ToFloat();
  130. }
  131. if (material.HasProperty("_SpecColor"))
  132. {
  133. var specColor = material.GetColor("_SpecColor");
  134. bMat.specular = specColor.ToFloat();
  135. }
  136. if (material.HasProperty("_Shininess"))
  137. {
  138. var specShininess = material.GetFloat("_Shininess");
  139. bMat.specularPower = specShininess * 128;
  140. }
  141. if (material.HasProperty("_Emission"))
  142. {
  143. var emissiveColor = material.GetColor("_Emission");
  144. bMat.emissive = emissiveColor.ToFloat();
  145. }
  146. if (material.mainTexture)
  147. {
  148. var mainTexturePath = AssetDatabase.GetAssetPath(material.mainTexture);
  149. bMat.diffuseTexture = new BabylonTexture
  150. {
  151. uScale = material.mainTextureScale.x,
  152. vScale = material.mainTextureScale.y,
  153. uOffset = material.mainTextureOffset.x,
  154. vOffset = material.mainTextureOffset.y
  155. };
  156. var mainTexture2D = material.mainTexture as Texture2D;
  157. CopyTexture(mainTexturePath, mainTexture2D, bMat.diffuseTexture);
  158. var alphaCuttOff = 0f;
  159. if (material.HasProperty("_Cutoff"))
  160. {
  161. alphaCuttOff = material.GetFloat("_Cutoff");
  162. }
  163. if ((mainTexture2D && mainTexture2D.alphaIsTransparency) || alphaCuttOff > 0)
  164. {
  165. bMat.diffuseTexture.hasAlpha = true;
  166. bMat.backFaceCulling = false;
  167. }
  168. bMat.diffuse[0] = 1.0f;
  169. bMat.diffuse[1] = 1.0f;
  170. bMat.diffuse[2] = 1.0f;
  171. bMat.diffuse[3] = 1.0f;
  172. }
  173. bMat.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap");
  174. bMat.emissiveTexture = DumpTextureFromMaterial(material, "_Illum");
  175. bMat.ambientTexture = DumpTextureFromMaterial(material, "_LightMap");
  176. bMat.reflectionTexture = DumpTextureFromMaterial(material, "_Cube");
  177. //if (bMat.ambientTexture == null && renderer.lightmapIndex >= 0 && renderer.lightmapIndex != 255 && LightmapSettings.lightmaps.Length > renderer.lightmapIndex)
  178. //{
  179. // var lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapFar;
  180. // bMat.ambientTexture = DumpTexture(lightmap);
  181. // bMat.ambientTexture.coordinatesIndex = 1;
  182. // bMat.ambientTexture.uScale = renderer.lightmapTilingOffset.x;
  183. // bMat.ambientTexture.vScale = renderer.lightmapTilingOffset.y;
  184. // bMat.ambientTexture.uOffset = renderer.lightmapTilingOffset.z;
  185. // bMat.ambientTexture.vOffset = renderer.lightmapTilingOffset.w;
  186. //}
  187. materialsDictionary.Add(bMat.name, bMat);
  188. return bMat;
  189. }
  190. return materialsDictionary[material.name];
  191. }
  192. private BabylonTexture DumpTextureFromMaterial(Material material, string name)
  193. {
  194. if (!material.HasProperty(name))
  195. {
  196. return null;
  197. }
  198. var texture = material.GetTexture(name);
  199. return DumpTexture(texture, material, name);
  200. }
  201. private BabylonTexture DumpTexture(Texture texture, Material material = null, string name = "")
  202. {
  203. if (texture == null)
  204. {
  205. return null;
  206. }
  207. var texturePath = AssetDatabase.GetAssetPath(texture);
  208. var textureName = Path.GetFileName(texturePath);
  209. var babylonTexture = new BabylonTexture { name = textureName };
  210. if (material != null)
  211. {
  212. var textureScale = material.GetTextureScale(name);
  213. babylonTexture.uScale = textureScale.x;
  214. babylonTexture.vScale = textureScale.y;
  215. var textureOffset = material.GetTextureOffset(name);
  216. babylonTexture.uOffset = textureOffset.x;
  217. babylonTexture.vOffset = textureOffset.y;
  218. }
  219. var texture2D = texture as Texture2D;
  220. if (texture2D)
  221. {
  222. babylonTexture.hasAlpha = texture2D.alphaIsTransparency;
  223. CopyTexture(texturePath, texture2D, babylonTexture);
  224. }
  225. else
  226. {
  227. var cubemap = texture as Cubemap;
  228. if (cubemap != null)
  229. {
  230. CopyTextureCube(texturePath, cubemap, babylonTexture);
  231. }
  232. }
  233. return babylonTexture;
  234. }
  235. }
  236. }