BabylonExporter.Light.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. private BabylonLight ExportLight(Node lightNode, BabylonScene babylonScene)
  11. {
  12. var maxLight = (lightNode.Object as Light);
  13. var babylonLight = new BabylonLight();
  14. RaiseMessage(maxLight.Name, true);
  15. babylonLight.name = lightNode.Name;
  16. babylonLight.id = lightNode.GetGuid().ToString();
  17. // Type
  18. var lightState = Loader.Global.LightState.Create();
  19. maxLight._Light.EvalLightState(0, Interval.Forever._IInterval, lightState);
  20. var directionScale = -1;
  21. switch (lightState.Type)
  22. {
  23. case LightType.OmniLgt:
  24. babylonLight.type = 0;
  25. break;
  26. case LightType.SpotLgt:
  27. babylonLight.type = 2;
  28. babylonLight.angle = (float)(maxLight.GetFallOffSize(0, Interval.Forever) * Math.PI / 180.0f);
  29. babylonLight.exponent = 1;
  30. break;
  31. case LightType.DirectLgt:
  32. babylonLight.type = 1;
  33. // Shadows
  34. if (maxLight.ShadowMethod == 1)
  35. {
  36. ExportShadowGenerator(lightNode, babylonScene);
  37. }
  38. break;
  39. case LightType.AmbientLgt:
  40. babylonLight.type = 3;
  41. babylonLight.groundColor = new float[] { 0, 0, 0 };
  42. directionScale = 1;
  43. break;
  44. }
  45. // Position
  46. var wm = lightNode.GetWorldMatrix(0, false);
  47. var position = wm.Trans;
  48. babylonLight.position = position.ToArraySwitched();
  49. // Direction
  50. var target = lightNode._Node.Target;
  51. if (target != null)
  52. {
  53. var targetWm = target.GetObjTMBeforeWSM(0, Interval.Forever._IInterval);
  54. var targetPosition = targetWm.Trans;
  55. var direction = targetPosition.Subtract(position);
  56. babylonLight.direction = direction.ToArraySwitched();
  57. }
  58. else
  59. {
  60. var dir = wm.GetRow(2).MultiplyBy(directionScale);
  61. babylonLight.direction = dir.ToArraySwitched();
  62. }
  63. // Exclusion
  64. var maxScene = Kernel.Scene;
  65. var inclusion = maxLight._Light.ExclList.TestFlag(1); //NT_INCLUDE
  66. var checkExclusionList = maxLight._Light.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
  67. if (checkExclusionList)
  68. {
  69. var list = new List<string>();
  70. foreach (var meshNode in maxScene.NodesListBySuperClass(SuperClassID.GeometricObject))
  71. {
  72. if (meshNode._Node.CastShadows == 1)
  73. {
  74. var inList = maxLight._Light.ExclList.FindNode(meshNode._Node) != -1;
  75. if ((!inList && inclusion) || (inList && !inclusion))
  76. {
  77. list.Add(meshNode.GetGuid().ToString());
  78. }
  79. }
  80. }
  81. babylonLight.excludedMeshesIds = list.ToArray();
  82. }
  83. // Other fields
  84. babylonLight.intensity = maxLight.GetIntensity(0, Interval.Forever);
  85. babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
  86. babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
  87. if (maxLight.UseAttenuation)
  88. {
  89. babylonLight.range = maxLight.GetAttenuation(0, 1, Interval.Forever);
  90. }
  91. babylonScene.LightsList.Add(babylonLight);
  92. return babylonLight;
  93. }
  94. }
  95. }