MaterialInspector.ts 13 KB

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