BabylonExporter.Light.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 void ExportLight(Node lightNode, BabylonScene babylonScene)
  11. {
  12. if (lightNode._Node.GetBoolProperty("babylonjs_noexport"))
  13. {
  14. return;
  15. }
  16. var maxLight = (lightNode.Object as Light);
  17. var babylonLight = new BabylonLight();
  18. RaiseMessage(maxLight.Name, true);
  19. babylonLight.name = lightNode.Name;
  20. babylonLight.id = lightNode.GetGuid().ToString();
  21. // Type
  22. var lightState = Loader.Global.LightState.Create();
  23. maxLight._Light.EvalLightState(0, Interval.Forever._IInterval, lightState);
  24. var directionScale = -1;
  25. switch (lightState.Type)
  26. {
  27. case LightType.OmniLgt:
  28. babylonLight.type = 0;
  29. break;
  30. case LightType.SpotLgt:
  31. babylonLight.type = 2;
  32. babylonLight.angle = (float)(maxLight.GetFallOffSize(0, Interval.Forever) * Math.PI / 180.0f);
  33. babylonLight.exponent = 1;
  34. break;
  35. case LightType.DirectLgt:
  36. babylonLight.type = 1;
  37. break;
  38. case LightType.AmbientLgt:
  39. babylonLight.type = 3;
  40. babylonLight.groundColor = new float[] { 0, 0, 0 };
  41. directionScale = 1;
  42. break;
  43. }
  44. // Shadows
  45. if (maxLight.ShadowMethod == 1)
  46. {
  47. if (lightState.Type == LightType.DirectLgt)
  48. {
  49. ExportShadowGenerator(lightNode, babylonScene);
  50. }
  51. else
  52. {
  53. RaiseWarning("Shadows maps are only supported for directional lights", true);
  54. }
  55. }
  56. // Position
  57. var wm = lightNode.GetWorldMatrix(0, false);
  58. var position = wm.Trans;
  59. babylonLight.position = position.ToArraySwitched();
  60. // Direction
  61. var target = lightNode._Node.Target;
  62. if (target != null)
  63. {
  64. var targetWm = target.GetObjTMBeforeWSM(0, Interval.Forever._IInterval);
  65. var targetPosition = targetWm.Trans;
  66. var direction = targetPosition.Subtract(position);
  67. babylonLight.direction = direction.ToArraySwitched();
  68. }
  69. else
  70. {
  71. var dir = wm.GetRow(2).MultiplyBy(directionScale);
  72. babylonLight.direction = dir.ToArraySwitched();
  73. }
  74. // Exclusion
  75. var maxScene = Kernel.Scene;
  76. var inclusion = maxLight._Light.ExclList.TestFlag(1); //NT_INCLUDE
  77. var checkExclusionList = maxLight._Light.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
  78. if (checkExclusionList)
  79. {
  80. var list = new List<string>();
  81. foreach (var meshNode in maxScene.NodesListBySuperClass(SuperClassID.GeometricObject))
  82. {
  83. if (meshNode._Node.CastShadows == 1)
  84. {
  85. var inList = maxLight._Light.ExclList.FindNode(meshNode._Node) != -1;
  86. if ((!inList && inclusion) || (inList && !inclusion))
  87. {
  88. list.Add(meshNode.GetGuid().ToString());
  89. }
  90. }
  91. }
  92. babylonLight.excludedMeshesIds = list.ToArray();
  93. }
  94. // Other fields
  95. babylonLight.intensity = maxLight.GetIntensity(0, Interval.Forever);
  96. babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
  97. babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Interval.Forever).ToArray() : new float[] { 0, 0, 0 };
  98. if (maxLight.UseAttenuation)
  99. {
  100. babylonLight.range = maxLight.GetAttenuation(0, 1, Interval.Forever);
  101. }
  102. // Animations
  103. var animations = new List<BabylonAnimation>();
  104. if (!ExportVector3Controller(lightNode._Node.TMController.PositionController, "position", animations))
  105. {
  106. ExportVector3Animation("position", animations, key =>
  107. {
  108. var worldMatrix = lightNode.GetWorldMatrix(key, lightNode.HasParent());
  109. return worldMatrix.Trans.ToArraySwitched();
  110. });
  111. }
  112. ExportVector3Animation("direction", animations, key =>
  113. {
  114. var targetNode = lightNode._Node.Target;
  115. if (targetNode != null)
  116. {
  117. var targetWm = target.GetObjTMBeforeWSM(0, Interval.Forever._IInterval);
  118. var targetPosition = targetWm.Trans;
  119. var direction = targetPosition.Subtract(position);
  120. return direction.ToArraySwitched();
  121. }
  122. var dir = wm.GetRow(2).MultiplyBy(directionScale);
  123. return dir.ToArraySwitched();
  124. });
  125. ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Interval.Forever) });
  126. babylonLight.animations = animations.ToArray();
  127. if (lightNode._Node.GetBoolProperty("babylonjs_autoanimate"))
  128. {
  129. babylonLight.autoAnimate = true;
  130. babylonLight.autoAnimateFrom = (int)lightNode._Node.GetFloatProperty("babylonjs_autoanimate_from");
  131. babylonLight.autoAnimateTo = (int)lightNode._Node.GetFloatProperty("babylonjs_autoanimate_to");
  132. babylonLight.autoAnimateLoop = lightNode._Node.GetBoolProperty("babylonjs_autoanimateloop");
  133. }
  134. babylonScene.LightsList.Add(babylonLight);
  135. }
  136. }
  137. }