BabylonExporter.Light.cs 8.8 KB

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