0.2.0-dev Download ZIP
80 lines ·3.2 KB Raw
| 1 | using System.Text; |
| 2 | using EvilEngine.Adapters.Hpl3.Maps; |
| 3 | using EvilEngine.Conversion; |
| 4 | using EvilEngine.Formats; |
| 5 | using EvilEngine.Formats.Maps; |
| 6 | using EvilEngine.Mounts; |
| 7 | |
| 8 | namespace EvilEngine.Adapters.Hpl3; |
| 9 | |
| 10 | public sealed class Hpl3SkyConverter : IConverter |
| 11 | { |
| 12 | public const string Part = "sky"; |
| 13 | |
| 14 | public string Id => "hpl3.sky"; |
| 15 | |
| 16 | public int Version => 3; |
| 17 | |
| 18 | public ContentKind Produces => ContentKind.Sky; |
| 19 | |
| 20 | public bool Accepts(ConversionContext context) |
| 21 | { |
| 22 | return Map(context) is { } map && context.Exists(map); |
| 23 | } |
| 24 | |
| 25 | public void Convert(ConversionContext context, Stream output) |
| 26 | { |
| 27 | var map = Map(context) ?? throw new InvalidOperationException($"{context.Id} isn't a map's sky."); |
| 28 | var root = Hpl3Map.Parse(map, context.Read(map)); |
| 29 | var layers = new List<SkyLayer>(); |
| 30 | if (SkyBox(root) is { } box && box["Texture"] is { } written) |
| 31 | { |
| 32 | var colour = Hpl3Map.Numbers(box["Color"], 4, 1); |
| 33 | var brightness = Hpl3Map.Numbers(box["Brightness"], 1, 1)[0]; |
| 34 | if (new Hpl3Files(context).Find(written) is { } texture) |
| 35 | layers.Add(new SkyLayer.Cube(context.Reference(ContentKind.Texture, texture.WithoutExtension.Value), |
| 36 | new MapVector(Hpl3Map.Round(colour[0] * brightness), Hpl3Map.Round(colour[1] * brightness), |
| 37 | Hpl3Map.Round(colour[2] * brightness)))); |
| 38 | else |
| 39 | context.Warn($"{context.Id} names sky texture '{written}', which isn't in its namespace."); |
| 40 | } |
| 41 | |
| 42 | var sky = new Sky(layers) { Fog = Hpl3ManifestConverter.Fog(root) }; |
| 43 | if (root.DescendantsAndSelf().FirstOrDefault(element => element.Name == "DirLight") is { } light && |
| 44 | Hpl3Map.Flag(light["Active"])) |
| 45 | { |
| 46 | var travels = Hpl3Map.Numbers(light["Direction"], 3, 0); |
| 47 | var length = Math.Sqrt(travels[0] * travels[0] + travels[1] * travels[1] + travels[2] * travels[2]); |
| 48 | var diffuse = Hpl3Map.Numbers(light["DiffuseColor"], 4, 1); |
| 49 | var brightness = Hpl3Map.Numbers(light["Brightness"], 4, 1); |
| 50 | if (length > 0) |
| 51 | sky = sky with |
| 52 | { |
| 53 | Sun = new SkySun( |
| 54 | new MapVector(Hpl3Map.Round(-travels[0] / length), Hpl3Map.Round(-travels[1] / length), |
| 55 | Hpl3Map.Round(-travels[2] / length)), |
| 56 | new MapVector(Hpl3Map.Round(diffuse[0] * brightness[0]), |
| 57 | Hpl3Map.Round(diffuse[1] * brightness[1]), |
| 58 | Hpl3Map.Round(diffuse[2] * brightness[2])), null) |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | output.Adopt(Encoding.UTF8.GetBytes(SkyText.Write(sky))); |
| 63 | } |
| 64 | |
| 65 | public static GameXmlElement? SkyBox(GameXmlElement root) |
| 66 | { |
| 67 | return root.DescendantsAndSelf().FirstOrDefault(element => element.Name == "SkyBox") is { } box && |
| 68 | Hpl3Map.Flag(box["Active"]) && box["Texture"] is { Length: > 0 } |
| 69 | ? box |
| 70 | : null; |
| 71 | } |
| 72 | |
| 73 | private static VirtualPath? Map(ConversionContext context) |
| 74 | { |
| 75 | var name = context.Id.Name; |
| 76 | return name.EndsWith($"/{Part}", StringComparison.Ordinal) |
| 77 | ? VirtualPath.Parse($"{name[..^(Part.Length + 1)]}.hpm") |
| 78 | : (VirtualPath?)null; |
| 79 | } |
| 80 | } |