BabylonExporter.Texture.cs 4.9 KB

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