EditorMaterial.as 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. // Urho3D material editor
  2. Window@ materialWindow;
  3. Material@ editMaterial;
  4. XMLFile@ oldMaterialState;
  5. bool inMaterialRefresh = true;
  6. View3D@ materialPreview;
  7. Scene@ previewScene;
  8. Node@ previewCameraNode;
  9. Node@ previewLightNode;
  10. Light@ previewLight;
  11. Node@ previewModelNode;
  12. StaticModel@ previewModel;
  13. void CreateMaterialEditor()
  14. {
  15. if (materialWindow !is null)
  16. return;
  17. materialWindow = ui.LoadLayout(cache.GetResource("XMLFile", "UI/EditorMaterialWindow.xml"));
  18. ui.root.AddChild(materialWindow);
  19. materialWindow.opacity = uiMaxOpacity;
  20. InitMaterialPreview();
  21. RefreshMaterialEditor();
  22. int height = Min(ui.root.height - 60, 500);
  23. materialWindow.SetSize(300, height);
  24. CenterDialog(materialWindow);
  25. HideMaterialEditor();
  26. SubscribeToEvent(materialWindow.GetChild("NewButton", true), "Released", "NewMaterial");
  27. SubscribeToEvent(materialWindow.GetChild("RevertButton", true), "Released", "RevertMaterial");
  28. SubscribeToEvent(materialWindow.GetChild("SaveButton", true), "Released", "SaveMaterial");
  29. SubscribeToEvent(materialWindow.GetChild("SaveAsButton", true), "Released", "SaveMaterialAs");
  30. SubscribeToEvent(materialWindow.GetChild("CloseButton", true), "Released", "HideMaterialEditor");
  31. SubscribeToEvent(materialWindow.GetChild("NewParameterDropDown", true), "ItemSelected", "CreateShaderParameter");
  32. SubscribeToEvent(materialWindow.GetChild("DeleteParameterButton", true), "Released", "DeleteShaderParameter");
  33. SubscribeToEvent(materialWindow.GetChild("NewTechniqueButton", true), "Released", "NewTechnique");
  34. SubscribeToEvent(materialWindow.GetChild("DeleteTechniqueButton", true), "Released", "DeleteTechnique");
  35. SubscribeToEvent(materialWindow.GetChild("SortTechniquesButton", true), "Released", "SortTechniques");
  36. SubscribeToEvent(materialWindow.GetChild("ConstantBiasEdit", true), "TextChanged", "EditConstantBias");
  37. SubscribeToEvent(materialWindow.GetChild("ConstantBiasEdit", true), "TextFinished", "EditConstantBias");
  38. SubscribeToEvent(materialWindow.GetChild("SlopeBiasEdit", true), "TextChanged", "EditSlopeBias");
  39. SubscribeToEvent(materialWindow.GetChild("SlopeBiasEdit", true), "TextFinished", "EditSlopeBias");
  40. SubscribeToEvent(materialWindow.GetChild("CullModeEdit", true), "ItemSelected", "EditCullMode");
  41. SubscribeToEvent(materialWindow.GetChild("ShadowCullModeEdit", true), "ItemSelected", "EditShadowCullMode");
  42. }
  43. bool ShowMaterialEditor()
  44. {
  45. RefreshMaterialEditor();
  46. materialWindow.visible = true;
  47. materialWindow.BringToFront();
  48. return true;
  49. }
  50. void HideMaterialEditor()
  51. {
  52. materialWindow.visible = false;
  53. }
  54. void InitMaterialPreview()
  55. {
  56. previewScene = Scene("PreviewScene");
  57. previewScene.CreateComponent("Octree");
  58. Node@ zoneNode = previewScene.CreateChild("Zone");
  59. Zone@ zone = zoneNode.CreateComponent("Zone");
  60. zone.boundingBox = BoundingBox(-1000, 1000);
  61. zone.ambientColor = Color(0.15, 0.15, 0.15);
  62. zone.fogColor = Color(0, 0, 0);
  63. zone.fogStart = 10.0;
  64. zone.fogEnd = 100.0;
  65. previewCameraNode = previewScene.CreateChild("PreviewCamera");
  66. previewCameraNode.position = Vector3(0, 0, -1.5);
  67. Camera@ camera = previewCameraNode.CreateComponent("Camera");
  68. camera.nearClip = 0.1f;
  69. camera.farClip = 100.0f;
  70. previewLightNode = previewScene.CreateChild("PreviewLight");
  71. previewLightNode.direction = Vector3(0.5, -0.5, 0.5);
  72. previewLight = previewLightNode.CreateComponent("Light");
  73. previewLight.lightType = LIGHT_DIRECTIONAL;
  74. previewLight.specularIntensity = 0.5;
  75. previewModelNode = previewScene.CreateChild("PreviewModel");
  76. previewModelNode.rotation = Quaternion(0, 0, 0);
  77. previewModel = previewModelNode.CreateComponent("StaticModel");
  78. previewModel.model = cache.GetResource("Model", "Models/Sphere.mdl");
  79. materialPreview = materialWindow.GetChild("MaterialPreview", true);
  80. materialPreview.SetFixedHeight(100);
  81. materialPreview.SetView(previewScene, camera);
  82. materialPreview.autoUpdate = false;
  83. SubscribeToEvent(materialPreview, "DragMove", "RotateMaterialPreview");
  84. }
  85. void EditMaterial(Material@ mat)
  86. {
  87. if (editMaterial !is null)
  88. UnsubscribeFromEvent(editMaterial, "ReloadFinished");
  89. editMaterial = mat;
  90. if (editMaterial !is null)
  91. SubscribeToEvent(editMaterial, "ReloadFinished", "RefreshMaterialEditor");
  92. ShowMaterialEditor();
  93. }
  94. void RefreshMaterialEditor()
  95. {
  96. RefreshMaterialPreview();
  97. RefreshMaterialName();
  98. RefreshMaterialTechniques();
  99. RefreshMaterialTextures();
  100. RefreshMaterialShaderParameters();
  101. RefreshMaterialMiscParameters();
  102. }
  103. void RefreshMaterialPreview()
  104. {
  105. previewModel.material = editMaterial;
  106. materialPreview.QueueUpdate();
  107. }
  108. void RefreshMaterialName()
  109. {
  110. UIElement@ container = materialWindow.GetChild("NameContainer", true);
  111. container.RemoveAllChildren();
  112. LineEdit@ nameEdit = CreateAttributeLineEdit(container, null, 0, 0);
  113. if (editMaterial !is null)
  114. nameEdit.text = editMaterial.name;
  115. SubscribeToEvent(nameEdit, "TextFinished", "EditMaterialName");
  116. Button@ pickButton = CreateResourcePickerButton(container, null, 0, 0, "Pick");
  117. SubscribeToEvent(pickButton, "Released", "PickEditMaterial");
  118. }
  119. void RefreshMaterialTechniques(bool fullUpdate = true)
  120. {
  121. ListView@ list = materialWindow.GetChild("TechniqueList", true);
  122. if (editMaterial is null)
  123. return;
  124. if (fullUpdate == true)
  125. {
  126. list.RemoveAllItems();
  127. for (uint i = 0; i < editMaterial.numTechniques; ++i)
  128. {
  129. TechniqueEntry entry = editMaterial.techniqueEntries[i];
  130. UIElement@ container = UIElement();
  131. container.SetLayout(LM_HORIZONTAL, 4);
  132. container.SetFixedHeight(ATTR_HEIGHT);
  133. list.AddItem(container);
  134. LineEdit@ nameEdit = CreateAttributeLineEdit(container, null, i, 0);
  135. nameEdit.name = "TechniqueNameEdit" + String(i);
  136. Button@ pickButton = CreateResourcePickerButton(container, null, i, 0, "Pick");
  137. SubscribeToEvent(pickButton, "Released", "PickMaterialTechnique");
  138. Button@ openButton = CreateResourcePickerButton(container, null, i, 0, "Open");
  139. SubscribeToEvent(openButton, "Released", "OpenResource");
  140. if (entry.technique !is null)
  141. nameEdit.text = entry.technique.name;
  142. SubscribeToEvent(nameEdit, "TextFinished", "EditMaterialTechnique");
  143. UIElement@ container2 = UIElement();
  144. container2.SetLayout(LM_HORIZONTAL, 4);
  145. container2.SetFixedHeight(ATTR_HEIGHT);
  146. list.AddItem(container2);
  147. Text@ text = container2.CreateChild("Text");
  148. text.style = "EditorAttributeText";
  149. text.text = "Quality";
  150. LineEdit@ attrEdit = CreateAttributeLineEdit(container2, null, i, 0);
  151. attrEdit.text = String(entry.qualityLevel);
  152. SubscribeToEvent(attrEdit, "TextChanged", "EditTechniqueQuality");
  153. SubscribeToEvent(attrEdit, "TextFinished", "EditTechniqueQuality");
  154. text = container2.CreateChild("Text");
  155. text.style = "EditorAttributeText";
  156. text.text = "LOD Distance";
  157. attrEdit = CreateAttributeLineEdit(container2, null, i, 0);
  158. attrEdit.text = String(entry.lodDistance);
  159. SubscribeToEvent(attrEdit, "TextChanged", "EditTechniqueLodDistance");
  160. SubscribeToEvent(attrEdit, "TextFinished", "EditTechniqueLodDistance");
  161. }
  162. }
  163. else
  164. {
  165. for (uint i = 0; i < editMaterial.numTechniques; ++i)
  166. {
  167. TechniqueEntry entry = editMaterial.techniqueEntries[i];
  168. LineEdit@ nameEdit = materialWindow.GetChild("TechniqueNameEdit" + String(i), true);
  169. if (nameEdit is null)
  170. continue;
  171. nameEdit.text = entry.technique !is null ? entry.technique.name : "";
  172. }
  173. }
  174. }
  175. void RefreshMaterialTextures(bool fullUpdate = true)
  176. {
  177. if (fullUpdate)
  178. {
  179. ListView@ list = materialWindow.GetChild("TextureList", true);
  180. list.RemoveAllItems();
  181. for (uint i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
  182. {
  183. UIElement@ parent = CreateAttributeEditorParentWithSeparatedLabel(list, GetTextureUnitName(TextureUnit(i)), i, 0, false);
  184. UIElement@ container = UIElement();
  185. container.SetLayout(LM_HORIZONTAL, 4, IntRect(10, 0, 4, 0));
  186. container.SetFixedHeight(ATTR_HEIGHT);
  187. parent.AddChild(container);
  188. LineEdit@ nameEdit = CreateAttributeLineEdit(container, null, i, 0);
  189. nameEdit.name = "TextureNameEdit" + String(i);
  190. Button@ pickButton = CreateResourcePickerButton(container, null, i, 0, "Pick");
  191. SubscribeToEvent(pickButton, "Released", "PickMaterialTexture");
  192. Button@ openButton = CreateResourcePickerButton(container, null, i, 0, "Open");
  193. SubscribeToEvent(openButton, "Released", "OpenResource");
  194. if (editMaterial !is null)
  195. {
  196. Texture@ texture = editMaterial.textures[i];
  197. if (texture !is null)
  198. nameEdit.text = texture.name;
  199. }
  200. SubscribeToEvent(nameEdit, "TextFinished", "EditMaterialTexture");
  201. }
  202. }
  203. else
  204. {
  205. for (uint i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
  206. {
  207. LineEdit@ nameEdit = materialWindow.GetChild("TextureNameEdit" + String(i), true);
  208. if (nameEdit is null)
  209. continue;
  210. String textureName;
  211. if (editMaterial !is null)
  212. {
  213. Texture@ texture = editMaterial.textures[i];
  214. if (texture !is null)
  215. textureName = texture.name;
  216. }
  217. nameEdit.text = textureName;
  218. }
  219. }
  220. }
  221. void RefreshMaterialShaderParameters()
  222. {
  223. ListView@ list = materialWindow.GetChild("ShaderParameterList", true);
  224. list.RemoveAllItems();
  225. if (editMaterial is null)
  226. return;
  227. Array<String>@ parameterNames = editMaterial.shaderParameterNames;
  228. for (uint i = 0; i < parameterNames.length; ++i)
  229. {
  230. VariantType type = editMaterial.shaderParameters[parameterNames[i]].type;
  231. Variant value = editMaterial.shaderParameters[parameterNames[i]];
  232. UIElement@ parent = CreateAttributeEditorParent(list, parameterNames[i], 0, 0);
  233. uint numCoords = type - VAR_FLOAT + 1;
  234. Array<String> coordValues = value.ToString().Split(' ');
  235. for (uint j = 0; j < numCoords; ++j)
  236. {
  237. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, null, 0, 0);
  238. attrEdit.vars["Coordinate"] = j;
  239. attrEdit.vars["Name"] = parameterNames[i];
  240. attrEdit.text = coordValues[j];
  241. SubscribeToEvent(attrEdit, "TextChanged", "EditShaderParameter");
  242. SubscribeToEvent(attrEdit, "TextFinished", "EditShaderParameter");
  243. }
  244. }
  245. }
  246. void RefreshMaterialMiscParameters()
  247. {
  248. if (editMaterial is null)
  249. return;
  250. BiasParameters bias = editMaterial.depthBias;
  251. inMaterialRefresh = true;
  252. LineEdit@ attrEdit = materialWindow.GetChild("ConstantBiasEdit", true);
  253. attrEdit.text = String(bias.constantBias);
  254. attrEdit = materialWindow.GetChild("SlopeBiasEdit", true);
  255. attrEdit.text = String(bias.slopeScaledBias);
  256. DropDownList@ attrList = materialWindow.GetChild("CullModeEdit", true);
  257. attrList.selection = editMaterial.cullMode;
  258. attrList = materialWindow.GetChild("ShadowCullModeEdit", true);
  259. attrList.selection = editMaterial.shadowCullMode;
  260. inMaterialRefresh = false;
  261. }
  262. void RotateMaterialPreview(StringHash eventType, VariantMap& eventData)
  263. {
  264. int elemX = eventData["ElementX"].GetInt();
  265. int elemY = eventData["ElementY"].GetInt();
  266. if (materialPreview.height > 0 && materialPreview.width > 0)
  267. {
  268. float yaw = ((materialPreview.height / 2) - elemY) * (90.0 / materialPreview.height);
  269. float pitch = ((materialPreview.width / 2) - elemX) * (90.0 / materialPreview.width);
  270. previewModelNode.rotation = previewModelNode.rotation.Slerp(Quaternion(yaw, pitch, 0), 0.1);
  271. materialPreview.QueueUpdate();
  272. }
  273. }
  274. void EditMaterialName(StringHash eventType, VariantMap& eventData)
  275. {
  276. LineEdit@ nameEdit = eventData["Element"].GetUIElement();
  277. String newMaterialName = nameEdit.text.Trimmed();
  278. if (!newMaterialName.empty)
  279. {
  280. Material@ newMaterial = cache.GetResource("Material", newMaterialName);
  281. if (newMaterial !is null)
  282. EditMaterial(newMaterial);
  283. }
  284. }
  285. void PickEditMaterial()
  286. {
  287. @resourcePicker = GetResourcePicker(ShortStringHash("Material"));
  288. if (resourcePicker is null)
  289. return;
  290. String lastPath = resourcePicker.lastPath;
  291. if (lastPath.empty)
  292. lastPath = sceneResourcePath;
  293. CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  294. SubscribeToEvent(uiFileSelector, "FileSelected", "PickEditMaterialDone");
  295. }
  296. void PickEditMaterialDone(StringHash eventType, VariantMap& eventData)
  297. {
  298. StoreResourcePickerPath();
  299. CloseFileSelector();
  300. if (!eventData["OK"].GetBool())
  301. {
  302. @resourcePicker = null;
  303. return;
  304. }
  305. String resourceName = eventData["FileName"].GetString();
  306. Resource@ res = GetPickedResource(resourceName);
  307. if (res !is null)
  308. EditMaterial(cast<Material>(res));
  309. @resourcePicker = null;
  310. }
  311. void NewMaterial()
  312. {
  313. EditMaterial(Material());
  314. }
  315. void RevertMaterial()
  316. {
  317. if (editMaterial is null)
  318. return;
  319. BeginMaterialEdit();
  320. cache.ReloadResource(editMaterial);
  321. EndMaterialEdit();
  322. RefreshMaterialEditor();
  323. }
  324. void SaveMaterial()
  325. {
  326. if (editMaterial is null || editMaterial.name.empty)
  327. return;
  328. String fullName = cache.GetResourceFileName(editMaterial.name);
  329. if (fullName.empty)
  330. return;
  331. File saveFile(fullName, FILE_WRITE);
  332. editMaterial.Save(saveFile);
  333. }
  334. void SaveMaterialAs()
  335. {
  336. if (editMaterial is null)
  337. return;
  338. @resourcePicker = GetResourcePicker(ShortStringHash("Material"));
  339. if (resourcePicker is null)
  340. return;
  341. String lastPath = resourcePicker.lastPath;
  342. if (lastPath.empty)
  343. lastPath = sceneResourcePath;
  344. CreateFileSelector("Save material as", "Save", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  345. SubscribeToEvent(uiFileSelector, "FileSelected", "SaveMaterialAsDone");
  346. }
  347. void SaveMaterialAsDone(StringHash eventType, VariantMap& eventData)
  348. {
  349. StoreResourcePickerPath();
  350. CloseFileSelector();
  351. @resourcePicker = null;
  352. if (editMaterial is null)
  353. return;
  354. if (!eventData["OK"].GetBool())
  355. {
  356. @resourcePicker = null;
  357. return;
  358. }
  359. String fullName = eventData["FileName"].GetString();
  360. File saveFile(fullName, FILE_WRITE);
  361. if (editMaterial.Save(saveFile))
  362. {
  363. saveFile.Close();
  364. // Load the new resource to update the name in the editor
  365. Material@ newMat = cache.GetResource("Material", GetResourceNameFromFullName(fullName));
  366. if (newMat !is null)
  367. EditMaterial(newMat);
  368. }
  369. }
  370. void EditShaderParameter(StringHash eventType, VariantMap& eventData)
  371. {
  372. if (editMaterial is null)
  373. return;
  374. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  375. uint coordinate = attrEdit.vars["Coordinate"].GetUInt();
  376. String name = attrEdit.vars["Name"].GetString();
  377. Variant oldValue = editMaterial.shaderParameters[name];
  378. Array<String> coordValues = oldValue.ToString().Split(' ');
  379. coordValues[coordinate] = String(attrEdit.text.ToFloat());
  380. String valueString;
  381. for (uint i = 0; i < coordValues.length; ++i)
  382. {
  383. valueString += coordValues[i];
  384. valueString += " ";
  385. }
  386. Variant newValue;
  387. newValue.FromString(oldValue.type, valueString);
  388. BeginMaterialEdit();
  389. editMaterial.shaderParameters[name] = newValue;
  390. EndMaterialEdit();
  391. }
  392. void CreateShaderParameter(StringHash eventType, VariantMap& eventData)
  393. {
  394. if (editMaterial is null)
  395. return;
  396. LineEdit@ nameEdit = materialWindow.GetChild("ParameterNameEdit", true);
  397. String newName = nameEdit.text.Trimmed();
  398. if (newName.empty)
  399. return;
  400. DropDownList@ dropDown = eventData["Element"].GetUIElement();
  401. Variant newValue;
  402. switch (dropDown.selection)
  403. {
  404. case 0:
  405. newValue = float(0);
  406. break;
  407. case 1:
  408. newValue = Vector2(0, 0);
  409. break;
  410. case 2:
  411. newValue = Vector3(0, 0, 0);
  412. break;
  413. case 3:
  414. newValue = Vector4(0, 0, 0, 0);
  415. break;
  416. }
  417. BeginMaterialEdit();
  418. editMaterial.shaderParameters[newName] = newValue;
  419. EndMaterialEdit();
  420. RefreshMaterialShaderParameters();
  421. }
  422. void DeleteShaderParameter()
  423. {
  424. if (editMaterial is null)
  425. return;
  426. LineEdit@ nameEdit = materialWindow.GetChild("ParameterNameEdit", true);
  427. String name = nameEdit.text.Trimmed();
  428. if (name.empty)
  429. return;
  430. BeginMaterialEdit();
  431. editMaterial.RemoveShaderParameter(name);
  432. EndMaterialEdit();
  433. RefreshMaterialShaderParameters();
  434. }
  435. void PickMaterialTexture(StringHash eventType, VariantMap& eventData)
  436. {
  437. if (editMaterial is null)
  438. return;
  439. UIElement@ button = eventData["Element"].GetUIElement();
  440. resourcePickIndex = button.vars["Index"].GetUInt();
  441. @resourcePicker = GetResourcePicker(ShortStringHash("Texture2D"));
  442. if (resourcePicker is null)
  443. return;
  444. String lastPath = resourcePicker.lastPath;
  445. if (lastPath.empty)
  446. lastPath = sceneResourcePath;
  447. CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  448. SubscribeToEvent(uiFileSelector, "FileSelected", "PickMaterialTextureDone");
  449. }
  450. void PickMaterialTextureDone(StringHash eventType, VariantMap& eventData)
  451. {
  452. StoreResourcePickerPath();
  453. CloseFileSelector();
  454. if (!eventData["OK"].GetBool())
  455. {
  456. @resourcePicker = null;
  457. return;
  458. }
  459. String resourceName = eventData["FileName"].GetString();
  460. Resource@ res = GetPickedResource(resourceName);
  461. if (res !is null && editMaterial !is null)
  462. {
  463. BeginMaterialEdit();
  464. editMaterial.textures[resourcePickIndex] = res;
  465. EndMaterialEdit();
  466. RefreshMaterialTextures(false);
  467. }
  468. @resourcePicker = null;
  469. }
  470. void EditMaterialTexture(StringHash eventType, VariantMap& eventData)
  471. {
  472. if (editMaterial is null)
  473. return;
  474. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  475. String textureName = attrEdit.text.Trimmed();
  476. uint index = attrEdit.vars["Index"].GetUInt();
  477. BeginMaterialEdit();
  478. if (!textureName.empty)
  479. {
  480. Texture@ texture = cache.GetResource(GetExtension(textureName) == ".xml" ? "TextureCube" : "Texture2D", textureName);
  481. editMaterial.textures[index] = texture;
  482. }
  483. else
  484. editMaterial.textures[index] = null;
  485. EndMaterialEdit();
  486. }
  487. void NewTechnique()
  488. {
  489. if (editMaterial is null)
  490. return;
  491. BeginMaterialEdit();
  492. editMaterial.numTechniques = editMaterial.numTechniques + 1;
  493. EndMaterialEdit();
  494. RefreshMaterialTechniques();
  495. }
  496. void DeleteTechnique()
  497. {
  498. if (editMaterial is null || editMaterial.numTechniques < 2)
  499. return;
  500. BeginMaterialEdit();
  501. editMaterial.numTechniques = editMaterial.numTechniques - 1;
  502. EndMaterialEdit();
  503. RefreshMaterialTechniques();
  504. }
  505. void PickMaterialTechnique(StringHash eventType, VariantMap& eventData)
  506. {
  507. if (editMaterial is null)
  508. return;
  509. UIElement@ button = eventData["Element"].GetUIElement();
  510. resourcePickIndex = button.vars["Index"].GetUInt();
  511. @resourcePicker = GetResourcePicker(ShortStringHash("Technique"));
  512. if (resourcePicker is null)
  513. return;
  514. String lastPath = resourcePicker.lastPath;
  515. if (lastPath.empty)
  516. lastPath = sceneResourcePath;
  517. CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  518. SubscribeToEvent(uiFileSelector, "FileSelected", "PickMaterialTechniqueDone");
  519. }
  520. void PickMaterialTechniqueDone(StringHash eventType, VariantMap& eventData)
  521. {
  522. StoreResourcePickerPath();
  523. CloseFileSelector();
  524. if (!eventData["OK"].GetBool())
  525. {
  526. @resourcePicker = null;
  527. return;
  528. }
  529. String resourceName = eventData["FileName"].GetString();
  530. Resource@ res = GetPickedResource(resourceName);
  531. if (res !is null && editMaterial !is null)
  532. {
  533. BeginMaterialEdit();
  534. TechniqueEntry entry = editMaterial.techniqueEntries[resourcePickIndex];
  535. editMaterial.SetTechnique(resourcePickIndex, res, entry.qualityLevel, entry.lodDistance);
  536. EndMaterialEdit();
  537. RefreshMaterialTechniques(false);
  538. }
  539. @resourcePicker = null;
  540. }
  541. void EditMaterialTechnique(StringHash eventType, VariantMap& eventData)
  542. {
  543. if (editMaterial is null)
  544. return;
  545. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  546. String techniqueName = attrEdit.text.Trimmed();
  547. uint index = attrEdit.vars["Index"].GetUInt();
  548. BeginMaterialEdit();
  549. Technique@ newTech;
  550. if (!techniqueName.empty)
  551. newTech = cache.GetResource("Technique", techniqueName);
  552. TechniqueEntry entry = editMaterial.techniqueEntries[index];
  553. editMaterial.SetTechnique(index, newTech, entry.qualityLevel, entry.lodDistance);
  554. EndMaterialEdit();
  555. }
  556. void EditTechniqueQuality(StringHash eventType, VariantMap& eventData)
  557. {
  558. if (editMaterial is null)
  559. return;
  560. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  561. uint newQualityLevel = attrEdit.text.ToUInt();
  562. uint index = attrEdit.vars["Index"].GetUInt();
  563. BeginMaterialEdit();
  564. TechniqueEntry entry = editMaterial.techniqueEntries[index];
  565. editMaterial.SetTechnique(index, entry.technique, newQualityLevel, entry.lodDistance);
  566. EndMaterialEdit();
  567. }
  568. void EditTechniqueLodDistance(StringHash eventType, VariantMap& eventData)
  569. {
  570. if (editMaterial is null)
  571. return;
  572. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  573. float newLodDistance = attrEdit.text.ToFloat();
  574. uint index = attrEdit.vars["Index"].GetUInt();
  575. BeginMaterialEdit();
  576. TechniqueEntry entry = editMaterial.techniqueEntries[index];
  577. editMaterial.SetTechnique(index, entry.technique, entry.qualityLevel, newLodDistance);
  578. EndMaterialEdit();
  579. }
  580. void SortTechniques()
  581. {
  582. if (editMaterial is null)
  583. return;
  584. BeginMaterialEdit();
  585. editMaterial.SortTechniques();
  586. EndMaterialEdit();
  587. RefreshMaterialTechniques();
  588. }
  589. void EditConstantBias(StringHash eventType, VariantMap& eventData)
  590. {
  591. if (editMaterial is null || inMaterialRefresh)
  592. return;
  593. BeginMaterialEdit();
  594. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  595. BiasParameters bias = editMaterial.depthBias;
  596. bias.constantBias = attrEdit.text.ToFloat();
  597. editMaterial.depthBias = bias;
  598. EndMaterialEdit();
  599. }
  600. void EditSlopeBias(StringHash eventType, VariantMap& eventData)
  601. {
  602. if (editMaterial is null || inMaterialRefresh)
  603. return;
  604. BeginMaterialEdit();
  605. LineEdit@ attrEdit = eventData["Element"].GetUIElement();
  606. BiasParameters bias = editMaterial.depthBias;
  607. bias.slopeScaledBias = attrEdit.text.ToFloat();
  608. editMaterial.depthBias = bias;
  609. EndMaterialEdit();
  610. }
  611. void EditCullMode(StringHash eventType, VariantMap& eventData)
  612. {
  613. if (editMaterial is null || inMaterialRefresh)
  614. return;
  615. BeginMaterialEdit();
  616. DropDownList@ attrEdit = eventData["Element"].GetUIElement();
  617. editMaterial.cullMode = CullMode(attrEdit.selection);
  618. EndMaterialEdit();
  619. }
  620. void EditShadowCullMode(StringHash eventType, VariantMap& eventData)
  621. {
  622. if (editMaterial is null || inMaterialRefresh)
  623. return;
  624. BeginMaterialEdit();
  625. DropDownList@ attrEdit = eventData["Element"].GetUIElement();
  626. editMaterial.shadowCullMode = CullMode(attrEdit.selection);
  627. EndMaterialEdit();
  628. }
  629. void BeginMaterialEdit()
  630. {
  631. if (editMaterial is null)
  632. return;
  633. oldMaterialState = XMLFile();
  634. XMLElement materialElem = oldMaterialState.CreateRoot("material");
  635. editMaterial.Save(materialElem);
  636. }
  637. void EndMaterialEdit()
  638. {
  639. if (editMaterial is null)
  640. return;
  641. EditMaterialAction@ action = EditMaterialAction();
  642. action.Define(editMaterial, oldMaterialState);
  643. SaveEditAction(action);
  644. materialPreview.QueueUpdate();
  645. }