MaterialInspector.ts 13 KB

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