BabylonExporter.Light.cs 5.9 KB

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