using System; using System.Collections.Generic; using System.Linq; using System.Text; using HarmonyLib; using Newtonsoft.Json.Linq; using Sandbox.Game.World; using Sandbox.Graphics.GUI; using SpaceEngineers.Game.GUI; using VRage.Game; using VRage.Utils; using VRageMath; namespace XFE.SeAgent.Plugin.Graphics { internal sealed partial class RayTracing { private SettingsScreen settingsScreen; private void InstallSettingsMenu() => harmony.Patch( AccessTools.DeclaredMethod(typeof(MyGuiScreenOptionsGraphics),"RecreateControls"), postfix:new HarmonyMethod(typeof(RayTracing),nameof(AfterGraphicsControls))); private static void AfterGraphicsControls(MyGuiScreenOptionsGraphics __instance,bool constructor) { if(!constructor || active==null)return; var button=new MyGuiControlButton(position:new Vector2(.172f,-.437f),text:new StringBuilder("光追 / 路径追踪"),textScale:.65f, onButtonClick: _=>active?.OpenSettings()) {Name="XFE_RayTracingSettings",Size=new Vector2(.25f,.042f)}; __instance.Controls.Add(button); } private void OpenSettings() { if(settingsScreen!=null)return; settingsScreen=new SettingsScreen(this); settingsScreen.Closed+=(screen,unloading)=>settingsScreen=null; MyGuiSandbox.AddScreen(settingsScreen); } // Bounded automation of these exact native controls for integration tests. // It uses the same draft/apply/cancel callbacks as the in-game menu. public JObject SettingsMenu(JObject args) { string action=(string)args["action"]??"status"; if(action=="openGraphics")MyGuiSandbox.AddScreen(new MyGuiScreenOptionsGraphics()); else if(action=="open")OpenSettings(); else if(action=="set") { if(settingsScreen==null)throw new InvalidOperationException("Settings menu is not open."); settingsScreen.SetValues(args["values"] as JObject??throw new ArgumentException("values object required")); } else if(action=="apply") { if(settingsScreen==null)throw new InvalidOperationException("Settings menu is not open."); settingsScreen.Apply(); } else if(action=="cancel")settingsScreen?.CloseScreenNow(); else if(action=="closeGraphics") { foreach(var s in MyScreenManager.Screens.ToArray())if(s is MyGuiScreenOptionsGraphics)s.CloseScreenNow(); } else if(action!="status")throw new ArgumentException("Unknown graphics menu action."); return new JObject { ["open"]=settingsScreen!=null,["draft"]=settingsScreen?.Values(),["controls"]=settingsScreen?.Layout(),["error"]=settingsScreen?.Error, ["graphicsEntryPresent"]=MyScreenManager.Screens.OfType().Any(s=>s.Controls.Any(c=>c.Name=="XFE_RayTracingSettings")) }; } private sealed class SettingsScreen:MyGuiScreenBase { private readonly RayTracing owner; private readonly Dictionary fields=new Dictionary(); private readonly MyGuiControlLabel error; private int row; public string Error {get;private set;} public override string GetFriendlyName()=>"XFE Ray Tracing Graphics Settings"; public SettingsScreen(RayTracing owner):base(new Vector2(.5f,.5f),MyGuiConstants.SCREEN_BACKGROUND_COLOR,new Vector2(.8f,.94f),false) { this.owner=owner;EnabledBackgroundFade=true; AddCaption("光追与路径追踪",null,new Vector2(0,.006f)); var o=owner.options; Combo("mode","渲染模式",o.Enabled?(o.PathTracing?2:1):0,new[]{"关闭","混合光线追踪","路径追踪(近场实验)"}); Combo("fullResolution","射线分辨率",o.FullResolution?1:0,new[]{"半分辨率","原生分辨率"}); Combo("samples","路径追踪每帧采样",o.Samples-1,Enumerable.Range(1,8).Select(i=>i+" 个样本").ToArray()); Combo("bounces","最大反弹次数",o.Bounces-1,Enumerable.Range(1,8).Select(i=>i+" 次").ToArray()); Check("denoise","时域积累与空间去噪",o.Denoise); Check("glass","玻璃反射与折射",o.Glass); Check("voxels","小行星与地表体素",o.Voxels); Combo("voxelLod","体素精度",o.VoxelLod,new[]{"精细(1 米)","均衡(2 米)","快速(4 米)"}); Slider("radius","环境遮蔽范围(米)",o.Radius,.25f,12); Slider("strength","环境遮蔽强度",o.Strength,0,1); Slider("shadowStrength","接触阴影强度",o.ShadowStrength,0,.5f); Slider("reflections","混合 / 玻璃反射强度",o.Reflections,0,1); Slider("indirect","间接光强度",o.Indirect,0,1); Controls.Add(new MyGuiControlLabel(position:new Vector2(-.35f,.285f),text:"路径追踪替换近场不透明表面的光照;远景与天空保留原版。",textScale:.62f)); Controls.Add(new MyGuiControlLabel(position:new Vector2(-.35f,.315f),text:"主画面分辨率不变。原生射线、多采样与多反弹会增加显卡负载。",textScale:.62f)); error=new MyGuiControlLabel(position:new Vector2(-.35f,.345f),text:"",textScale:.6f);Controls.Add(error); Controls.Add(new MyGuiControlButton(position:new Vector2(-.14f,.402f),text:new StringBuilder("应用并保存"),onButtonClick:_=>Apply()){Name="XFE_Apply"}); Controls.Add(new MyGuiControlButton(position:new Vector2(.14f,.402f),text:new StringBuilder("取消"),onButtonClick:_=>CloseScreen()){Name="XFE_Cancel"}); ((MyGuiControlCombobox)fields["mode"]).ItemSelected+=RefreshEnabled; ((MyGuiControlCheckbox)fields["voxels"]).IsCheckedChanged=_=>RefreshEnabled();RefreshEnabled(); } private Vector2 PositionFor(string key,string caption,MyGuiControlBase control) { float y=-.345f+row++*.046f; Controls.Add(new MyGuiControlLabel(position:new Vector2(-.35f,y),text:caption,textScale:.72f,originAlign:MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER)); control.Name=key;fields.Add(key,control);Controls.Add(control);return new Vector2(.15f,y); } private void Combo(string key,string caption,int selected,string[] items) { var c=new MyGuiControlCombobox();c.Position=PositionFor(key,caption,c);c.Size=new Vector2(.34f,.04f); for(int i=0;i((MyGuiControlCombobox)fields[key]).GetSelectedKey(); private void RefreshEnabled() { bool enabled=Selected("mode")!=0,path=Selected("mode")==2; foreach(var f in fields)if(f.Key!="mode")f.Value.Enabled=enabled; fields["samples"].Enabled=enabled && path; fields["voxelLod"].Enabled=enabled && ((MyGuiControlCheckbox)fields["voxels"]).IsChecked; foreach(var key in new[]{"radius","strength","shadowStrength","indirect"})fields[key].Enabled=enabled && !path; } public JObject Values() { var v=new JObject();foreach(var field in fields) { if(field.Value is MyGuiControlCombobox c)v[field.Key]=c.GetSelectedKey(); else if(field.Value is MyGuiControlCheckbox b)v[field.Key]=b.IsChecked; else if(field.Value is MyGuiControlSlider s)v[field.Key]=s.Value; } return v; } public JArray Layout()=>new JArray(Controls.Select(c=>new JObject { ["name"]=c.Name,["enabled"]=c.Enabled, ["x"]=c.Position.X,["y"]=c.Position.Y,["width"]=c.Size.X,["height"]=c.Size.Y, ["text"]=(c as MyGuiControlLabel)?.Text })); public void SetValues(JObject values) { foreach(var p in values.Properties()) { if(!fields.TryGetValue(p.Name,out var field))throw new ArgumentException("Unknown control: "+p.Name); if(field is MyGuiControlCombobox c) {if(p.Value.Type!=JTokenType.Integer)throw new ArgumentException("Integer selection required");c.SelectItemByKey((long)p.Value);} else if(field is MyGuiControlCheckbox b)b.IsChecked=(bool)p.Value; else if(field is MyGuiControlSlider s)s.Value=(float)p.Value; } RefreshEnabled(); } public void Apply() { try { if(MySession.Static!=null && MySession.Static.OnlineMode!=MyOnlineModeEnum.OFFLINE)throw new InvalidOperationException("此实验功能目前仅支持离线世界。"); var v=Values();long mode=(long)v["mode"];v.Remove("mode"); v["enabled"]=mode!=0;v["pathTracing"]=mode==2;v["fullResolution"]=(long)v["fullResolution"]==1; v["samples"]=(long)v["samples"]+1;v["bounces"]=(long)v["bounces"]+1;v["persist"]=true;v["diagnostic"]=0; owner.Configure(v);CloseScreen(); }catch(Exception ex){Error=ex.Message;error.Text="无法应用:"+ex.Message;} } } } }