MaterialInspector.ts 14 KB

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