From edcec02e94fafef1fe3602d58122bf8d663cef87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 19 Mar 2023 01:04:32 +0100 Subject: [PATCH] Fixed AssetHandle serialization - Serialize MeshComponents - Fixed handling renamed files --- GlitchyEditor/content/Scenes/Simple3D.scene | 112 +++++++++--- GlitchyEditor/content/Scenes/physics2D.scene | 66 ++++---- .../content/Textures/RocketMaterial.mat | 31 ++++ .../content/Textures/RocketMaterial.mat.ass | 4 + .../TestMat/rustediron2_albedo.png.ass | 16 +- .../TestMat/rustediron2_metallic.png.ass | 12 +- .../TestMat/rustediron2_normal.png.ass | 12 +- .../TestMat/rustediron2_roughness.png.ass | 11 +- .../content/Textures/TestMaterial.mat | 8 +- GlitchyEditor/content/Textures/rocket.dds.ass | 5 +- GlitchyEditor/src/Assets/AssetHierarchy.bf | 24 ++- GlitchyEditor/src/EditorContentManager.bf | 21 +-- GlitchyEngine/src/Content/Asset.bf | 10 +- GlitchyEngine/src/Content/AssetHandle.bf | 75 +++++++++ GlitchyEngine/src/Renderer/MeshComponent.bf | 23 +-- GlitchyEngine/src/World/Components.bf | 16 +- .../src/World/MeshRendererComponent.bf | 22 +-- GlitchyEngine/src/World/Scene.bf | 159 +----------------- GlitchyEngine/src/World/SceneSerializer.bf | 18 +- 19 files changed, 324 insertions(+), 321 deletions(-) create mode 100644 GlitchyEditor/content/Textures/RocketMaterial.mat create mode 100644 GlitchyEditor/content/Textures/RocketMaterial.mat.ass diff --git a/GlitchyEditor/content/Scenes/Simple3D.scene b/GlitchyEditor/content/Scenes/Simple3D.scene index e5ff043..421c22e 100644 --- a/GlitchyEditor/content/Scenes/Simple3D.scene +++ b/GlitchyEditor/content/Scenes/Simple3D.scene @@ -30,10 +30,10 @@ } }, MeshComponent = { - Mesh = "Models\\sphere.glb" + Mesh = "Models/sphere.glb" }, MeshRendererComponent = { - Material = "Textures\\TestMaterial.mat" + Material = "Textures/TestMaterial.mat" } }, { @@ -48,20 +48,20 @@ Z = -4 }, Rotation = { - X = 0.614328, - Y = 0.239776, - Z = 0.031567, - W = 0.751074 + X = 0.614328027, + Y = 0.239776045, + Z = 0.0315669999, + W = 0.751073837 }, Scale = { - X = 0.999999, + X = 0.999998868, Y = 1, - Z = 1.000001 + Z = 1.00000095 }, EditorEulerRotation = { - X = 1.308998, - Y = 0.349066, - Z = 0.349066 + X = 1.30899811, + Y = 0.349065989, + Z = 0.349065989 } }, LightComponent = { @@ -69,8 +69,8 @@ Illuminance = 10, Color = { R = 1, - G = 0.991772, - B = 0.740862 + G = 0.991771996, + B = 0.74086225 } } }, @@ -103,10 +103,10 @@ } }, MeshComponent = { - Mesh = "Models\\plane.glb" + Mesh = "Models/plane.glb" }, MeshRendererComponent = { - Material = "Textures\\TestMaterial.mat" + Material = "Textures/TestMaterial.mat" } }, { @@ -121,10 +121,10 @@ Z = -5 }, Rotation = { - X = 0.21644, + X = 0.216440007, Y = 0, Z = 0, - W = 0.976296 + W = 0.976296008 }, Scale = { X = 1, @@ -132,7 +132,7 @@ Z = 1 }, EditorEulerRotation = { - X = 0.436332, + X = 0.436332017, Y = 0, Z = 0 } @@ -140,15 +140,85 @@ CameraComponent = { Primary = true, ProjectionType = .InfinitePerspective, - PerspectiveFovY = 1.047198, - PerspectiveNearPlane = 0.1, + PerspectiveFovY = 1.30899692, + PerspectiveNearPlane = 0.100000001, PerspectiveFarPlane = 10000, OrthographicHeight = 10, OrthographicNearPlane = 0, OrthographicFarPlane = 10, - AspectRatio = 2.156692, + AspectRatio = 2.19888878, FixedAspectRatio = false } + }, + { + Id = 420718992779301759, + NameComponent = { + Name = "Entity" + }, + TransformComponent = { + Position = { + X = 3, + Y = 0.501076996, + Z = -0.669378757 + }, + Rotation = { + X = 0.18301262, + Y = -0.683013558, + Z = 0.683012128, + W = 0.183013007 + }, + Scale = { + X = 1, + Y = 1, + Z = 1 + }, + EditorEulerRotation = { + X = 1.57079446, + Y = -2.61799312, + Z = 0 + } + }, + MeshComponent = { + Mesh = "Models/plane.glb" + }, + MeshRendererComponent = { + Material = "" + } + }, + { + Id = 8080288871271083510, + NameComponent = { + Name = "Rocket" + }, + TransformComponent = { + Position = { + X = 2.37353587, + Y = 0.629615188, + Z = -1.34831977 + }, + Rotation = { + X = 0.270598024, + Y = -0.65328145, + Z = 0.65328151, + W = 0.270598054 + }, + Scale = { + X = 0.999999881, + Y = 0.999999821, + Z = 0.999999702 + }, + EditorEulerRotation = { + X = 1.57079637, + Y = -2.3561945, + Z = 8.94069743e-08 + } + }, + MeshComponent = { + Mesh = "Models/plane.glb" + }, + MeshRendererComponent = { + Material = "Textures/RocketMaterial.mat" + } } ] } \ No newline at end of file diff --git a/GlitchyEditor/content/Scenes/physics2D.scene b/GlitchyEditor/content/Scenes/physics2D.scene index 8f19e80..ccb3ee2 100644 --- a/GlitchyEditor/content/Scenes/physics2D.scene +++ b/GlitchyEditor/content/Scenes/physics2D.scene @@ -6,15 +6,15 @@ NameComponent = { Name = "Quad" }, - SpriterRendererComponent = { + SpriteRendererComponent = { Color = { R = 0, - G = 0.551178, - B = 0.328787, + G = 0.5511778, + B = 0.32878691, A = 1 }, IsCircle = false, - Sprite = "\\Textures\\TestMat\\rustediron2_albedo.png", + Sprite = "Textures/TestMat/rustediron2_albedo.png", UvTransform = { X = 0, Y = 0, @@ -69,15 +69,15 @@ NameComponent = { Name = "Floor" }, - SpriterRendererComponent = { + SpriteRendererComponent = { Color = { R = 1, - G = 0.941886, - B = 0.401485, + G = 0.941886246, + B = 0.401484907, A = 1 }, IsCircle = false, - Sprite = null, + Sprite = "", UvTransform = { X = 0, Y = 0, @@ -158,13 +158,13 @@ CameraComponent = { Primary = true, ProjectionType = .InfinitePerspective, - PerspectiveFovY = 1.308997, - PerspectiveNearPlane = 0.1, + PerspectiveFovY = 1.30899692, + PerspectiveNearPlane = 0.100000001, PerspectiveFarPlane = 10000, OrthographicHeight = 10, OrthographicNearPlane = 0, OrthographicFarPlane = 10, - AspectRatio = 2.969697, + AspectRatio = 2.19888878, FixedAspectRatio = false } }, @@ -173,15 +173,15 @@ NameComponent = { Name = "Circle" }, - SpriterRendererComponent = { + SpriteRendererComponent = { Color = { - R = 1, - G = 1, + R = 0.299884498, + G = 0.972924948, B = 1, A = 1 }, IsCircle = true, - Sprite = "\\Textures\\rocket.png", + Sprite = "", UvTransform = { X = 0, Y = 0, @@ -191,8 +191,8 @@ }, TransformComponent = { Position = { - X = -0.121203, - Y = 0.66016, + X = -0.12120305, + Y = 0.660160244, Z = 0 }, Rotation = { @@ -235,34 +235,34 @@ }, TransformComponent = { Position = { - X = 3.883276, + X = 3.88327599, Y = 0, - Z = -0.808795 + Z = -0.808795214 }, Rotation = { - X = 0.497987, - Y = -0.103421, - Z = 0.15538, - W = 0.846859 + X = 0.497987002, + Y = -0.103420995, + Z = 0.155380026, + W = 0.846859217 }, Scale = { - X = 0.999998, + X = 0.999997795, Y = 1, Z = 1 }, EditorEulerRotation = { - X = 1.090894, - Y = -0.340794, - Z = 0.160858 + X = 1.09089446, + Y = -0.340793997, + Z = 0.160857946 } }, LightComponent = { LightType = .Directional, - Illuminance = 12.9, + Illuminance = 12.8999996, Color = { - R = 0.985467, + R = 0.985467017, G = 1, - B = 0.569979 + B = 0.569978774 } } }, @@ -275,7 +275,7 @@ Position = { X = 0, Y = 0, - Z = -1.523338 + Z = -1.47202551 }, Rotation = { X = 0, @@ -295,10 +295,10 @@ } }, MeshComponent = { - Mesh = "\\Models\\sphere.glb" + Mesh = "Models/sphere.glb" }, MeshRendererComponent = { - Material = "\\Textures\\TestMaterial.mat" + Material = "Textures/TestMaterial.mat" } } ] diff --git a/GlitchyEditor/content/Textures/RocketMaterial.mat b/GlitchyEditor/content/Textures/RocketMaterial.mat new file mode 100644 index 0000000..52eb50f --- /dev/null +++ b/GlitchyEditor/content/Textures/RocketMaterial.mat @@ -0,0 +1,31 @@ +{ + Effect = "Shaders/myEffect.hlsl", + Textures = [ + "AlbedoTexture": "Textures/rocket.png", + "NormalTexture": "Textures/TestMat/rustediron2_normal.png", + "MetallicTexture": "Textures/TestMat/rustediron2_metallic.png", + "RoughnessTexture": "Textures/TestMat/rustediron2_roughness.png" + ], + Variables = [ + "AlbedoColor": .ColorRGBA{ + Value = { + R = 1, + G = 1, + B = 1, + A = 1 + } + }, + "NormalScaling": .Float2{ + Value = { + X = 1, + Y = 1 + } + }, + "MetallicFactor": .Float{ + Value = 0 + }, + "RoughnessFactor": .Float{ + Value = 1 + } + ] +} \ No newline at end of file diff --git a/GlitchyEditor/content/Textures/RocketMaterial.mat.ass b/GlitchyEditor/content/Textures/RocketMaterial.mat.ass new file mode 100644 index 0000000..4eb3f21 --- /dev/null +++ b/GlitchyEditor/content/Textures/RocketMaterial.mat.ass @@ -0,0 +1,4 @@ +{ + AssetLoader = "MaterialAssetLoader", + Config = (GlitchyEditor.Assets.ModelAssetLoaderConfig){}/* No reflection data for GlitchyEditor.Assets.ModelAssetLoaderConfig. Add [BonTarget] or force it */ +} \ No newline at end of file diff --git a/GlitchyEditor/content/Textures/TestMat/rustediron2_albedo.png.ass b/GlitchyEditor/content/Textures/TestMat/rustediron2_albedo.png.ass index 1b89c85..f0b6937 100644 --- a/GlitchyEditor/content/Textures/TestMat/rustediron2_albedo.png.ass +++ b/GlitchyEditor/content/Textures/TestMat/rustediron2_albedo.png.ass @@ -7,16 +7,20 @@ MinFilter = .Anisotropic, MagFilter = .Anisotropic, MipFilter = .Anisotropic, + FilterMode = .Default, ComparisonFunction = .Never, AddressModeU = .Wrap, - AddressModeV = .Border, - AddressModeW = .Clamp, - MipMaxLOD = 160, + AddressModeV = .Wrap, + AddressModeW = .Wrap, + MipLODBias = 0, + MipMinLOD = 0, + MipMaxLOD = 3, MaxAnisotropy = 16, BorderColor = { - R = 0.756863, - G = 0.2, - A = 0.956863 + R = 1, + G = 1, + B = 1, + A = 1 } } } diff --git a/GlitchyEditor/content/Textures/TestMat/rustediron2_metallic.png.ass b/GlitchyEditor/content/Textures/TestMat/rustediron2_metallic.png.ass index 1f70168..ccdb9a9 100644 --- a/GlitchyEditor/content/Textures/TestMat/rustediron2_metallic.png.ass +++ b/GlitchyEditor/content/Textures/TestMat/rustediron2_metallic.png.ass @@ -1,16 +1,20 @@ { AssetLoader = "EditorTextureAssetLoader", Config = (GlitchyEditor.Assets.EditorTextureAssetLoaderConfig){ + _generateMipMaps = false, + _isSrgb = false, _samplerStateDescription = { MinFilter = .Linear, MagFilter = .Linear, MipFilter = .Linear, + FilterMode = .Default, ComparisonFunction = .Never, - AddressModeU = .Clamp, - AddressModeV = .Clamp, + AddressModeU = .Wrap, + AddressModeV = .Wrap, AddressModeW = .Clamp, - MipMinLOD = -340282346638528859811704183484516925440, - MipMaxLOD = 340282346638528859811704183484516925440, + MipLODBias = 0, + MipMinLOD = -3.40282347e+38, + MipMaxLOD = 3.40282347e+38, MaxAnisotropy = 1, BorderColor = { R = 1, diff --git a/GlitchyEditor/content/Textures/TestMat/rustediron2_normal.png.ass b/GlitchyEditor/content/Textures/TestMat/rustediron2_normal.png.ass index 1f70168..d366f65 100644 --- a/GlitchyEditor/content/Textures/TestMat/rustediron2_normal.png.ass +++ b/GlitchyEditor/content/Textures/TestMat/rustediron2_normal.png.ass @@ -1,16 +1,20 @@ { AssetLoader = "EditorTextureAssetLoader", Config = (GlitchyEditor.Assets.EditorTextureAssetLoaderConfig){ + _generateMipMaps = false, + _isSrgb = false, _samplerStateDescription = { MinFilter = .Linear, MagFilter = .Linear, MipFilter = .Linear, + FilterMode = .Default, ComparisonFunction = .Never, - AddressModeU = .Clamp, - AddressModeV = .Clamp, + AddressModeU = .Wrap, + AddressModeV = .Wrap, AddressModeW = .Clamp, - MipMinLOD = -340282346638528859811704183484516925440, - MipMaxLOD = 340282346638528859811704183484516925440, + MipLODBias = 2.5999999, + MipMinLOD = -Infinity, + MipMaxLOD = Infinity, MaxAnisotropy = 1, BorderColor = { R = 1, diff --git a/GlitchyEditor/content/Textures/TestMat/rustediron2_roughness.png.ass b/GlitchyEditor/content/Textures/TestMat/rustediron2_roughness.png.ass index 0d007db..dd43ef6 100644 --- a/GlitchyEditor/content/Textures/TestMat/rustediron2_roughness.png.ass +++ b/GlitchyEditor/content/Textures/TestMat/rustediron2_roughness.png.ass @@ -1,17 +1,20 @@ { AssetLoader = "EditorTextureAssetLoader", Config = (GlitchyEditor.Assets.EditorTextureAssetLoaderConfig){ + _generateMipMaps = false, _isSrgb = true, _samplerStateDescription = { MinFilter = .Linear, MagFilter = .Linear, MipFilter = .Linear, + FilterMode = .Default, ComparisonFunction = .Never, - AddressModeU = .Clamp, - AddressModeV = .Clamp, + AddressModeU = .Wrap, + AddressModeV = .Wrap, AddressModeW = .Clamp, - MipMinLOD = -340282346638528859811704183484516925440, - MipMaxLOD = 340282346638528859811704183484516925440, + MipLODBias = 0, + MipMinLOD = -3.40282347e+38, + MipMaxLOD = 3.40282347e+38, MaxAnisotropy = 1, BorderColor = { R = 1, diff --git a/GlitchyEditor/content/Textures/TestMaterial.mat b/GlitchyEditor/content/Textures/TestMaterial.mat index aa4142c..a8d3b45 100644 --- a/GlitchyEditor/content/Textures/TestMaterial.mat +++ b/GlitchyEditor/content/Textures/TestMaterial.mat @@ -1,10 +1,10 @@ { - Effect = "Shaders/myEffect.hlsl", + Effect = "Shaders\\myEffect.hlsl", Textures = [ - "AlbedoTexture": "Textures/TestMat/rustediron2_albedo.png", + "AlbedoTexture": "Textures\\TestMat\\rustediron2_albedo.png", "NormalTexture": "Textures\\TestMat\\rustediron2_normal.png", - "MetallicTexture": "", - "RoughnessTexture": "" + "MetallicTexture": "Textures\\TestMat\\rustediron2_metallic.png", + "RoughnessTexture": "Textures\\TestMat\\rustediron2_roughness.png" ], Variables = [ "AlbedoColor": .ColorRGBA{ diff --git a/GlitchyEditor/content/Textures/rocket.dds.ass b/GlitchyEditor/content/Textures/rocket.dds.ass index 6392b9b..c350a18 100644 --- a/GlitchyEditor/content/Textures/rocket.dds.ass +++ b/GlitchyEditor/content/Textures/rocket.dds.ass @@ -4,13 +4,14 @@ _isSrgb = true, _samplerStateDescription = { MinFilter = .Linear, + MagFilter = .Linear, MipFilter = .Linear, ComparisonFunction = .Never, AddressModeU = .Clamp, AddressModeV = .Clamp, AddressModeW = .Clamp, - MipMinLOD = -340282346638528859811704183484516925440, - MipMaxLOD = 340282346638528859811704183484516925440, + MipMinLOD = -3.40282347e+38, + MipMaxLOD = 3.40282347e+38, MaxAnisotropy = 1, BorderColor = { R = 1, diff --git a/GlitchyEditor/src/Assets/AssetHierarchy.bf b/GlitchyEditor/src/Assets/AssetHierarchy.bf index 0daf565..da8b003 100644 --- a/GlitchyEditor/src/Assets/AssetHierarchy.bf +++ b/GlitchyEditor/src/Assets/AssetHierarchy.bf @@ -34,6 +34,24 @@ public class SubAsset public Texture2D PreviewImage ~ _?.ReleaseRef(); } +public static class AssetIdentifier +{ + public const char8 DirectorySeparatorChar = '/'; + + public static void Fixup(String assetIdentifier) + { + const String DotSeperator = $".{DirectorySeparatorChar}"; + const String SeperatorDot = $"{DirectorySeparatorChar}."; + + assetIdentifier.Replace('\\', DirectorySeparatorChar); + assetIdentifier.Replace(DotSeperator, ""); + assetIdentifier.Replace(SeperatorDot, ""); + + if (assetIdentifier.StartsWith(DirectorySeparatorChar)) + assetIdentifier.Remove(0, 1); + } +} + class AssetHierarchy { FileSystemWatcher fsw ~ { @@ -173,6 +191,7 @@ class AssetHierarchy { String identifier = scope .(node.Path.Length); Path.GetRelativePath(node.Path, _contentDirectory, identifier); + AssetIdentifier.Fixup(identifier); node.AssetFile = new AssetFile(_contentManager, identifier, node.Path, node.IsDirectory); } @@ -380,7 +399,7 @@ class AssetHierarchy _pathToAssetNode.Remove(oldFileNameWithContentRoot); node->Path.Set(newFileNameWithContentRoot); - _pathToAssetNode.Add(newFileNameWithContentRoot, node); + _pathToAssetNode.Add(node->Path, node); node->Name.Clear(); Path.GetFileName(newFileNameWithContentRoot, node->Name); @@ -388,7 +407,10 @@ class AssetHierarchy String oldIdentifier = scope .(node->AssetFile.[Friend]_identifier); node->AssetFile.[Friend]_path.Set(node->Path); + node->AssetFile.[Friend]_identifier.Set(newFilePath); + AssetIdentifier.Fixup(node->AssetFile.[Friend]_identifier); + node->AssetFile.[Friend]_assetConfigPath..Set(node->Path).Append(AssetFile.ConfigFileExtension); OnFileRenamed(node.Value, oldIdentifier); diff --git a/GlitchyEditor/src/EditorContentManager.bf b/GlitchyEditor/src/EditorContentManager.bf index 5c6e592..8ca2fcc 100644 --- a/GlitchyEditor/src/EditorContentManager.bf +++ b/GlitchyEditor/src/EditorContentManager.bf @@ -59,9 +59,8 @@ class EditorContentManager : IContentManager Asset asset = assetNode.AssetFile.LoadedAsset; - asset.Identifier = assetNode.AssetFile.Identifier; - _identiferToHandle.Remove(oldIdentifier); + asset.Identifier = assetNode.AssetFile.Identifier; _identiferToHandle.Add(asset.Identifier, asset.Handle); } @@ -334,24 +333,6 @@ class EditorContentManager : IContentManager private class MissingAsset : Asset {} - public class AssetIdentifier - { - public const char8 DirectorySeparatorChar = '/'; - - public static void Fixup(String assetIdentifier) - { - const String DotSeperator = $".{DirectorySeparatorChar}"; - const String SeperatorDot = $"{DirectorySeparatorChar}."; - - assetIdentifier.Replace('\\', DirectorySeparatorChar); - assetIdentifier.Replace(DotSeperator, ""); - assetIdentifier.Replace(SeperatorDot, ""); - - if (assetIdentifier.StartsWith(DirectorySeparatorChar)) - assetIdentifier.Remove(0, 1); - } - } - public AssetHandle LoadAsset(StringView identifier, bool blocking = false) { Debug.Profiler.ProfileResourceFunction!(); diff --git a/GlitchyEngine/src/Content/Asset.bf b/GlitchyEngine/src/Content/Asset.bf index 02c5913..067d6b0 100644 --- a/GlitchyEngine/src/Content/Asset.bf +++ b/GlitchyEngine/src/Content/Asset.bf @@ -63,7 +63,15 @@ abstract class Asset : RefCounter Deserialize.String!(reader, ref identifier, environment); - Asset asset = Content.GetAsset(Content.LoadAsset(identifier)); + AssetHandle handle = Content.LoadAsset(identifier); + + if (handle == .Invalid) + { + value.Assign(null); + return .Ok; + } + + Asset asset = Content.GetAsset(handle); if (asset != null) { diff --git a/GlitchyEngine/src/Content/AssetHandle.bf b/GlitchyEngine/src/Content/AssetHandle.bf index 03694ac..10f1c84 100644 --- a/GlitchyEngine/src/Content/AssetHandle.bf +++ b/GlitchyEngine/src/Content/AssetHandle.bf @@ -1,9 +1,12 @@ +using Bon; +using Bon.Integrated; using System; using xxHash; using System.Collections; using System.Reflection; using System.Diagnostics; using GlitchyEngine.Core; + namespace GlitchyEngine.Content; struct AssetHandle : IHashable @@ -27,6 +30,12 @@ struct AssetHandle : IHashable _uuid = uuid; } + static this + { + gBonEnv.typeHandlers.Add(typeof(AssetHandle), + ((.)new => AssetSerialize, new => AssetDeserialize)); + } + [Inline] public T Get(IContentManager contentManager = null) where T : Asset { @@ -34,6 +43,36 @@ struct AssetHandle : IHashable } public int GetHashCode() => _uuid.GetHashCode(); + + static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment, SerializeValueState state) + { + Log.EngineLogger.Assert(value.type == typeof(AssetHandle)); + + AssetHandle handle = value.Get(); + + if (handle.IsInvalid) + writer.String(""); + else + { + let identifier = handle.Get().Identifier; + writer.String(identifier); + } + } + + static Result AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state) + { + Log.EngineLogger.Assert(value.type == typeof(AssetHandle)); + + String identifier = scope .(); + + Deserialize.String!(reader, ref identifier, environment); + + AssetHandle handle = Content.LoadAsset(identifier); + + value.Assign(handle); + + return .Ok; + } } struct AssetHandle where T : Asset @@ -72,6 +111,12 @@ struct AssetHandle where T : Asset _asset = null; } + static this + { + gBonEnv.typeHandlers.Add(typeof(AssetHandle), + ((.)new => AssetSerialize, new => AssetDeserialize)); + } + public static implicit operator Self(AssetHandle handle) { return Self(handle); @@ -258,4 +303,34 @@ struct AssetHandle where T : Asset { return AssetHandle(this._handle, this._contentManager); } + + static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment, SerializeValueState state) + { + Log.EngineLogger.Assert(value.type == typeof(AssetHandle)); + + AssetHandle handle = value.Get>(); + + if (handle.IsInvalid) + writer.String(""); + else + { + let identifier = handle.Get().Identifier; + writer.String(identifier); + } + } + + static Result AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state) + { + Log.EngineLogger.Assert(value.type == typeof(AssetHandle)); + + String identifier = scope .(); + + Deserialize.String!(reader, ref identifier, environment); + + AssetHandle handle = Content.LoadAsset(identifier); + + value.Assign>(handle); + + return .Ok; + } } diff --git a/GlitchyEngine/src/Renderer/MeshComponent.bf b/GlitchyEngine/src/Renderer/MeshComponent.bf index b1be528..3558a57 100644 --- a/GlitchyEngine/src/Renderer/MeshComponent.bf +++ b/GlitchyEngine/src/Renderer/MeshComponent.bf @@ -4,27 +4,8 @@ using GlitchyEngine.Content; namespace GlitchyEngine.Renderer { - public struct MeshComponent// : IDisposableComponent + public struct MeshComponent { - public AssetHandle Mesh {get; set mut;} = .Invalid; - /* - private GeometryBinding _mesh; - public AssetHandle Mesh - { - [Inline] - get => _mesh; - set mut - { - if(_mesh == value) - return; - - SetReference!(_mesh, value); - } - } - - public void Dispose() mut - { - ReleaseRefAndNullify!(_mesh); - }*/ + public AssetHandle Mesh = .Invalid; } } diff --git a/GlitchyEngine/src/World/Components.bf b/GlitchyEngine/src/World/Components.bf index 274f3bc..bbfb816 100644 --- a/GlitchyEngine/src/World/Components.bf +++ b/GlitchyEngine/src/World/Components.bf @@ -45,21 +45,9 @@ namespace GlitchyEngine.World } [Component("Sprite Renderer")] - struct SpriteRendererComponent// : IDisposableComponent + struct SpriteRendererComponent { - private AssetHandle _sprite = .Invalid; - - public AssetHandle Sprite - { - get => _sprite; - set mut - { - if (_sprite == value) - return; - _sprite = value; - //SetReference!(_sprite, value); - } - } + public AssetHandle Sprite = .Invalid; public ColorRGBA Color = .White; public Vector4 UvTransform = .(0, 0, 1, 1); diff --git a/GlitchyEngine/src/World/MeshRendererComponent.bf b/GlitchyEngine/src/World/MeshRendererComponent.bf index 11ebada..1146ee1 100644 --- a/GlitchyEngine/src/World/MeshRendererComponent.bf +++ b/GlitchyEngine/src/World/MeshRendererComponent.bf @@ -5,26 +5,8 @@ using GlitchyEngine.Content; namespace GlitchyEngine.World { /// A component that allows to render a mesh. - public struct MeshRendererComponent// : IDisposableComponent + public struct MeshRendererComponent { - private AssetHandle _material = .Invalid; - - public AssetHandle Material - { - [Inline] - get => _material; - set mut - { - if(_material == value) - return; - - _material = value; - } - } - - /*public void Dispose() mut - { - _material?.ReleaseRef(); - }*/ + public AssetHandle Material = .Invalid; } } diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index 5cc9e65..b92249a 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -47,14 +47,6 @@ namespace GlitchyEngine.World public this() { - /*Entity entity = CreateEntity("Green Quad"); - entity.AddComponent(.(ColorRGBA.SRgbToLinear(.(0.2f, 0.9f, 0.15f)))); - - Entity entity2 = CreateEntity("Red Square"); - var v = entity2.AddComponent(.(ColorRGBA.SRgbToLinear(.(0.95f, 0.1f, 0.3f)))); - v.Sprite = new Texture2D("Textures/rocket.dds"); - v.Sprite.SamplerState = SamplerStateManager.PointClamp;*/ - _onComponentAddedHandlers.Add(typeof(CameraComponent), (e, t, c) => { CameraComponent* cameraComponent = (.)c; @@ -99,6 +91,7 @@ namespace GlitchyEngine.World // Copy components //CopyComponents(this, target); /* Parent will be copied below*/ CopyComponents(this, target); + CopyComponents(this, target); CopyComponents(this, target); CopyComponents(this, target); CopyComponents(this, target); @@ -335,156 +328,6 @@ namespace GlitchyEngine.World } finalTarget.ReleaseRef(); - - - /*Debug.Profiler.ProfileRendererFunction!(); - - finalTarget.AddRef(); - - TransformSystem.Update(_ecsWorld); - - // Run scripts - for (var (entity, script) in _ecsWorld.Enumerate()) - { - if (script.Instance == null) - { - script.Instance = script.InstantiateFunction(); - script.Instance._entity = Entity(entity, this); - script.Instance.[Friend]OnCreate(); - } - - script.Instance.[Friend]OnUpdate(gameTime); - } - - // Find camera - Camera* primaryCamera = null; - Matrix primaryCameraTransform = default; - RenderTargetGroup renderTarget = null; - - for (var (entity, transform, camera) in _ecsWorld.Enumerate()) - { - if (camera.Primary)// && camera.RenderTarget != null) - { - primaryCamera = &camera.Camera; - primaryCameraTransform = transform.WorldTransform; - //renderTarget = camera.RenderTarget..AddRef(); - } - } - - if (primaryCamera != null) - { - // 3D render - Renderer.BeginScene(*primaryCamera, primaryCameraTransform, _compositeTarget, finalTarget); - - for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate()) - { - Renderer.Submit(mesh.Mesh, meshRenderer.Material, entity, transform.WorldTransform); - } - - for (var (entity, transform, light) in _ecsWorld.Enumerate()) - { - Renderer.Submit(light.SceneLight, transform.WorldTransform); - } - - Renderer.EndScene(); - - // TODO: alphablending (handle in Renderer2D) - // TODO: 2D-Postprocessing requires rendering into separate target instead of directly into compositeTarget - - RenderCommand.SetRenderTargetGroup(_compositeTarget); - RenderCommand.BindRenderTargets(); - - // Sprite renderer - Renderer2D.BeginScene(*primaryCamera, primaryCameraTransform, .BackToFront); - - for (var (entity, transform, sprite) in _ecsWorld.Enumerate()) - { - Renderer2D.DrawSprite(transform.WorldTransform, sprite, entity.Index); - } - - Renderer2D.EndScene(); - - // Gamma correct composit target and draw it into viewport - { - RenderCommand.UnbindRenderTargets(); - RenderCommand.SetRenderTargetGroup(finalTarget, false); - RenderCommand.BindRenderTargets(); - - _gammaCorrectEffect.SetTexture("Texture", _compositeTarget, 0); - // TODO: iiihhh - _gammaCorrectEffect.Bind(Application.Get().Window.Context); - - FullscreenQuad.Draw(); - } - } - - finalTarget.ReleaseRef();*/ - - - - - - /* - Debug.Profiler.ProfileRendererFunction!(); - - TransformSystem.Update(_ecsWorld); - - for (var (entity, script) in _ecsWorld.Enumerate()) - { - if (script.Instance == null) - { - script.Instance = script.InstantiateFunction(); - script.Instance._entity = Entity(entity, this); - script.Instance.[Friend]OnCreate(); - } - - script.Instance.[Friend]OnUpdate(gameTime); - } - - Camera* primaryCamera = null; - Matrix primaryCameraTransform = default; - RenderTargetGroup renderTarget = null; - - for (var (entity, transform, camera) in _ecsWorld.Enumerate()) - { - if (camera.Primary && camera.RenderTarget != null) - { - primaryCamera = &camera.Camera; - primaryCameraTransform = transform.WorldTransform; - renderTarget = camera.RenderTarget..AddRef(); - } - } - - if (primaryCamera != null) - { - // 3D render - Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, finalTarget); - - for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate()) - { - Renderer.Submit(mesh.Mesh, meshRenderer.Material, transform.WorldTransform); - } - - for (var (entity, transform, light) in _ecsWorld.Enumerate()) - { - Renderer.Submit(light.SceneLight, transform.WorldTransform); - } - - Renderer.EndScene(); - - // Sprite renderer - Renderer2D.BeginScene(*primaryCamera, primaryCameraTransform); - - for (var (entity, transform, sprite) in _ecsWorld.Enumerate()) - { - Renderer2D.DrawQuad(transform.WorldTransform, sprite.Sprite, sprite.Color); - } - - Renderer2D.EndScene(); - - renderTarget?.ReleaseRef(); - } - */ } public void UpdateEditor(GameTime gameTime, EditorCamera camera, RenderTargetGroup viewportTarget, delegate void() DebugDraw3D, delegate void() DrawDebug2D) diff --git a/GlitchyEngine/src/World/SceneSerializer.bf b/GlitchyEngine/src/World/SceneSerializer.bf index c23c41a..8663dd4 100644 --- a/GlitchyEngine/src/World/SceneSerializer.bf +++ b/GlitchyEngine/src/World/SceneSerializer.bf @@ -84,11 +84,13 @@ class SceneSerializer Serialize.Value(writer, "Name", component.Name); }); - SerializeComponent(writer, entity, "SpriterRendererComponent", scope (component) => + SerializeComponent(writer, entity, "SpriteRendererComponent", scope (component) => { Serialize.Value(writer, "Color", component.Color); Serialize.Value(writer, "IsCircle", component.IsCircle); - Serialize.Value(writer, "Sprite", component.Sprite.Get().Identifier); + + Serialize.Value(writer, "Sprite", component.Sprite); + Serialize.Value(writer, "UvTransform", component.UvTransform); }); @@ -172,12 +174,12 @@ class SceneSerializer SerializeComponent(writer, entity, "MeshComponent", scope (component) => { - Serialize.Value(writer, "Mesh", component.Mesh.Identifier); + Serialize.Value(writer, "Mesh", component.Mesh); }); SerializeComponent(writer, entity, "MeshRendererComponent", scope (component) => { - Serialize.Value(writer, "Material", component.Material.Identifier); + Serialize.Value(writer, "Material", component.Material); }); } @@ -329,7 +331,7 @@ class SceneSerializer return .Ok; })); - case "SpriterRendererComponent": + case "SpriteRendererComponent": Try!(DeserializeComponent(reader, entity, scope (component) => { Try!(Deserialize.Value(reader, "Color", out component.Color)); @@ -337,7 +339,7 @@ class SceneSerializer Try!(Deserialize.Value(reader, "IsCircle", out component.IsCircle)); reader.EntryEnd(); - component.Sprite = DeserializeAssetHandle!("Sprite"); + Try!(Deserialize.Value(reader, "Sprite", out component.Sprite)); reader.EntryEnd(); Try!(Deserialize.Value(reader, "UvTransform", out component.UvTransform)); @@ -486,14 +488,14 @@ class SceneSerializer case "MeshComponent": Try!(DeserializeComponent(reader, entity, scope (component) => { - component.Mesh = DeserializeAssetHandle!("Mesh"); + Try!(Deserialize.Value(reader, "Mesh", out component.Mesh)); return .Ok; })); case "MeshRendererComponent": Try!(DeserializeComponent(reader, entity, scope (component) => { - component.Material = DeserializeAssetHandle!("Material"); + Try!(Deserialize.Value(reader, "Material", out component.Material)); return .Ok; }));