SceneBuilder.Materials.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, bool isLightmap = false)
  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. if (isLightmap)
  87. {
  88. Color[] pixels = texture2D.GetPixels(0, 0, texture2D.width, texture2D.height);
  89. for (int index = 0; index < pixels.Length; index++)
  90. {
  91. pixels[index].r = pixels[index].r * pixels[index].a * 5;
  92. pixels[index].g = pixels[index].g * pixels[index].a * 5;
  93. pixels[index].b = pixels[index].b * pixels[index].a * 5;
  94. }
  95. tempTexture.SetPixels(pixels);
  96. }
  97. else {
  98. tempTexture.SetPixels32(texture2D.GetPixels32());
  99. }
  100. tempTexture.Apply();
  101. File.WriteAllBytes(texturePath, useJPG ? tempTexture.EncodeToJPG() : tempTexture.EncodeToPNG());
  102. needToDelete = true;
  103. // Restore
  104. textureImporter.isReadable = previousIsReadable;
  105. textureImporter.normalmap = previousNormalMap;
  106. textureImporter.lightmap = previousLightmap;
  107. textureImporter.convertToNormalmap = previousConvertToNormalmap;
  108. textureImporter.textureType = previousTextureType;
  109. textureImporter.grayscaleToAlpha = previousGrayscaleToAlpha;
  110. AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
  111. }
  112. catch (Exception ex)
  113. {
  114. Debug.LogException(ex);
  115. }
  116. }
  117. var textureName = Path.GetFileName(texturePath);
  118. babylonTexture.name = textureName;
  119. babylonScene.AddTexture(texturePath);
  120. if (needToDelete)
  121. {
  122. File.Delete(texturePath);
  123. }
  124. }
  125. private BabylonMaterial DumpMaterial(Material material, Renderer renderer)
  126. {
  127. var materialNotSupported = false;
  128. if (!materialsDictionary.ContainsKey(material.name))
  129. {
  130. var bMat = new BabylonMaterial
  131. {
  132. name = material.name,
  133. id = Guid.NewGuid().ToString(),
  134. diffuse = new float[4],
  135. specular = new float[4]
  136. };
  137. bMat.diffuse[0] = 1.0f;
  138. bMat.diffuse[1] = 1.0f;
  139. bMat.diffuse[2] = 1.0f;
  140. bMat.diffuse[3] = 1.0f;
  141. if (material.HasProperty("_Color"))
  142. {
  143. bMat.diffuse = material.color.ToFloat();
  144. }
  145. if (material.HasProperty("_SpecColor"))
  146. {
  147. var specColor = material.GetColor("_SpecColor");
  148. bMat.specular = specColor.ToFloat();
  149. }
  150. if (material.HasProperty("_Shininess"))
  151. {
  152. var specShininess = material.GetFloat("_Shininess");
  153. bMat.specularPower = specShininess * 128;
  154. }
  155. if (material.HasProperty("_Emission"))
  156. {
  157. var emissiveColor = material.GetColor("_Emission");
  158. bMat.emissive = emissiveColor.ToFloat();
  159. }
  160. if (material.mainTexture && material.mainTexture.GetType().FullName == "UnityEngine.ProceduralTexture")
  161. {
  162. materialNotSupported = true;
  163. Debug.LogWarning("ProceduralTexture: " + material.mainTexture.name + " not supported by Babylon.js");
  164. }
  165. if (material.mainTexture && !(materialNotSupported))
  166. {
  167. var mainTexturePath = AssetDatabase.GetAssetPath(material.mainTexture);
  168. bMat.diffuseTexture = new BabylonTexture
  169. {
  170. uScale = material.mainTextureScale.x,
  171. vScale = material.mainTextureScale.y,
  172. uOffset = material.mainTextureOffset.x,
  173. vOffset = material.mainTextureOffset.y
  174. };
  175. var mainTexture2D = material.mainTexture as Texture2D;
  176. CopyTexture(mainTexturePath, mainTexture2D, bMat.diffuseTexture);
  177. var alphaCuttOff = 0f;
  178. if (material.HasProperty("_Cutoff"))
  179. {
  180. alphaCuttOff = material.GetFloat("_Cutoff");
  181. }
  182. if ((mainTexture2D && mainTexture2D.alphaIsTransparency) || alphaCuttOff > 0)
  183. {
  184. bMat.diffuseTexture.hasAlpha = true;
  185. bMat.backFaceCulling = false;
  186. }
  187. bMat.diffuse[0] = 1.0f;
  188. bMat.diffuse[1] = 1.0f;
  189. bMat.diffuse[2] = 1.0f;
  190. bMat.diffuse[3] = 1.0f;
  191. }
  192. bMat.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap");
  193. bMat.emissiveTexture = DumpTextureFromMaterial(material, "_Illum");
  194. bMat.ambientTexture = DumpTextureFromMaterial(material, "_LightMap");
  195. bMat.reflectionTexture = DumpTextureFromMaterial(material, "_Cube");
  196. if (bMat.ambientTexture == null && renderer.lightmapIndex >= 0 && renderer.lightmapIndex != 255 && LightmapSettings.lightmaps.Length > renderer.lightmapIndex)
  197. {
  198. var lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapFar;
  199. bMat.lightmapTexture = DumpTexture(lightmap, isLightmap: true);
  200. bMat.lightmapTexture.coordinatesIndex = 1;
  201. bMat.useLightmapAsShadowmap = true;
  202. bMat.lightmapTexture.uScale = renderer.lightmapScaleOffset.x;
  203. bMat.lightmapTexture.vScale = renderer.lightmapScaleOffset.y;
  204. bMat.lightmapTexture.uOffset = renderer.lightmapScaleOffset.z;
  205. bMat.lightmapTexture.vOffset = renderer.lightmapScaleOffset.w;
  206. }
  207. materialsDictionary.Add(bMat.name, bMat);
  208. return bMat;
  209. }
  210. return materialsDictionary[material.name];
  211. }
  212. private BabylonTexture DumpTextureFromMaterial(Material material, string name)
  213. {
  214. if (!material.HasProperty(name))
  215. {
  216. return null;
  217. }
  218. var texture = material.GetTexture(name);
  219. return DumpTexture(texture, material, name);
  220. }
  221. private BabylonTexture DumpTexture(Texture texture, Material material = null, string name = "", bool isLightmap = false)
  222. {
  223. if (texture == null)
  224. {
  225. return null;
  226. }
  227. var texturePath = AssetDatabase.GetAssetPath(texture);
  228. var textureName = Path.GetFileName(texturePath);
  229. var babylonTexture = new BabylonTexture { name = textureName };
  230. if (material != null)
  231. {
  232. var textureScale = material.GetTextureScale(name);
  233. babylonTexture.uScale = textureScale.x;
  234. babylonTexture.vScale = textureScale.y;
  235. var textureOffset = material.GetTextureOffset(name);
  236. babylonTexture.uOffset = textureOffset.x;
  237. babylonTexture.vOffset = textureOffset.y;
  238. }
  239. var texture2D = texture as Texture2D;
  240. if (texture2D)
  241. {
  242. babylonTexture.hasAlpha = texture2D.alphaIsTransparency;
  243. CopyTexture(texturePath, texture2D, babylonTexture, isLightmap);
  244. }
  245. else
  246. {
  247. var cubemap = texture as Cubemap;
  248. if (cubemap != null)
  249. {
  250. CopyTextureCube(texturePath, cubemap, babylonTexture);
  251. }
  252. }
  253. return babylonTexture;
  254. }
  255. }
  256. }