BabylonExporter.Light.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 babylonLight = new BabylonLight();
  30. RaiseMessage(lightNode.Name, 1);
  31. babylonLight.name = lightNode.Name;
  32. babylonLight.id = lightNode.MaxNode.GetGuid().ToString();
  33. var initialized = gameLight.InitializeData;
  34. // Type
  35. var directionScale = -1;
  36. switch (gameLight.LightType)
  37. {
  38. case Autodesk.Max.IGameLight.LightType.Omni:
  39. babylonLight.type = 0;
  40. break;
  41. case Autodesk.Max.IGameLight.LightType.Fspot:
  42. case Autodesk.Max.IGameLight.LightType.Tspot:
  43. babylonLight.type = 2;
  44. float fallOff = 0;
  45. gameLight.LightFallOff.GetPropertyValue(ref fallOff, 0, true);
  46. babylonLight.angle = (float)(fallOff* Math.PI / 180.0f);
  47. babylonLight.exponent = 1;
  48. break;
  49. case Autodesk.Max.IGameLight.LightType.Dir:
  50. case Autodesk.Max.IGameLight.LightType.Tdir:
  51. babylonLight.type = 1;
  52. break;
  53. case Autodesk.Max.IGameLight.LightType.Unknown:
  54. babylonLight.type = 3;
  55. babylonLight.groundColor = new float[] { 0, 0, 0 };
  56. directionScale = 1;
  57. break;
  58. }
  59. // Shadows
  60. if (gameLight.CastShadows)
  61. {
  62. if (babylonLight.type == 1)
  63. {
  64. ExportShadowGenerator(lightNode.MaxNode, babylonScene);
  65. }
  66. else
  67. {
  68. RaiseWarning("Shadows maps are only supported for directional lights", 2);
  69. }
  70. }
  71. // Position
  72. var wm = lightNode.GetWorldTM(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.GetWorldTM(0);
  80. var targetPosition = targetWm.Translation;
  81. var direction = targetPosition.Subtract(position);
  82. babylonLight.direction = new float[] { direction.X, direction.Y, direction.Z };
  83. }
  84. else
  85. {
  86. IMatrix3 rotMatrix = Loader.Global.Matrix3.Create();
  87. wm.Rotation.MakeMatrix(rotMatrix, true);
  88. IPoint3 p = Loader.Global.Point3.Create(1, 0, 0);
  89. var dir = rotMatrix.VectorTransform(p);
  90. babylonLight.direction = new float[] { dir.X, dir.Y, dir.Z };
  91. }
  92. var maxLight = (lightNode.MaxNode.ObjectRef as ILightObject);
  93. var maxScene = Loader.Core.RootNode;
  94. // Exclusion
  95. var inclusion = maxLight.ExclList.TestFlag(1); //NT_INCLUDE
  96. var checkExclusionList = maxLight.ExclList.TestFlag(2); //NT_AFFECT_ILLUM
  97. if (checkExclusionList)
  98. {
  99. var excllist = new List<string>();
  100. var incllist = new List<string>();
  101. foreach (var meshNode in maxScene.NodesListBySuperClass(SClass_ID.Geomobject))
  102. {
  103. if (meshNode.CastShadows == 1)
  104. {
  105. var inList = maxLight.ExclList.FindNode(meshNode) != -1;
  106. if (inList)
  107. {
  108. if (inclusion)
  109. {
  110. incllist.Add(meshNode.GetGuid().ToString());
  111. }
  112. else
  113. {
  114. excllist.Add(meshNode.GetGuid().ToString());
  115. }
  116. }
  117. }
  118. }
  119. babylonLight.includedOnlyMeshesIds = incllist.ToArray();
  120. babylonLight.excludedMeshesIds = excllist.ToArray();
  121. }
  122. // Other fields
  123. babylonLight.intensity = maxLight.GetIntensity(0, Tools.Forever);
  124. IPoint3 lightColor = Loader.Global.Point3.Create(0,0,0);
  125. gameLight.LightColor.GetPropertyValue(lightColor, 0);
  126. babylonLight.diffuse = new float[] { lightColor.X, lightColor.Y, lightColor.Z };
  127. babylonLight.specular = new float[] { lightColor.X, lightColor.Y, lightColor.Z };
  128. if (maxLight.UseAtten)
  129. {
  130. babylonLight.range = maxLight.GetAtten(0, 1, Tools.Forever);
  131. }
  132. //// Animations
  133. //var animations = new List<BabylonAnimation>();
  134. //if (!ExportVector3Controller(lightNode.TMController.PositionController, "position", animations))
  135. //{
  136. // ExportVector3Animation("position", animations, key =>
  137. // {
  138. // var worldMatrix = lightNode.GetWorldMatrix(key, lightNode.HasParent());
  139. // return worldMatrix.Trans.ToArraySwitched();
  140. // });
  141. //}
  142. //ExportVector3Animation("direction", animations, key =>
  143. //{
  144. // var targetNode = lightNode.Target;
  145. // if (targetNode != null)
  146. // {
  147. // var targetWm = target.GetObjTMBeforeWSM(0, Tools.Forever);
  148. // var targetPosition = targetWm.Trans;
  149. // var direction = targetPosition.Subtract(position);
  150. // return direction.ToArraySwitched();
  151. // }
  152. // var dir = wm.GetRow(2).MultiplyBy(directionScale);
  153. // return dir.ToArraySwitched();
  154. //});
  155. //ExportFloatAnimation("intensity", animations, key => new[] { maxLight.GetIntensity(key, Tools.Forever) });
  156. //babylonLight.animations = animations.ToArray();
  157. //if (lightNode.GetBoolProperty("babylonjs_autoanimate"))
  158. //{
  159. // babylonLight.autoAnimate = true;
  160. // babylonLight.autoAnimateFrom = (int)lightNode.GetFloatProperty("babylonjs_autoanimate_from");
  161. // babylonLight.autoAnimateTo = (int)lightNode.GetFloatProperty("babylonjs_autoanimate_to");
  162. // babylonLight.autoAnimateLoop = lightNode.GetBoolProperty("babylonjs_autoanimateloop");
  163. //}
  164. babylonScene.LightsList.Add(babylonLight);
  165. }
  166. }
  167. }