From a878a7a1438415060dcb7ec8984b7bed5bc550a5 Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Mon, 8 Jun 2026 19:46:18 -0500 Subject: [PATCH] texture mapping actually works omg! --- packages/game/JSExport/GameInterface.cs | 4 +- packages/game/JSExport/MappedGltf.cs | 23 ---------- packages/game/Models/MonoGltf.cs | 56 ++++++++----------------- 3 files changed, 19 insertions(+), 64 deletions(-) delete mode 100644 packages/game/JSExport/MappedGltf.cs diff --git a/packages/game/JSExport/GameInterface.cs b/packages/game/JSExport/GameInterface.cs index 891dc8318..3dceb791d 100644 --- a/packages/game/JSExport/GameInterface.cs +++ b/packages/game/JSExport/GameInterface.cs @@ -109,7 +109,7 @@ public partial class GameInterface { "gun", "DT_Guns" }, }; - public string TankPart(string tag, string part) + public byte[] TankPart(string tag, string part) { try { @@ -146,7 +146,7 @@ public partial class GameInterface var gltf = new MonoGltf(mesh); - return gltf.Write(); + return gltf.Write($"{tag}/${part}"); } catch (Exception e) { diff --git a/packages/game/JSExport/MappedGltf.cs b/packages/game/JSExport/MappedGltf.cs deleted file mode 100644 index ce3dac0eb..000000000 --- a/packages/game/JSExport/MappedGltf.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Text.Json; -using Microsoft.JavaScript.NodeApi; -using SharpGLTF.Materials; - -namespace BlitzKit.Game.JSExport; - -[JSExport] -public readonly struct MappedGltf -{ - public string Json { get; } - public Dictionary> TextureMap { get; } - - public MappedGltf(string json, Dictionary> textureMap) - { - Json = json; - TextureMap = textureMap; - } - - public string ToJson() - { - return JsonSerializer.Serialize(this); - } -} diff --git a/packages/game/Models/MonoGltf.cs b/packages/game/Models/MonoGltf.cs index 217790dd4..be3f9cb57 100644 --- a/packages/game/Models/MonoGltf.cs +++ b/packages/game/Models/MonoGltf.cs @@ -122,48 +122,26 @@ public class MonoGltf } } - public string Write() + public byte[] Write(string name) { - try - { - var model = scene.ToGltf2(); + var stream = new MemoryStream(); + var context = WriteContext + .CreateFromStream(stream) + .WithSettingsFrom( + new() + { + ImageWriting = ResourceWriteMode.SatelliteFile, + ImageWriteCallback = (context, assetName, image) => + { + return "/textures/my-ass.png"; + }, + } + ); + var model = scene.ToGltf2(); - var stream = new MemoryStream(); - var context = WriteContext - .CreateFromStream(stream) - .WithSettingsFrom(new WriteSettings() { ImageWriting = ResourceWriteMode.SatelliteFile }); + context.WriteBinarySchema2(name, model); - context.ImageWriteCallback = (WriteContext context, string assetName, MemoryImage image) => - { - // image.Content is the raw data - // var bytes = image.Content.ToArray(); - - // store.Files[suggestedName] = bytes; - - // // return URI used in JSON - // - return assetName; - }; - - foreach (var img in model.LogicalImages) - { - Console.WriteLine(img.Content.SourcePath); - Console.WriteLine(img.Content.FileExtension); - } - - model.SaveGLTF - - context.WriteTextSchema2("model.gltf", model); - - stream.Position = 0; - using var reader = new StreamReader(stream); - - return reader.ReadToEnd(); - } - catch (Exception ex) - { - throw new Exception("Failed to write GLTF", ex); - } + return stream.ToArray(); } private static (