MaterialInspector.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import ScriptWidget = require("ui/ScriptWidget");
  23. import UIEvents = require("ui/UIEvents");
  24. import EditorUI = require("ui/EditorUI");
  25. import EditorEvents = require("editor/EditorEvents");
  26. import TextureSelector = require("./TextureSelector");
  27. var techniqueSource = new Atomic.UIMenuItemSource();
  28. var solidSource = new Atomic.UIMenuItemSource();
  29. solidSource.addItem(new Atomic.UIMenuItem("Diffuse", "Diffuse"));
  30. solidSource.addItem(new Atomic.UIMenuItem("Diffuse Emissive", "Diffuse Emissive"));
  31. solidSource.addItem(new Atomic.UIMenuItem("Diffuse Normal", "Diffuse Normal"));
  32. solidSource.addItem(new Atomic.UIMenuItem("Diffuse Specular", "Diffuse Specular"));
  33. solidSource.addItem(new Atomic.UIMenuItem("Diffuse Normal Specular", "Diffuse Normal Specular"));
  34. solidSource.addItem(new Atomic.UIMenuItem("Diffuse Unlit", "Diffuse Unlit"));
  35. solidSource.addItem(new Atomic.UIMenuItem("No Texture", "No Texture"));
  36. var tranSource = new Atomic.UIMenuItemSource();
  37. tranSource.addItem(new Atomic.UIMenuItem("Alpha", "Alpha"));
  38. tranSource.addItem(new Atomic.UIMenuItem("Alpha Mask", "Alpha Mask"));
  39. tranSource.addItem(new Atomic.UIMenuItem("Additive", "Additive"));
  40. tranSource.addItem(new Atomic.UIMenuItem("Additive Alpha", "Additive Alpha"));
  41. tranSource.addItem(new Atomic.UIMenuItem("Emissive Alpha", "Emissive Alpha"));
  42. tranSource.addItem(new Atomic.UIMenuItem("Alpha AO", "Alpha AO"));
  43. tranSource.addItem(new Atomic.UIMenuItem("Alpha Mask AO", "Alpha Mask AO"));
  44. var lightmapSource = new Atomic.UIMenuItemSource();
  45. lightmapSource.addItem(new Atomic.UIMenuItem("Lightmap", "Lightmap"));
  46. lightmapSource.addItem(new Atomic.UIMenuItem("Lightmap Alpha", "Lightmap Alpha"));
  47. var projectSource = new Atomic.UIMenuItemSource();
  48. var _ = new Atomic.UIMenuItem("Solid");
  49. _.subSource = solidSource;
  50. techniqueSource.addItem(_);
  51. _ = new Atomic.UIMenuItem("Transparency");
  52. _.subSource = tranSource;
  53. techniqueSource.addItem(_);
  54. _ = new Atomic.UIMenuItem("Lightmap");
  55. _.subSource = lightmapSource;
  56. techniqueSource.addItem(_);
  57. _ = new Atomic.UIMenuItem("Project");
  58. _.subSource = projectSource;
  59. techniqueSource.addItem(_);
  60. var techniqueLookup = {
  61. "Techniques/Diff.xml": "Diffuse",
  62. "Techniques/DiffEmissive.xml": "Diffuse Emissive",
  63. "Techniques/DiffNormal.xml": "Diffuse Normal",
  64. "Techniques/DiffSpec.xml": "Diffuse Specular",
  65. "Techniques/DiffNormalSpec.xml": "Diffuse Normal Specular",
  66. "Techniques/DiffUnlit.xml": "Diffuse Unlit",
  67. "Techniques/DiffAlpha.xml": "Alpha",
  68. "Techniques/DiffAlphaMask.xml": "Alpha Mask",
  69. "Techniques/DiffAdd.xml": "Additive",
  70. "Techniques/NoTexture.xml": "No Texture",
  71. "Techniques/DiffLightMap.xml": "Lightmap",
  72. "Techniques/DiffLightMapAlpha.xml": "Lightmap Alpha"
  73. };
  74. var techniqueReverseLookup = {};
  75. var projectTechniques = {};
  76. for (var key in techniqueLookup) {
  77. techniqueReverseLookup[techniqueLookup[key]] = key;
  78. }
  79. class MaterialInspector extends ScriptWidget {
  80. currentTexture: Atomic.UITextureWidget = null;
  81. tunit: number;
  82. textureWidget: Atomic.UITextureWidget
  83. constructor() {
  84. super();
  85. this.fd.id = "Vera";
  86. this.fd.size = 11;
  87. this.subscribeToEvent(EditorEvents.RemoveCurrentAssetAssigned, (ev: EditorEvents.RemoveCurrentAssetAssignedEvent) => this.createTextureRemoveButtonCallback(this.tunit, this.textureWidget));
  88. }
  89. createShaderParametersSection(): Atomic.UISection {
  90. var section = new Atomic.UISection();
  91. section.text = "Shader Paramaters";
  92. section.value = 1;
  93. section.fontDescription = this.fd;
  94. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  95. attrsVerticalLayout.spacing = 3;
  96. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  97. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  98. section.contentRoot.addChild(attrsVerticalLayout);
  99. var params = this.material.getShaderParameters();
  100. for (var i in params) {
  101. var attrLayout = new Atomic.UILayout();
  102. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  103. var name = new Atomic.UITextField();
  104. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  105. name.skinBg = "InspectorTextAttrName";
  106. name.text = params[i].name;
  107. name.fontDescription = this.fd;
  108. attrLayout.addChild(name);
  109. var field = new Atomic.UIEditField();
  110. field.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  111. field.skinBg = "TBAttrEditorField";
  112. field.fontDescription = this.fd;
  113. var lp = new Atomic.UILayoutParams();
  114. lp.width = 140;
  115. field.layoutParams = lp;
  116. field.id = params[i].name;
  117. field.text = params[i].valueString;
  118. field.subscribeToEvent(field, "WidgetEvent", function (ev: Atomic.UIWidgetEvent) {
  119. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  120. var field = <Atomic.UIEditField>ev.target;
  121. this.material.setShaderParameter(field.id, field.text);
  122. }
  123. }.bind(this));
  124. attrLayout.addChild(field);
  125. attrsVerticalLayout.addChild(attrLayout);
  126. // print(params[i].name, " : ", params[i].value, " : ", params[i].type);
  127. }
  128. return section;
  129. }
  130. getTextureThumbnail(texture: Atomic.Texture): Atomic.Texture {
  131. if (!texture) return null;
  132. var db = ToolCore.getAssetDatabase();
  133. var asset = db.getAssetByPath(texture.name);
  134. if (!asset)
  135. return texture;
  136. var thumbnail = asset.cachePath + "_thumbnail.png";
  137. var cache = Atomic.getResourceCache();
  138. var thumb = <Atomic.Texture2D>cache.getTempResource("Texture2D", thumbnail);
  139. if (thumb)
  140. return thumb;
  141. return texture;
  142. }
  143. onTechniqueSet(techniqueName: string) {
  144. this.techniqueButton.text = techniqueName;
  145. var cache = Atomic.getResourceCache();
  146. var technique = <Atomic.Technique>cache.getResource("Technique", techniqueReverseLookup[techniqueName]);
  147. if (technique == null)
  148. technique = <Atomic.Technique>cache.getResource("Technique", "Techniques/" + techniqueName + ".xml");
  149. this.material.setTechnique(0, technique);
  150. }
  151. createTechniquePopup(): Atomic.UIWidget {
  152. this.loadProjectTechniques();
  153. var button = this.techniqueButton = new Atomic.UIButton();
  154. var technique = this.material.getTechnique(0);
  155. button.text = technique.name;
  156. button.fontDescription = this.fd;
  157. var lp = new Atomic.UILayoutParams();
  158. lp.width = 140;
  159. button.layoutParams = lp;
  160. button.onClick = function () {
  161. var menu = new Atomic.UIMenuWindow(button, "technique popup");
  162. menu.fontDescription = this.fd;
  163. menu.show(techniqueSource);
  164. button.subscribeToEvent(button, "WidgetEvent", function (ev: Atomic.UIWidgetEvent) {
  165. if (ev.type != Atomic.UI_EVENT_TYPE_CLICK)
  166. return;
  167. if (ev.target && ev.target.id == "technique popup") {
  168. this.onTechniqueSet(ev.refid);
  169. }
  170. }.bind(this));
  171. }.bind(this);
  172. return button;
  173. }
  174. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  175. var dragObject = ev.dragObject;
  176. if (dragObject.object && dragObject.object.typeName == "Asset") {
  177. var asset = <ToolCore.Asset>dragObject.object;
  178. if (asset.importerTypeName == importerTypeName) {
  179. return asset.importer;
  180. }
  181. }
  182. return null;
  183. }
  184. openTextureSelectionBox(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  185. var inspector = this;
  186. EditorUI.getModelOps().showResourceSelection("Select Texture", "TextureImporter", "Texture2D", function (asset: ToolCore.Asset, args: any) {
  187. var texture = <Atomic.Texture2D>Atomic.cache.getResource("Texture2D", asset.path);
  188. if (texture) {
  189. inspector.material.setTexture(textureUnit, texture);
  190. textureWidget.texture = inspector.getTextureThumbnail(texture);
  191. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": texture.getName() });
  192. }
  193. });
  194. }
  195. // Big Texture Button(referenced texture file path in project frame)
  196. createTextureButtonCallback(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  197. return () => {
  198. var texture = this.material.getTexture(textureUnit);
  199. if (textureWidget.getTexture() != null) {
  200. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": texture.getName() });
  201. } else {
  202. this.openTextureSelectionBox(textureUnit, textureWidget);
  203. }
  204. return true;
  205. };
  206. }
  207. // Small Texture Button (Opens texture selection window)
  208. createTextureReferenceButtonCallback(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  209. return () => {
  210. this.tunit = textureUnit;
  211. this.textureWidget = textureWidget;
  212. this.openTextureSelectionBox(textureUnit, textureWidget);
  213. return true;
  214. };
  215. }
  216. //Remove Texture Button
  217. createTextureRemoveButtonCallback(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  218. var texture = this.material.getTexture(textureUnit);
  219. if (texture != null && textureWidget != null) {
  220. textureWidget.setTexture(null);
  221. }
  222. }
  223. createTextureSection(): Atomic.UISection {
  224. var section = new Atomic.UISection();
  225. section.text = "Textures";
  226. section.value = 1;
  227. section.fontDescription = this.fd;
  228. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  229. attrsVerticalLayout.spacing = 3;
  230. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  231. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  232. section.contentRoot.addChild(attrsVerticalLayout);
  233. // TODO: Filter on technique
  234. var textureUnits = [Atomic.TU_DIFFUSE, Atomic.TU_NORMAL, Atomic.TU_SPECULAR, Atomic.TU_EMISSIVE];
  235. for (var i in textureUnits) {
  236. var tunit: Atomic.TextureUnit = textureUnits[i];
  237. var tunitName = Atomic.Material.getTextureUnitName(tunit);
  238. var attrLayout = new Atomic.UILayout();
  239. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  240. var name = new Atomic.UITextField();
  241. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  242. name.skinBg = "InspectorTextAttrName";
  243. name.text = tunitName;
  244. name.fontDescription = this.fd;
  245. attrLayout.addChild(name);
  246. var textureWidget = new Atomic.UITextureWidget();
  247. var tlp = new Atomic.UILayoutParams();
  248. tlp.width = 64;
  249. tlp.height = 64;
  250. textureWidget.layoutParams = tlp;
  251. textureWidget.texture = this.getTextureThumbnail(this.material.getTexture(tunit));
  252. var textureButton = new Atomic.UIButton();
  253. textureButton.skinBg = "TBButton.flatoutline";
  254. textureButton["tunit"] = tunit;
  255. textureButton["textureWidget"] = textureWidget;
  256. //Create drop-down buttons to open Texture Selection Dialog Box
  257. var textureRefButton = new Atomic.UIButton();
  258. textureRefButton.skinBg = "arrow.down";
  259. textureRefButton["tunit"] = tunit;
  260. textureRefButton["textureWidget"] = textureWidget;
  261. textureButton.onClick = this.createTextureButtonCallback(tunit, textureWidget);
  262. textureRefButton.onClick = this.createTextureReferenceButtonCallback(tunit, textureWidget);
  263. textureButton.contentRoot.addChild(textureWidget);
  264. attrLayout.addChild(textureButton);
  265. attrLayout.addChild(textureRefButton);
  266. attrsVerticalLayout.addChild(attrLayout);
  267. // handle dropping of texture on widget
  268. textureButton.subscribeToEvent(textureButton, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  269. var importer = this.acceptAssetDrag("TextureImporter", ev);
  270. if (importer) {
  271. var textureImporter = <ToolCore.TextureImporter>importer;
  272. var asset = textureImporter.asset;
  273. var texture = <Atomic.Texture2D>Atomic.cache.getResource("Texture2D", asset.path);
  274. if (texture) {
  275. this.material.setTexture(ev.target["tunit"], texture);
  276. (<Atomic.UITextureWidget>ev.target["textureWidget"]).texture = this.getTextureThumbnail(texture);
  277. this.sendEvent("InspectorProjectReference", { "path": texture.getName(), "ButtonID": texture.getName() });
  278. }
  279. }
  280. });
  281. }
  282. return section;
  283. }
  284. loadProjectTechniques() {
  285. projectSource.clear();
  286. var resourcePath = ToolCore.toolSystem.project.getResourcePath();
  287. var TechniqueAssets = ToolCore.getAssetDatabase().getFolderAssets(resourcePath + "Techniques");
  288. for (var key in TechniqueAssets) {
  289. projectTechniques[key] = TechniqueAssets[key].name;
  290. projectSource.addItem(new Atomic.UIMenuItem(projectTechniques[key], projectTechniques[key]));
  291. }
  292. }
  293. inspect(asset: ToolCore.Asset, material: Atomic.Material) {
  294. // Add folders to resource directory
  295. this.asset = asset;
  296. this.material = material;
  297. var mlp = new Atomic.UILayoutParams();
  298. mlp.width = 310;
  299. var materialLayout = new Atomic.UILayout();
  300. materialLayout.spacing = 4;
  301. materialLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  302. materialLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  303. materialLayout.layoutParams = mlp;
  304. materialLayout.axis = Atomic.UI_AXIS_Y;
  305. // node attr layout
  306. var materialSection = new Atomic.UISection();
  307. materialSection.text = "Material";
  308. materialSection.value = 1;
  309. materialSection.fontDescription = this.fd;
  310. materialLayout.addChild(materialSection);
  311. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  312. attrsVerticalLayout.spacing = 3;
  313. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  314. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  315. // NAME
  316. var nameLayout = new Atomic.UILayout();
  317. nameLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  318. var name = new Atomic.UITextField();
  319. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  320. name.skinBg = "InspectorTextAttrName";
  321. name.text = "Name";
  322. name.fontDescription = this.fd;
  323. nameLayout.addChild(name);
  324. var field = new Atomic.UIEditField();
  325. field.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  326. field.skinBg = "TBAttrEditorField";
  327. field.fontDescription = this.fd;
  328. var lp = new Atomic.UILayoutParams();
  329. lp.width = 160;
  330. field.layoutParams = lp;
  331. field.text = Atomic.splitPath(material.name).fileName;
  332. nameLayout.addChild(field);
  333. attrsVerticalLayout.addChild(nameLayout);
  334. // TECHNIQUE LAYOUT
  335. var techniqueLayout = new Atomic.UILayout();
  336. techniqueLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_GRAVITY;
  337. techniqueLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_PREFERRED;
  338. name = new Atomic.UITextField();
  339. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  340. name.skinBg = "InspectorTextAttrName";
  341. name.text = "Technique";
  342. name.fontDescription = this.fd;
  343. techniqueLayout.addChild(name);
  344. var techniquePopup = this.createTechniquePopup();
  345. techniqueLayout.addChild(techniquePopup);
  346. attrsVerticalLayout.addChild(techniqueLayout);
  347. materialSection.contentRoot.addChild(attrsVerticalLayout);
  348. materialLayout.addChild(this.createTextureSection());
  349. materialLayout.addChild(this.createShaderParametersSection());
  350. var button = new Atomic.UIButton();
  351. button.fontDescription = this.fd;
  352. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  353. button.text = "Save";
  354. button.onClick = function () {
  355. var importer = <ToolCore.MaterialImporter>this.asset.getImporter();
  356. importer.saveMaterial();
  357. }.bind(this);
  358. materialLayout.addChild(button);
  359. this.addChild(materialLayout);
  360. }
  361. techniqueButton: Atomic.UIButton;
  362. material: Atomic.Material;
  363. asset: ToolCore.Asset;
  364. nameTextField: Atomic.UITextField;
  365. fd: Atomic.UIFontDescription = new Atomic.UIFontDescription();
  366. }
  367. export = MaterialInspector;