|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using CommandLine; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Npgsql; |
| 9 | +using SharpGLTF.Materials; |
| 10 | +using Wkb2Gltf; |
| 11 | +using Wkx; |
| 12 | +using WkbTriangle = Wkb2Gltf.Triangle; |
| 13 | + |
| 14 | +namespace GltfExport; |
| 15 | + |
| 16 | +class Program |
| 17 | +{ |
| 18 | + static void Main(string[] args) |
| 19 | + { |
| 20 | + var version = Assembly.GetEntryAssembly()?.GetName().Version; |
| 21 | + Console.WriteLine($"Tool: GltfExport {version}"); |
| 22 | + |
| 23 | + Parser.Default.ParseArguments<Options>(args).WithParsed(o => |
| 24 | + { |
| 25 | + Console.WriteLine($"Table: {o.Table}"); |
| 26 | + Console.WriteLine($"Geometry column: {o.GeometryColumn}"); |
| 27 | + Console.WriteLine($"Id column: {o.IdColumn}"); |
| 28 | + Console.WriteLine($"Shaders column: {(string.IsNullOrEmpty(o.ShadersColumn) ? "-" : o.ShadersColumn)}"); |
| 29 | + Console.WriteLine($"Output directory: {o.Output}"); |
| 30 | + Console.WriteLine($"Default color: {o.DefaultColor}"); |
| 31 | + Console.WriteLine($"Default metallic roughness: {o.DefaultMetallicRoughness}"); |
| 32 | + Console.WriteLine($"Double sided: {o.DoubleSided}"); |
| 33 | + Console.WriteLine($"Default alpha mode: {o.DefaultAlphaMode}"); |
| 34 | + Console.WriteLine($"Alpha cutoff: {o.AlphaCutoff}"); |
| 35 | + |
| 36 | + Directory.CreateDirectory(o.Output); |
| 37 | + |
| 38 | + var sql = BuildQuery(o.Table, o.GeometryColumn, o.IdColumn, o.ShadersColumn); |
| 39 | + |
| 40 | + using var conn = new NpgsqlConnection(o.Connection); |
| 41 | + conn.Open(); |
| 42 | + |
| 43 | + var cmd = new NpgsqlCommand(sql, conn); |
| 44 | + var reader = cmd.ExecuteReader(); |
| 45 | + |
| 46 | + var written = 0; |
| 47 | + var skipped = 0; |
| 48 | + |
| 49 | + while (reader.Read()) |
| 50 | + { |
| 51 | + var id = reader.GetFieldValue<object>(0).ToString()!; |
| 52 | + var safeId = SanitizeFileName(id); |
| 53 | + |
| 54 | + byte[]? glbBytes = null; |
| 55 | + try |
| 56 | + { |
| 57 | + var stream = reader.GetStream(1); |
| 58 | + var geometry = Geometry.Deserialize<WkbSerializer>(stream); |
| 59 | + |
| 60 | + ShaderColors? shaderColors = null; |
| 61 | + if (!string.IsNullOrEmpty(o.ShadersColumn)) |
| 62 | + { |
| 63 | + var json = reader.IsDBNull(2) ? null : reader.GetString(2); |
| 64 | + if (json != null) |
| 65 | + { |
| 66 | + shaderColors = JsonConvert.DeserializeObject<ShaderColors>(json); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + var record = new GeometryRecord(0) { Geometry = geometry, Shader = shaderColors }; |
| 71 | + var triangles = record.GetTriangles(translation: null); |
| 72 | + |
| 73 | + glbBytes = GlbCreator.GetGlb( |
| 74 | + triangles: new List<List<WkbTriangle>> { triangles }, |
| 75 | + createGltf: true, |
| 76 | + defaultColor: o.DefaultColor, |
| 77 | + defaultMetallicRoughness: o.DefaultMetallicRoughness, |
| 78 | + defaultDoubleSided: o.DoubleSided, |
| 79 | + defaultAlphaMode: o.DefaultAlphaMode, |
| 80 | + alphaCutoff: o.AlphaCutoff |
| 81 | + ); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + Console.WriteLine($"Warning: skipping id '{id}': {ex.Message}"); |
| 86 | + skipped++; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (glbBytes == null) |
| 91 | + { |
| 92 | + Console.WriteLine($"Warning: skipping id '{id}': no geometry produced."); |
| 93 | + skipped++; |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + var outputFile = Path.Combine(o.Output, $"{safeId}.glb"); |
| 98 | + File.WriteAllBytes(outputFile, glbBytes); |
| 99 | + written++; |
| 100 | + } |
| 101 | + |
| 102 | + reader.Close(); |
| 103 | + conn.Close(); |
| 104 | + |
| 105 | + Console.WriteLine($"Done. Written: {written}, Skipped: {skipped}."); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private static string BuildQuery(string table, string geomColumn, string idColumn, string shadersColumn) |
| 110 | + { |
| 111 | + var select = $"SELECT {idColumn}::text, ST_AsBinary({geomColumn})"; |
| 112 | + if (!string.IsNullOrEmpty(shadersColumn)) |
| 113 | + { |
| 114 | + select += $", {shadersColumn}"; |
| 115 | + } |
| 116 | + return $"{select} FROM {table}"; |
| 117 | + } |
| 118 | + |
| 119 | + private static string SanitizeFileName(string name) |
| 120 | + { |
| 121 | + var invalid = Path.GetInvalidFileNameChars(); |
| 122 | + return string.Concat(name.Select(c => invalid.Contains(c) ? '_' : c)); |
| 123 | + } |
| 124 | +} |
0 commit comments