MaterialInspector.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 _ = new Atomic.UIMenuItem("Solid");
  48. _.subSource = solidSource;
  49. techniqueSource.addItem(_);
  50. _ = new Atomic.UIMenuItem("Transparency");
  51. _.subSource = tranSource;
  52. techniqueSource.addItem(_);
  53. _ = new Atomic.UIMenuItem("Lightmap");
  54. _.subSource = lightmapSource;
  55. techniqueSource.addItem(_);
  56. var techniqueLookup = {
  57. "Techniques/Diff.xml": "Diffuse",
  58. "Techniques/DiffEmissive.xml": "Diffuse Emissive",
  59. "Techniques/DiffNormal.xml": "Diffuse Normal",
  60. "Techniques/DiffSpec.xml": "Diffuse Specular",
  61. "Techniques/DiffNormalSpec.xml": "Diffuse Normal Specular",
  62. "Techniques/DiffUnlit.xml": "Diffuse Unlit",
  63. "Techniques/DiffAlpha.xml": "Alpha",
  64. "Techniques/DiffAlphaMask.xml": "Alpha Mask",
  65. "Techniques/DiffAdd.xml": "Additive",
  66. "Techniques/NoTexture.xml": "No Texture",
  67. "Techniques/DiffLightMap.xml": "Lightmap",
  68. "Techniques/DiffLightMapAlpha.xml": "Lightmap Alpha"
  69. };
  70. var techniqueReverseLookup = {};
  71. for (var key in techniqueLookup) {
  72. techniqueReverseLookup[techniqueLookup[key]] = key;
  73. }
  74. class MaterialInspector extends ScriptWidget {
  75. currentTexture: Atomic.UITextureWidget = null;
  76. tunit: number;
  77. textureWidget: Atomic.UITextureWidget
  78. constructor() {
  79. super();
  80. this.fd.id = "Vera";
  81. this.fd.size = 11;
  82. this.subscribeToEvent(EditorEvents.RemoveCurrentAssetAssigned, (ev: EditorEvents.RemoveCurrentAssetAssignedEvent) => this.createTextureRemoveButtonCallback(this.tunit, this.textureWidget));
  83. }
  84. createShaderParametersSection(): Atomic.UISection {
  85. var section = new Atomic.UISection();
  86. section.text = "Shader Paramaters";
  87. section.value = 1;
  88. section.fontDescription = this.fd;
  89. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  90. attrsVerticalLayout.spacing = 3;
  91. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  92. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  93. section.contentRoot.addChild(attrsVerticalLayout);
  94. var params = this.material.getShaderParameters();
  95. for (var i in params) {
  96. var attrLayout = new Atomic.UILayout();
  97. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  98. var name = new Atomic.UITextField();
  99. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  100. name.skinBg = "InspectorTextAttrName";
  101. name.text = params[i].name;
  102. name.fontDescription = this.fd;
  103. attrLayout.addChild(name);
  104. var field = new Atomic.UIEditField();
  105. field.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  106. field.skinBg = "TBAttrEditorField";
  107. field.fontDescription = this.fd;
  108. var lp = new Atomic.UILayoutParams();
  109. lp.width = 140;
  110. field.layoutParams = lp;
  111. field.id = params[i].name;
  112. field.text = params[i].valueString;
  113. field.subscribeToEvent(field, "WidgetEvent", function(ev: Atomic.UIWidgetEvent) {
  114. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  115. var field = <Atomic.UIEditField> ev.target;
  116. this.material.setShaderParameter(field.id, field.text);
  117. }
  118. }.bind(this));
  119. attrLayout.addChild(field);
  120. attrsVerticalLayout.addChild(attrLayout);
  121. // print(params[i].name, " : ", params[i].value, " : ", params[i].type);
  122. }
  123. return section;
  124. }
  125. getTextureThumbnail(texture: Atomic.Texture): Atomic.Texture {
  126. if (!texture) return null;
  127. var db = ToolCore.getAssetDatabase();
  128. var asset = db.getAssetByPath(texture.name);
  129. if (!asset)
  130. return texture;
  131. var thumbnail = asset.cachePath + "_thumbnail.png";
  132. var cache = Atomic.getResourceCache();
  133. var thumb = <Atomic.Texture2D> cache.getTempResource("Texture2D", thumbnail);
  134. if (thumb)
  135. return thumb;
  136. return texture;
  137. }
  138. onTechniqueSet(techniqueName: string) {
  139. this.techniqueButton.text = techniqueName;
  140. var cache = Atomic.getResourceCache();
  141. var technique = <Atomic.Technique> cache.getResource("Technique", techniqueReverseLookup[techniqueName]);
  142. this.material.setTechnique(0, technique);
  143. }
  144. createTechniquePopup(): Atomic.UIWidget {
  145. var button = this.techniqueButton = new Atomic.UIButton();
  146. var technique = this.material.getTechnique(0);
  147. button.text = techniqueLookup[technique.name];
  148. button.fontDescription = this.fd;
  149. var lp = new Atomic.UILayoutParams();
  150. lp.width = 140;
  151. button.layoutParams = lp;
  152. button.onClick = function() {
  153. var menu = new Atomic.UIMenuWindow(button, "technique popup");
  154. menu.fontDescription = this.fd;
  155. menu.show(techniqueSource);
  156. button.subscribeToEvent(button, "WidgetEvent", function(ev: Atomic.UIWidgetEvent) {
  157. if (ev.type != Atomic.UI_EVENT_TYPE_CLICK)
  158. return;
  159. if (ev.target && ev.target.id == "technique popup") {
  160. this.onTechniqueSet(ev.refid);
  161. }
  162. }.bind(this));
  163. }.bind(this);
  164. return button;
  165. }
  166. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  167. var dragObject = ev.dragObject;
  168. if (dragObject.object && dragObject.object.typeName == "Asset") {
  169. var asset = <ToolCore.Asset> dragObject.object;
  170. if (asset.importerTypeName == importerTypeName) {
  171. return asset.importer;
  172. }
  173. }
  174. return null;
  175. }
  176. openTextureSelectionBox(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  177. var inspector = this;
  178. EditorUI.getModelOps().showResourceSelection("Select Texture", "TextureImporter", "Texture2D", function (asset: ToolCore.Asset, args: any) {
  179. var texture = <Atomic.Texture2D>Atomic.cache.getResource("Texture2D", asset.path);
  180. if (texture) {
  181. inspector.material.setTexture(textureUnit, texture);
  182. textureWidget.texture = inspector.getTextureThumbnail(texture);
  183. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": texture.getName() });
  184. }
  185. });
  186. }
  187. // Big Texture Button(referenced texture file path in project frame)
  188. createTextureButtonCallback(textureUnit:number, textureWidget:Atomic.UITextureWidget) {
  189. return () => {
  190. var texture = this.material.getTexture(textureUnit);
  191. if (textureWidget.getTexture() != null) {
  192. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": texture.getName() });
  193. } else {
  194. this.openTextureSelectionBox(textureUnit, textureWidget);
  195. }
  196. return true;
  197. };
  198. }
  199. // Small Texture Button (Opens texture selection window)
  200. createTextureReferenceButtonCallback(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  201. return () => {
  202. this.tunit = textureUnit;
  203. this.textureWidget = textureWidget;
  204. this.openTextureSelectionBox(textureUnit, textureWidget);
  205. return true;
  206. };
  207. }
  208. //Remove Texture Button
  209. createTextureRemoveButtonCallback(textureUnit: number, textureWidget: Atomic.UITextureWidget) {
  210. var texture = this.material.getTexture(textureUnit);
  211. if (texture != null && textureWidget != null) {
  212. textureWidget.setTexture(null);
  213. }
  214. }
  215. createTextureSection(): Atomic.UISection {
  216. var section = new Atomic.UISection();
  217. section.text = "Textures";
  218. section.value = 1;
  219. section.fontDescription = this.fd;
  220. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  221. attrsVerticalLayout.spacing = 3;
  222. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  223. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  224. section.contentRoot.addChild(attrsVerticalLayout);
  225. // TODO: Filter on technique
  226. var textureUnits = [ Atomic.TU_DIFFUSE, Atomic.TU_NORMAL, Atomic.TU_SPECULAR, Atomic.TU_EMISSIVE ];
  227. for (var i in textureUnits) {
  228. var tunit: Atomic.TextureUnit = textureUnits[i];
  229. var tunitName = Atomic.Material.getTextureUnitName(tunit);
  230. var attrLayout = new Atomic.UILayout();
  231. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  232. var name = new Atomic.UITextField();
  233. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  234. name.skinBg = "InspectorTextAttrName";
  235. name.text = tunitName;
  236. name.fontDescription = this.fd;
  237. attrLayout.addChild(name);
  238. var textureWidget = new Atomic.UITextureWidget();
  239. var tlp = new Atomic.UILayoutParams();
  240. tlp.width = 64;
  241. tlp.height = 64;
  242. textureWidget.layoutParams = tlp;
  243. textureWidget.texture = this.getTextureThumbnail(this.material.getTexture(tunit));
  244. var textureButton = new Atomic.UIButton();
  245. textureButton.skinBg = "TBButton.flatoutline";
  246. textureButton["tunit"] = tunit;
  247. textureButton["textureWidget"] = textureWidget;
  248. //Create drop-down buttons to open Texture Selection Dialog Box
  249. var textureRefButton = new Atomic.UIButton();
  250. textureRefButton.skinBg = "arrow.down";
  251. textureRefButton["tunit"] = tunit;
  252. textureRefButton["textureWidget"] = textureWidget;
  253. textureButton.onClick = this.createTextureButtonCallback(tunit, textureWidget);
  254. textureRefButton.onClick = this.createTextureReferenceButtonCallback(tunit, textureWidget);
  255. textureButton.contentRoot.addChild(textureWidget);
  256. attrLayout.addChild(textureButton);
  257. attrLayout.addChild(textureRefButton);
  258. attrsVerticalLayout.addChild(attrLayout);
  259. // handle dropping of texture on widget
  260. textureButton.subscribeToEvent(textureButton, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  261. var importer = this.acceptAssetDrag("TextureImporter", ev);
  262. if (importer) {
  263. var textureImporter = <ToolCore.TextureImporter> importer;
  264. var asset = textureImporter.asset;
  265. var texture = <Atomic.Texture2D> Atomic.cache.getResource("Texture2D", asset.path);
  266. if (texture) {
  267. this.material.setTexture(ev.target["tunit"], texture);
  268. (<Atomic.UITextureWidget>ev.target["textureWidget"]).texture = this.getTextureThumbnail(texture);
  269. this.sendEvent("InspectorProjectReference", { "path": texture.getName(), "ButtonID": texture.getName() });
  270. }
  271. }
  272. });
  273. }
  274. return section;
  275. }
  276. inspect(asset: ToolCore.Asset, material: Atomic.Material) {
  277. this.asset = asset;
  278. this.material = material;
  279. var mlp = new Atomic.UILayoutParams();
  280. mlp.width = 310;
  281. var materialLayout = new Atomic.UILayout();
  282. materialLayout.spacing = 4;
  283. materialLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  284. materialLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  285. materialLayout.layoutParams = mlp;
  286. materialLayout.axis = Atomic.UI_AXIS_Y;
  287. // node attr layout
  288. var materialSection = new Atomic.UISection();
  289. materialSection.text = "Material";
  290. materialSection.value = 1;
  291. materialSection.fontDescription = this.fd;
  292. materialLayout.addChild(materialSection);
  293. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  294. attrsVerticalLayout.spacing = 3;
  295. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  296. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  297. // NAME
  298. var nameLayout = new Atomic.UILayout();
  299. nameLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  300. var name = new Atomic.UITextField();
  301. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  302. name.skinBg = "InspectorTextAttrName";
  303. name.text = "Name";
  304. name.fontDescription = this.fd;
  305. nameLayout.addChild(name);
  306. var field = new Atomic.UIEditField();
  307. field.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  308. field.skinBg = "TBAttrEditorField";
  309. field.fontDescription = this.fd;
  310. var lp = new Atomic.UILayoutParams();
  311. lp.width = 160;
  312. field.layoutParams = lp;
  313. field.text = Atomic.splitPath(material.name).fileName;
  314. nameLayout.addChild(field);
  315. attrsVerticalLayout.addChild(nameLayout);
  316. // TECHNIQUE LAYOUT
  317. var techniqueLayout = new Atomic.UILayout();
  318. techniqueLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_GRAVITY;
  319. techniqueLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_PREFERRED;
  320. name = new Atomic.UITextField();
  321. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  322. name.skinBg = "InspectorTextAttrName";
  323. name.text = "Technique";
  324. name.fontDescription = this.fd;
  325. techniqueLayout.addChild(name);
  326. var techniquePopup = this.createTechniquePopup();
  327. techniqueLayout.addChild(techniquePopup);
  328. attrsVerticalLayout.addChild(techniqueLayout);
  329. materialSection.contentRoot.addChild(attrsVerticalLayout);
  330. materialLayout.addChild(this.createTextureSection());
  331. materialLayout.addChild(this.createShaderParametersSection());
  332. var button = new Atomic.UIButton();
  333. button.fontDescription = this.fd;
  334. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  335. button.text = "Save";
  336. button.onClick = function() {
  337. var importer = <ToolCore.MaterialImporter> this.asset.getImporter();
  338. importer.saveMaterial();
  339. }.bind(this);
  340. materialLayout.addChild(button);
  341. this.addChild(materialLayout);
  342. }
  343. techniqueButton: Atomic.UIButton;
  344. material: Atomic.Material;
  345. asset: ToolCore.Asset;
  346. nameTextField: Atomic.UITextField;
  347. fd: Atomic.UIFontDescription = new Atomic.UIFontDescription();
  348. }
  349. export = MaterialInspector;