Interact with Materials from scripts

This commit is contained in:
Simon Lübeß
2025-02-14 11:54:07 +01:00
parent e5437c6ff6
commit aaed4199d3
24 changed files with 739 additions and 64 deletions
@@ -0,0 +1,5 @@
{
Effect = 7757902626263730438,
Textures = [],
Constants = []
}
@@ -0,0 +1,13 @@
{
AssetLoader = null,
Config = null,
Importer = "MaterialImporter",
ImporterConfig = null,
Processor = null,
ProcessorConfig = null,
Exporter = null,
ExporterConfig = {
_compression = .None
},
AssetHandle = 14657247797693923776
}
@@ -0,0 +1,5 @@
{
Effect = 2172704887168568810,
Textures = [],
Constants = []
}
@@ -0,0 +1,13 @@
{
AssetLoader = null,
Config = null,
Importer = "MaterialImporter",
ImporterConfig = null,
Processor = null,
ProcessorConfig = null,
Exporter = null,
ExporterConfig = {
_compression = .None
},
AssetHandle = 15110879072477854834
}
+4 -3
View File
@@ -187,11 +187,12 @@ class TextureViewer
Width = 100,
Height = 100,
ColorTargetDescriptions = TargetDescription[](
.(.R8G8B8A8_UNorm, clearColor: .Color(ColorRGBA.Pink)),
.(.R32_UInt)
.(.R8G8B8A8_UNorm, clearColor: .Color(ColorRGBA.Pink), ownDebugName: new $"Color"),
.(.R32_UInt, ownDebugName: new $"Enity ID (Unused)")
),
DepthTargetDescription = .(.D24_UNorm_S8_UInt)
DepthTargetDescription = .(.D24_UNorm_S8_UInt, ownDebugName: new $"Depth Stencil")
});
_targets.[Friend]Identifier = "Asset Viewer";
}
float2 _position;
+5 -2
View File
@@ -342,11 +342,12 @@ namespace GlitchyEditor
Width = 100,
Height = 100,
ColorTargetDescriptions = TargetDescription[](
.(.R16G16B16A16_Float),
.(.R32_UInt)
.(.R16G16B16A16_Float, ownDebugName: new String("HDR Color")),
.(.R32_UInt, ownDebugName: new String("EntityID"))
),
DepthTargetDescription = .(.D24_UNorm_S8_UInt)
});
_cameraTarget.[Friend]Identifier = "Camera Target";
_editorViewportTarget = new RenderTargetGroup(.()
{
@@ -355,6 +356,7 @@ namespace GlitchyEditor
ColorTargetDescriptions = TargetDescription[](
.(.R8G8B8A8_UNorm))
});
_editorViewportTarget.[Friend]Identifier = "Editor Viewport Target";
_gameViewportTarget = new RenderTargetGroup(.()
{
@@ -363,6 +365,7 @@ namespace GlitchyEditor
ColorTargetDescriptions = TargetDescription[](
.(.R8G8B8A8_UNorm))
});
_editorViewportTarget.[Friend]Identifier = "Game Viewport Target";
_editorIcons = new EditorIcons("Resources/Textures/EditorIcons.dds", .(64, 64));
_editorIcons.SamplerState = SamplerStateManager.AnisotropicClamp;
@@ -109,6 +109,26 @@ namespace GlitchyEngine.Renderer
#endif
}
public enum TypematchResult
{
Ok,
MatrixDimensionMismatch,
ElementTypeMismatch,
}
public TypematchResult CheckTypematch(int rows, int cols, ShaderVariableType type)
{
Debug.Profiler.ProfileRendererFunction!();
if (rows != _rows || cols != _columns)
return .MatrixDimensionMismatch;
if (type != _elementType)
return .ElementTypeMismatch;
return .Ok;
}
public void EnsureTypeMatch<T>()
{
switch(typeof(T))
+61 -24
View File
@@ -196,7 +196,7 @@ public class Material : Asset
}
}
private mixin SetVariable(String name, var value)
private mixin SetVariable(StringView name, var value)
{
if(_variables.TryGetValue(name, let entry))
{
@@ -204,35 +204,72 @@ public class Material : Asset
}
}
public void SetVariable(String name, bool value) => SetVariable!(name, value);
public void SetVariable(String name, bool2 value) => SetVariable!(name, value);
public void SetVariable(String name, bool3 value) => SetVariable!(name, value);
public void SetVariable(String name, bool4 value) => SetVariable!(name, value);
public enum SetVariableError
{
VariableNotFound,
ElementTypeMismatch,
MatrixDimensionMismatch,
ProvidedBufferTooShort,
}
public void SetVariable(String name, int32 value) => SetVariable!(name, value);
public void SetVariable(String name, int2 value) => SetVariable!(name, value);
public void SetVariable(String name, int3 value) => SetVariable!(name, value);
public void SetVariable(String name, int4 value) => SetVariable!(name, value);
internal Result<void, SetVariableError> SetVariableRaw(StringView name, ShaderVariableType elementType, int32 rows, int32 columns, int32 arrayLength, Span<uint8> rawData)
{
if(!_variables.TryGetValue(name, let variable))
{
return .Err(.VariableNotFound);
}
public void SetVariable(String name, uint32 value) => SetVariable!(name, value);
public void SetVariable(String name, uint2 value) => SetVariable!(name, value);
public void SetVariable(String name, uint3 value) => SetVariable!(name, value);
public void SetVariable(String name, uint4 value) => SetVariable!(name, value);
let matchResult = variable.CheckTypematch(rows, columns, elementType);
public void SetVariable(String name, float value) => SetVariable!(name, value);
public void SetVariable(String name, float2 value) => SetVariable!(name, value);
public void SetVariable(String name, float3 value) => SetVariable!(name, value);
public void SetVariable(String name, float4 value) => SetVariable!(name, value);
switch (matchResult)
{
case .Ok:
// Nothing went wrong.
case .ElementTypeMismatch:
return .Err(.ElementTypeMismatch);
case .MatrixDimensionMismatch:
return .Err(.MatrixDimensionMismatch);
}
public void SetVariable(String name, Color value) => SetVariable!(name, value);
public void SetVariable(String name, ColorRGB value) => SetVariable!(name, value);
public void SetVariable(String name, ColorRGBA value) => SetVariable!(name, value);
if (rawData.Length < variable._sizeInBytes)
return .Err(.ProvidedBufferTooShort);
public void SetVariable(String name, Matrix3x3 value) => SetVariable!(name, value);
public void SetVariable(String name, Matrix4x3 value) => SetVariable!(name, value);
public void SetVariable(String name, Matrix value) => SetVariable!(name, value);
// TODO: This method is very stupid atm.
// We could e.g. allow array length mismatches by simply copying the smaller amount of elements.
variable.SetRawData(rawData.Ptr);
public void GetVariable<T>(String name, out T value) where T : struct
return .Ok;
}
public void SetVariable(StringView name, bool value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int32 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint32 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float value) => SetVariable!(name, value);
public void SetVariable(StringView name, float2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Color value) => SetVariable!(name, value);
public void SetVariable(StringView name, ColorRGB value) => SetVariable!(name, value);
public void SetVariable(StringView name, ColorRGBA value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix3x3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix4x3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix value) => SetVariable!(name, value);
public void GetVariable<T>(StringView name, out T value) where T : struct
{
Debug.Profiler.ProfileRendererFunction!();
+44 -16
View File
@@ -98,6 +98,7 @@ namespace GlitchyEngine.Renderer
static GBuffer _gBuffer;
static AssetHandle<Effect> TestFullscreenEffect;
static Material TestDeferredMaterial ~ _?.ReleaseRef();
static AssetHandle<Effect> s_tonemappingEffect;
static BlendState _gBufferBlend;
@@ -179,6 +180,9 @@ namespace GlitchyEngine.Renderer
static void InitDeferredRenderer()
{
TestFullscreenEffect = Content.LoadAsset("Resources/Shaders/simpleLight.fx", null, true);
TestDeferredMaterial = new Material(TestFullscreenEffect);
s_tonemappingEffect = Content.LoadAsset("Resources/Shaders/SimpleTonemapping.fx", null, true);
_gBuffer = new GBuffer();
@@ -362,32 +366,56 @@ namespace GlitchyEngine.Renderer
{
Debug.Profiler.ProfileRendererScope!("Draw Light");
Effect fsEffect = TestFullscreenEffect.Get();
//Effect fsEffect = TestFullscreenEffect.Get();
if (fsEffect == null)
if (TestDeferredMaterial == null)
continue;
float3 lightDir = -light.Transform.Forward;
fsEffect.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
fsEffect.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4);
fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 5);
/*TestDeferredMaterial.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
TestDeferredMaterial.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
TestDeferredMaterial.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
TestDeferredMaterial.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
TestDeferredMaterial.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4);
TestDeferredMaterial.SetTexture("GBuffer_Material", _gBuffer.Target, 5);*/
fsEffect.Variables["LightColor"].SetData(light.Light.Color);
fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance);
fsEffect.Variables["LightDir"].SetData(lightDir);
TestDeferredMaterial.SetVariable("LightColor", light.Light.Color);
TestDeferredMaterial.SetVariable("Illuminance", light.Light.Illuminance);
TestDeferredMaterial.SetVariable("LightDir", lightDir);
fsEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition);
TestDeferredMaterial.SetVariable("CameraPos", _sceneConstants.CameraPosition);
fsEffect.Variables["Scaling"].SetData(scaling);
TestDeferredMaterial.SetVariable("Scaling", scaling);
fsEffect.ApplyChanges();
fsEffect.Bind();
TestDeferredMaterial.Bind();
//RenderCommand.BindEffect(TestFullscreenEffect);
// TODO: Don't do that please...
using (let vb = _gBuffer.Target.GetViewBinding(0))
{
RenderCommand.BindTexture(vb, 0, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(1))
{
RenderCommand.BindTexture(vb, 1, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(2))
{
RenderCommand.BindTexture(vb, 2, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(3))
{
RenderCommand.BindTexture(vb, 3, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(4))
{
RenderCommand.BindTexture(vb, 4, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(5))
{
RenderCommand.BindTexture(vb, 5, .Pixel);
}
FullscreenQuad.Draw();
}
+4 -4
View File
@@ -641,8 +641,8 @@ namespace GlitchyEngine.Renderer
s_quadInstanceBuffer.SetData<QuadBatchVertex>(s_rawQuadInstances.Ptr, s_setQuadInstances, 0, .WriteDiscard);
//s_currentQuadEffect.ApplyChanges();
//s_currentQuadEffect.Bind();
// TODO: Bind scene buffer
material.SetVariable("ViewProjection", sceneViewProjection);
material.Bind();
using (TextureViewBinding tvb = texture.GetViewBinding())
@@ -1151,8 +1151,8 @@ namespace GlitchyEngine.Renderer
uvTransform = sprite.TextureCoordinates;
}
// TODO: Material for Sprite renderer
DrawQuad(transform, spriteTexture ?? s_whiteTexture, null, spriteRenderer.Color, uvTransform, entityId);
Material material = spriteRenderer.Material.Get();
DrawQuad(transform, spriteTexture ?? s_whiteTexture, material, spriteRenderer.Color, uvTransform, entityId);
}
public static void DrawCircle(Matrix transform, CircleRendererComponent* spriteRenderer, uint32 entityId)
+176 -2
View File
@@ -12,15 +12,18 @@ using GlitchyEngine.Scripting;
using GlitchyEngine.Serialization;
using GlitchyEngine.Editor;
using GlitchyEngine.World.Components;
using GlitchyEngine.Content;
using GlitchyEngine.Renderer;
using static GlitchyEngine.Renderer.Text.FontRenderer;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
using internal GlitchyEngine.Content;
class MessageOrigin
{
private String _fileName;
private String _fileName ~ delete:append _;
private int _lineNumber;
public StringView FileName => _fileName;
@@ -91,8 +94,11 @@ static class ScriptGlue
RegisterComponent<TransformComponent>("GlitchyEngine.Core.Transform");
RegisterComponent<Rigidbody2DComponent>("GlitchyEngine.Physics.Rigidbody2D");
RegisterComponent<CameraComponent>("GlitchyEngine.Core.Camera");
RegisterComponent<SpriteRendererComponent>("GlitchyEngine.Graphics.SpriteRenderer");
RegisterComponent<CircleRendererComponent>("GlitchyEngine.Graphics.CircleRenderer");
RegisterComponent<TextRendererComponent>("GlitchyEngine.Graphics.Text.TextRenderer");
RegisterComponent<MeshComponent>("GlitchyEngine.Graphics.Mesh");
RegisterComponent<MeshRendererComponent>("GlitchyEngine.Graphics.MeshRenderer");
}
[RegisterMethod]
@@ -211,7 +217,7 @@ static class ScriptGlue
#region Exception Helpers
/// Throws an argument exception.
/// Throws an ArgumentException in the mono runtime.
[NoReturn]
static void ThrowArgumentException(char8* argument, char8* message)
{
@@ -219,6 +225,22 @@ static class ScriptGlue
Mono.mono_raise_exception(exception);
}
/// Throws an InvalidOperationException in the mono runtime.
[NoReturn]
static void ThrowInvalidOperationException(char8* message)
{
MonoException* exception = Mono.mono_get_exception_invalid_operation(message);
Mono.mono_raise_exception(exception);
}
/// Throws an NotImplementedException in the mono runtime.
[NoReturn]
static void ThrowNotImplementedException(char8* message)
{
MonoException* exception = Mono.mono_get_exception_invalid_operation(message);
Mono.mono_raise_exception(exception);
}
#endregion
#region Log
@@ -998,6 +1020,53 @@ static class ScriptGlue
#endregion
#region SpriteRenderer
[RegisterCall("ScriptGlue::SpriteRenderer_GetColor")]
static void SpriteRenderer_GetColor(UUID entityId, out ColorRGBA color)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
color = spriteRenderer.Color;
}
[RegisterCall("ScriptGlue::SpriteRenderer_SetColor")]
static void SpriteRenderer_SetColor(UUID entityId, ColorRGBA color)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
spriteRenderer.Color = color;
}
[RegisterCall("ScriptGlue::SpriteRenderer_GetUvTransform")]
static void SpriteRenderer_GetUvTransform(UUID entityId, out float4 uvTransform)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
uvTransform = spriteRenderer.UvTransform;
}
[RegisterCall("ScriptGlue::SpriteRenderer_SetUvTransform")]
static void SpriteRenderer_SetUvTransform(UUID entityId, float4 uvTransform)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
spriteRenderer.UvTransform = uvTransform;
}
[RegisterCall("ScriptGlue::SpriteRenderer_GetMaterial")]
static void SpriteRenderer_GetMaterial(UUID entityId, out AssetHandle assetId)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
assetId = spriteRenderer.Material;
}
[RegisterCall("ScriptGlue::SpriteRenderer_SetMaterial")]
static void SpriteRenderer_SetMaterial(UUID entityId, AssetHandle assetId)
{
SpriteRendererComponent* spriteRenderer = GetComponentSafe<SpriteRendererComponent>(entityId);
spriteRenderer.Material = assetId;
}
#endregion
#region TextRenderer
[RegisterCall("ScriptGlue::TextRenderer_SetIsRichText")]
@@ -1084,6 +1153,27 @@ static class ScriptGlue
#endregion
#region Mesh
#endregion
#region MeshRenderer
[RegisterCall("ScriptGlue::MeshRenderer_GetMaterial")]
static void MeshRenderer_GetMaterial(UUID entityId, out AssetHandle assetId)
{
MeshRendererComponent* meshRenderer = GetComponentSafe<MeshRendererComponent>(entityId);
assetId = meshRenderer.Material;
}
[RegisterCall("ScriptGlue::MeshRenderer_SetMaterial")]
static void MeshRenderer_SetMaterial(UUID entityId, AssetHandle assetId)
{
MeshRendererComponent* meshRenderer = GetComponentSafe<MeshRendererComponent>(entityId);
meshRenderer.Material = assetId;
}
#endregion
#region Math
private static void RegisterMathFunctions()
@@ -1234,6 +1324,89 @@ static class ScriptGlue
#endregion
#region Asset
/*static mixin GetAssetOrThrow(UUID assetId)
{
AssetHandle handle = .(assetId);
Asset asset = Content.GetAsset(handle, blocking: true);
if (asset == null)
{
ThrowInvalidOperationException(scope $"No asset exists for AssetHandle \"{assetId}\".");
}
asset
}*/
static mixin GetAssetOrThrow<T>(UUID assetId) where T : Asset
{
AssetHandle handle = .(assetId);
T asset = Content.GetAsset<T>(handle, blocking: true);
if (asset == null)
{
ThrowInvalidOperationException(scope $"No asset exists for AssetHandle \"{assetId}\".");
}
asset
}
static mixin GetAssetOrThrow<T>(AssetHandle assetHandle) where T : Asset
{
T asset = Content.GetAsset<T>(assetHandle, blocking: true);
if (asset == null)
{
ThrowInvalidOperationException(scope $"No asset exists for AssetHandle \"{assetHandle}\".");
}
asset
}
[RegisterCall("ScriptGlue::Asset_GetIdentifier")]
static void Asset_GetIdentifier(UUID assetId, out MonoString* text)
{
Asset asset = GetAssetOrThrow!<Asset>(assetId);
text = Mono.mono_string_new_len(ScriptEngine.[Friend]s_AppDomain, asset.Identifier.Ptr, (uint32)asset.Identifier.Length);
}
[RegisterCall("ScriptGlue::Asset_SetIdentifier")]
static void Asset_GetIdentifier(UUID assetId, MonoString* text)
{
ThrowNotImplementedException("Asset.GetIdentifier is not implemented.");
/*AssetHandle handle = .(assetId);
Asset asset = Content.GetAsset(handle, blocking: true);
char8* rawText = Mono.mono_string_to_utf8(text);
// TODO: I think it's not THAT easy.
asset.Identifier = StringView(rawText);
Mono.mono_free(rawText);*/
}
#endregion
#region Material
[RegisterCall("ScriptGlue::Material_SetVariable")]
static void Material_SetVariable(AssetHandle assetHandle, MonoString* managedVariableName, ShaderVariableType elementType, int32 rows, int32 columns, int32 arrayLength, uint8* rawData, int32 dataLength)
{
Material material = GetAssetOrThrow!<Material>(assetHandle);
char8* rawVariableName = Mono.mono_string_to_utf8(managedVariableName);
defer Mono.mono_free(rawVariableName);
StringView variableName = StringView(rawVariableName);
material.[Friend]SetVariableRaw(variableName, elementType, rows, columns , arrayLength, Span<uint8>(rawData, dataLength));
}
#endregion
#region ImGui Extension
[RegisterCall("ScriptGlue::ImGuiExtension_ListElementGrabber")]
@@ -1242,6 +1415,7 @@ static class ScriptGlue
ImGui.ImGui.ListElementGrabber();
}
#endregion
private static void RegisterCall<T>(String name, T method) where T : var
@@ -50,6 +50,8 @@ namespace GlitchyEngine.World
public ColorRGBA Color = .White;
public float4 UvTransform = .(0, 0, 1, 1);
public AssetHandle<Material> Material = .Invalid;
public this()
{
}
-2
View File
@@ -233,8 +233,6 @@ class SceneRenderer
FontRenderer.DrawText(text.PreparedText, transform.WorldTransform, text.Color, entity.Index);
}
//FontRenderer.DrawText(_smallLinesInfo, 0, 0);
Renderer2D.EndScene();
Renderer2D.BeginScene(camera, .BackToFront);
+6
View File
@@ -293,6 +293,12 @@ static class Mono
[LinkName(.C)]
public static extern MonoException* mono_get_exception_argument(char8* arg, char8* msg);
[LinkName(.C)]
public static extern MonoException* mono_get_exception_invalid_operation(char8* msg);
[LinkName(.C)]
public static extern MonoException* mono_get_exception_not_implemented(char8* msg);
#endregion
[LinkName(.C)]
+35 -2
View File
@@ -276,9 +276,12 @@ internal class EntityEditor
var value = (T)reference!;
if (format == null && value is float || value is double || value is Half)
if (format == null)
{
if (value is float || value is double || value is Half)
format = "0.#####";
else
format = "0";
}
if (!format.StartsWith("%"))
@@ -512,14 +515,44 @@ internal class EntityEditor
{
newValue = ShowDecimalEditor(reference, fieldType, fieldName, attributes);
}
else if (fieldType == typeof(ColorHSV))
{
string fieldId = StartNewProperty(fieldName, attributes);
ColorHSV value = (ColorHSV)reference!;
value.H /= 360.0f;
if (ImGui.ColorEdit3(fieldId, ref Unsafe.As<ColorHSV, Vector3>(ref value),
ImGuiColorEditFlags.DisplayHSV | ImGuiColorEditFlags.InputHSV | ImGuiColorEditFlags.Float))
{
value.H *= 360.0f;
newValue = value;
}
}
else if (fieldType == typeof(ColorRGB))
{
string fieldId = StartNewProperty(fieldName, attributes);
ColorRGB value = (ColorRGB)reference!;
if (ImGui.ColorEdit3(fieldId, ref Unsafe.As<ColorRGB, Vector3>(ref value), ImGuiColorEditFlags.Float))
newValue = value;
}
else if (fieldType == typeof(ColorRGBA))
{
string fieldId = StartNewProperty(fieldName, attributes);
ColorRGBA value = (ColorRGBA)reference!;
if (ImGui.ColorEdit4(fieldId, ref Unsafe.As<ColorRGBA, Vector4>(ref value)))
if (ImGui.ColorEdit4(fieldId, ref Unsafe.As<ColorRGBA, Vector4>(ref value), ImGuiColorEditFlags.Float))
newValue = value;
}
else if (fieldType == typeof(Color))
{
string fieldId = StartNewProperty(fieldName, attributes);
Color value = (Color)reference!;
ColorRGBA color = (ColorRGBA)value;
if (ImGui.ColorEdit4(fieldId, ref Unsafe.As<ColorRGBA, Vector4>(ref color)))
newValue = (Color)color;
}
else if (fieldType.TryGetCustomAttribute(out VectorAttribute vectorAttribute))
{
newValue = ShowVectorEditor(reference, fieldType, fieldName, vectorAttribute, attributes);
+22
View File
@@ -0,0 +1,22 @@
using GlitchyEngine.Core;
namespace GlitchyEngine.Graphics;
/// <summary>
/// The base class for all assets.
/// </summary>
public class Asset : EngineObject
{
/// <summary>
/// Gets or sets the Identifier of the asset.
/// </summary>
public string Identifier
{
get
{
ScriptGlue.Asset_GetIdentifier(_uuid, out string identifier);
return identifier;
}
set => ScriptGlue.Asset_SetIdentifier(_uuid, value);
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ public class CircleRenderer : Component
/// <summary>
/// Gets or sets the UV transform for the texture. XY are an offset, ZW are scaling
/// </summary>
public float4 UvTransform
public float4 UVTransform
{
get
{
+15
View File
@@ -0,0 +1,15 @@
using System.Diagnostics.SymbolStore;
using GlitchyEngine.Math;
namespace GlitchyEngine.Graphics;
public class Material : Asset
{
public void SetVariable(string name, float4 value)
{
unsafe
{
ScriptGlue.Material_SetVariable(_uuid, name, ScriptGlue.ShaderVariableType.Float, 1, 4, 1, &value, sizeof(float4));
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using GlitchyEngine.Core;
namespace GlitchyEngine.Graphics;
/// <summary>
/// Holds a reference to a Mesh.
/// Can be used in combination with a <see cref="MeshRenderer"/> on the same <see cref="Entity"/> to render the mesh.
/// </summary>
public class Mesh : Component
{
}
+23
View File
@@ -0,0 +1,23 @@
using GlitchyEngine.Core;
namespace GlitchyEngine.Graphics;
/// <summary>
/// Renders a Mesh. The Mesh can be specified by adding a <see cref="Mesh"/> component to the same <see cref="Entity"/>.
/// </summary>
public class MeshRenderer : Component
{
/// <summary>
/// Gets or sets the Material of this <see cref="MeshRenderer"/>.
/// </summary>
public Material Material
{
get
{
ScriptGlue.MeshRenderer_GetMaterial(_uuid, out UUID materialHandle);
return new Material { _uuid = materialHandle };
}
set => ScriptGlue.MeshRenderer_SetMaterial(_uuid, value._uuid);
}
}
+52
View File
@@ -0,0 +1,52 @@
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine.Graphics;
/// <summary>
/// Component that renders a sprite.
/// </summary>
public class SpriteRenderer : Component
{
// TODO: Texture2D/Sprite
/// <summary>
/// Gets or sets the tint color.
/// </summary>
public ColorRGBA Color
{
get
{
ScriptGlue.SpriteRenderer_GetColor(_uuid, out ColorRGBA color);
return color;
}
set => ScriptGlue.SpriteRenderer_SetColor(_uuid, value);
}
/// <summary>
/// Gets or sets the UV-transform.
/// </summary>
public UVTransform UVTransform
{
get
{
ScriptGlue.SpriteRenderer_GetUvTransform(_uuid, out UVTransform uvTransform);
return uvTransform;
}
set => ScriptGlue.SpriteRenderer_SetUvTransform(_uuid, value);
}
/// <summary>
/// Gets or sets the Material of this SpriteRenderer.
/// </summary>
public Material Material
{
get
{
ScriptGlue.SpriteRenderer_GetMaterial(_uuid, out UUID materialHandle);
return new Material { _uuid = materialHandle };
}
set => ScriptGlue.SpriteRenderer_SetMaterial(_uuid, value._uuid);
}
}
+39
View File
@@ -0,0 +1,39 @@
using System.Runtime.InteropServices;
using GlitchyEngine.Math;
namespace GlitchyEngine.Graphics;
/// <summary>
/// Stores transformations that can be applied to UV-Coordinates.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct UVTransform
{
/// <summary>
/// The offset that will be added to the UV-Coordinate.
/// </summary>
public float2 Offset;
/// <summary>
/// The scaling that will be applied to the UV-Coordinate.
/// </summary>
public float2 Scale;
/// <summary>
/// Creates a new instance of <see cref="UVTransform"/> with the given scale and offset.
/// </summary>
/// <param name="offset"><inheritdoc cref="Offset"/></param>
/// <param name="scale"><inheritdoc cref="Scale"/></param>
public UVTransform(float2 offset, float2 scale)
{
Offset = offset;
Scale = scale;
}
/// <summary>
/// Transforms the given uv-coordinate.
/// </summary>
public float2 Transform(float2 uvCoordinate)
{
return Offset + uvCoordinate * Scale;
}
}
+115
View File
@@ -0,0 +1,115 @@
namespace GlitchyEngine.Math;
/// <summary>
/// Represents a color with hue, saturation and value.
/// </summary>
public struct ColorHSV
{
/// <summary>
/// The hue of the color. Range: 0 - 360°
/// </summary>
public float H;
/// <summary>
/// The saturation of the color. Range: 0 - 1
/// </summary>
public float S;
/// <summary>
/// The value of the color. Range: 0 - 1
/// </summary>
public float V;
/// <summary>
/// Creates a new instance of a <see cref="ColorHSV"/> based on the given hue, saturation and value.
/// </summary>
/// <param name="h"><inheritdoc cref="H"/></param>
/// <param name="s"><inheritdoc cref="S"/></param>
/// <param name="v"><inheritdoc cref="V"/></param>
public ColorHSV(float h, float s, float v)
{
H = h;
S = s;
V = v;
}
/// <summary>
/// Creates a new instance of a <see cref="ColorHSV"/> based on the given <see cref="ColorRGB"/>.
/// </summary>
/// <param name="colorRGB">The RGB color that is to be converted to an HSV color</param>
public ColorHSV(ColorRGB colorRGB)
{
this = (ColorHSV)colorRGB;
}
public static explicit operator ColorRGB(ColorHSV hsv)
{
float c = hsv.V * hsv.S;
float x = c * (1.0f - Math.abs((hsv.H / 60.0f) % 2.0f - 1.0f));
float m = hsv.V - c;
ColorRGB rgb = new ColorRGB();
hsv.H %= 360.0f;
if (hsv.H < 60.0f)
rgb = new ColorRGB(c, x, 0);
else if (hsv.H < 120.0f)
rgb = new ColorRGB(x, c, 0);
else if (hsv.H < 180.0f)
rgb = new ColorRGB(0, c, x);
else if (hsv.H < 240.0f)
rgb = new ColorRGB(0, x, c);
else if (hsv.H < 300.0f)
rgb = new ColorRGB(x, 0, c);
else if (hsv.H < 360.0f)
rgb = new ColorRGB(c, 0, x);
return new ColorRGB(rgb.R + m, rgb.G + m, rgb.B + m);
}
public static explicit operator ColorHSV(ColorRGB rgb)
{
float r = rgb.R;
float g = rgb.G;
float b = rgb.B;
float h;
float s;
float v;
float maxChannel = Math.max(r, Math.max(g, b));
float minChannel = Math.min(r, Math.min(g, b));
v = maxChannel;
if (minChannel == maxChannel)
{
h = 0.0f;
s = 0.0f;
}
else
{
s = (maxChannel - minChannel) / maxChannel;
float rc = (maxChannel - r) / (maxChannel - minChannel);
float gc = (maxChannel - g) / (maxChannel - minChannel);
float bc = (maxChannel - b) / (maxChannel - minChannel);
if (r == maxChannel)
{
h = 0.0f + bc - gc;
}
else if (g == maxChannel)
{
h = 2.0f + rc - bc;
}
else
{
h = 4.0f + gc - rc;
}
h = (h / 6.0f) % 1.0f;
h *= 360.0f;
}
return new ColorHSV(h, s, v);
}
}
+63 -4
View File
@@ -3,6 +3,7 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using GlitchyEngine.Core;
using GlitchyEngine.Editor;
using GlitchyEngine.Graphics;
using GlitchyEngine.Graphics.Text;
using GlitchyEngine.Math;
using GlitchyEngine.Physics;
@@ -256,6 +257,28 @@ internal static class ScriptGlue
#endregion
#region SpriteRenderer
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_GetColor(UUID entityId, out ColorRGBA color);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_SetColor(UUID entityId, ColorRGBA color);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_GetUvTransform(UUID entityId, out UVTransform uvTransform);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_SetUvTransform(UUID entityId, UVTransform uvTransform);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_GetMaterial(UUID entityId, out UUID materialId);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SpriteRenderer_SetMaterial(UUID entityId, UUID materialId);
#endregion
#region TextRenderer
[MethodImpl(MethodImplOptions.InternalCall)]
@@ -290,6 +313,17 @@ internal static class ScriptGlue
#endregion
#region MeshRenderer
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MeshRenderer_GetMaterial(UUID entityId, out UUID materialId);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MeshRenderer_SetMaterial(UUID entityId, UUID materialId);
#endregion
#region Math
[MethodImpl(MethodImplOptions.InternalCall)]
@@ -309,7 +343,7 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void UUID_CreateNew(out UUID uuid);
#region Application
#region Application
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Application_IsEditor();
@@ -323,9 +357,9 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Application_IsInPlayMode();
#endregion
#endregion
#region Serialization
#region Serialization
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Serialization_SerializeField(IntPtr serializationContext, SerializationType type, string name, object? value, string? fullTypeName = null);
@@ -342,7 +376,32 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void Serialization_GetObjectTypeName(IntPtr internalContext, out string fullTypeName);
#endregion
#endregion
#region Asset
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Asset_GetIdentifier(UUID assetId, out string text);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Asset_SetIdentifier(UUID assetId, string text);
#endregion
#region Material
public enum ShaderVariableType : byte
{
Bool,
Float,
Int,
UInt
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern unsafe bool Material_SetVariable(UUID assetId, String variableName, ShaderVariableType elementType, int rows, int columns, int arrayLength, void* rawData, int dataLength);
#endregion
#region ImGui