MaterialInspector.ts 14 KB

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