BabylonExporter.Material.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.Max;
  4. using BabylonExport.Entities;
  5. using MaxSharp;
  6. namespace Max2Babylon
  7. {
  8. partial class BabylonExporter
  9. {
  10. readonly List<Material> referencedMaterials = new List<Material>();
  11. private void ExportMaterial(Material materialNode, BabylonScene babylonScene)
  12. {
  13. var name = materialNode._Mtl.Name;
  14. var id = materialNode.GetGuid().ToString();
  15. RaiseMessage(name, true);
  16. if (materialNode.NumSubMaterials > 0)
  17. {
  18. var babylonMultimaterial = new BabylonMultiMaterial();
  19. babylonMultimaterial.name = name;
  20. babylonMultimaterial.id = id;
  21. var guids = new List<string>();
  22. for (var index = 0; index < materialNode.NumSubMaterials; index++)
  23. {
  24. var subMat = materialNode.GetSubMaterial(index) as Material;
  25. if (subMat != null)
  26. {
  27. guids.Add(subMat.GetGuid().ToString());
  28. if (!referencedMaterials.Contains(subMat))
  29. {
  30. referencedMaterials.Add(subMat);
  31. ExportMaterial(subMat, babylonScene);
  32. }
  33. }
  34. else
  35. {
  36. guids.Add(Guid.Empty.ToString());
  37. }
  38. }
  39. babylonMultimaterial.materials = guids.ToArray();
  40. babylonScene.MultiMaterialsList.Add(babylonMultimaterial);
  41. }
  42. var babylonMaterial = new BabylonMaterial();
  43. babylonMaterial.name = name;
  44. babylonMaterial.id = id;
  45. babylonMaterial.ambient = materialNode.GetAmbient(0).ToArray();
  46. babylonMaterial.diffuse = materialNode.GetDiffuse(0).ToArray();
  47. babylonMaterial.specular = materialNode.GetSpecular(0).Scale(materialNode.GetShininess(0));
  48. babylonMaterial.specularPower = materialNode.GetShinyStrength(0);
  49. babylonMaterial.emissive = materialNode.SelfIlluminationColorOn ? materialNode.GetSelfIllumColor(0).ToArray() : materialNode.GetDiffuse(0).Scale(materialNode.GetSelfIllumination(0));
  50. babylonMaterial.alpha = 1.0f - materialNode.GetTransparency(0);
  51. var stdMat = materialNode._Mtl.GetParamBlock(0).Owner as IStdMat2;
  52. if (stdMat != null)
  53. {
  54. // Textures
  55. babylonMaterial.ambientTexture = ExportTexture(stdMat, 0, babylonScene); // Ambient
  56. babylonMaterial.diffuseTexture = ExportTexture(stdMat, 1, babylonScene); // Diffuse
  57. babylonMaterial.specularTexture = ExportTexture(stdMat, 2, babylonScene); // Specular
  58. babylonMaterial.emissiveTexture = ExportTexture(stdMat, 5, babylonScene); // Emissive
  59. babylonMaterial.opacityTexture = ExportTexture(stdMat, 6, babylonScene); // Opacity
  60. babylonMaterial.bumpTexture = ExportTexture(stdMat, 8, babylonScene); // Bump
  61. babylonMaterial.reflectionTexture = ExportTexture(stdMat, 9, babylonScene); // Reflection
  62. // Constraints
  63. if (babylonMaterial.diffuseTexture != null)
  64. {
  65. babylonMaterial.emissive = new [] { 1.0f, 1.0f, 1.0f };
  66. }
  67. if (babylonMaterial.emissiveTexture != null)
  68. {
  69. babylonMaterial.emissive = new float[]{0, 0, 0};
  70. }
  71. if (babylonMaterial.opacityTexture != null && babylonMaterial.diffuseTexture != null &&
  72. babylonMaterial.diffuseTexture.name == babylonMaterial.opacityTexture.name &&
  73. babylonMaterial.diffuseTexture.hasAlpha && !babylonMaterial.opacityTexture.getAlphaFromRGB)
  74. {
  75. // This is a alpha testing purpose
  76. babylonMaterial.opacityTexture = null;
  77. babylonMaterial.diffuseTexture.hasAlpha = true;
  78. RaiseWarning("Opacity texture was removed because alpha from diffuse texture can be use instead", true);
  79. RaiseWarning("If you do not want this behavior, just set Alpha Source = None on your diffuse texture", true);
  80. }
  81. }
  82. babylonScene.MaterialsList.Add(babylonMaterial);
  83. }
  84. }
  85. }