BabylonExporter.Texture.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Autodesk.Max;
  4. using Autodesk.Max.MaxSDK.Util;
  5. using BabylonExport.Entities;
  6. using MaxSharp;
  7. namespace Max2Babylon
  8. {
  9. partial class BabylonExporter
  10. {
  11. private BabylonTexture ExportTexture(IStdMat2 stdMat, int index, BabylonScene babylonScene)
  12. {
  13. if (!stdMat.MapEnabled(index))
  14. {
  15. return null;
  16. }
  17. var babylonTexture = new BabylonTexture();
  18. var texMap = stdMat.GetSubTexmap(index);
  19. var texture = texMap.GetParamBlock(0).Owner as IBitmapTex;
  20. if (texture == null)
  21. {
  22. return null;
  23. }
  24. babylonTexture.hasAlpha = (texture.AlphaSource != 3);
  25. babylonTexture.getAlphaFromRGB = (texture.AlphaSource == 2);
  26. babylonTexture.level = stdMat.GetTexmapAmt(index, 0);
  27. var uvGen = texture.UVGen;
  28. switch (uvGen.GetCoordMapping(0))
  29. {
  30. case 1: //MAP_SPHERICAL
  31. babylonTexture.coordinatesMode = 1;
  32. break;
  33. case 2: //MAP_PLANAR
  34. babylonTexture.coordinatesMode = 2;
  35. break;
  36. default:
  37. babylonTexture.coordinatesMode = 0;
  38. break;
  39. }
  40. babylonTexture.coordinatesIndex = uvGen.MapChannel - 1;
  41. if (uvGen.MapChannel > 2)
  42. {
  43. RaiseWarning(string.Format("Unsupported map channel, Only channel 1 and 2 are supported."));
  44. }
  45. babylonTexture.uOffset = uvGen.GetUOffs(0);
  46. babylonTexture.vOffset = uvGen.GetVOffs(0);
  47. babylonTexture.uScale = uvGen.GetUScl(0);
  48. babylonTexture.vScale = uvGen.GetVScl(0);
  49. if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
  50. {
  51. babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
  52. }
  53. babylonTexture.uAng = uvGen.GetUAng(0);
  54. babylonTexture.vAng = uvGen.GetVAng(0);
  55. babylonTexture.wAng = uvGen.GetWAng(0);
  56. babylonTexture.wrapU = 0; // CLAMP
  57. if ((uvGen.TextureTiling & 1) != 0) // WRAP
  58. {
  59. babylonTexture.wrapU = 1;
  60. }
  61. else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
  62. {
  63. babylonTexture.wrapU = 2;
  64. }
  65. babylonTexture.wrapV = 0; // CLAMP
  66. if ((uvGen.TextureTiling & 2) != 0) // WRAP
  67. {
  68. babylonTexture.wrapV = 1;
  69. }
  70. else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
  71. {
  72. babylonTexture.wrapV = 2;
  73. }
  74. babylonTexture.name = Path.GetFileName(texture.MapName);
  75. // Animations
  76. var animations = new List<BabylonAnimation>();
  77. ExportFloatAnimation("uOffset", animations, key => new []{uvGen.GetUOffs(key)});
  78. ExportFloatAnimation("vOffset", animations, key => new[] { uvGen.GetVOffs(key) });
  79. ExportFloatAnimation("uScale", animations, key => new[] { uvGen.GetUScl(key) });
  80. ExportFloatAnimation("vScale", animations, key => new[] { uvGen.GetVScl(key) });
  81. ExportFloatAnimation("uAng", animations, key => new[] { uvGen.GetUAng(key) });
  82. ExportFloatAnimation("vAng", animations, key => new[] { uvGen.GetVAng(key) });
  83. ExportFloatAnimation("wAng", animations, key => new[] { uvGen.GetWAng(key) });
  84. babylonTexture.animations = animations.ToArray();
  85. // Copy texture to output
  86. try
  87. {
  88. if (File.Exists(texture.MapName))
  89. {
  90. File.Copy(texture.MapName, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  91. }
  92. else
  93. {
  94. var texturepath = Path.Combine(Path.GetDirectoryName(Loader.Core.CurFilePath), babylonTexture.name);
  95. if (File.Exists(texturepath))
  96. {
  97. File.Copy(texturepath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  98. }
  99. else
  100. {
  101. RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), true);
  102. }
  103. }
  104. }
  105. catch
  106. {
  107. RaiseWarning(string.Format("Unable to copy {0} to output folder.", babylonTexture.name), true);
  108. }
  109. return babylonTexture;
  110. }
  111. }
  112. }