BabylonExporter.Light.cs 9.0 KB

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