diff --git a/GlitchyEditor/src/Assets/MaterialAssetLoader.bf b/GlitchyEditor/src/Assets/MaterialAssetLoader.bf index 9433146..a87c73b 100644 --- a/GlitchyEditor/src/Assets/MaterialAssetLoader.bf +++ b/GlitchyEditor/src/Assets/MaterialAssetLoader.bf @@ -84,11 +84,10 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor { StringView path = .((char8*)payload.Data, (int)payload.DataSize); - AssetHandle newTexture = Content.LoadAsset(path);//new Texture2D(path, true)) - + AssetHandle newTexture = Content.LoadAsset(path); + newTexture.Get().SamplerState = SamplerStateManager.AnisotropicWrap; - material.SetTexture(texture.key, newTexture); - // TODO!!! + material.SetTexture(texture.key, newTexture.Cast()); } ImGui.EndDragDropTarget(); @@ -98,7 +97,6 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor private void ShowVariables(Material material, Effect effect) { - for (let (name, arguments) in effect.[Friend]_variableDescriptions) { let variable = effect.Variables[name]; @@ -350,9 +348,9 @@ class MaterialAssetLoader : IAssetLoader, IAssetSaver //, IReloadingAssetLoader for (let (slotName, textureIdentifier) in materialFile.Textures) { - Texture texture = Content.GetAsset(contentManager.LoadAsset(textureIdentifier), contentManager); + AssetHandle texture = contentManager.LoadAsset(textureIdentifier); - if (texture == null) + if (texture.IsInvalid) { Log.EngineLogger.Error($"Failed to load texture \"{textureIdentifier}\"."); // TODO: LoadAsset should return an error texture. @@ -408,7 +406,9 @@ class MaterialAssetLoader : IAssetLoader, IAssetSaver //, IReloadingAssetLoader for (let (slotName, texture) in material.[Friend]_textures) { - materialFile.Textures.Add(new String(slotName), new String(texture?.Identifier ?? "")); + Texture textureAsset = texture.Get(); + + materialFile.Textures.Add(new String(slotName), new String(textureAsset?.Identifier ?? "")); } Effect effect = material.Effect; diff --git a/GlitchyEditor/src/EditWindows/PropertiesWindow.bf b/GlitchyEditor/src/EditWindows/PropertiesWindow.bf index 09bdf42..616b479 100644 --- a/GlitchyEditor/src/EditWindows/PropertiesWindow.bf +++ b/GlitchyEditor/src/EditWindows/PropertiesWindow.bf @@ -19,7 +19,6 @@ class PropertiesWindow : EditorWindow private append String _selectedFileName = .(); private AssetHandle _currentAssetHandle; - //private Asset _currentAsset; public this(Editor editor) { @@ -83,14 +82,6 @@ class PropertiesWindow : EditorWindow _currentAssetHandle = _editor.ContentManager.LoadAsset(assetFile.Identifier); } - /*if (asset != _currentAsset) - { - _currentAsset?.ReleaseRef(); - _currentAsset = asset; - _currentAsset?.AddRef(); - }*/ - - // TODO: allow changing AssetLoader // assetFile.AssetConfig.AssetLoade diff --git a/GlitchyEngine/src/Content/AssetHandle.bf b/GlitchyEngine/src/Content/AssetHandle.bf index e8e1b2d..3903f46 100644 --- a/GlitchyEngine/src/Content/AssetHandle.bf +++ b/GlitchyEngine/src/Content/AssetHandle.bf @@ -13,6 +13,9 @@ struct AssetHandle : IHashable /// Defines an asset that is invalid. public const AssetHandle Invalid = .(UUID(0xAAAA'AAAA'AAAA'AAAA)); + public bool IsValid => this == .Invalid; + public bool IsInvalid => !IsValid; + /// Create a new random AssetHandle public this() { @@ -46,6 +49,9 @@ struct AssetHandle where T : Asset public const Self Invalid = .(); + public bool IsValid => this == .Invalid; + public bool IsInvalid => !IsValid; + public this(AssetHandle handle, IContentManager contentManager = null) { _handle = handle; @@ -81,20 +87,9 @@ struct AssetHandle where T : Asset return handle.Get(); } - public T Get(IContentManager contentManager = null) mut + public T Get(IContentManager contentManager = null) { - // We only care whether we are in a different frame -> we only compare the lower 8 bits. - //uint8 actualFrame = (uint8)Application.Get().GameTime.FrameCount; - //var actualActualFrame = Application.Get().GameTime.FrameCount; - - //if (actualFrame != _currentFrame) - { - _asset = Content.GetAsset(_handle, contentManager == null ? _contentManager : contentManager); - //_currentFrame = actualFrame; - //_actualCurrentFrame = Application.Get().GameTime.FrameCount; - } - - return _asset; + return Content.GetAsset(_handle, contentManager == null ? _contentManager : contentManager); } [Comptime, OnCompile(.TypeInit)] @@ -250,4 +245,17 @@ struct AssetHandle where T : Asset Compiler.EmitTypeBody(typeof(Self), code); } } + + public AssetHandle Cast() + where NewT : Asset + where T : NewT + { + return AssetHandle(this._handle, this._contentManager); + } + + // TODO: Cast up? + public AssetHandle Cast() where NewT : T + { + return AssetHandle(this._handle, this._contentManager); + } } diff --git a/GlitchyEngine/src/Renderer/Material.bf b/GlitchyEngine/src/Renderer/Material.bf index 4f62000..7822bfc 100644 --- a/GlitchyEngine/src/Renderer/Material.bf +++ b/GlitchyEngine/src/Renderer/Material.bf @@ -14,7 +14,7 @@ public class Material : Asset private uint8[] _rawVariables ~ delete _; - private Dictionary _textures = new .(); + private Dictionary> _textures = new .() ~ delete _; private Dictionary _variables = new .() ~ delete _; @@ -29,25 +29,17 @@ public class Material : Asset // Get texture slots from effect for(let (name, entry) in _effect.Textures) { - // TODO: Do we want to be able to define textures in the shader? - /*var texture = entry.BoundTexture; - texture.AddRef();*/ + // TODO: We need to be able to define default textures in the shader. + // At least things like "Black", "White", "Normal" + // At best whole paths. Shouldn't be that hard to do... + /*var texture = entry.BoundTexture;*/ - _textures.Add(name, null); + _textures.Add(name, .Invalid); } InitRawData(); } - public ~this() - { - for(let (name, texture) in _textures) - { - texture?.ReleaseRef(); - } - - delete _textures; - } /** @brief Initializes the raw data array for the variables. */ @@ -90,13 +82,13 @@ public class Material : Asset * @param name The name of the texture to set. * @param texture The texture to bind to the effect. */ - public void SetTexture(String name, Texture texture) + public void SetTexture(String name, AssetHandle texture) { if(_textures.TryGetValue(name, var entry)) { - entry?.ReleaseRef(); + //entry?.ReleaseRef(); _textures[name] = texture; - texture?.AddRef(); + //texture?.AddRef(); } else {