BabylonExporter.Light.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 || lightState.Type == LightType.OmniLgt)
  65. {
  66. ExportShadowGenerator(lightNode.MaxNode, babylonScene);
  67. }
  68. else
  69. {
  70. RaiseWarning("Shadows maps are only supported for point, 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 MAX2017
  108. if (meshNode.CastShadows)
  109. #else
  110. if (meshNode.CastShadows == 1)
  111. #endif
  112. {
  113. var inList = maxLight.ExclList.FindNode(meshNode) != -1;
  114. if (inList)
  115. {
  116. if (inclusion)
  117. {
  118. incllist.Add(meshNode.GetGuid().ToString());
  119. }
  120. else
  121. {
  122. excllist.Add(meshNode.GetGuid().ToString());
  123. }
  124. }
  125. }
  126. }
  127. babylonLight.includedOnlyMeshesIds = incllist.ToArray();
  128. babylonLight.excludedMeshesIds = excllist.ToArray();
  129. }
  130. // Other fields
  131. babylonLight.intensity = maxLight.GetIntensity(0, Tools.Forever);
  132. babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  133. babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  134. if (maxLight.UseAtten)
  135. {
  136. babylonLight.range = maxLight.GetAtten(0, 3, Tools.Forever);
  137. }
  138. // Animations
  139. var animations = new List<BabylonAnimation>();
  140. ExportVector3Animation("position", animations, key =>
  141. {
  142. var mat = lightNode.GetObjectTM(key);
  143. if (lightNode.NodeParent != null)
  144. {
  145. var parentWorld = lightNode.NodeParent.GetObjectTM(key);
  146. mat.MultiplyBy(parentWorld.Inverse);
  147. }
  148. var pos = mat.Translation;
  149. return new[] { pos.X, pos.Y, pos.Z };
  150. });
  151. ExportVector3Animation("direction", animations, key =>
  152. {
  153. var wmLight = lightNode.GetObjectTM(key);
  154. if (lightNode.NodeParent != null)
  155. {
  156. var parentWorld = lightNode.NodeParent.GetObjectTM(key);
  157. wmLight.MultiplyBy(parentWorld.Inverse);
  158. }
  159. var positionLight = wmLight.Translation;
  160. var lightTarget = gameLight.LightTarget;
  161. if (lightTarget != null)
  162. {
  163. var targetWm = lightTarget.GetObjectTM(key);
  164. var targetPosition = targetWm.Translation;
  165. var direction = targetPosition.Subtract(positionLight).Normalize;
  166. return new[] { direction.X, direction.Y, direction.Z };
  167. }
  168. else
  169. {
  170. var vDir = Loader.Global.Point3.Create(0, -1, 0);
  171. vDir = wmLight.ExtractMatrix3().VectorTransform(vDir).Normalize;
  172. return new[] { vDir.X, vDir.Y, vDir.Z };
  173. }
  174. });
  175. ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Tools.Forever) });
  176. ExportColor3Animation("diffuse", animations, key =>
  177. {
  178. return lightState.AffectDiffuse? maxLight.GetRGBColor(key, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
  179. });
  180. babylonLight.animations = animations.ToArray();
  181. if (lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimate"))
  182. {
  183. babylonLight.autoAnimate = true;
  184. babylonLight.autoAnimateFrom = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  185. babylonLight.autoAnimateTo = (int)lightNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to");
  186. babylonLight.autoAnimateLoop = lightNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop");
  187. }
  188. babylonScene.LightsList.Add(babylonLight);
  189. }
  190. }
  191. }