|
3 | 3 | using UnityEngine; |
4 | 4 | using UnityEngine.UIElements; |
5 | 5 | using CommonVars; |
| 6 | +using System.IO; |
| 7 | +using System.Xml; |
| 8 | +using System.Xml.Serialization; |
6 | 9 |
|
7 | 10 | #if UNITY_EDITOR |
8 | 11 | #if UNITY_2021 |
9 | 12 | #else |
10 | 13 | using UnityEditor.UIElements; |
11 | 14 | using UnityEditor; |
12 | 15 | namespace TrueTrace { |
| 16 | + |
| 17 | + |
| 18 | + public class SavePopup2 : PopupWindowContent |
| 19 | + { |
| 20 | + string PresetName = "Null"; |
| 21 | + RayTracingObject ThisOBJ; |
| 22 | + int SaveIndex; |
| 23 | + |
| 24 | + public SavePopup2(RayTracingObject ThisOBJ, int SaveIndex) { |
| 25 | + this.ThisOBJ = ThisOBJ; |
| 26 | + this.SaveIndex = SaveIndex; |
| 27 | + } |
| 28 | + public override Vector2 GetWindowSize() |
| 29 | + { |
| 30 | + return new Vector2(460, 50); |
| 31 | + } |
| 32 | + |
| 33 | + public override void OnGUI(Rect rect) { |
| 34 | + // Debug.Log("ONINSPECTORGUI"); |
| 35 | + |
| 36 | + PresetName = GUILayout.TextField(PresetName, 32); |
| 37 | + |
| 38 | + if(GUILayout.Button("Save Preset")) { |
| 39 | + RayObjs PresetRays; |
| 40 | + int CopyIndex = -1; |
| 41 | + UnityEditor.AssetDatabase.Refresh(); |
| 42 | + using (var A = new StringReader(Resources.Load<TextAsset>("Utility/MaterialPresets").text)) { |
| 43 | + var serializer = new XmlSerializer(typeof(RayObjs)); |
| 44 | + PresetRays = serializer.Deserialize(A) as RayObjs; |
| 45 | + int RayReadCount = PresetRays.RayObj.Count; |
| 46 | + for(int i = 0; i < RayReadCount; i++) { |
| 47 | + if(PresetRays.RayObj[i].MatName.Equals(PresetName)) { |
| 48 | + CopyIndex = i; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + RayObjectDatas TempRay = new RayObjectDatas() { |
| 54 | + ID = 0, |
| 55 | + MatName = PresetName, |
| 56 | + OptionID = (int)ThisOBJ.MaterialOptions[SaveIndex], |
| 57 | + TransCol = ThisOBJ.TransmissionColor[SaveIndex], |
| 58 | + BaseCol = ThisOBJ.BaseColor[SaveIndex], |
| 59 | + MetRemap = ThisOBJ.MetallicRemap[SaveIndex], |
| 60 | + RoughRemap = ThisOBJ.RoughnessRemap[SaveIndex], |
| 61 | + Emiss = ThisOBJ.emmission[SaveIndex], |
| 62 | + EmissCol = ThisOBJ.EmissionColor[SaveIndex], |
| 63 | + Rough = ThisOBJ.Roughness[SaveIndex], |
| 64 | + IOR = ThisOBJ.IOR[SaveIndex], |
| 65 | + Met = ThisOBJ.Metallic[SaveIndex], |
| 66 | + SpecTint = ThisOBJ.SpecularTint[SaveIndex], |
| 67 | + Sheen = ThisOBJ.Sheen[SaveIndex], |
| 68 | + SheenTint = ThisOBJ.SheenTint[SaveIndex], |
| 69 | + Clearcoat = ThisOBJ.ClearCoat[SaveIndex], |
| 70 | + ClearcoatGloss = ThisOBJ.ClearCoatGloss[SaveIndex], |
| 71 | + Anisotropic = ThisOBJ.Anisotropic[SaveIndex], |
| 72 | + Flatness = ThisOBJ.Flatness[SaveIndex], |
| 73 | + DiffTrans = ThisOBJ.DiffTrans[SaveIndex], |
| 74 | + SpecTrans = ThisOBJ.SpecTrans[SaveIndex], |
| 75 | + FollowMat = ThisOBJ.FollowMaterial[SaveIndex], |
| 76 | + ScatterDist = ThisOBJ.ScatterDist[SaveIndex], |
| 77 | + Spec = ThisOBJ.Specular[SaveIndex], |
| 78 | + AlphaCutoff = ThisOBJ.AlphaCutoff[SaveIndex], |
| 79 | + NormStrength = ThisOBJ.NormalStrength[SaveIndex], |
| 80 | + Hue = ThisOBJ.Hue[SaveIndex], |
| 81 | + Brightness = ThisOBJ.Brightness[SaveIndex], |
| 82 | + Contrast = ThisOBJ.Contrast[SaveIndex], |
| 83 | + Saturation = ThisOBJ.Saturation[SaveIndex], |
| 84 | + BlendColor = ThisOBJ.BlendColor[SaveIndex], |
| 85 | + BlendFactor = ThisOBJ.BlendFactor[SaveIndex], |
| 86 | + MainTexScaleOffset = ThisOBJ.MainTexScaleOffset[SaveIndex], |
| 87 | + SecondaryTextureScale = ThisOBJ.SecondaryTextureScale[SaveIndex], |
| 88 | + Rotation = ThisOBJ.Rotation[SaveIndex], |
| 89 | + Flags = ThisOBJ.Flags[SaveIndex] |
| 90 | + }; |
| 91 | + if(CopyIndex != -1) PresetRays.RayObj[CopyIndex] = TempRay; |
| 92 | + else PresetRays.RayObj.Add(TempRay); |
| 93 | + |
| 94 | + using(StreamWriter writer = new StreamWriter(Application.dataPath + "/TrueTrace/Resources/Utility/MaterialPresets.xml")) { |
| 95 | + var serializer = new XmlSerializer(typeof(RayObjs)); |
| 96 | + serializer.Serialize(writer.BaseStream, PresetRays); |
| 97 | + UnityEditor.AssetDatabase.Refresh(); |
| 98 | + } |
| 99 | + this.editorWindow.Close(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + } |
| 104 | + } |
| 105 | + public class LoadPopup2 : PopupWindowContent |
| 106 | + { |
| 107 | + RayObjs PresetRays; |
| 108 | + Vector2 ScrollPosition; |
| 109 | + MasterMaterialEditor SourceWindow; |
| 110 | + public LoadPopup2(MasterMaterialEditor editor) { |
| 111 | + this.SourceWindow = editor; |
| 112 | + } |
| 113 | + private void CallEditorFunction(RayObjectDatas RayObj) { |
| 114 | + if(SourceWindow != null) { |
| 115 | + SourceWindow.LoadFunction(RayObj); |
| 116 | + } |
| 117 | + } |
| 118 | + public override Vector2 GetWindowSize() |
| 119 | + { |
| 120 | + return new Vector2(460, 250); |
| 121 | + } |
| 122 | + |
| 123 | + public override void OnGUI(Rect rect) { |
| 124 | + UnityEditor.AssetDatabase.Refresh(); |
| 125 | + using (var A = new StringReader(Resources.Load<TextAsset>("Utility/MaterialPresets").text)) { |
| 126 | + var serializer = new XmlSerializer(typeof(RayObjs)); |
| 127 | + PresetRays = serializer.Deserialize(A) as RayObjs; |
| 128 | + } |
| 129 | + int PresetLength = PresetRays.RayObj.Count; |
| 130 | + ScrollPosition = GUILayout.BeginScrollView(ScrollPosition, GUILayout.Width(460), GUILayout.Height(250)); |
| 131 | + for(int i = 0; i < PresetLength; i++) { |
| 132 | + GUILayout.BeginHorizontal(); |
| 133 | + if(GUILayout.Button(PresetRays.RayObj[i].MatName)) {CallEditorFunction(PresetRays.RayObj[i]); this.editorWindow.Close();} |
| 134 | + if(GUILayout.Button("Delete")) { |
| 135 | + PresetRays.RayObj.RemoveAt(i); |
| 136 | + using(StreamWriter writer = new StreamWriter(Application.dataPath + "/TrueTrace/Resources/Utility/MaterialPresets.xml")) { |
| 137 | + var serializer = new XmlSerializer(typeof(RayObjs)); |
| 138 | + serializer.Serialize(writer.BaseStream, PresetRays); |
| 139 | + UnityEditor.AssetDatabase.Refresh(); |
| 140 | + } |
| 141 | + OnGUI(new Rect(0,0,100,10)); |
| 142 | + } |
| 143 | + GUILayout.EndHorizontal(); |
| 144 | + } |
| 145 | + GUILayout.EndScrollView(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + |
13 | 151 | public class MasterMaterialEditor : EditorWindow { |
14 | 152 | [MenuItem("TrueTrace/Master Material Editor")] |
15 | 153 | public static void ShowWindow() { |
16 | 154 | GetWindow<MasterMaterialEditor>("Material Editor"); |
17 | 155 | } |
| 156 | + int Selected2; |
| 157 | + RayTracingObject TempRTO2; |
| 158 | + public void LoadFunction(RayObjectDatas RayObj) { |
| 159 | + TempRTO2.MaterialOptions[Selected2] = (RayTracingObject.Options)RayObj.OptionID; |
| 160 | + TempRTO2.TransmissionColor[Selected2] = RayObj.TransCol; |
| 161 | + TempRTO2.BaseColor[Selected2] = RayObj.BaseCol; |
| 162 | + TempRTO2.MetallicRemap[Selected2] = RayObj.MetRemap; |
| 163 | + TempRTO2.RoughnessRemap[Selected2] = RayObj.RoughRemap; |
| 164 | + TempRTO2.emmission[Selected2] = RayObj.Emiss; |
| 165 | + TempRTO2.EmissionColor[Selected2] = RayObj.EmissCol; |
| 166 | + TempRTO2.Roughness[Selected2] = RayObj.Rough; |
| 167 | + TempRTO2.IOR[Selected2] = RayObj.IOR; |
| 168 | + TempRTO2.Metallic[Selected2] = RayObj.Met; |
| 169 | + TempRTO2.SpecularTint[Selected2] = RayObj.SpecTint; |
| 170 | + TempRTO2.Sheen[Selected2] = RayObj.Sheen; |
| 171 | + TempRTO2.SheenTint[Selected2] = RayObj.SheenTint; |
| 172 | + TempRTO2.ClearCoat[Selected2] = RayObj.Clearcoat; |
| 173 | + TempRTO2.ClearCoatGloss[Selected2] = RayObj.ClearcoatGloss; |
| 174 | + TempRTO2.Anisotropic[Selected2] = RayObj.Anisotropic; |
| 175 | + TempRTO2.Flatness[Selected2] = RayObj.Flatness; |
| 176 | + TempRTO2.DiffTrans[Selected2] = RayObj.DiffTrans; |
| 177 | + TempRTO2.SpecTrans[Selected2] = RayObj.SpecTrans; |
| 178 | + TempRTO2.FollowMaterial[Selected2] = RayObj.FollowMat; |
| 179 | + TempRTO2.ScatterDist[Selected2] = RayObj.ScatterDist; |
| 180 | + TempRTO2.Specular[Selected2] = RayObj.Spec; |
| 181 | + TempRTO2.AlphaCutoff[Selected2] = RayObj.AlphaCutoff; |
| 182 | + TempRTO2.NormalStrength[Selected2] = RayObj.NormStrength; |
| 183 | + TempRTO2.Hue[Selected2] = RayObj.Hue; |
| 184 | + TempRTO2.Brightness[Selected2] = RayObj.Brightness; |
| 185 | + TempRTO2.Contrast[Selected2] = RayObj.Contrast; |
| 186 | + TempRTO2.Saturation[Selected2] = RayObj.Saturation; |
| 187 | + TempRTO2.BlendColor[Selected2] = RayObj.BlendColor; |
| 188 | + TempRTO2.BlendFactor[Selected2] = RayObj.BlendFactor; |
| 189 | + TempRTO2.MainTexScaleOffset[Selected2] = RayObj.MainTexScaleOffset; |
| 190 | + TempRTO2.SecondaryTextureScale[Selected2] = RayObj.SecondaryTextureScale; |
| 191 | + TempRTO2.Rotation[Selected2] = RayObj.Rotation; |
| 192 | + TempRTO2.Flags[Selected2] = RayObj.Flags; |
| 193 | + TempRTO2.CallMaterialEdited(true); |
| 194 | + } |
| 195 | + |
| 196 | + |
18 | 197 |
|
19 | 198 | private List<TreeViewItemData<string>> TraverseChildren(Transform t, ref int id, int depth) { |
20 | 199 | List<TreeViewItemData<string>> TempList = new List<TreeViewItemData<string>>(); |
@@ -258,11 +437,11 @@ public void CreateGUI() { |
258 | 437 | MiscBar1.style.height = 20; |
259 | 438 |
|
260 | 439 | Slider RoughnessSlider = new Slider() {label = RTO.Flags[Selected].GetFlag(CommonFunctions.Flags.UseSmoothness) ? "Smoothness: " : "Roughness: ", value = RTO.Roughness[Selected], highValue = 1.0f, lowValue = 0}; |
261 | | - RoughnessSlider.RegisterValueChangedCallback(evt => {RTO.Roughness[Selected] = evt.newValue; RTO.CallMaterialEdited(true);}); |
| 440 | + RoughnessSlider.RegisterValueChangedCallback(evt => {RTO.Roughness[Selected] = (RTO.Flags[Selected].GetFlag(CommonFunctions.Flags.UseSmoothness) ? (1.0f - evt.newValue) : evt.newValue); RTO.CallMaterialEdited(true);}); |
262 | 441 | RoughnessSlider.style.width = 350; |
263 | 442 | RoughnessSlider.showInputField = true; |
264 | 443 | Toggle IsSmoothness = new Toggle("Use Smoothness: ") {value = RTO.Flags[Selected].GetFlag(CommonFunctions.Flags.UseSmoothness)}; |
265 | | - IsSmoothness.RegisterValueChangedCallback(evt => {RTO.Flags[Selected] = CommonFunctions.SetFlagVar(RTO.Flags[Selected], CommonFunctions.Flags.UseSmoothness, evt.newValue); if(evt.newValue) RoughnessSlider.label = "Smoothness: "; else RoughnessSlider.label = "Roughness";RTO.CallMaterialEdited(true);}); |
| 444 | + IsSmoothness.RegisterValueChangedCallback(evt => {RTO.Flags[Selected] = CommonFunctions.SetFlagVar(RTO.Flags[Selected], CommonFunctions.Flags.UseSmoothness, evt.newValue); if(evt.newValue) RoughnessSlider.label = "Smoothness: "; else RoughnessSlider.label = "Roughness";RTO.CallMaterialEdited(true); RTO.Roughness[Selected] = 1.0f - RTO.Roughness[Selected];}); |
266 | 445 |
|
267 | 446 | Toggle InvertSmoothTex = new Toggle("Invert Roughness Tex: ") {value = RTO.Flags[Selected].GetFlag(CommonFunctions.Flags.InvertSmoothnessTexture)}; |
268 | 447 | InvertSmoothTex.RegisterValueChangedCallback(evt => {RTO.Flags[Selected] = CommonFunctions.SetFlagVar(RTO.Flags[Selected], CommonFunctions.Flags.InvertSmoothnessTexture, evt.newValue);RTO.CallMaterialEdited(true);}); |
@@ -425,19 +604,23 @@ public void CreateGUI() { |
425 | 604 | RotationSlider.style.width = 350; |
426 | 605 | RotationSlider.showInputField = true; |
427 | 606 |
|
| 607 | + Button SavePresetButton = new Button(() => UnityEditor.PopupWindow.Show(new Rect(0,0,10,10), new SavePopup2(RTO, Selected))) {text = "Save Material Preset"}; |
| 608 | + Button LoadPresetButton = new Button(() => {TempRTO2 = RTO; Selected2 = Selected; UnityEditor.PopupWindow.Show(new Rect(0,0,100,10), new LoadPopup2(this));}){text = "Load Material Preset"}; |
428 | 609 |
|
429 | 610 | TempWindow.Add(new Label(RTO.name)); |
| 611 | + TempWindow.Add(SavePresetButton); |
| 612 | + TempWindow.Add(LoadPresetButton); |
430 | 613 | TempWindow.Add(MatTypePopup); |
431 | 614 | TempWindow.Add(ColField); |
432 | 615 | TempWindow.Add(EmissField); |
433 | 616 | TempWindow.Add(EmissColField); |
434 | 617 | TempWindow.Add(EmissionBar); |
435 | 618 | TempWindow.Add(MiscBar1); |
436 | 619 | TempWindow.Add(MiscBar2); |
437 | | - TempWindow.Add(RoughnessSlider); |
438 | | - TempWindow.Add(RoughnessMinMax); |
439 | 620 | TempWindow.Add(MetallicSlider); |
440 | 621 | TempWindow.Add(MetallicMinMax); |
| 622 | + TempWindow.Add(RoughnessSlider); |
| 623 | + TempWindow.Add(RoughnessMinMax); |
441 | 624 |
|
442 | 625 | TempWindow.Add(IORSlider); |
443 | 626 | TempWindow.Add(SpecularSlider); |
|
0 commit comments