ExporterWindow.cs 45 KB

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