ExporterWindow.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using JsonFx.Json;
  9. using JsonFx.Json.Resolvers;
  10. using JsonFx.Serialization;
  11. using JsonFx.Serialization.Resolvers;
  12. using BabylonHosting;
  13. namespace Unity3D2Babylon
  14. {
  15. public class ExporterWindow : EditorWindow
  16. {
  17. public const double BabylonVersion = 2.5;
  18. public const string ToolkitVersion = "1.0";
  19. public const string DefaultHost = "localhost";
  20. public const int DefaultPort = 8888;
  21. public static ExportationOptions exportationOptions = null;
  22. public static readonly List<string> logs = new List<string>();
  23. Vector2 scrollPosMain;
  24. bool showLighting = false;
  25. bool showCollision = false;
  26. bool showShader = false;
  27. bool showPreview = false;
  28. EditorWindow assetStore = null;
  29. public static void ReportProgress(float value, string message = "")
  30. {
  31. EditorUtility.DisplayProgressBar("Babylon.js", message, value);
  32. if (!string.IsNullOrEmpty(message))
  33. {
  34. logs.Add(message);
  35. }
  36. }
  37. public static bool ShowMessage(string message, string title = "Babylon.js", string ok = "OK", string cancel = "Cancel")
  38. {
  39. return EditorUtility.DisplayDialog(title, message, ok, cancel);
  40. }
  41. public ExportationOptions CreateSettings()
  42. {
  43. ExportationOptions result = new ExportationOptions();
  44. string apath = Application.dataPath.Replace("/Assets", "");
  45. string ufile = Path.Combine(apath, "UnityBabylonOptions.ini");
  46. if (File.Exists(ufile))
  47. {
  48. var readText = File.ReadAllText(ufile);
  49. var jsReader = new JsonReader();
  50. result = jsReader.Read<ExportationOptions>(readText);
  51. }
  52. return result;
  53. }
  54. public void SaveSettings(bool refresh = false)
  55. {
  56. if (refresh) GetSceneInfomation(false);
  57. string apath = Application.dataPath.Replace("/Assets", "");
  58. string ufile = Path.Combine(apath, "UnityBabylonOptions.ini");
  59. var settings = new DataWriterSettings() { PrettyPrint = true };
  60. var jsWriter = new JsonWriter(settings);
  61. File.WriteAllText(ufile, jsWriter.Write(exportationOptions));
  62. }
  63. [MenuItem("BabylonJS/Scene Exporter", false, 0)]
  64. public static void InitExporter()
  65. {
  66. var exporter = (ExporterWindow)GetWindow(typeof(ExporterWindow));
  67. exporter.minSize = new Vector2(420.0f, 480.0f);
  68. exporter.OnInitialize();
  69. }
  70. [MenuItem("BabylonJS/Output Window", false, 1)]
  71. public static void InitOutput()
  72. {
  73. var output = (ExporterOutput)GetWindow(typeof(ExporterOutput));
  74. output.OnInitialize();
  75. }
  76. [MenuItem("BabylonJS/Update Libraries", false, 2)]
  77. public static void InitUpdate()
  78. {
  79. string prodVersion = ExporterWindow.exportationOptions.ProductionVersion.ToString();
  80. if (prodVersion.IndexOf(".", StringComparison.OrdinalIgnoreCase) < 0)
  81. {
  82. prodVersion += ".0";
  83. }
  84. string updateMsg = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "Are you sure you want to update libraries using the github preview release version?" : "Are you sure you want to update libraries using the github stable release version " + prodVersion + "?";
  85. if (ExporterWindow.ShowMessage(updateMsg, "Babylon.js", "Update"))
  86. {
  87. EditorUtility.DisplayProgressBar("Babylon.js", "Updating github editor toolkit library files...", 1);
  88. string libPath = Path.Combine(Application.dataPath, "Babylon/Library/");
  89. string bjsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/babylon.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".js";
  90. string bjsTsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/babylon.d.ts" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".d.ts";
  91. string c2dPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/canvas2D/babylon.canvas2d.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".canvas2d.js";
  92. string c2dTsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/canvas2D/babylon.canvas2d.d.ts" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".canvas2d.d.ts";
  93. string cannonPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/cannon.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/cannon.js";
  94. string oimoPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/Oimo.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/Oimo.js";
  95. try
  96. {
  97. EditorUtility.DisplayProgressBar("Babylon.js", "Updating babylon.bjs...", 0.10f);
  98. Tools.DownloadFile(bjsPath, Path.Combine(libPath, "babylon.bjs"));
  99. EditorUtility.DisplayProgressBar("Babylon.js", "Updating babylon.d.ts...", 0.20f);
  100. Tools.DownloadFile(bjsTsPath, Path.Combine(libPath, "babylon.d.ts"));
  101. EditorUtility.DisplayProgressBar("Babylon.js", "Updating canvas2d.bjs...", 0.30f);
  102. Tools.DownloadFile(c2dPath, Path.Combine(libPath, "canvas2d.bjs"));
  103. EditorUtility.DisplayProgressBar("Babylon.js", "Updating canvas2d.d.ts...", 0.40f);
  104. Tools.DownloadFile(c2dTsPath, Path.Combine(libPath, "canvas2d.d.ts"));
  105. EditorUtility.DisplayProgressBar("Babylon.js", "Updating cannon.bjs...", 0.50f);
  106. Tools.DownloadFile(cannonPath, Path.Combine(libPath, "cannon.bjs"));
  107. EditorUtility.DisplayProgressBar("Babylon.js", "Updating oimo.bjs...", 0.60f);
  108. Tools.DownloadFile(oimoPath, Path.Combine(libPath, "oimo.bjs"));
  109. EditorUtility.DisplayProgressBar("Babylon.js", "Updating navmesh.bjs...", 0.70f);
  110. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.navigation.mesh.js", Path.Combine(libPath, "navmesh.bjs"));
  111. EditorUtility.DisplayProgressBar("Babylon.js", "Updating manager.bjs...", 0.80f);
  112. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.scenemanager.js", Path.Combine(libPath, "manager.bjs"));
  113. EditorUtility.DisplayProgressBar("Babylon.js", "Updating manager.d.ts...", 0.90f);
  114. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.scenemanager.d.ts", Path.Combine(libPath, "manager.d.ts"));
  115. }
  116. catch (System.Exception ex)
  117. {
  118. UnityEngine.Debug.LogException(ex);
  119. }
  120. finally
  121. {
  122. EditorUtility.DisplayProgressBar("Babylon.js", "Refresing assets database...", 1.0f);
  123. AssetDatabase.Refresh();
  124. EditorUtility.ClearProgressBar();
  125. }
  126. }
  127. }
  128. [MenuItem("BabylonJS/Babylon Dashboard", false, 999)]
  129. public static void InitDashboard()
  130. {
  131. Application.OpenURL("http://www.babylonjs.com");
  132. }
  133. [MenuItem("Assets/Create/BabylonJS/Javascript Class", false, 101)]
  134. public static void CreateJavascript()
  135. {
  136. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  137. if (String.IsNullOrEmpty(path))
  138. {
  139. path = "Assets";
  140. }
  141. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  142. {
  143. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  144. }
  145. string filename = Path.Combine(path, "NewJavascript.bjs");
  146. string template = "Assets/Babylon/Templates/Scripts/javascript.template";
  147. if (!File.Exists(template))
  148. {
  149. string defaultTemplate = "// Babylon Javascript File";
  150. File.WriteAllText(template, defaultTemplate);
  151. }
  152. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  153. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  154. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  155. }
  156. [MenuItem("Assets/Create/BabylonJS/Typescript Class", false, 102)]
  157. public static void CreateTypescript()
  158. {
  159. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  160. if (String.IsNullOrEmpty(path))
  161. {
  162. path = "Assets";
  163. }
  164. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  165. {
  166. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  167. }
  168. string filename = Path.Combine(path, "NewTypescript.ts");
  169. string template = "Assets/Babylon/Templates/Scripts/typescript.template";
  170. if (!File.Exists(template))
  171. {
  172. string defaultTemplate = "// Babylon Typescript File";
  173. File.WriteAllText(template, defaultTemplate);
  174. }
  175. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  176. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  177. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  178. }
  179. [MenuItem("Assets/Create/BabylonJS/Scene Controller", false, 201)]
  180. public static void CreateSceneController_TS()
  181. {
  182. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  183. if (String.IsNullOrEmpty(path))
  184. {
  185. path = "Assets";
  186. }
  187. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  188. {
  189. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  190. }
  191. string filename = Path.Combine(path, "NewSceneController.ts");
  192. string template = "Assets/Babylon/Templates/Scripts/controller.template";
  193. if (!File.Exists(template))
  194. {
  195. string defaultTemplate = "// Babylon Scene Controller";
  196. File.WriteAllText(template, defaultTemplate);
  197. }
  198. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  199. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  200. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  201. }
  202. [MenuItem("Assets/Create/BabylonJS/Mesh Component", false, 301)]
  203. public static void CreateMeshComponent_TS()
  204. {
  205. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  206. if (String.IsNullOrEmpty(path))
  207. {
  208. path = "Assets";
  209. }
  210. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  211. {
  212. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  213. }
  214. string filename = Path.Combine(path, "NewMeshComponent.ts");
  215. string template = "Assets/Babylon/Templates/Scripts/mesh.template";
  216. if (!File.Exists(template))
  217. {
  218. string defaultTemplate = "// Babylon Mesh Class";
  219. File.WriteAllText(template, defaultTemplate);
  220. }
  221. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  222. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  223. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  224. }
  225. [MenuItem("Assets/Create/BabylonJS/Light Component", false, 302)]
  226. public static void CreateLightComponent_TS()
  227. {
  228. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  229. if (String.IsNullOrEmpty(path))
  230. {
  231. path = "Assets";
  232. }
  233. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  234. {
  235. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  236. }
  237. string filename = Path.Combine(path, "NewLightComponent.ts");
  238. string template = "Assets/Babylon/Templates/Scripts/light.template";
  239. if (!File.Exists(template))
  240. {
  241. string defaultTemplate = "// Babylon Light Class";
  242. File.WriteAllText(template, defaultTemplate);
  243. }
  244. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  245. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  246. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  247. }
  248. [MenuItem("Assets/Create/BabylonJS/Camera Component", false, 303)]
  249. public static void CreateCameraComponent_TS()
  250. {
  251. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  252. if (String.IsNullOrEmpty(path))
  253. {
  254. path = "Assets";
  255. }
  256. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  257. {
  258. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  259. }
  260. string filename = Path.Combine(path, "NewCameraComponent.ts");
  261. string template = "Assets/Babylon/Templates/Scripts/camera.template";
  262. if (!File.Exists(template))
  263. {
  264. string defaultTemplate = "// Babylon Camera Class";
  265. File.WriteAllText(template, defaultTemplate);
  266. }
  267. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  268. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  269. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  270. }
  271. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Babylon Scene Controller (C#)", false, 901)]
  272. public static void CreateEditorController_CS()
  273. {
  274. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  275. if (String.IsNullOrEmpty(path))
  276. {
  277. path = "Assets";
  278. }
  279. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  280. {
  281. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  282. }
  283. string filename = Path.Combine(path, "NewSceneController.cs");
  284. string template = "Assets/Babylon/Templates/Scripts/editora.template";
  285. if (!File.Exists(template))
  286. {
  287. string defaultTemplate = "// Babylon Controller Class";
  288. File.WriteAllText(template, defaultTemplate);
  289. }
  290. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  291. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  292. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  293. }
  294. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Babylon Script Component (C#)", false, 902)]
  295. public static void CreateEditorComponent_CS()
  296. {
  297. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  298. if (String.IsNullOrEmpty(path))
  299. {
  300. path = "Assets";
  301. }
  302. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  303. {
  304. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  305. }
  306. string filename = Path.Combine(path, "NewScriptComponent.cs");
  307. string template = "Assets/Babylon/Templates/Scripts/editorb.template";
  308. if (!File.Exists(template))
  309. {
  310. string defaultTemplate = "// Babylon Script Class";
  311. File.WriteAllText(template, defaultTemplate);
  312. }
  313. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  314. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  315. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  316. }
  317. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Universal Shader Program (GLSL)", false, 999)]
  318. public static void CreateShader()
  319. {
  320. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  321. if (String.IsNullOrEmpty(path))
  322. {
  323. path = "Assets";
  324. }
  325. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  326. {
  327. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  328. }
  329. string filename = Path.Combine(path, "NewShaderProgram.shader");
  330. string template = "Assets/Babylon/Templates/Shaders/amiga.template";
  331. if (!File.Exists(template))
  332. {
  333. string defaultTemplate = "// Babylon Shader Class";
  334. File.WriteAllText(template, defaultTemplate);
  335. }
  336. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  337. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  338. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  339. }
  340. public void OnInitialize() { }
  341. public void OnEnable()
  342. {
  343. this.titleContent = new GUIContent("Exporter");
  344. if (ExporterWindow.exportationOptions == null)
  345. {
  346. ExporterWindow.exportationOptions = CreateSettings();
  347. }
  348. // Attach unity editor buttons
  349. UnityEditor.EditorApplication.playmodeStateChanged = () =>
  350. {
  351. if (exportationOptions != null && exportationOptions.AttachUnityEditor)
  352. {
  353. bool wantsToPlay = UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode;
  354. bool wantsToPause = UnityEditor.EditorApplication.isPaused;
  355. if (wantsToPlay || wantsToPause)
  356. {
  357. UnityEditor.EditorApplication.isPlaying = false;
  358. UnityEditor.EditorApplication.isPaused = false;
  359. }
  360. if (wantsToPlay) Execute();
  361. }
  362. };
  363. // Activate asset store window
  364. if (this.assetStore == null)
  365. {
  366. this.assetStore = Tools.GetAssetStoreWindow();
  367. }
  368. // Activate internl web server
  369. this.StartServer();
  370. }
  371. public void StartServer()
  372. {
  373. if (exportationOptions.HostPreviewPage)
  374. {
  375. // Validate default project folder selected
  376. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  377. {
  378. UnityEngine.Debug.LogWarning("No default project file selected. Web server not started.");
  379. return;
  380. }
  381. // Validate default project folder exists
  382. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  383. {
  384. UnityEngine.Debug.LogWarning("No default project file created. Web server not started.");
  385. return;
  386. }
  387. bool supported = WebServer.IsSupported;
  388. string prefix = "http://*:";
  389. string root = exportationOptions.DefaultProjectFolder;
  390. int port = exportationOptions.DefaultServerPort;
  391. bool started = WebServer.Activate(prefix, root, port);
  392. if (started) UnityEngine.Debug.Log("Babylon.js web server started on port: " + port.ToString());
  393. if (!started && supported) UnityEngine.Debug.LogWarning("Babylon.js web server is available.");
  394. if (!started && !supported) UnityEngine.Debug.LogWarning("Babylon.js web server is not available.");
  395. }
  396. }
  397. public void OnGUI()
  398. {
  399. GUILayout.Label("BabylonJS Toolkit - Version: " + ExporterWindow.ToolkitVersion, EditorStyles.boldLabel);
  400. EditorGUI.BeginDisabledGroup(true);
  401. exportationOptions.DefaultProjectFolder = EditorGUILayout.TextField("", exportationOptions.DefaultProjectFolder);
  402. EditorGUI.EndDisabledGroup();
  403. if (GUILayout.Button("Select Project Folder"))
  404. {
  405. SelectFolder();
  406. }
  407. if (GUILayout.Button("Save Export Settings"))
  408. {
  409. SaveSettings();
  410. ShowMessage("Export settings saved.");
  411. }
  412. scrollPosMain = EditorGUILayout.BeginScrollView(scrollPosMain, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
  413. EditorGUILayout.Space();
  414. exportationOptions.DefaultBuildPath = EditorGUILayout.TextField(" Project Build Path", exportationOptions.DefaultBuildPath);
  415. EditorGUILayout.Space();
  416. exportationOptions.DefaultScenePath = EditorGUILayout.TextField(" Project Scene Path", exportationOptions.DefaultScenePath);
  417. EditorGUILayout.Space();
  418. exportationOptions.DefaultScriptPath = EditorGUILayout.TextField(" Project Script Path", exportationOptions.DefaultScriptPath);
  419. EditorGUILayout.Space();
  420. exportationOptions.DefaultIndexPage = EditorGUILayout.TextField(" Project Index Page", exportationOptions.DefaultIndexPage);
  421. EditorGUILayout.Space();
  422. EditorGUILayout.BeginHorizontal();
  423. exportationOptions.ExportPhysics = EditorGUILayout.Toggle(" Enable Physics Engine", exportationOptions.ExportPhysics);
  424. exportationOptions.DefaultPhysicsEngine = (int)(BabylonPhysicsEngine)EditorGUILayout.EnumPopup("Default Physics Engine", (BabylonPhysicsEngine)exportationOptions.DefaultPhysicsEngine, GUILayout.ExpandWidth(true));
  425. EditorGUILayout.EndHorizontal();
  426. EditorGUILayout.Space();
  427. EditorGUILayout.BeginHorizontal();
  428. GUILayout.Label(" Light Rotation Offset");
  429. exportationOptions.LightRotationOffset = EditorGUILayout.Vector3Field("", exportationOptions.LightRotationOffset, GUILayout.ExpandWidth(false));
  430. EditorGUILayout.EndHorizontal();
  431. EditorGUILayout.Space();
  432. exportationOptions.LightIntensityFactor = EditorGUILayout.Slider(" Light Intensity Factor", exportationOptions.LightIntensityFactor, 0, 10.0f);
  433. EditorGUILayout.Space();
  434. exportationOptions.ReflectionDefaultLevel = EditorGUILayout.Slider(" Default Reflection Level", exportationOptions.ReflectionDefaultLevel, 0, 1.0f);
  435. EditorGUILayout.Space();
  436. exportationOptions.DefaultImageFormat = (int)(BabylonImageFormat)EditorGUILayout.EnumPopup(" Prefered Texture Format", (BabylonImageFormat)exportationOptions.DefaultImageFormat, GUILayout.ExpandWidth(true));
  437. EditorGUILayout.Space();
  438. exportationOptions.DefaultQualityLevel = (int)EditorGUILayout.Slider(" Texture Image Quality", exportationOptions.DefaultQualityLevel, 0, 100);
  439. EditorGUILayout.Space();
  440. showCollision = EditorGUILayout.Foldout(showCollision, "Scene Collision Options");
  441. if (showCollision)
  442. {
  443. EditorGUILayout.Space();
  444. exportationOptions.ExportCollisions = EditorGUILayout.Toggle(" Enable Collisions", exportationOptions.ExportCollisions);
  445. EditorGUILayout.Space();
  446. EditorGUILayout.BeginHorizontal();
  447. GUILayout.Label(" Camera Ellipsoid");
  448. exportationOptions.CameraEllipsoid = EditorGUILayout.Vector3Field("", exportationOptions.CameraEllipsoid, GUILayout.ExpandWidth(false));
  449. EditorGUILayout.EndHorizontal();
  450. EditorGUILayout.Space();
  451. EditorGUILayout.BeginHorizontal();
  452. GUILayout.Label(" Default Scene Gravity");
  453. exportationOptions.Gravity = EditorGUILayout.Vector3Field("", exportationOptions.Gravity, GUILayout.ExpandWidth(false));
  454. EditorGUILayout.EndHorizontal();
  455. EditorGUILayout.Space();
  456. exportationOptions.DefaultColliderDetail = (int)(BabylonColliderDetail)EditorGUILayout.EnumPopup(" Default Collider Detail", (BabylonColliderDetail)exportationOptions.DefaultColliderDetail, GUILayout.ExpandWidth(true));
  457. EditorGUILayout.Space();
  458. exportationOptions.WorkerCollisions = EditorGUILayout.Toggle(" Enable Worker Collisions", exportationOptions.WorkerCollisions);
  459. EditorGUILayout.Space();
  460. }
  461. showShader = EditorGUILayout.Foldout(showShader, "Shader Program Options");
  462. if (showShader)
  463. {
  464. EditorGUILayout.Space();
  465. exportationOptions.EmbeddedShaders = EditorGUILayout.Toggle(" Embed Shader Files", exportationOptions.EmbeddedShaders);
  466. EditorGUILayout.Space();
  467. exportationOptions.DefaultShaderFolder = EditorGUILayout.TextField(" Output Src Shader Path", exportationOptions.DefaultShaderFolder);
  468. EditorGUILayout.Space();
  469. }
  470. showLighting = EditorGUILayout.Foldout(showLighting, "Lightmap Baking Options");
  471. if (showLighting)
  472. {
  473. EditorGUILayout.Space();
  474. EditorGUILayout.BeginHorizontal();
  475. exportationOptions.ExportLightmaps = EditorGUILayout.Toggle(" Export Lightmaps", exportationOptions.ExportLightmaps);
  476. exportationOptions.DefaultLightmapBaking = (int)(BabylonLightmapBaking)EditorGUILayout.EnumPopup(" Synchronous Baking", (BabylonLightmapBaking)exportationOptions.DefaultLightmapBaking, GUILayout.ExpandWidth(true));
  477. EditorGUILayout.EndHorizontal();
  478. EditorGUILayout.Space();
  479. exportationOptions.DefaultLightmapMode = (int)(BabylonLightmapMode)EditorGUILayout.EnumPopup(" Light Mapper Mode", (BabylonLightmapMode)exportationOptions.DefaultLightmapMode, GUILayout.ExpandWidth(true));
  480. EditorGUILayout.Space();
  481. exportationOptions.DefaultCoordinatesIndex = (int)EditorGUILayout.Slider(" Coordinates Index", exportationOptions.DefaultCoordinatesIndex, 0, 1);
  482. EditorGUILayout.Space();
  483. EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  484. exportationOptions.ExportShadows = EditorGUILayout.Toggle(" Enable Shadow Map", exportationOptions.ExportShadows);
  485. GUILayout.Label(" Default Shadow Map Size");
  486. exportationOptions.ShadowMapSize = EditorGUILayout.IntField("", exportationOptions.ShadowMapSize, GUILayout.Width(50));
  487. EditorGUILayout.EndHorizontal();
  488. EditorGUILayout.Space();
  489. exportationOptions.DefaultLightFilter = (int)(BabylonLightingFilter)EditorGUILayout.EnumPopup(" Shadow Map Filter", (BabylonLightingFilter)exportationOptions.DefaultLightFilter, GUILayout.ExpandWidth(true));
  490. EditorGUILayout.Space();
  491. exportationOptions.ShadowMapBias = EditorGUILayout.Slider(" Shadow Map Bias", exportationOptions.ShadowMapBias, 0, 1.0f);
  492. EditorGUILayout.Space();
  493. exportationOptions.ShadowBlurScale = EditorGUILayout.Slider(" Shadow Blur Scale", exportationOptions.ShadowBlurScale, 0, 5.0f);
  494. EditorGUILayout.Space();
  495. }
  496. showPreview = EditorGUILayout.Foldout(showPreview, "Default Exporting Options");
  497. if (showPreview)
  498. {
  499. EditorGUILayout.Space();
  500. exportationOptions.AttachUnityEditor = EditorGUILayout.Toggle(" Attach Unity Editor", exportationOptions.AttachUnityEditor);
  501. EditorGUILayout.Space();
  502. exportationOptions.HostPreviewPage = EditorGUILayout.Toggle(" Host Preview Server", exportationOptions.HostPreviewPage);
  503. EditorGUILayout.Space();
  504. exportationOptions.ShowDebugControls = EditorGUILayout.Toggle(" Show Debug Controls", exportationOptions.ShowDebugControls);
  505. EditorGUILayout.Space();
  506. exportationOptions.DefaultServerPort = EditorGUILayout.IntField(" Default Server Port", exportationOptions.DefaultServerPort);
  507. EditorGUILayout.Space();
  508. exportationOptions.DefaultPreviewWindow = (int)(BabylonPreviewWindow)EditorGUILayout.EnumPopup(" Default Preview Window", (BabylonPreviewWindow)exportationOptions.DefaultPreviewWindow, GUILayout.ExpandWidth(true));
  509. EditorGUILayout.Space();
  510. exportationOptions.BuildJavaScript = EditorGUILayout.Toggle(" Build Javascript Files", exportationOptions.BuildJavaScript);
  511. EditorGUILayout.Space();
  512. exportationOptions.CompileTypeScript = EditorGUILayout.Toggle(" Build Typescript Files", exportationOptions.CompileTypeScript);
  513. EditorGUILayout.Space();
  514. exportationOptions.DefaultTypeSriptPath = EditorGUILayout.TextField(" Typescript Compiler", exportationOptions.DefaultTypeSriptPath);
  515. EditorGUILayout.Space();
  516. exportationOptions.DefaultNodeRuntimePath = EditorGUILayout.TextField(" Node Runtime System", exportationOptions.DefaultNodeRuntimePath);
  517. EditorGUILayout.Space();
  518. exportationOptions.ProductionVersion = EditorGUILayout.FloatField(" Stable Babylon Version", exportationOptions.ProductionVersion);
  519. EditorGUILayout.Space();
  520. exportationOptions.DefaultUpdateOptions = (int)(BabylonUpdateOptions)EditorGUILayout.EnumPopup(" Github Update Version", (BabylonUpdateOptions)exportationOptions.DefaultUpdateOptions, GUILayout.ExpandWidth(true));
  521. EditorGUILayout.Space();
  522. }
  523. EditorGUILayout.EndScrollView();
  524. EditorGUILayout.Space();
  525. // Exporter buttons
  526. if (GUILayout.Button("Build Script"))
  527. {
  528. if (ExporterWindow.ShowMessage("Are you sure you want to build the project script files?", "Babylon.js", "Build"))
  529. {
  530. Build(true);
  531. }
  532. }
  533. if (GUILayout.Button("Export Scene"))
  534. {
  535. Export(false);
  536. }
  537. if (GUILayout.Button("Export & Preview"))
  538. {
  539. Export(true);
  540. }
  541. if (ExporterWindow.exportationOptions.HostPreviewPage == true)
  542. {
  543. if (GUILayout.Button("Start Preview Server"))
  544. {
  545. StartServer();
  546. }
  547. }
  548. if (GUILayout.Button("Launch Preview Window"))
  549. {
  550. Execute();
  551. }
  552. EditorGUILayout.Space();
  553. }
  554. public void OnInspectorUpdate()
  555. {
  556. this.Repaint();
  557. }
  558. public void SelectFolder()
  559. {
  560. exportationOptions.DefaultProjectFolder = EditorUtility.SaveFolderPanel("Please select a folder", exportationOptions.DefaultProjectFolder, "");
  561. }
  562. public void Build(bool solo, string[] info = null)
  563. {
  564. if (solo)
  565. {
  566. // Validate default project folder selected
  567. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  568. {
  569. ShowMessage("No default project file selected.");
  570. return;
  571. }
  572. // Validate default project folder exists
  573. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  574. {
  575. ShowMessage("No default project file created.");
  576. return;
  577. }
  578. }
  579. try
  580. {
  581. var sceneInfo = info ?? GetSceneInfomation(false);
  582. string scriptPath = sceneInfo[2];
  583. string projectScript = sceneInfo[4];
  584. // Save and clear console
  585. if (solo)
  586. {
  587. try { UnityEngine.Debug.ClearDeveloperConsole(); } catch { }
  588. SaveSettings();
  589. }
  590. // Assemble javascript files
  591. string javascriptFile = Tools.FormatProjectJavaScript(scriptPath, projectScript);
  592. if (solo || exportationOptions.BuildJavaScript)
  593. {
  594. ReportProgress(1, "Building project javascript files...");
  595. Tools.BuildProjectJavaScript(scriptPath, projectScript, javascriptFile);
  596. }
  597. // Compile typescript files
  598. if (solo || exportationOptions.CompileTypeScript)
  599. {
  600. ReportProgress(1, "Compiling project typescript files... This may take a while.");
  601. string config = String.Empty;
  602. string options = Path.Combine(Application.dataPath, "Babylon/Templates/Config/options.json");
  603. if (File.Exists(options)) config = File.ReadAllText(options);
  604. Tools.BuildProjectTypeScript(exportationOptions.DefaultNodeRuntimePath, exportationOptions.DefaultTypeSriptPath, scriptPath, javascriptFile, config);
  605. }
  606. }
  607. catch (Exception ex)
  608. {
  609. UnityEngine.Debug.LogException(ex);
  610. }
  611. finally
  612. {
  613. if (solo) EditorUtility.ClearProgressBar();
  614. }
  615. }
  616. public void Export(bool preview)
  617. {
  618. try
  619. {
  620. // Validate lightmap bake in progress
  621. if (exportationOptions.ExportLightmaps && exportationOptions.DefaultLightmapBaking == (int)BabylonLightmapBaking.Enabled && Lightmapping.isRunning)
  622. {
  623. ShowMessage("There is a bake already in progress.");
  624. return;
  625. }
  626. // Validate default project folder selected
  627. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  628. {
  629. ShowMessage("No default project file selected.");
  630. return;
  631. }
  632. // Validate default project folder exists
  633. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  634. {
  635. if (ExporterWindow.ShowMessage("Create default project folder: " + exportationOptions.DefaultProjectFolder, "Babylon.js - Project not found", "Create"))
  636. {
  637. Directory.CreateDirectory(exportationOptions.DefaultProjectFolder);
  638. }
  639. else
  640. {
  641. return;
  642. }
  643. }
  644. // Get validate scene path info
  645. string[] sceneInfo = GetSceneInfomation(true);
  646. string sceneName = sceneInfo[0];
  647. string scenePath = sceneInfo[1];
  648. string scriptPath = sceneInfo[2];
  649. string outputFile = sceneInfo[3];
  650. string projectScript = sceneInfo[4];
  651. if (!ExporterWindow.ShowMessage("Export current scene to babylon: " + sceneName, "Babylon.js", "Export"))
  652. {
  653. return;
  654. }
  655. // Save current scene info
  656. SaveSettings();
  657. ExporterWindow.logs.Clear();
  658. Stopwatch watch = new Stopwatch();
  659. watch.Start();
  660. ReportProgress(0, "Exporting " + scenePath);
  661. // Auto lightmap baking
  662. if (exportationOptions.ExportLightmaps && exportationOptions.DefaultLightmapBaking == (int)BabylonLightmapBaking.Enabled)
  663. {
  664. ReportProgress(1, "Baking lightmap textures... This may take a while.");
  665. Lightmapping.GIWorkflowMode workflow = Lightmapping.giWorkflowMode;
  666. Lightmapping.giWorkflowMode = Lightmapping.GIWorkflowMode.OnDemand;
  667. Lightmapping.Bake();
  668. Lightmapping.giWorkflowMode = workflow;
  669. }
  670. // Save all open scenes
  671. ReportProgress(1, "Saving open scene information...");
  672. UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
  673. // Build project preview
  674. if (preview)
  675. {
  676. Tools.GenerateProjectIndexPage(exportationOptions.DefaultProjectFolder, exportationOptions.ShowDebugControls, exportationOptions.DefaultScenePath, Path.GetFileName(outputFile), exportationOptions.DefaultScriptPath, Path.GetFileName(projectScript));
  677. if (exportationOptions.BuildJavaScript || exportationOptions.CompileTypeScript)
  678. {
  679. Build(false, sceneInfo);
  680. }
  681. }
  682. // Build current scene
  683. BabylonSceneController sceneController = Tools.GetSceneController();
  684. var sceneBuilder = new SceneBuilder(scenePath, sceneName, exportationOptions, sceneController, scriptPath);
  685. sceneBuilder.ConvertFromUnity();
  686. ReportProgress(1, "Generating babylon scene... This may take a while.");
  687. sceneBuilder.WriteToBabylonFile(outputFile);
  688. watch.Stop();
  689. ReportProgress(1, string.Format("Exportation done in {0:0.00}s", watch.Elapsed.TotalSeconds));
  690. EditorUtility.ClearProgressBar();
  691. sceneBuilder.GenerateStatus(logs);
  692. string done = preview ? "Preview" : "OK";
  693. bool ok = ShowMessage("Scene exportation complete.", "Babylon.js", done);
  694. if (preview && ok)
  695. {
  696. Preview();
  697. }
  698. }
  699. catch (Exception ex)
  700. {
  701. EditorUtility.ClearProgressBar();
  702. ShowMessage("A problem occurred: " + ex.Message + ex.StackTrace, "Error");
  703. }
  704. }
  705. public string[] GetSceneInfomation(bool validate)
  706. {
  707. string[] result = new string[6];
  708. string sceneName = SceneManager.GetActiveScene().name;
  709. if (String.IsNullOrEmpty(exportationOptions.DefaultBuildPath))
  710. {
  711. exportationOptions.DefaultBuildPath = "Build";
  712. }
  713. string buildPath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultBuildPath));
  714. if (validate && !Directory.Exists(buildPath))
  715. {
  716. Directory.CreateDirectory(buildPath);
  717. }
  718. if (String.IsNullOrEmpty(exportationOptions.DefaultScenePath))
  719. {
  720. exportationOptions.DefaultScenePath = "Scenes";
  721. }
  722. string scenePath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultScenePath));
  723. if (validate && !Directory.Exists(scenePath))
  724. {
  725. Directory.CreateDirectory(scenePath);
  726. }
  727. if (String.IsNullOrEmpty(exportationOptions.DefaultScriptPath))
  728. {
  729. exportationOptions.DefaultScriptPath = "Scripts";
  730. }
  731. string scriptPath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultScriptPath));
  732. if (validate && !Directory.Exists(scriptPath))
  733. {
  734. Directory.CreateDirectory(scriptPath);
  735. }
  736. if (String.IsNullOrEmpty(exportationOptions.DefaultIndexPage))
  737. {
  738. exportationOptions.DefaultIndexPage = "Index.html";
  739. }
  740. if (String.IsNullOrEmpty(exportationOptions.DefaultTypeSriptPath))
  741. {
  742. exportationOptions.DefaultTypeSriptPath = Tools.GetDefaultTypeScriptPath();
  743. }
  744. if (String.IsNullOrEmpty(exportationOptions.DefaultNodeRuntimePath))
  745. {
  746. exportationOptions.DefaultNodeRuntimePath = Tools.GetDefaultNodeRuntimePath();
  747. }
  748. if (exportationOptions.DefaultServerPort < 1024)
  749. {
  750. exportationOptions.DefaultServerPort = ExporterWindow.DefaultPort;
  751. }
  752. string projectName = Application.productName;
  753. if (String.IsNullOrEmpty(projectName))
  754. {
  755. projectName = "Application";
  756. }
  757. string outputFile = Tools.FormatSafePath(Path.Combine(scenePath, sceneName.Replace(" ", "") + ".babylon"));
  758. string projectScript = Tools.FormatSafePath(Path.Combine(scenePath, projectName.Replace(" ", "") + ".babylon"));
  759. result[0] = sceneName;
  760. result[1] = scenePath;
  761. result[2] = buildPath;
  762. result[3] = outputFile;
  763. result[4] = projectScript;
  764. return result;
  765. }
  766. public void Preview()
  767. {
  768. string hostProtocol = "http://";
  769. string previewUrl = hostProtocol + "localhost:" + exportationOptions.DefaultServerPort.ToString() + "/" + exportationOptions.DefaultIndexPage;
  770. if (exportationOptions.DefaultPreviewWindow == (int)BabylonPreviewWindow.AttachUnityBrowser)
  771. {
  772. var browser = Tools.AttachToAssetStoreWindow("Babylon", previewUrl);
  773. if (browser == null)
  774. {
  775. ShowMessage("The asset store browser must be opened. Please try your request again after the asset store has opened.", "Babylon.js - Failed to attach browser");
  776. }
  777. }
  778. else
  779. {
  780. Application.OpenURL(previewUrl);
  781. }
  782. }
  783. public void Execute()
  784. {
  785. Scene scene = SceneManager.GetActiveScene();
  786. string[] sceneInfo = GetSceneInfomation(true);
  787. string outputFile = sceneInfo[3];
  788. if (!scene.isDirty && File.Exists(outputFile))
  789. {
  790. Preview();
  791. }
  792. else
  793. {
  794. Export(true);
  795. }
  796. }
  797. }
  798. }