MaterialInspector.ts 16 KB

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