using System.IO.Compression; using System.Numerics; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text.Json; using XFEEDToolBox.Core.Models; namespace XFEEDToolBox.Core.Rendering; [StructLayout(LayoutKind.Sequential)] public readonly record struct GalaxyOriginalPlaneVertex(Vector3 Position,Vector3 Normal,Vector2 Uv); /// Exact authored plane, BC7-sRGB source and source-packed instance transform. public sealed class GalaxyOriginalPlaneResources { public ReadOnlyMemory PackedMesh {get;} public ReadOnlyMemory TexturePayload {get;} public IReadOnlyList Vertices {get;} public IReadOnlyList Indices {get;} public IReadOnlyList Mips {get;} public Vector4 Material0 {get;}=new(.9459999799728394f,1,1,.9170001149177551f); public Vector4 Material1 {get;}=new(14392.1904296875f,1772.406982421875f,0,0); public static readonly Vector3 AbsoluteCentre=new(50000,40955,50000); public const float WorldScale=100000; public static readonly Vector4 Quaternion=new(DecodeQuaternion(32767),DecodeQuaternion(65534),DecodeQuaternion(32767),DecodeQuaternion(32767)); private static float DecodeQuaternion(uint value)=>MathF.FusedMultiplyAdd(value,1f/32767,-1); internal GalaxyOriginalPlaneResources(JsonElement json,byte[] mesh,byte[] texture) { if(mesh.Length!=196||Convert.ToHexStringLower(SHA256.HashData(mesh))!="ceb482f68e17a1d05cd8c36f488ee1d46bc003e8eed290284e83fb5a73dec743"|| texture.Length!=5596160||Convert.ToHexStringLower(SHA256.HashData(texture))!="e3643ba432b84d2c34ca512122133559a813bbb80545fcd1c03c87087a7f4cde") throw new InvalidDataException("原银河平面资源校验失败。"); PackedMesh=mesh;TexturePayload=texture; Indices=Array.AsReadOnly(MemoryMarshal.Cast(mesh.AsSpan(24,12)).ToArray()); var vertices=new GalaxyOriginalPlaneVertex[4]; for(var i=0;i<4;i++) { var raw=mesh.AsSpan(36+i*40,40);var packed=BitConverter.ToUInt64(raw);var exponent=raw[11]&127; if(exponent>19)throw new InvalidDataException("原银河平面位置格式不支持。"); var reciprocal=1f/((1<<(20-exponent))-1);var bias=(float)(1<>21)&0x1fffff)*reciprocal-bias,((packed>>42)&0x1fffff)*reciprocal-bias); var normalWord=BitConverter.ToUInt32(raw[8..]); var normal=new Vector3((normalWord&255)*(1f/127)-1,((normalWord>>8)&255)*(1f/127)-1,((normalWord>>16)&255)*(1f/127)-1); var uvWord=BitConverter.ToUInt32(raw[16..]); var uv=new Vector2(((uvWord&65535)*(1f/32767)-1)*16,((uvWord>>16)*(1f/32767)-1)*16); vertices[i]=new(position,normal,uv); } Vertices=Array.AsReadOnly(vertices); Mips=Array.AsReadOnly(JsonSerializer.Deserialize(json.GetProperty("texture").GetProperty("mips"),ToolboxSettings.JsonOptions)!); if(Mips.Count!=12||Mips[^1].Offset+Mips[^1].Size!=texture.Length)throw new InvalidDataException("原银河平面Mip布局无效。"); } } public static class GalaxyOriginalPlane { private static readonly Lazy reference=new(Read); public static GalaxyOriginalPlaneResources Reference=>reference.Value; private static GalaxyOriginalPlaneResources Read() { var assembly=typeof(GalaxyOriginalPlane).Assembly; using var jsonStream=assembly.GetManifestResourceStream("XFEEDToolBox.Core.Rendering.original-galaxy-plane.json")!; using var json=JsonDocument.Parse(jsonStream); byte[] Payload(string name,int bytes) { using var stream=assembly.GetManifestResourceStream("XFEEDToolBox.Core.Rendering."+name)!; using var gzip=new GZipStream(stream,CompressionMode.Decompress);var data=new byte[bytes];gzip.ReadExactly(data); if(gzip.ReadByte()!=-1)throw new InvalidDataException("原银河平面数据超出范围。");return data; } return new(json.RootElement,Payload("original-galaxy-plane-mesh.bin.gz",196),Payload("original-galaxy-plane-texture.bc7.gz",5596160)); } }