MaterialInspector.ts 16 KB

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