diff --git a/GlitchyEditor/resources/Textures/EditorIcons.dds.ass b/GlitchyEditor/resources/Textures/EditorIcons.dds.ass index 7d720ed..2e9b553 100644 --- a/GlitchyEditor/resources/Textures/EditorIcons.dds.ass +++ b/GlitchyEditor/resources/Textures/EditorIcons.dds.ass @@ -2,36 +2,10 @@ AssetLoader = null, Config = null, Importer = "TextureImporter", - ImporterConfig = (GlitchyEditor.Assets.Importers.TextureImporterConfig){ - _isSrgb = false - }, - Processor = "TextureProcessor", - ProcessorConfig = (GlitchyEditor.Assets.Processors.TextureProcessorConfig){ - _generateMipMaps = .No, - _samplerStateDescription = { - MinFilter = .Linear, - MagFilter = .Linear, - MipFilter = .Linear, - FilterMode = .Default, - ComparisonFunction = .Never, - AddressModeU = .Clamp, - AddressModeV = .Clamp, - AddressModeW = .Clamp, - MipLODBias = 0, - MipMinLOD = -3.40282347e+38, - MipMaxLOD = 3.40282347e+38, - MaxAnisotropy = 1, - BorderColor = { - R = 1, - G = 1, - B = 1, - A = 1 - } - } - }, - Exporter = "TextureExporter", - ExporterConfig = { - _compression = .LZ4 - }, - AssetHandle = 17972970543152031508 + ImporterConfig = null, + Processor = null, + ProcessorConfig = null, + Exporter = null, + ExporterConfig = null, + AssetHandle = 17408033053158961990 } \ No newline at end of file diff --git a/GlitchyEditor/resources/Textures/ErrorTexture2D.png.ass b/GlitchyEditor/resources/Textures/ErrorTexture2D.png.ass index 71f3761..9ede3c2 100644 --- a/GlitchyEditor/resources/Textures/ErrorTexture2D.png.ass +++ b/GlitchyEditor/resources/Textures/ErrorTexture2D.png.ass @@ -5,21 +5,21 @@ ImporterConfig = (GlitchyEditor.Assets.Importers.TextureImporterConfig){ _isSrgb = false }, - Processor = "TextureProcessor", + Processor = null, ProcessorConfig = (GlitchyEditor.Assets.Processors.TextureProcessorConfig){ _generateMipMaps = .No, _samplerStateDescription = { - MinFilter = .Point, - MagFilter = .Point, - MipFilter = .Point, + MinFilter = .Linear, + MagFilter = .Linear, + MipFilter = .Linear, FilterMode = .Default, ComparisonFunction = .Never, AddressModeU = .Clamp, AddressModeV = .Clamp, AddressModeW = .Clamp, MipLODBias = 0, - MipMinLOD = -3.40282347e+38, - MipMaxLOD = 3.40282347e+38, + MipMinLOD = -Infinity, + MipMaxLOD = Infinity, MaxAnisotropy = 1, BorderColor = { R = 1, @@ -27,11 +27,14 @@ B = 1, A = 1 } - } + }, + _textureType = .Texture, + _sprites = [ + ] }, - Exporter = "TextureExporter", + Exporter = null, ExporterConfig = { _compression = .None }, - AssetHandle = 8620184366354358183 + AssetHandle = 6043007137039118289 } \ No newline at end of file diff --git a/GlitchyEditor/resources/Textures/PlaceholderTexture2D.png.ass b/GlitchyEditor/resources/Textures/PlaceholderTexture2D.png.ass index 1a77dbe..c04a274 100644 --- a/GlitchyEditor/resources/Textures/PlaceholderTexture2D.png.ass +++ b/GlitchyEditor/resources/Textures/PlaceholderTexture2D.png.ass @@ -5,7 +5,7 @@ ImporterConfig = (GlitchyEditor.Assets.Importers.TextureImporterConfig){ _isSrgb = false }, - Processor = "TextureProcessor", + Processor = null, ProcessorConfig = (GlitchyEditor.Assets.Processors.TextureProcessorConfig){ _generateMipMaps = .No, _samplerStateDescription = { @@ -18,8 +18,8 @@ AddressModeV = .Clamp, AddressModeW = .Clamp, MipLODBias = 0, - MipMinLOD = -3.40282347e+38, - MipMaxLOD = 3.40282347e+38, + MipMinLOD = -Infinity, + MipMaxLOD = Infinity, MaxAnisotropy = 1, BorderColor = { R = 1, @@ -27,11 +27,14 @@ B = 1, A = 1 } - } + }, + _textureType = .Texture, + _sprites = [ + ] }, - Exporter = "TextureExporter", + Exporter = null, ExporterConfig = { _compression = .None }, - AssetHandle = 7647313657967374590 + AssetHandle = 1772897904610089584 } \ No newline at end of file diff --git a/GlitchyEditor/src/AssetFile.bf b/GlitchyEditor/src/AssetFile.bf index ea4f293..6fcc2d5 100644 --- a/GlitchyEditor/src/AssetFile.bf +++ b/GlitchyEditor/src/AssetFile.bf @@ -63,7 +63,7 @@ class AssetFile public EditorContentManager ContentManager => _contentManager; - public bool UseNewAssetPipeline => _assetConfig?.ImporterConfig != null; + public bool UseNewAssetPipeline => _assetConfig?.Importer != null; [AllowAppend] public this(EditorContentManager contentManager, AssetNode assetNode) @@ -141,8 +141,8 @@ class AssetFile GenerateAssetHandle(); IAssetImporter assetImporter = _contentManager.GetAssetImporter(fileExtension); - IAssetProcessor assetProcessor = ?; //_contentManager.GetAssetProcessor(assetImporter.); - IAssetExporter assetExporter = ?; //_contentManager.GetAssetProcessor(assetImporter.); + //IAssetProcessor assetProcessor = ?; //_contentManager.GetAssetProcessor(assetImporter.); + //IAssetExporter assetExporter = ?; //_contentManager.GetAssetProcessor(assetImporter.); // TODO! //if (assetPipeline case .Err) @@ -165,16 +165,7 @@ class AssetFile _assetConfig.Importer = new String(); assetImporter?.GetType()?.GetName(_assetConfig.Importer); - _assetConfig.ImporterConfig = assetImporter?.CreateDefaultConfig(); - - _assetConfig.Processor = new String(); - assetProcessor?.GetType()?.GetName(_assetConfig.Processor); - _assetConfig.ProcessorConfig = assetProcessor.CreateDefaultConfig(); - - _assetConfig.Exporter = new String(); - assetExporter?.GetType()?.GetName(_assetConfig.Exporter); - _assetConfig.ExporterConfig = assetExporter.CreateDefaultConfig(); - + SaveAssetConfig(); } @@ -195,6 +186,17 @@ class AssetFile _assetConfig = newAssetConfig; } + public void SaveAssetConfigIfChanged() + { + if (_assetConfig.Config?.Changed == true || + _assetConfig.ImporterConfig?.Changed == true || + _assetConfig.ProcessorConfig?.Changed == true || + _assetConfig.ExporterConfig?.Changed == true) + { + SaveAssetConfig(); + } + } + public void SaveAssetConfig() { //BonEnvironment bonEnv = scope .(); @@ -206,7 +208,10 @@ class AssetFile Bon.SerializeIntoFile(_assetConfig, _assetConfigPath); _assetConfig.Config?.[Friend]_changed = false; + _assetConfig.ImporterConfig?.[Friend]_changed = false; + _assetConfig.ProcessorConfig?.[Friend]_changed = false; + _assetConfig.ExporterConfig?.[Friend]_changed = false; gBonEnv.serializeFlags = oldFlags; } -} \ No newline at end of file +} diff --git a/GlitchyEditor/src/Assets/AssetConverter.bf b/GlitchyEditor/src/Assets/AssetConverter.bf index 86a441f..a35a6a3 100644 --- a/GlitchyEditor/src/Assets/AssetConverter.bf +++ b/GlitchyEditor/src/Assets/AssetConverter.bf @@ -46,36 +46,21 @@ class AssetConverter private void Process(AssetFile assetFile) { - IAssetImporter importer = _contentManager.GetAssetImporter(assetFile); - IAssetProcessor processor = _contentManager.GetAssetProcessor(assetFile); + Result importResult = ImportResource(assetFile); - if (importer == null) - { - Log.EngineLogger.Error("Importer is null!"); - return; - } - - if (processor == null) - { - Log.EngineLogger.Error("Processor is null!"); - return; - } + ImportedResource importedResource = null; + defer { delete importedResource; } - Result importResult = importer.Import(assetFile.AssetFile.Path, - assetFile.AssetFile.Identifier, assetFile.AssetConfig.ImporterConfig); - - if (importResult case .Err) + if (!(importResult case .Ok(out importedResource))) { Log.EngineLogger.Error("Failed to import asset!"); return; } - defer { delete importResult.Value; } + List processedResources = scope .(); + defer { ClearAndDeleteItems!(processedResources); } - List resources = scope .(); - defer { ClearAndDeleteItems!(resources); } - - Result processResult = processor.Process(importResult.Value, assetFile.AssetConfig.ProcessorConfig, resources); + Result processResult = ProcessResource(assetFile, importedResource, processedResources); if (processResult case .Err) { @@ -83,12 +68,41 @@ class AssetConverter return; } - for (ProcessedResource resource in resources) + for (ProcessedResource resource in processedResources) { - TrySilent!(ExportResource(assetFile, resource)); + if (ExportResource(assetFile, resource) case .Err) + { + Log.EngineLogger.Error("Failed to export asset."); + } } } + private Result ImportResource(AssetFile assetFile) + { + IAssetImporter importer = _contentManager.GetAssetImporter(assetFile); + + if (importer == null) + { + Log.EngineLogger.Error("Importer is null!"); + return .Err; + } + + return importer.Import(assetFile.AssetFile.Path, assetFile.AssetFile.Identifier, assetFile.AssetConfig); + } + + private Result ProcessResource(AssetFile assetFile, ImportedResource importedResource, List outProcessedResources) + { + IAssetProcessor processor = _contentManager.GetAssetProcessor(importedResource.GetType()); + + if (processor == null) + { + Log.EngineLogger.Error($"No asset processor for type {importedResource.GetType()} found."); + return .Err; + } + + return processor.Process(importedResource, assetFile.AssetConfig, outProcessedResources); + } + private Result ExportResource(AssetFile assetFile, ProcessedResource resource) { IAssetExporter exporter = _contentManager.GetAssetExporter(resource.AssetType); @@ -101,7 +115,7 @@ class AssetConverter MemoryStream memoryStream = scope .(); - Result exportResult = exporter.Export(memoryStream, resource, assetFile.AssetConfig.ExporterConfig); + Result exportResult = exporter.Export(memoryStream, resource, assetFile.AssetConfig); if (exportResult case .Err) { @@ -109,11 +123,16 @@ class AssetConverter return .Err; } + if (assetFile.AssetConfig.ExporterConfig == null) + { + assetFile.AssetConfig.ExporterConfig = new AssetExporterConfig(); + } + CachedAsset assetInfo = scope CachedAsset(); - assetInfo.Handle = assetFile.AssetConfig.AssetHandle; + assetInfo.Handle = resource.AssetHandle; assetInfo.CreationTimestamp = DateTime.UtcNow; assetInfo.Compression = assetFile.AssetConfig.ExporterConfig.Compression; - assetInfo.AssetIdentifier = new AssetIdentifier(assetFile.AssetFile.Identifier); + assetInfo.AssetIdentifier = new AssetIdentifier(resource.AssetIdentifier); assetInfo.AssetType = resource.AssetType; if (_contentManager.AssetCache.SaveAsset(assetInfo, memoryStream.Memory) case .Err) @@ -122,6 +141,8 @@ class AssetConverter return .Err; } + assetFile.SaveAssetConfigIfChanged(); + return .Ok; } } \ No newline at end of file diff --git a/GlitchyEditor/src/Assets/AssetNode.bf b/GlitchyEditor/src/Assets/AssetNode.bf index 25cc9f5..41e0e5c 100644 --- a/GlitchyEditor/src/Assets/AssetNode.bf +++ b/GlitchyEditor/src/Assets/AssetNode.bf @@ -2,6 +2,7 @@ using System; using System.Collections; using GlitchyEngine.Renderer; using GlitchyEngine.Content; + namespace GlitchyEditor.Assets; // TODO: Why exactly are AssetFile and AssetNode separated? diff --git a/GlitchyEditor/src/Assets/Exporters/SpriteExporter.bf b/GlitchyEditor/src/Assets/Exporters/SpriteExporter.bf new file mode 100644 index 0000000..303784c --- /dev/null +++ b/GlitchyEditor/src/Assets/Exporters/SpriteExporter.bf @@ -0,0 +1,39 @@ +using System; +using System.IO; +using GlitchyEngine.Renderer; +using GlitchyEditor.Assets.Processors; +using GlitchyEngine; +using GlitchyEditor.Assets.Importers; +using GlitchyEngine.Content; + +namespace GlitchyEditor.Assets.Exporters; + +class SpriteExporter : IAssetExporter +{ + public static AssetType ExportedAssetType => .Sprite; + + public AssetExporterConfig CreateDefaultConfig() + { + return new AssetExporterConfig(); + } + + public Result Export(Stream stream, ProcessedResource processedResource, AssetConfig config) + { + Log.EngineLogger.AssertDebug(processedResource is ProcessedSprite); + + ProcessedSprite processedSprite = (.)processedResource; + + /* + + File Format: + TextureHandle (8 bytes) + TextureCoords (16 bytes) [float4] + + */ + + Try!(stream.Write(processedSprite.TextureHandle)); + Try!(stream.Write(processedSprite.TextureCoordinates)); + + return .Ok; + } +} diff --git a/GlitchyEditor/src/Assets/Exporters/TextureExporter.bf b/GlitchyEditor/src/Assets/Exporters/TextureExporter.bf index b888c56..d8941ca 100644 --- a/GlitchyEditor/src/Assets/Exporters/TextureExporter.bf +++ b/GlitchyEditor/src/Assets/Exporters/TextureExporter.bf @@ -17,7 +17,7 @@ class TextureExporter : IAssetExporter return new AssetExporterConfig(); } - public Result Export(Stream stream, ProcessedResource processedResource, AssetExporterConfig config) + public Result Export(Stream stream, ProcessedResource processedResource, AssetConfig config) { Log.EngineLogger.AssertDebug(processedResource is ProcessedTexture); diff --git a/GlitchyEditor/src/Assets/Importers/Config.bf b/GlitchyEditor/src/Assets/Importers/Config.bf index 70ce495..5113c0a 100644 --- a/GlitchyEditor/src/Assets/Importers/Config.bf +++ b/GlitchyEditor/src/Assets/Importers/Config.bf @@ -8,7 +8,7 @@ namespace GlitchyEditor.Assets.Importers; abstract class Config { [BonIgnore] - protected bool _changed; + protected bool _changed = true; public bool Changed => _changed; diff --git a/GlitchyEditor/src/Assets/Importers/Interfaces.bf b/GlitchyEditor/src/Assets/Importers/Interfaces.bf index add3bc6..6deb2e4 100644 --- a/GlitchyEditor/src/Assets/Importers/Interfaces.bf +++ b/GlitchyEditor/src/Assets/Importers/Interfaces.bf @@ -15,7 +15,7 @@ interface IAssetImporter /// The type that ImportedResource returns. static Type ProcessedAssetType { get; } - Result Import(StringView fullFileName, AssetIdentifier assetIdentifier, AssetImporterConfig config); + Result Import(StringView fullFileName, AssetIdentifier assetIdentifier, AssetConfig config); } interface IAssetProcessor @@ -25,7 +25,7 @@ interface IAssetProcessor /// The asset type that this processor can process. static Type ProcessedAssetType { get; } - Result Process(ImportedResource importedResource, AssetProcessorConfig config, List outProcessedResources); + Result Process(ImportedResource importedResource, AssetConfig config, List outProcessedResources); } interface IAssetExporter @@ -35,5 +35,5 @@ interface IAssetExporter /// The asset type that this exporter can export. static AssetType ExportedAssetType { get; } - Result Export(Stream stream, ProcessedResource processedObject, AssetExporterConfig config); + Result Export(Stream stream, ProcessedResource processedObject, AssetConfig config); } diff --git a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf index 112b133..ded959c 100644 --- a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf +++ b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf @@ -6,6 +6,7 @@ using GlitchyEngine.Content; using GlitchyEngine; using GlitchyEngine.Renderer; using GlitchyEngine.Math; +//using System.Threading; using GlitchyEditor.Assets.Processors; using ImGui; @@ -53,22 +54,29 @@ class TextureImporterConfig : AssetImporterConfig } } - class TextureImporter : IAssetImporter { private static readonly List _fileExtensions = new .(){".png", ".dds"} ~ delete _; public static List FileExtensions => _fileExtensions; + + public static Type ProcessedAssetType => typeof(ImportedTexture); public AssetImporterConfig CreateDefaultConfig() { return new TextureImporterConfig(); } - public Result Import(StringView fullFileName, AssetIdentifier assetIdentifier, AssetImporterConfig config) + public Result Import(StringView fullFileName, AssetIdentifier assetIdentifier, AssetConfig config) { + //Thread.Sleep(1000); - Log.EngineLogger.AssertDebug(config is TextureImporterConfig); + TextureImporterConfig textureImporterConfig = config.ImporterConfig as TextureImporterConfig; + + if (textureImporterConfig == null) + { + config.ImporterConfig = textureImporterConfig = new TextureImporterConfig(); + } ImportedTexture importedData = new ImportedTexture(new AssetIdentifier(assetIdentifier.FullIdentifier)); @@ -76,7 +84,7 @@ class TextureImporter : IAssetImporter FileStream stream = scope FileStream(); Try!(stream.Open(fullFileName, .Read, .Read)); - Result importResult = ImportTexture(stream, importedData, (TextureImporterConfig)config); + Result importResult = ImportTexture(stream, importedData, (TextureImporterConfig)textureImporterConfig); stream.Close(); diff --git a/GlitchyEditor/src/Assets/Processors/ProcessedResource.bf b/GlitchyEditor/src/Assets/Processors/ProcessedResource.bf index 505e6bb..de7de96 100644 --- a/GlitchyEditor/src/Assets/Processors/ProcessedResource.bf +++ b/GlitchyEditor/src/Assets/Processors/ProcessedResource.bf @@ -6,12 +6,17 @@ abstract class ProcessedResource { private AssetIdentifier _assetIdentifier ~ delete _; + private AssetHandle _assetHandle; + public AssetIdentifier AssetIdentifier => _assetIdentifier; + public AssetHandle AssetHandle => _assetHandle; + public abstract AssetType AssetType {get;} - public this(AssetIdentifier ownAssetIdentifier) + public this(AssetIdentifier ownAssetIdentifier, AssetHandle assetHandle) { _assetIdentifier = ownAssetIdentifier; + _assetHandle = assetHandle; } } \ No newline at end of file diff --git a/GlitchyEditor/src/Assets/Processors/TextureProcessor.bf b/GlitchyEditor/src/Assets/Processors/TextureProcessor.bf index 7a0f0d3..7e7dbd5 100644 --- a/GlitchyEditor/src/Assets/Processors/TextureProcessor.bf +++ b/GlitchyEditor/src/Assets/Processors/TextureProcessor.bf @@ -60,6 +60,12 @@ class TextureProcessorConfig : AssetProcessorConfig public List Sprites => _sprites; + public void AddSprite(SpriteDesc sprite) + { + _sprites.Add(sprite); + _changed = true; + } + public override void ShowEditor(AssetFile assetFile) { if (ImGui.BeginPopupModal("Apply Changes", null, .AlwaysAutoResize)) @@ -227,11 +233,18 @@ class TextureProcessorConfig : AssetProcessorConfig } } +[BonTarget] +enum TextureCoordinates +{ + case Pixel(int2 Min, int2 Max); + case Relative(float2 Offset, float2 Size); +} + +[BonTarget] public class SpriteDesc { public String Name ~ delete _; - public int2 TopLeft; - public int2 Size; + public TextureCoordinates TextureCoordinates; public AssetHandle AssetHandle; } @@ -308,7 +321,7 @@ class ProcessedTexture : ProcessedResource public TextureSurface[,] Surfaces; - public this(AssetIdentifier ownAssetIdentifier) : base(ownAssetIdentifier) + public this(AssetIdentifier ownAssetIdentifier, AssetHandle assetHandle) : base(ownAssetIdentifier, assetHandle) { } @@ -358,21 +371,18 @@ class ProcessedSprite : ProcessedResource { public override AssetType AssetType => .Sprite; - private AssetHandle _texture; + private AssetHandle Handle; - private int2 _topLeft; - private int2 _size; + private float4 _textureCoordinates; - public AssetHandle Texture => _texture; + public AssetHandle TextureHandle => Handle; - public int2 TopLeft => _topLeft; - public int2 Size => _size; + public float4 TextureCoordinates => _textureCoordinates; - public this(AssetIdentifier ownAssetIdentifier, AssetHandle texture, int2 topLeft, int2 size) : base(ownAssetIdentifier) + public this(AssetIdentifier ownAssetIdentifier, AssetHandle assetHandle, AssetHandle textureHandle, float4 textureCoordinates) : base(ownAssetIdentifier, assetHandle) { - _texture = texture; - _topLeft = topLeft; - _size = size; + Handle = textureHandle; + _textureCoordinates = textureCoordinates; } } @@ -385,17 +395,23 @@ class TextureProcessor : IAssetProcessor return new TextureProcessorConfig(); } - public Result Process(ImportedResource importedObject, AssetProcessorConfig config, List outProcessedResources) + public Result Process(ImportedResource importedObject, AssetConfig config, List outProcessedResources) { - Log.EngineLogger.AssertDebug(config is TextureProcessorConfig); Log.EngineLogger.AssertDebug(importedObject is ImportedTexture); - return Try!(ProcessTexture(importedObject as ImportedTexture, config as TextureProcessorConfig, outProcessedResources)); + TextureProcessorConfig textureProcessorConfig = config.ProcessorConfig as TextureProcessorConfig; + + if (textureProcessorConfig == null) + { + config.ProcessorConfig = textureProcessorConfig = new TextureProcessorConfig(); + } + + return Try!(ProcessTexture(importedObject as ImportedTexture, config, textureProcessorConfig, outProcessedResources)); } - private Result ProcessTexture(ImportedTexture importedTexture, TextureProcessorConfig config, List outProcessedResources) + private Result ProcessTexture(ImportedTexture importedTexture, AssetConfig assetConfig, TextureProcessorConfig textureConfig, List outProcessedResources) { - ProcessedTexture processedTexture = new ProcessedTexture(new AssetIdentifier(importedTexture.AssetIdentifier.FullIdentifier)); + ProcessedTexture processedTexture = new ProcessedTexture(new AssetIdentifier(importedTexture.AssetIdentifier.FullIdentifier), assetConfig.AssetHandle); processedTexture.Dimension = importedTexture.TextureInfo.Dimension; processedTexture.PixelFormat = (.)importedTexture.TextureInfo.PixelFormat; @@ -404,7 +420,7 @@ class TextureProcessor : IAssetProcessor processedTexture.Height = importedTexture.TextureInfo.Height; processedTexture.Depth = importedTexture.TextureInfo.Depth; - processedTexture.SamplerStateDescription = config.SamplerStateDescription; + processedTexture.SamplerStateDescription = textureConfig.SamplerStateDescription; processedTexture.SetSurfaceCount(importedTexture.TextureInfo.ArraySize, importedTexture.TextureInfo.MipMapCount); @@ -416,22 +432,25 @@ class TextureProcessor : IAssetProcessor processedTexture.Surfaces[loadedSurface.ArrayIndex * (processedTexture.IsCubeMap ? 6 : 1) + loadedSurface.CubeFace, loadedSurface.MipLevel] = surface; } - if (!(config.GenerateMipMaps case .No)) + if (!(textureConfig.GenerateMipMaps case .No)) { // TODO: Unpack BC-Formats to RGBA - GenerateMipMaps(processedTexture, config); + GenerateMipMaps(processedTexture, textureConfig); } + outProcessedResources.Add(processedTexture); + // TODO: Pack to BC-Format or what ever was selected. - if (config.TextureType == .Sprite) + if (textureConfig.TextureType == .Sprite) { + CreateSprites(processedTexture, textureConfig, outProcessedResources); } return .Ok; } - private void CreateSprites(ProcessedTexture processedTexture, TextureProcessorConfig config, List outProcessedResources) + private void CreateSprites(ProcessedTexture processedTexture, TextureProcessorConfig textureConfig, List outProcessedResources) { if (processedTexture.Dimension != .Texture2D) { @@ -439,15 +458,41 @@ class TextureProcessor : IAssetProcessor return; } - if (config.Sprites.Count == 0) + if (textureConfig.Sprites.Count == 0) { - /*ProcessedSprite sprite = new ProcessedSprite(processedTexture.); - - processedTexture.Sprites.Add(new SpriteDesc(){ + textureConfig.AddSprite(new SpriteDesc(){ Name = new String("Sprite"), - TopLeft = .(0, 0), - Size = .((.)processedTexture.Width, (.)processedTexture.Height) - });*/ + // TODO: Maybe add a switch for pixel perfect or relative texture coordinates + TextureCoordinates = .Pixel(.(0, 0), .((.)processedTexture.Width, (.)processedTexture.Height)), + // TODO: Get handle from central authority (contentmanager?) + AssetHandle = AssetHandle() + }); + } + + float2 textureResolution = .(processedTexture.Width, processedTexture.Height); + + for (SpriteDesc spriteDesc in textureConfig.Sprites) + { + float4 textureCoordinates = .(); + + if (spriteDesc.TextureCoordinates case .Pixel(let min, let max)) + { + textureCoordinates = float4((float2)min / textureResolution, (float2)max / textureResolution); + } + else if (spriteDesc.TextureCoordinates case .Relative(let offset, let size)) + { + textureCoordinates = .(offset, size); + } + else + { + Log.EngineLogger.Error($"Unknown texture coordinate type {spriteDesc.TextureCoordinates}"); + return; + } + + ProcessedSprite sprite = new ProcessedSprite(new AssetIdentifier(processedTexture.AssetIdentifier, spriteDesc.Name), spriteDesc.AssetHandle, processedTexture.AssetHandle, + textureCoordinates); + + outProcessedResources.Add(sprite); } } diff --git a/GlitchyEditor/src/EditorApp.bf b/GlitchyEditor/src/EditorApp.bf index 5134a82..fc4a13d 100644 --- a/GlitchyEditor/src/EditorApp.bf +++ b/GlitchyEditor/src/EditorApp.bf @@ -23,7 +23,8 @@ namespace GlitchyEditor protected override IContentManager InitContentManager() { _contentManager = new EditorContentManager(); - + + // TODO: Get rid of legacy loaders _contentManager.RegisterAssetLoader(); _contentManager.SetAsDefaultAssetLoader(".glb", ".gltf"); _contentManager.SetAssetPropertiesEditor(=> ModelAssetPropertiesEditor.Factory); @@ -40,8 +41,8 @@ namespace GlitchyEditor _contentManager.RegisterAssetProcessor(); _contentManager.RegisterAssetExporter(); - _contentManager.ConfigureDefaultProcessing(".png", ".dds"); - + _contentManager.RegisterAssetExporter(); + _contentManager.SetGlobalAssetCacheDirectory(".cache"); _contentManager.SetResourcesDirectory("Resources"); diff --git a/GlitchyEditor/src/EditorContentManager.bf b/GlitchyEditor/src/EditorContentManager.bf index 784386f..a9d9e21 100644 --- a/GlitchyEditor/src/EditorContentManager.bf +++ b/GlitchyEditor/src/EditorContentManager.bf @@ -198,15 +198,6 @@ class EditorContentManager : IContentManager return null; } - // TODO: This type sucks! - public Result<(IAssetImporter Importer, IAssetProcessor Processor, IAssetExporter Exporter)> GetDefaultProcessors(StringView fileExtension) - { - if (_defaultAssetProcessors.TryGetValue(fileExtension, let value)) - return value; - - return .Err; - } - private append List _supportedExtensions = .() ~ ClearAndDeleteItems!(_); private append List _assetLoaders = .() ~ ClearAndDeleteItems!(_); private append Dictionary _defaultAssetLoaders = .(); @@ -220,12 +211,8 @@ class EditorContentManager : IContentManager private append List _assetImporters = .() ~ ClearAndDeleteItems!(_); private append Dictionary _extensionToAssetImporter = .(); - private append List _assetProcessors = .() ~ ClearAndDeleteItems!(_); - private append Dictionary _importedResourceToAssetProcessor = .(); - //private append List _assetExporters = .() ~ ClearAndDeleteItems!(_); - private append Dictionary _defaultAssetProcessors = .() ~ delete:append _; - - private append Dictionary _assetExporters = .() ~ ClearDictionaryAndDeleteValues!(_); + private append Dictionary _importedResourceToAssetProcessor = .() ~ ClearDictionaryAndDeleteValues!(_); + private append Dictionary _assetTypeToAssetExporter = .() ~ ClearDictionaryAndDeleteValues!(_); public void RegisterAssetLoader() where T : new, class, IAssetLoader { @@ -242,7 +229,6 @@ class EditorContentManager : IContentManager public void RegisterAssetImporter() where T : new, class, IAssetImporter { T assetImporter = new T(); - _assetImporters.Add(assetImporter); for (StringView ext in T.FileExtensions) @@ -255,18 +241,16 @@ class EditorContentManager : IContentManager public void RegisterAssetProcessor() where T : new, class, IAssetProcessor { - T assetProcessor = new T(); + Log.EngineLogger.AssertDebug(!_importedResourceToAssetProcessor.ContainsKey(T.ProcessedAssetType), "Cannot register multiple processor for the same imported resource type."); - _assetProcessors.Add(assetProcessor); + _importedResourceToAssetProcessor.Add(T.ProcessedAssetType, new T()); } public void RegisterAssetExporter() where T : new, class, IAssetExporter { - T assetExporter = new T(); + Log.EngineLogger.AssertDebug(!_assetTypeToAssetExporter.ContainsKey(T.ExportedAssetType), "Cannot register multiple exporters for one asset type."); - Log.EngineLogger.AssertDebug(_assetExporters.ContainsKey(T.ExportedAssetType), "Cannot register multiple exporters for one asset type."); - - _assetExporters.Add(T.ExportedAssetType, assetExporter); + _assetTypeToAssetExporter.Add(T.ExportedAssetType, new T()); } public IAssetImporter GetAssetImporter(StringView fileExtension) @@ -278,12 +262,42 @@ class EditorContentManager : IContentManager return null; } - - public IAssetImporter GetAssetProcessor(Type importedResourceType) + + public IAssetImporter GetAssetImporter(AssetFile file) { - if (_extensionToAssetImporter.TryGetValue(fileExtension, let importer)) + IAssetImporter result = null; + + String typeName = scope .(128); + + for (IAssetImporter importer in _extensionToAssetImporter.Values) { - return importer; + importer.GetType().GetName(typeName..Clear()); + + if (typeName == file.AssetConfig.Importer) + { + result = importer; + break; + } + } + + return result; + } + + public IAssetProcessor GetAssetProcessor(Type importedResourceType) + { + if (_importedResourceToAssetProcessor.TryGetValue(importedResourceType, let processor)) + { + return processor; + } + + return null; + } + + public IAssetExporter GetAssetExporter(AssetType assetType) + { + if (_assetTypeToAssetExporter.TryGetValue(assetType, let exporter)) + { + return exporter; } return null; @@ -317,64 +331,6 @@ class EditorContentManager : IContentManager } } } - - public void ConfigureDefaultProcessing(params Span fileExtensions) where TImport : IAssetImporter where TProcess : IAssetProcessor where TExport : IAssetExporter - { - for (var ext in fileExtensions) - { - // Find file extension in registered file extensions - String foundExtension = null; - - for (var supportedExt in _supportedExtensions) - { - if (supportedExt == ext) - { - foundExtension = supportedExt; - break; - } - } - - Log.EngineLogger.Assert(foundExtension != null, "File Extension is not registered."); - - //IAssetImporter importer = _assetImporters.Where((i) => i.GetType() == typeof(TImport)).First(); - //IAssetProcessor processor = _assetProcessors.Where((i) => i.GetType() == typeof(TProcess)).First(); - //IAssetExporter exporter = _assetExporters.Where((i) => i.GetType() == typeof(TExport)).First(); - - IAssetImporter importer = null; - IAssetProcessor processor = null; - IAssetExporter exporter = null; - - for (var i in _assetImporters) - { - if (i.GetType() == typeof(TImport)) - { - importer = i; - break; - } - } - - for (var i in _assetProcessors) - { - if (i.GetType() == typeof(TProcess)) - { - processor = i; - break; - } - } - - // TODO: How do we get the exporter for the config? - /*for (var i in _assetExporters) - { - if (i.GetType() == typeof(TExport)) - { - exporter = i; - break; - } - }*/ - - _defaultAssetProcessors[foundExtension] = (importer, processor, exporter); - } - } public void SetAssetPropertiesEditor(Type assetLoaderType, function AssetPropertiesEditor(AssetFile) editorFactory) { @@ -602,7 +558,7 @@ class EditorContentManager : IContentManager if (resultNode case .Err) { - Log.EngineLogger.Error($"Could not find asset with handle {handle}."); + Log.EngineLogger.Error($"Could not find asset with handle {handle} in hierarchy."); return .Invalid; } @@ -748,76 +704,6 @@ class EditorContentManager : IContentManager return assetLoader; } - public IAssetImporter GetAssetImporter(AssetFile file) - { - IAssetImporter result = null; - - String typeName = scope .(128); - - for (IAssetImporter importer in _assetImporters) - { - importer.GetType().GetName(typeName..Clear()); - - if (typeName == file.AssetConfig.Importer) - { - result = importer; - break; - } - } - - return result; - } - - public IAssetProcessor GetAssetProcessor(AssetFile file) - { - IAssetProcessor result = null; - - String typeName = scope .(128); - - for (IAssetProcessor processor in _assetProcessors) - { - processor.GetType().GetName(typeName..Clear()); - - if (typeName == file.AssetConfig.Processor) - { - result = processor; - break; - } - } - - return result; - } - /* - public IAssetExporter GetAssetExporter(AssetFile file) - { - IAssetExporter result = null; - - String typeName = scope .(128); - - for (IAssetExporter exporter in _assetExporters) - { - exporter.GetType().GetName(typeName..Clear()); - - if (typeName == file.AssetConfig.Exporter) - { - result = exporter; - break; - } - } - - return result; - } - */ - public IAssetExporter GetAssetExporter(AssetType assetType) - { - if (_assetExporters.TryGetValue(assetType, let exporter)) - { - return exporter; - } - - return null; - } - public enum SaveAssetError { case Unknown; @@ -1057,6 +943,7 @@ class EditorContentManager : IContentManager // TODO: obviously use a map or something... TextureLoader textureLoader = new .() ~ delete _; + SpriteLoader spriteLoader = new .() ~ delete _; private IProcessedAssetLoader GetLoader(AssetType assetType) { @@ -1064,6 +951,8 @@ class EditorContentManager : IContentManager { case .Texture: return textureLoader; + case .Sprite: + return spriteLoader; default: return null; } diff --git a/GlitchyEngine/src/Content/AssetIdentifier.bf b/GlitchyEngine/src/Content/AssetIdentifier.bf index 2dc4cf3..6439618 100644 --- a/GlitchyEngine/src/Content/AssetIdentifier.bf +++ b/GlitchyEngine/src/Content/AssetIdentifier.bf @@ -35,13 +35,13 @@ public class AssetIdentifier [AllowAppend] public this(StringView assetIdentifier, StringView subAssetIdentifier) { - String identifier = append String(assetIdentifier.Length + 1 + subAssetIdentifier.Length); - identifier.AppendF($"{assetIdentifier}:{subAssetIdentifier}"); + String fullIdentifier = append String(assetIdentifier.Length + 1 + subAssetIdentifier.Length); + fullIdentifier.AppendF($"{assetIdentifier}:{subAssetIdentifier}"); - Fixup(identifier); + Fixup(fullIdentifier); + _fullIdentifier = fullIdentifier; _subassetSeperator = _fullIdentifier.IndexOf(':', 0); - } public static StringView operator implicit(AssetIdentifier identifier) => identifier.FullIdentifier; diff --git a/GlitchyEngine/src/Content/ContentManager.bf b/GlitchyEngine/src/Content/ContentManager.bf index c813f82..5906740 100644 --- a/GlitchyEngine/src/Content/ContentManager.bf +++ b/GlitchyEngine/src/Content/ContentManager.bf @@ -105,6 +105,19 @@ namespace GlitchyEngine.Content return (T)asset; } + + /// Loads the specified asset with the given contentManager or the current applications content manager. + public static Asset GetAsset(AssetHandle handle, IContentManager contentManager = null) + { + var contentManager; + + if (contentManager == null) + contentManager = Application.Instance.ContentManager; + + Asset asset = contentManager.GetAsset(null, handle); + + return asset; + } public static AssetHandle ManageAsset(Asset asset, IContentManager contentManager = null) { diff --git a/GlitchyEngine/src/Content/SpriteLoader.bf b/GlitchyEngine/src/Content/SpriteLoader.bf new file mode 100644 index 0000000..e45a6ad --- /dev/null +++ b/GlitchyEngine/src/Content/SpriteLoader.bf @@ -0,0 +1,19 @@ +using System; +using System.IO; +using System.Collections; +using GlitchyEngine.Math; +using GlitchyEngine.Renderer; +using System.Threading; + +namespace GlitchyEngine.Content; + +class SpriteLoader : IProcessedAssetLoader +{ + public Result Load(Stream dataStream) + { + AssetHandle textureHandle = Try!(dataStream.Read()); + float4 textureCoordinates = Try!(dataStream.Read()); + + return new Sprite(textureHandle, textureCoordinates); + } +} diff --git a/GlitchyEngine/src/Content/TextureLoader.bf b/GlitchyEngine/src/Content/TextureLoader.bf index afc44b3..92b6dd7 100644 --- a/GlitchyEngine/src/Content/TextureLoader.bf +++ b/GlitchyEngine/src/Content/TextureLoader.bf @@ -3,6 +3,7 @@ using System.IO; using System.Collections; using GlitchyEngine.Math; using GlitchyEngine.Renderer; +using System.Threading; namespace GlitchyEngine.Content; @@ -73,6 +74,8 @@ class TextureLoader : IProcessedAssetLoader result.SamplerState = samplerState; } + //Thread.Sleep(1000); + return result; } diff --git a/GlitchyEngine/src/Renderer/Renderer2D.bf b/GlitchyEngine/src/Renderer/Renderer2D.bf index 5d3f27e..cbbaf26 100644 --- a/GlitchyEngine/src/Renderer/Renderer2D.bf +++ b/GlitchyEngine/src/Renderer/Renderer2D.bf @@ -1091,7 +1091,22 @@ namespace GlitchyEngine.Renderer public static void DrawSprite(Matrix transform, SpriteRendererComponent* spriteRenderer, uint32 entityId) { - DrawQuad(transform, spriteRenderer.Sprite.Get() ?? s_whiteTexture, spriteRenderer.Color, spriteRenderer.UvTransform, entityId); + Asset asset = Content.GetAsset(spriteRenderer.Sprite); + + Texture2D spriteTexture = null; + float4 uvTransform = spriteRenderer.UvTransform; + + if (Texture2D texture = asset as Texture2D) + { + spriteTexture = texture; + } + else if (Sprite sprite = asset as Sprite) + { + spriteTexture = Content.GetAsset(sprite.TextureHandle); + uvTransform = sprite.TextureCoordinates; + } + + DrawQuad(transform, spriteTexture ?? s_whiteTexture, spriteRenderer.Color, uvTransform, entityId); } public static void DrawCircle(Matrix transform, CircleRendererComponent* spriteRenderer, uint32 entityId) diff --git a/GlitchyEngine/src/Renderer/Sprite.bf b/GlitchyEngine/src/Renderer/Sprite.bf new file mode 100644 index 0000000..6525b25 --- /dev/null +++ b/GlitchyEngine/src/Renderer/Sprite.bf @@ -0,0 +1,20 @@ +using GlitchyEngine.Content; +using GlitchyEngine.Math; + +namespace GlitchyEngine.Renderer; + +class Sprite : Asset +{ + private AssetHandle _textureHandle; + private float4 _textureCoordinates; + + public AssetHandle TextureHandle => _textureHandle; + + public float4 TextureCoordinates => _textureCoordinates; + + public this(AssetHandle textureHandle, float4 textureCoordinates) + { + _textureHandle = textureHandle; + _textureCoordinates = textureCoordinates; + } +} \ No newline at end of file diff --git a/GlitchyEngine/src/World/Components/Components.bf b/GlitchyEngine/src/World/Components/Components.bf index 3e02122..17c79eb 100644 --- a/GlitchyEngine/src/World/Components/Components.bf +++ b/GlitchyEngine/src/World/Components/Components.bf @@ -45,7 +45,7 @@ namespace GlitchyEngine.World [Component("Sprite Renderer")] struct SpriteRendererComponent { - public AssetHandle Sprite = .Invalid; + public AssetHandle Sprite = .Invalid; public ColorRGBA Color = .White; public float4 UvTransform = .(0, 0, 1, 1);