BabylonExporter.Light.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. void ExportDefaultLight(BabylonScene babylonScene)
  10. {
  11. var babylonLight = new BabylonLight();
  12. babylonLight.name = "Default light";
  13. babylonLight.id = Guid.NewGuid().ToString();
  14. babylonLight.type = 3;
  15. babylonLight.groundColor = new float[] { 0, 0, 0 };
  16. babylonLight.direction = new[] { 0, 1.0f, 0 };
  17. babylonLight.intensity = 1;
  18. babylonLight.diffuse = new[] { 1.0f, 1.0f, 1.0f };
  19. babylonLight.specular = new[] { 1.0f, 1.0f, 1.0f };
  20. babylonScene.LightsList.Add(babylonLight);
  21. }
  22. private void ExportLight(IIGameNode lightNode, BabylonScene babylonScene)
  23. {
  24. if (lightNode.MaxNode.GetBoolProperty("babylonjs_noexport"))
  25. {
  26. return;
  27. }
  28. var gameLight = lightNode.IGameObject.AsGameLight();
  29. var initialized = gameLight.InitializeData;
  30. var babylonLight = new BabylonLight();
  31. RaiseMessage(lightNode.Name, 1);
  32. babylonLight.name = lightNode.Name;
  33. babylonLight.id = lightNode.MaxNode.GetGuid().ToString();
  34. // Type
  35. var maxLight = (lightNode.MaxNode.ObjectRef as ILightObject);
  36. var lightState = Loader.Global.LightState.Create();
  37. maxLight.EvalLightState(0, Tools.Forever, lightState);
  38. switch (lightState.Type)
  39. {
  40. case LightType.OmniLgt:
  41. babylonLight.type = 0;
  42. break;
  43. case LightType.SpotLgt:
  44. babylonLight.type = 2;
  45. babylonLight.angle = (float)(maxLight.GetFallsize(0, Tools.Forever) * Math.PI / 180.0f);
  46. babylonLight.exponent = 1;
  47. break;
  48. case LightType.DirectLgt:
  49. babylonLight.type = 1;
  50. break;
  51. case LightType.AmbientLgt:
  52. babylonLight.type = 3;
  53. babylonLight.groundColor = new float[] { 0, 0, 0 };
  54. break;
  55. }
  56. // Shadows
  57. if (maxLight.ShadowMethod == 1)
  58. {
  59. if (lightState.Type == LightType.DirectLgt)
  60. {
  61. ExportShadowGenerator(lightNode.MaxNode, babylonScene);
  62. }
  63. else
  64. {
  65. RaiseWarning("Shadows maps are only supported for directional lights", 2);
  66. }
  67. }
  68. // Position
  69. var wm = lightNode.GetObjectTM(0);
  70. var position = wm.Translation;
  71. babylonLight.position = new float[] { position.X, position.Y, position.Z };
  72. // Direction
  73. var target = gameLight.LightTarget;
  74. if (target != null)
  75. {
  76. var targetWm = target.GetObjectTM(0);
  77. var targetPosition = targetWm.Translation;
  78. var direction = targetPosition.Subtract(position).Normalize;
  79. babylonLight.direction = new float[] { direction.X, direction.Y, direction.Z };
  80. }
  81. else
  82. {
  83. var vDir = Loader.Global.Point3.Create(0, -1, 0);
  84. vDir = wm.ExtractMatrix3().VectorTransform(vDir).Normalize;
  85. babylonLight.direction = new float[] { vDir.X, vDir.Y, vDir.Z };
  86. }
  87. var maxScene = Loader.Core.RootNode;
  88. // Exclusion
  89. var inclusion = maxLight.ExclList.TestFlag(1); //NT_INCLUDE
  90. var checkExclusionList = maxLight.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
  91. if (checkExclusionList)
  92. {
  93. var excllist = new List<string>();
  94. var incllist = new List<string>();
  95. foreach (var meshNode in maxScene.NodesListBySuperClass(SClass_ID.Geomobject))
  96. {
  97. if (meshNode.CastShadows == 1)
  98. {
  99. var inList = maxLight.ExclList.FindNode(meshNode) != -1;
  100. if (inList)
  101. {
  102. if (inclusion)
  103. {
  104. incllist.Add(meshNode.GetGuid().ToString());
  105. }
  106. else
  107. {
  108. excllist.Add(meshNode.GetGuid().ToString());
  109. }
  110. }
  111. }
  112. }
  113. babylonLight.includedOnlyMeshesIds = incllist.ToArray();
  114. babylonLight.excludedMeshesIds = excllist.ToArray();
  115. }
  116. // Other fields
  117. babylonLight.intensity = maxLight.GetIntensity(0, Tools.Forever);
  118. babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  119. babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  120. if (maxLight.UseAtten)
  121. {
  122. babylonLight.range = maxLight.GetAtten(0, 1, Tools.Forever);
  123. }
  124. // Animations
  125. var animations = new List<BabylonAnimation>();
  126. ExportVector3Animation("position", animations, key =>
  127. {
  128. var mat = lightNode.GetObjectTM(key);
  129. var pos = mat.Translation;
  130. return new float[] { pos.X, pos.Y, pos.Z };
  131. });
  132. ExportVector3Animation("direction", animations, key =>
  133. {
  134. var wmLight = lightNode.GetObjectTM(key);
  135. var positionLight = wmLight.Translation;
  136. var lightTarget = gameLight.LightTarget;
  137. if (lightTarget != null)
  138. {
  139. var targetWm = lightTarget.GetObjectTM(key);
  140. var targetPosition = targetWm.Translation;
  141. var direction = targetPosition.Subtract(positionLight).Normalize;
  142. return new float[] { direction.X, direction.Y, direction.Z };
  143. }
  144. else
  145. {
  146. var vDir = Loader.Global.Point3.Create(0, -1, 0);
  147. vDir = wmLight.ExtractMatrix3().VectorTransform(vDir).Normalize;
  148. return new float[] { vDir.X, vDir.Y, vDir.Z };
  149. }
  150. });
  151. ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Tools.Forever) });
  152. babylonLight.animations = animations.ToArray();
  153. if (lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimate"))
  154. {
  155. babylonLight.autoAnimate = true;
  156. babylonLight.autoAnimateFrom = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  157. babylonLight.autoAnimateTo = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to");
  158. babylonLight.autoAnimateLoop = lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop");
  159. }
  160. babylonScene.LightsList.Add(babylonLight);
  161. }
  162. }
  163. }