0.2.0-dev Download ZIP
158 lines ·6.3 KB Raw
| 1 | using System.Runtime.InteropServices; |
| 2 | using EvilEngine.Formats.Textures; |
| 3 | using Godot; |
| 4 | using Godot.Collections; |
| 5 | using Texture = EvilEngine.Formats.Textures.Texture; |
| 6 | |
| 7 | namespace EvilEngine.Shared.World; |
| 8 | |
| 9 | public sealed record DecodedTexture(IReadOnlyList<Image> Images, bool Layered, bool Cube); |
| 10 | |
| 11 | public static class WorldTextures |
| 12 | { |
| 13 | private const int RgbeBlock = 1 << 16; |
| 14 | |
| 15 | private static readonly Lazy<ushort[]> RgbeHalves = new(() => |
| 16 | { |
| 17 | var table = new ushort[256 * 256]; |
| 18 | for (var exponent = 0; exponent < 256; exponent++) |
| 19 | { |
| 20 | var scale = Math.ScaleB(1.0, (sbyte)exponent) / 255; |
| 21 | for (var value = 0; value < 256; value++) |
| 22 | table[(exponent << 8) | value] = BitConverter.HalfToUInt16Bits((Half)(value * scale)); |
| 23 | } |
| 24 | |
| 25 | return table; |
| 26 | }); |
| 27 | |
| 28 | public static Image Checker() |
| 29 | { |
| 30 | var image = Image.CreateEmpty(16, 16, false, Image.Format.Rgba8); |
| 31 | for (var y = 0; y < 16; y++) |
| 32 | for (var x = 0; x < 16; x++) |
| 33 | image.SetPixel(x, y, (x / 8 + y / 8) % 2 == 0 ? new Color(1, 0, 1) : Colors.Black); |
| 34 | return image; |
| 35 | } |
| 36 | |
| 37 | public static DecodedTexture? Decode(Texture texture, out string? problem) |
| 38 | { |
| 39 | problem = null; |
| 40 | if (texture.Depth > 1 || (texture.Layers > 1 && texture.Cube)) |
| 41 | { |
| 42 | problem = $"{texture.Layers} layers of depth {texture.Depth} aren't drawn yet"; |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | var images = new List<Image>(); |
| 47 | for (var layer = 0; layer < texture.Layers; layer++) |
| 48 | for (var face = 0; face < texture.Faces; face++) |
| 49 | if (Face(texture, layer, face, out problem) is { } image) |
| 50 | images.Add(image); |
| 51 | else |
| 52 | return null; |
| 53 | return new DecodedTexture(images, texture.Layers > 1, texture.Cube); |
| 54 | } |
| 55 | |
| 56 | public static Godot.Texture Upload(DecodedTexture decoded) |
| 57 | { |
| 58 | if (decoded.Layered) |
| 59 | { |
| 60 | var array = new Texture2DArray(); |
| 61 | array.CreateFromImages(new Array<Image>(decoded.Images)); |
| 62 | return array; |
| 63 | } |
| 64 | |
| 65 | if (!decoded.Cube) |
| 66 | return ImageTexture.CreateFromImage(decoded.Images[0]); |
| 67 | var cube = new Cubemap(); |
| 68 | cube.CreateFromImages(new Array<Image>(decoded.Images)); |
| 69 | return cube; |
| 70 | } |
| 71 | |
| 72 | private static Image? Face(Texture texture, int layer, int face, out string? problem) |
| 73 | { |
| 74 | problem = null; |
| 75 | var (format, convert) = Format(texture); |
| 76 | if (format is null) |
| 77 | { |
| 78 | problem = $"{texture.Format} with swizzle {texture.Swizzle} isn't drawn yet"; |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | var full = (int)Math.Floor(Math.Log2(Math.Max(texture.Width, texture.Height))) + 1; |
| 83 | var levels = texture.Levels.Count == full ? full : 1; |
| 84 | var parts = new ReadOnlyMemory<byte>[levels]; |
| 85 | for (var level = 0; level < levels; level++) |
| 86 | { |
| 87 | var image = texture.Image(level, layer, face); |
| 88 | parts[level] = convert is null ? image : convert(image.ToArray()); |
| 89 | } |
| 90 | |
| 91 | var data = new byte[parts.Sum(part => part.Length)]; |
| 92 | var at = 0; |
| 93 | foreach (var part in parts) |
| 94 | { |
| 95 | part.Span.CopyTo(data.AsSpan(at)); |
| 96 | at += part.Length; |
| 97 | } |
| 98 | |
| 99 | return Image.CreateFromData(texture.Width, texture.Height, levels > 1, format.Value, data); |
| 100 | } |
| 101 | |
| 102 | private static (Image.Format? Format, Func<byte[], byte[]>? Convert) Format(Texture texture) |
| 103 | { |
| 104 | if (texture.Hdr == TextureHdr.SourceLightmap && texture.Format == TextureFormat.R8G8B8A8Unorm) |
| 105 | return (Image.Format.Rgbah, Rgbe); |
| 106 | var swizzle = texture.Swizzle; |
| 107 | if (swizzle == "rrr1" && texture.Format is TextureFormat.R8Unorm or TextureFormat.R8Srgb) |
| 108 | return (Image.Format.L8, null); |
| 109 | if (swizzle != "rgba") |
| 110 | return (null, null); |
| 111 | return texture.Format switch |
| 112 | { |
| 113 | TextureFormat.R8Unorm or TextureFormat.R8Srgb => (Image.Format.R8, null), |
| 114 | TextureFormat.R8G8Unorm or TextureFormat.R8G8Srgb => (Image.Format.Rg8, null), |
| 115 | TextureFormat.R8G8B8Unorm or TextureFormat.R8G8B8Srgb => (Image.Format.Rgb8, null), |
| 116 | TextureFormat.R8G8B8A8Unorm or TextureFormat.R8G8B8A8Srgb => (Image.Format.Rgba8, null), |
| 117 | TextureFormat.R16Sfloat => (Image.Format.Rh, null), |
| 118 | TextureFormat.R16G16Sfloat => (Image.Format.Rgh, null), |
| 119 | TextureFormat.R16G16B16A16Sfloat => (Image.Format.Rgbah, null), |
| 120 | TextureFormat.R32Sfloat => (Image.Format.Rf, null), |
| 121 | TextureFormat.R32G32Sfloat => (Image.Format.Rgf, null), |
| 122 | TextureFormat.R32G32B32Sfloat => (Image.Format.Rgbf, null), |
| 123 | TextureFormat.R32G32B32A32Sfloat => (Image.Format.Rgbaf, null), |
| 124 | TextureFormat.Bc1RgbUnorm or TextureFormat.Bc1RgbSrgb or TextureFormat.Bc1RgbaUnorm |
| 125 | or TextureFormat.Bc1RgbaSrgb => (Image.Format.Dxt1, null), |
| 126 | TextureFormat.Bc2Unorm or TextureFormat.Bc2Srgb => (Image.Format.Dxt3, null), |
| 127 | TextureFormat.Bc3Unorm or TextureFormat.Bc3Srgb => (Image.Format.Dxt5, null), |
| 128 | TextureFormat.Bc4Unorm => (Image.Format.RgtcR, null), |
| 129 | TextureFormat.Bc5Unorm => (Image.Format.RgtcRg, null), |
| 130 | TextureFormat.Bc6hUfloat => (Image.Format.BptcRgbfu, null), |
| 131 | TextureFormat.Bc6hSfloat => (Image.Format.BptcRgbf, null), |
| 132 | TextureFormat.Bc7Unorm or TextureFormat.Bc7Srgb => (Image.Format.BptcRgba, null), |
| 133 | _ => (null, null) |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | private static byte[] Rgbe(byte[] rgbe) |
| 138 | { |
| 139 | var halves = new byte[rgbe.Length * 2]; |
| 140 | var table = RgbeHalves.Value; |
| 141 | var one = BitConverter.HalfToUInt16Bits((Half)1); |
| 142 | var texels = rgbe.Length / 4; |
| 143 | Parallel.For(0, (texels + RgbeBlock - 1) / RgbeBlock, block => |
| 144 | { |
| 145 | var output = MemoryMarshal.Cast<byte, ushort>(halves.AsSpan()); |
| 146 | var end = Math.Min(texels, (block + 1) * RgbeBlock); |
| 147 | for (var i = block * RgbeBlock * 4; i < end * 4; i += 4) |
| 148 | { |
| 149 | var exponent = rgbe[i + 3] << 8; |
| 150 | output[i] = table[exponent | rgbe[i]]; |
| 151 | output[i + 1] = table[exponent | rgbe[i + 1]]; |
| 152 | output[i + 2] = table[exponent | rgbe[i + 2]]; |
| 153 | output[i + 3] = one; |
| 154 | } |
| 155 | }); |
| 156 | return halves; |
| 157 | } |
| 158 | } |