BabylonExporter.Light.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. var directionScale = -1;
  39. switch (lightState.Type)
  40. {
  41. case LightType.OmniLgt:
  42. babylonLight.type = 0;
  43. break;
  44. case LightType.SpotLgt:
  45. babylonLight.type = 2;
  46. babylonLight.angle = (float)(maxLight.GetFallsize(0, Tools.Forever) * Math.PI / 180.0f);
  47. babylonLight.exponent = 1;
  48. break;
  49. case LightType.DirectLgt:
  50. babylonLight.type = 1;
  51. break;
  52. case LightType.AmbientLgt:
  53. babylonLight.type = 3;
  54. babylonLight.groundColor = new float[] { 0, 0, 0 };
  55. directionScale = 1;
  56. break;
  57. }
  58. // Shadows
  59. if (maxLight.ShadowMethod == 1)
  60. {
  61. if (lightState.Type == LightType.DirectLgt)
  62. {
  63. ExportShadowGenerator(lightNode.MaxNode, babylonScene);
  64. }
  65. else
  66. {
  67. RaiseWarning("Shadows maps are only supported for directional lights", 2);
  68. }
  69. }
  70. {
  71. // Position
  72. var wm = lightNode.GetObjectTM(0);
  73. var position = wm.Translation;
  74. babylonLight.position = new float[] { position.X, position.Y, position.Z };
  75. // Direction
  76. var target = gameLight.LightTarget;
  77. if (target != null)
  78. {
  79. var targetWm = target.GetObjectTM(0);
  80. var targetPosition = targetWm.Translation;
  81. var direction = targetPosition.Subtract(position).Normalize;
  82. babylonLight.direction = new float[] { direction.X, direction.Y, direction.Z };
  83. }
  84. else
  85. {
  86. var vDir = Loader.Global.Point3.Create(0, -1, 0);
  87. vDir = wm.ExtractMatrix3().VectorTransform(vDir).Normalize;
  88. babylonLight.direction = new float[] { vDir.X, vDir.Y, vDir.Z };
  89. }
  90. }
  91. var maxScene = Loader.Core.RootNode;
  92. // Exclusion
  93. var inclusion = maxLight.ExclList.TestFlag(1); //NT_INCLUDE
  94. var checkExclusionList = maxLight.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
  95. if (checkExclusionList)
  96. {
  97. var excllist = new List<string>();
  98. var incllist = new List<string>();
  99. foreach (var meshNode in maxScene.NodesListBySuperClass(SClass_ID.Geomobject))
  100. {
  101. if (meshNode.CastShadows == 1)
  102. {
  103. var inList = maxLight.ExclList.FindNode(meshNode) != -1;
  104. if (inList)
  105. {
  106. if (inclusion)
  107. {
  108. incllist.Add(meshNode.GetGuid().ToString());
  109. }
  110. else
  111. {
  112. excllist.Add(meshNode.GetGuid().ToString());
  113. }
  114. }
  115. }
  116. }
  117. babylonLight.includedOnlyMeshesIds = incllist.ToArray();
  118. babylonLight.excludedMeshesIds = excllist.ToArray();
  119. }
  120. // Other fields
  121. babylonLight.intensity = maxLight.GetIntensity(0, Tools.Forever);
  122. babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  123. babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  124. if (maxLight.UseAtten)
  125. {
  126. babylonLight.range = maxLight.GetAtten(0, 1, Tools.Forever);
  127. }
  128. // Animations
  129. var animations = new List<BabylonAnimation>();
  130. //if (!ExportVector3Controller(lightNode.TMController.PositionController, "position", animations))
  131. //{
  132. ExportVector3Animation("position", animations, key =>
  133. {
  134. var mat = lightNode.GetObjectTM(key);
  135. var pos = mat.Translation;
  136. return new float[] { pos.X, pos.Y, pos.Z };
  137. });
  138. //}
  139. ExportVector3Animation("direction", animations, key =>
  140. {
  141. var wm = lightNode.GetObjectTM(key);
  142. var position = wm.Translation;
  143. var target = gameLight.LightTarget;
  144. if (target != null)
  145. {
  146. var targetWm = target.GetObjectTM(key);
  147. var targetPosition = targetWm.Translation;
  148. var direction = targetPosition.Subtract(position).Normalize;
  149. return new float[] { direction.X, direction.Y, direction.Z };
  150. }
  151. else
  152. {
  153. var vDir = Loader.Global.Point3.Create(0, -1, 0);
  154. vDir = wm.ExtractMatrix3().VectorTransform(vDir).Normalize;
  155. return new float[] { vDir.X, vDir.Y, vDir.Z };
  156. }
  157. });
  158. ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Tools.Forever) });
  159. babylonLight.animations = animations.ToArray();
  160. if (lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimate"))
  161. {
  162. babylonLight.autoAnimate = true;
  163. babylonLight.autoAnimateFrom = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  164. babylonLight.autoAnimateTo = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to");
  165. babylonLight.autoAnimateLoop = lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop");
  166. }
  167. babylonScene.LightsList.Add(babylonLight);
  168. }
  169. }
  170. }