BabylonExporter.Texture.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. var texture = texMap.GetParamBlock(0).Owner as IBitmapTex;
  102. if (texture == null)
  103. {
  104. return null;
  105. }
  106. if (forceAlpha)
  107. {
  108. babylonTexture.hasAlpha = true;
  109. babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2) || (texture.AlphaSource == 3);
  110. }
  111. else
  112. {
  113. babylonTexture.hasAlpha = (texture.AlphaSource != 3);
  114. babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2);
  115. }
  116. babylonTexture.level = stdMat.GetTexmapAmt(index, 0);
  117. var uvGen = texture.UVGen;
  118. switch (uvGen.GetCoordMapping(0))
  119. {
  120. case 1: //MAP_SPHERICAL
  121. babylonTexture.coordinatesMode = 1;
  122. break;
  123. case 2: //MAP_PLANAR
  124. babylonTexture.coordinatesMode = 2;
  125. break;
  126. default:
  127. babylonTexture.coordinatesMode = 0;
  128. break;
  129. }
  130. babylonTexture.coordinatesIndex = uvGen.MapChannel - 1;
  131. if (uvGen.MapChannel > 2)
  132. {
  133. RaiseWarning(string.Format("Unsupported map channel, Only channel 1 and 2 are supported."), 2);
  134. }
  135. babylonTexture.uOffset = uvGen.GetUOffs(0);
  136. babylonTexture.vOffset = uvGen.GetVOffs(0);
  137. babylonTexture.uScale = uvGen.GetUScl(0);
  138. babylonTexture.vScale = uvGen.GetVScl(0);
  139. if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
  140. {
  141. babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
  142. }
  143. babylonTexture.uAng = uvGen.GetUAng(0);
  144. babylonTexture.vAng = uvGen.GetVAng(0);
  145. babylonTexture.wAng = uvGen.GetWAng(0);
  146. babylonTexture.wrapU = 0; // CLAMP
  147. if ((uvGen.TextureTiling & 1) != 0) // WRAP
  148. {
  149. babylonTexture.wrapU = 1;
  150. }
  151. else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
  152. {
  153. babylonTexture.wrapU = 2;
  154. }
  155. babylonTexture.wrapV = 0; // CLAMP
  156. if ((uvGen.TextureTiling & 2) != 0) // WRAP
  157. {
  158. babylonTexture.wrapV = 1;
  159. }
  160. else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
  161. {
  162. babylonTexture.wrapV = 2;
  163. }
  164. babylonTexture.name = Path.GetFileName(texture.MapName);
  165. // Animations
  166. var animations = new List<BabylonAnimation>();
  167. ExportFloatAnimation("uOffset", animations, key => new[] { uvGen.GetUOffs(key) });
  168. ExportFloatAnimation("vOffset", animations, key => new[] { uvGen.GetVOffs(key) });
  169. ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) });
  170. ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) });
  171. ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) });
  172. ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) });
  173. ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) });
  174. babylonTexture.animations = animations.ToArray();
  175. var absolutePath = texture.Map.FullFilePath;
  176. // Copy texture to output
  177. try
  178. {
  179. if (File.Exists(absolutePath))
  180. {
  181. babylonTexture.isCube = IsTextureCube(absolutePath);
  182. if (CopyTexturesToOutput)
  183. {
  184. File.Copy(absolutePath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  185. }
  186. }
  187. else
  188. {
  189. RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), 2);
  190. }
  191. }
  192. catch
  193. {
  194. // silently fails
  195. }
  196. if (babylonTexture.isCube && !allowCube)
  197. {
  198. RaiseWarning(string.Format("Cube texture are only supported for reflection channel"), 2);
  199. }
  200. return babylonTexture;
  201. }
  202. }
  203. }