BabylonExporter.Light.cs 8.1 KB

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