using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Linq; using Newtonsoft.Json.Linq; using Sandbox.Game.Entities; using Sandbox.Game.World; using VRage.Utils; using VRageMath; using VRageRender; namespace XFE.SeAgent.Plugin.Graphics { internal sealed partial class RayTracing { private const uint GlassFlag=1u<<30, VoxelFlag=1u<<31; [StructLayout(LayoutKind.Sequential)] private struct GlassMaterial { public Vector4 Transmission, Optics; } [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] private static extern int RtGlassMaterials(IntPtr context,GlassMaterial[] materials,uint count); private readonly List glassMaterials=new List(); private readonly Dictionary glassIds=new Dictionary(StringComparer.Ordinal); private int glassTriangles; public JObject Inspect(string modelFilter=null) { if(modelFilter!=null && modelFilter.Length>128) throw new ArgumentException("model filter exceeds 128 characters."); var camera=MySector.MainCamera.Position; Func vec=v=>new JObject { ["x"]=v.X,["y"]=v.Y,["z"]=v.Z }; var panes=placements.Where(p=>p.Block!=null && meshesById.TryGetValue(p.Mesh,out var mesh) && mesh.Materials.Any(m=>(m&GlassFlag)!=0)) .Where(p=>string.IsNullOrEmpty(modelFilter) || (p.Owner.Model?.AssetName??"").IndexOf(modelFilter,StringComparison.OrdinalIgnoreCase)>=0) .OrderBy(p=>(p.Owner.Model?.AssetName??"").IndexOf("Window",StringComparison.OrdinalIgnoreCase)<0?1:0) .ThenBy(p=>Vector3D.DistanceSquared(camera,p.Owner.WorldMatrix.Translation)).Take(32); var voxels=MyEntities.GetEntities().OfType().Select(v=>v.RootVoxel).Where(v=>v!=null && !v.Closed).Distinct() .OrderBy(v=>Vector3D.DistanceSquared(camera,v.PositionComp.WorldAABB.Center)).Take(64); return new JObject { ["camera"]=vec(camera), ["glassMaterials"]=new JArray(glassIds.OrderBy(pair=>pair.Value).Take(128).Select(pair=> { var glass=glassMaterials[(int)pair.Value];return new JObject { ["id"]=pair.Value,["name"]=pair.Key, ["roughness"]=glass.Transmission.W,["ior"]=glass.Optics.X,["thicknessMeters"]=glass.Optics.Y }; })), ["interiorCockpit"]=interiorCockpit==null?null:new JObject { ["entityId"]=interiorCockpit.EntityId.ToString(), ["model"]=DisplayedModel(interiorCockpit)?.AssetName, ["parts"]=new JArray(DisplayedModel(interiorCockpit).GetMeshList().Select(m=>new JObject { ["name"]=m.Material?.Name,["technique"]=m.Material?.DrawTechnique.ToString(),["triangles"]=m.TriCount, ["glassCW"]=m.Material?.GlassCW,["glassCCW"]=m.Material?.GlassCCW, ["textures"]=m.Material?.Textures==null?null:JObject.FromObject(m.Material.Textures) })) }, ["panes"]=new JArray(panes.Select(p=> { var surface=GlassSurface(p);return new JObject { ["entityId"]=p.Owner.EntityId.ToString(),["model"]=p.Owner.Model?.AssetName, ["position"]=vec(p.Owner.WorldMatrix.Translation),["forward"]=vec(p.Owner.WorldMatrix.Forward),["up"]=vec(p.Owner.WorldMatrix.Up), ["glassCenter"]=vec(surface.Translation),["glassNormal"]=vec(surface.Forward),["glassUp"]=vec(surface.Up), ["min"]=vec(p.Owner.PositionComp.WorldAABB.Min),["max"]=vec(p.Owner.PositionComp.WorldAABB.Max) }; })), ["voxels"]=new JArray(voxels.Select(v=>new JObject { ["entityId"]=v.EntityId.ToString(),["name"]=v.StorageName,["type"]=v.GetType().Name, ["position"]=vec(v.WorldMatrix.Translation),["min"]=vec(v.PositionComp.WorldAABB.Min),["max"]=vec(v.PositionComp.WorldAABB.Max) })) }; } private MatrixD GlassSurface(Placement p) { var mesh=meshesById[p.Mesh];float area=0;Vector3 a=Vector3.Zero,b=Vector3.Right,c=Vector3.Up; Func vertex=i=>new Vector3(mesh.Vertices[i*8],mesh.Vertices[i*8+1],mesh.Vertices[i*8+2]); for(int i=0;iarea) {area=size;a=v0;b=v1;c=v2;} } var world=p.Local*p.Owner.WorldMatrix; return MatrixD.CreateWorld(Vector3D.Transform((a+b+c)/3,world),Vector3D.Normalize(Vector3D.TransformNormal(Vector3.Cross(b-a,c-a),world)),Vector3D.Normalize(Vector3D.TransformNormal(b-a,world))); } private uint GlassId(string name,string outside,string inside) { // Current MWM assets bind transparency by material name. Legacy CW / // CCW fields can both be empty (including the fighter cockpit). // Match the importer's preference for Name, then legacy inside/outside. string key=name??""; MyTransparentMaterial material; bool found=MyTransparentMaterials.TryGetMaterial(MyStringId.GetOrCompute(key),out material); if(!found && !string.IsNullOrEmpty(inside)) {key=inside;found=MyTransparentMaterials.TryGetMaterial(MyStringId.GetOrCompute(key),out material);} if(!found && !string.IsNullOrEmpty(outside)) {key=outside;found=MyTransparentMaterials.TryGetMaterial(MyStringId.GetOrCompute(key),out material);} if(glassIds.TryGetValue(key,out var id)) return id; if(glassMaterials.Count>=1024) throw new InvalidOperationException("More than 1024 dielectric materials."); Vector3 tint=Vector3.One; float roughness=.08f; if(found) { // Engine color is linear and premultiplied. Interpret only its hue // as absorption: native opacity is a dirt multiplier, not IOR. Vector3 color=new Vector3(material.Color.X,material.Color.Y,material.Color.Z); float peak=Math.Max(color.X,Math.Max(color.Y,color.Z)); if(peak>1e-4f) tint=Vector3.Lerp(Vector3.One,color/peak,.3f); roughness=MathHelper.Clamp((1-material.Gloss-material.GlossTextureAdd)*.25f,.025f,.4f); } id=(uint)glassMaterials.Count; glassMaterials.Add(new GlassMaterial { Transmission=new Vector4(tint,roughness), // VRAGE2 supplies artistic transparency, not physical thickness // or index of refraction. These are documented thin-pane defaults. Optics=new Vector4(1.52f,.01f,0,0) }); glassIds.Add(key,id); return id; } } }