BabylonExporter.Material.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.Max;
  4. using BabylonExport.Entities;
  5. namespace Max2Babylon
  6. {
  7. partial class BabylonExporter
  8. {
  9. readonly List<IMtl> referencedMaterials = new List<IMtl>();
  10. private void ExportMaterial(IMtl materialNode, BabylonScene babylonScene)
  11. {
  12. var name = materialNode.Name;
  13. var id = materialNode.GetGuid().ToString();
  14. RaiseMessage(name, 1);
  15. if (materialNode.NumSubMtls > 0)
  16. {
  17. var babylonMultimaterial = new BabylonMultiMaterial {name = name, id = id};
  18. var guids = new List<string>();
  19. for (var index = 0; index < materialNode.NumSubMtls; index++)
  20. {
  21. var subMat = materialNode.GetSubMtl(index);
  22. if (subMat != null)
  23. {
  24. guids.Add(subMat.GetGuid().ToString());
  25. if (!referencedMaterials.Contains(subMat))
  26. {
  27. referencedMaterials.Add(subMat);
  28. ExportMaterial(subMat, babylonScene);
  29. }
  30. }
  31. else
  32. {
  33. guids.Add(Guid.Empty.ToString());
  34. }
  35. }
  36. babylonMultimaterial.materials = guids.ToArray();
  37. babylonScene.MultiMaterialsList.Add(babylonMultimaterial);
  38. return;
  39. }
  40. var babylonMaterial = new BabylonMaterial
  41. {
  42. name = name,
  43. id = id,
  44. ambient = materialNode.GetAmbient(0, false).ToArray(),
  45. diffuse = materialNode.GetDiffuse(0, false).ToArray(),
  46. specular = materialNode.GetSpecular(0, false).Scale(materialNode.GetShinStr(0, false)),
  47. specularPower = materialNode.GetShininess(0, false)*256,
  48. emissive =
  49. materialNode.GetSelfIllumColorOn(0, false)
  50. ? materialNode.GetSelfIllumColor(0, false).ToArray()
  51. : materialNode.GetDiffuse(0, false).Scale(materialNode.GetSelfIllum(0, false)),
  52. alpha = 1.0f - materialNode.GetXParency(0, false)
  53. };
  54. var stdMat = materialNode.GetParamBlock(0).Owner as IStdMat2;
  55. if (stdMat != null)
  56. {
  57. babylonMaterial.backFaceCulling = !stdMat.TwoSided;
  58. babylonMaterial.wireframe = stdMat.Wire;
  59. // Textures
  60. BabylonFresnelParameters fresnelParameters;
  61. babylonMaterial.ambientTexture = ExportTexture(stdMat, 0, out fresnelParameters, babylonScene); // Ambient
  62. babylonMaterial.diffuseTexture = ExportTexture(stdMat, 1, out fresnelParameters, babylonScene); // Diffuse
  63. if (fresnelParameters != null)
  64. {
  65. babylonMaterial.diffuseFresnelParameters = fresnelParameters;
  66. }
  67. babylonMaterial.specularTexture = ExportTexture(stdMat, 2, out fresnelParameters, babylonScene); // Specular
  68. babylonMaterial.emissiveTexture = ExportTexture(stdMat, 5, out fresnelParameters, babylonScene); // Emissive
  69. if (fresnelParameters != null)
  70. {
  71. babylonMaterial.emissiveFresnelParameters = fresnelParameters;
  72. if (babylonMaterial.emissive[0] == 0 &&
  73. babylonMaterial.emissive[1] == 0 &&
  74. babylonMaterial.emissive[2] == 0)
  75. {
  76. babylonMaterial.emissive = new float[]{1, 1, 1};
  77. }
  78. }
  79. babylonMaterial.opacityTexture = ExportTexture(stdMat, 6, out fresnelParameters, babylonScene, false, true); // Opacity
  80. if (fresnelParameters != null)
  81. {
  82. babylonMaterial.opacityFresnelParameters = fresnelParameters;
  83. }
  84. babylonMaterial.bumpTexture = ExportTexture(stdMat, 8, out fresnelParameters, babylonScene); // Bump
  85. babylonMaterial.reflectionTexture = ExportTexture(stdMat, 9, out fresnelParameters, babylonScene, true); // Reflection
  86. if (fresnelParameters != null)
  87. {
  88. if (babylonMaterial.reflectionTexture == null)
  89. {
  90. RaiseWarning("Fallout cannot be used with reflection channel without a texture", 2);
  91. }
  92. else
  93. {
  94. babylonMaterial.reflectionFresnelParameters = fresnelParameters;
  95. }
  96. }
  97. // Constraints
  98. if (babylonMaterial.diffuseTexture != null)
  99. {
  100. babylonMaterial.diffuse = new [] { 1.0f, 1.0f, 1.0f };
  101. }
  102. if (babylonMaterial.emissiveTexture != null)
  103. {
  104. babylonMaterial.emissive = new float[]{0, 0, 0};
  105. }
  106. if (babylonMaterial.opacityTexture != null && babylonMaterial.diffuseTexture != null &&
  107. babylonMaterial.diffuseTexture.name == babylonMaterial.opacityTexture.name &&
  108. babylonMaterial.diffuseTexture.hasAlpha && !babylonMaterial.opacityTexture.getAlphaFromRGB)
  109. {
  110. // This is a alpha testing purpose
  111. babylonMaterial.opacityTexture = null;
  112. babylonMaterial.diffuseTexture.hasAlpha = true;
  113. RaiseWarning("Opacity texture was removed because alpha from diffuse texture can be use instead", 2);
  114. RaiseWarning("If you do not want this behavior, just set Alpha Source = None on your diffuse texture", 2);
  115. }
  116. }
  117. babylonScene.MaterialsList.Add(babylonMaterial);
  118. }
  119. }
  120. }