BabylonExporter.Texture.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = (IBitmapTex) texMap.GetParamBlock(0).Owner;
  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. babylonTexture.uAng = uvGen.GetUAng(0);
  50. babylonTexture.vAng = uvGen.GetVAng(0);
  51. babylonTexture.wAng = uvGen.GetWAng(0);
  52. babylonTexture.wrapU = 0; // CLAMP
  53. if ((uvGen.TextureTiling & 1) != 0) // WRAP
  54. {
  55. babylonTexture.wrapU = 1;
  56. }
  57. else if ((uvGen.TextureTiling & 4) != 0) // MIRROR
  58. {
  59. babylonTexture.wrapU = 2;
  60. }
  61. babylonTexture.wrapV = 0; // CLAMP
  62. if ((uvGen.TextureTiling & 2) != 0) // WRAP
  63. {
  64. babylonTexture.wrapV = 1;
  65. }
  66. else if ((uvGen.TextureTiling & 8) != 0) // MIRROR
  67. {
  68. babylonTexture.wrapV = 2;
  69. }
  70. babylonTexture.name = Path.GetFileName(texture.MapName);
  71. try
  72. {
  73. if (File.Exists(texture.MapName))
  74. {
  75. File.Copy(texture.MapName, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  76. }
  77. else
  78. {
  79. var texturepath = Path.Combine(Path.GetDirectoryName(Loader.Core.CurFilePath), babylonTexture.name);
  80. if (File.Exists(texturepath))
  81. {
  82. File.Copy(texturepath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
  83. }
  84. else
  85. {
  86. RaiseWarning(string.Format("Texture {0} not found.", babylonTexture.name), true);
  87. }
  88. }
  89. }
  90. catch
  91. {
  92. RaiseWarning(string.Format("Unable to copy {0} to output folder.", babylonTexture.name), true);
  93. }
  94. return babylonTexture;
  95. }
  96. }
  97. }