BabylonExporter.Texture.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Autodesk.Max;
  5. using BabylonExport.Entities;
  6. namespace Max2Babylon
  7. {
  8. partial class BabylonExporter
  9. {
  10. bool IsTextureCube(string filepath)
  11. {
  12. try
  13. {
  14. if (Path.GetExtension(filepath).ToLower() != ".dds")
  15. {
  16. return false;
  17. }
  18. var data = File.ReadAllBytes(filepath);
  19. var intArray = new int[data.Length / 4];
  20. Buffer.BlockCopy(data, 0, intArray, 0, intArray.Length * 4);
  21. int width = intArray[4];
  22. int height = intArray[3];
  23. int mipmapsCount = intArray[7];
  24. if ((width >> (mipmapsCount - 1)) > 1)
  25. {
  26. var expected = 1;
  27. var currentSize = Math.Max(width, height);
  28. while (currentSize > 1)
  29. {
  30. currentSize = currentSize >> 1;
  31. expected++;
  32. }
  33. RaiseWarning(string.Format("Mipmaps chain is not complete: {0} maps instead of {1} (based on texture max size: {2})", mipmapsCount, expected, width), 2);
  34. RaiseWarning(string.Format("You must generate a complete mipmaps chain for .dds)"), 2);
  35. RaiseWarning(string.Format("Mipmaps will be disabled for this texture. If you want automatic texture generation you cannot use a .dds)"), 2);
  36. }
  37. bool isCube = (intArray[28] & 0x200) == 0x200;
  38. return isCube;
  39. }
  40. catch
  41. {
  42. return false;
  43. }
  44. }
  45. private BabylonTexture ExportTexture(IStdMat2 stdMat, int index, out BabylonFresnelParameters fresnelParameters, BabylonScene babylonScene, bool allowCube = false, bool forceAlpha = false)
  46. {
  47. fresnelParameters = null;
  48. if (!stdMat.MapEnabled(index))
  49. {
  50. return null;
  51. }
  52. var babylonTexture = new BabylonTexture();
  53. var texMap = stdMat.GetSubTexmap(index);
  54. if (texMap == null)
  55. {
  56. RaiseWarning("Texture channel " + index + " activated but no texture found.");
  57. return null;
  58. }
  59. // Fallout
  60. if (texMap.ClassName == "Falloff") // This is the only way I found to detect it. This is crappy but it works
  61. {
  62. fresnelParameters = new BabylonFresnelParameters();
  63. var paramBlock = texMap.GetParamBlock(0);
  64. var color1 = paramBlock.GetColor(0, 0, 0);
  65. var color2 = paramBlock.GetColor(4, 0, 0);
  66. fresnelParameters.isEnabled = true;
  67. fresnelParameters.leftColor = color2.ToArray();
  68. fresnelParameters.rightColor = color1.ToArray();
  69. if (paramBlock.GetInt(8, 0, 0) == 2)
  70. {
  71. fresnelParameters.power = paramBlock.GetFloat(12, 0, 0);
  72. }
  73. else
  74. {
  75. fresnelParameters.power = 1;
  76. }
  77. var texMap1 = paramBlock.GetTexmap(2, 0, 0);
  78. var texMap1On = paramBlock.GetInt(3, 0, 0);
  79. var texMap2 = paramBlock.GetTexmap(6, 0, 0);
  80. var texMap2On = paramBlock.GetInt(7, 0, 0);
  81. if (texMap1 != null && texMap1On != 0)
  82. {
  83. texMap = texMap1;
  84. fresnelParameters.rightColor = new float[] { 1, 1, 1 };
  85. if (texMap2 != null && texMap2On != 0)
  86. {
  87. RaiseWarning(string.Format("You cannot specify two textures for falloff. Only one is supported"), 2);
  88. }
  89. }
  90. else if (texMap2 != null && texMap2On != 0)
  91. {
  92. fresnelParameters.leftColor = new float[] { 1, 1, 1 };
  93. texMap = texMap2;
  94. }
  95. else
  96. {
  97. return null;
  98. }
  99. }
  100. // Bitmap
  101. if (texMap.GetParamBlock(0) == null || texMap.GetParamBlock(0).Owner == null)
  102. {
  103. return null;
  104. }
  105. var texture = texMap.GetParamBlock(0).Owner as IBitmapTex;
  106. if (texture == null)
  107. {
  108. return null;
  109. }
  110. if (forceAlpha)
  111. {
  112. babylonTexture.hasAlpha = true;
  113. babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2) || (texture.AlphaSource == 3);
  114. }
  115. else
  116. {
  117. babylonTexture.hasAlpha = (texture.AlphaSource != 3);
  118. babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2);
  119. }
  120. babylonTexture.level = stdMat.GetTexmapAmt(index, 0);
  121. var uvGen = texture.UVGen;
  122. switch (uvGen.GetCoordMapping(0))
  123. {
  124. case 1: //MAP_SPHERICAL
  125. babylonTexture.coordinatesMode = 1;
  126. break;
  127. case 2: //MAP_PLANAR
  128. babylonTexture.coordinatesMode = 2;
  129. break;
  130. default:
  131. babylonTexture.coordinatesMode = 0;
  132. break;
  133. }
  134. babylonTexture.coordinatesIndex = uvGen.MapChannel - 1;
  135. if (uvGen.MapChannel > 2)
  136. {
  137. RaiseWarning(string.Format("Unsupported map channel, Only channel 1 and 2 are supported."), 2);
  138. }
  139. babylonTexture.uOffset = uvGen.GetUOffs(0);
  140. babylonTexture.vOffset = uvGen.GetVOffs(0);
  141. babylonTexture.uScale = uvGen.GetUScl(0);
  142. babylonTexture.vScale = uvGen.GetVScl(0);
  143. if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
  144. {
  145. babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
  146. }
  147. babylonTexture.uAng = uvGen.GetUAng(0);
  148. babylonTexture.vAng = uvGen.GetVAng(0);
  149. babylonTexture.wAng = uvGen.GetWAng(0);
  150. babylonTexture.wrapU = 0; // CLAMP
  151. if ((uvGen.TextureTiling & 1) != 0) // WRAP
  152. {
  153. babylonTexture.wrapU = 1;
  154. }
  155. else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
  156. {
  157. babylonTexture.wrapU = 2;
  158. }
  159. babylonTexture.wrapV = 0; // CLAMP
  160. if ((uvGen.TextureTiling & 2) != 0) // WRAP
  161. {
  162. babylonTexture.wrapV = 1;
  163. }
  164. else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
  165. {
  166. babylonTexture.wrapV = 2;
  167. }
  168. babylonTexture.name = Path.GetFileName(texture.MapName);
  169. // Animations
  170. var animations = new List<BabylonAnimation>();
  171. ExportFloatAnimation("uOffset", animations, key => new[] { uvGen.GetUOffs(key) });
  172. ExportFloatAnimation("vOffset", animations, key => new[] { -uvGen.GetVOffs(key) });
  173. ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) });
  174. ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) });
  175. ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) });
  176. ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) });
  177. ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) });
  178. babylonTexture.animations = animations.ToArray();
  179. var absolutePath = texture.Map.FullFilePath;
  180. // Copy texture to output
  181. try
  182. {
  183. if (File.Exists(absolutePath))
  184. {
  185. babylonTexture.isCube = IsTextureCube(absolutePath);
  186. if (CopyTexturesToOutput)
  187. {
  188. File.Copy(absolutePath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  189. }
  190. }
  191. else
  192. {
  193. RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), 2);
  194. }
  195. }
  196. catch
  197. {
  198. // silently fails
  199. }
  200. if (babylonTexture.isCube && !allowCube)
  201. {
  202. RaiseWarning(string.Format("Cube texture are only supported for reflection channel"), 2);
  203. }
  204. return babylonTexture;
  205. }
  206. }
  207. }