MaterialInspector.ts 14 KB

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