Texture inheritance for materials, also script access

This commit is contained in:
Simon Lübeß
2025-03-01 00:03:38 +01:00
parent 824c19c0b2
commit b5088329f5
13 changed files with 470 additions and 82 deletions
+37
View File
@@ -12,6 +12,7 @@ using System.Runtime.InteropServices;
using System.Text;
using GlitchyEngine.Core;
using GlitchyEngine.Extensions;
using GlitchyEngine.Graphics;
using GlitchyEngine.Math;
using GlitchyEngine.Math.Attributes;
using ImGuiNET;
@@ -570,6 +571,10 @@ internal class EntityEditor
{
newValue = ShowComponentDropTarget(fieldName, fieldType, reference, attributes);
}
else if (fieldType.IsSubclassOf(typeof(Asset)))
{
newValue = ShowAssetDropTarget(fieldName, fieldType, reference, attributes);
}
else if (fieldType == typeof(string))
{
newValue = ShowStringEditor(reference, fieldType, fieldName, attributes);
@@ -864,6 +869,38 @@ internal class EntityEditor
return newValue;
}
private static object? ShowAssetDropTarget(string fieldName, Type fieldType, object? currentValue, IEnumerable<Attribute>? attributes)
{
object? newValue = DidNotChange;
string fieldId = StartNewProperty(fieldName, attributes);
Asset? asset = currentValue as Asset;
UUID uuid = asset?.UUID ?? UUID.Zero;
if (ImGuiExtension.ShowAssetDropTarget(ref uuid))
{
if (uuid == UUID.Zero)
{
newValue = null;
}
else
{
try
{
newValue = ActivatorExtension.CreateEngineObject(fieldType, uuid);
}
catch (Exception e)
{
Log.Exception(e);
}
}
}
return newValue;
}
private static object? ShowEntityDropTarget(string fieldName, Type fieldType, object? currentValue, IEnumerable<Attribute>? attributes)
{
object? newValue = DidNotChange;
@@ -51,6 +51,43 @@ public static class ActivatorExtension
return null;
}
/// <summary>
/// Creates an instance of the given type inheriting from engine object and sets it's id.
/// </summary>
/// <param name="engineObjectType">The type of the engine object.</param>
/// <param name="objectId">The objects id.</param>
/// <returns>An instance of the engine object type; or <see langword="null"/> if the creation failed.</returns>
internal static EngineObject? CreateEngineObject(Type engineObjectType, UUID objectId)
{
EngineObject? engineObject = (EngineObject?)CreateInstanceSafe(engineObjectType);
if (engineObject != null)
engineObject._uuid = objectId;
return engineObject;
}
/// <summary>
/// Creates an instance of the given type inheriting from engine object and sets it's id.
/// </summary>
/// <param name="engineObjectType">The type of the engine object.</param>
/// <param name="objectId">The objects id.</param>
/// <returns>An instance of the engine object type; or <see langword="null"/> if the creation failed.</returns>
internal static T? CreateEngineObject<T>(Type engineObjectType, UUID objectId) where T : EngineObject
{
if (!typeof(T).IsAssignableFrom(engineObjectType))
{
return null;
}
EngineObject? engineObject = (EngineObject?)CreateInstanceSafe(engineObjectType);
if (engineObject != null)
engineObject._uuid = objectId;
return (T?)engineObject;
}
/// <summary>
/// Creates an instance of the given component type and sets it's entities id.
/// </summary>
+6
View File
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using ImGuiNET;
@@ -74,6 +75,11 @@ public static class ImGuiExtension
ScriptGlue.ImGuiExtension_ListElementGrabber();
}
public static bool ShowAssetDropTarget(ref UUID uuid)
{
return ScriptGlue.ImGuiExtension_ShowAssetDropTarget(ref uuid);
}
public static bool Checkbox2(string label, ref bool2 value) => CheckboxN(2, label, ref value.X);
public static bool Checkbox3(string label, ref bool3 value) => CheckboxN(3, label, ref value.X);
public static bool Checkbox4(string label, ref bool4 value) => CheckboxN(4, label, ref value.X);
+22
View File
@@ -1,4 +1,5 @@
using System.Diagnostics.SymbolStore;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine.Graphics;
@@ -20,4 +21,25 @@ public class Material : Asset
{
ScriptGlue.Material_ResetVariable(_uuid, name);
}
public void SetTexture(string name, Texture? texture)
{
ScriptGlue.Material_SetTexture(_uuid, name, texture?._uuid ?? UUID.Zero);
}
public Texture? GetTexture(string name)
{
ScriptGlue.Material_GetTexture(_uuid, name, out UUID textureId);
// TODO: Support different texture types?
return new Texture
{
_uuid = textureId
};
}
public void ResetTexture(string name)
{
ScriptGlue.Material_ResetTexture(_uuid, name);
}
}
+34 -3
View File
@@ -8,11 +8,16 @@ namespace GlitchyEngine.Graphics;
public class MeshRenderer : Component
{
/// <summary>
/// Gets or sets the Material of this <see cref="MeshRenderer"/>.
/// Gets or sets the instance Material of this <see cref="MeshRenderer"/>.
/// </summary>
/// <remarks>
/// If you get the <see cref="Material"/> and it is not yet a runtime-instance,
/// a new runtime-instance will be created and returned in its place.
/// If you get the <see cref="GlitchyEngine.Graphics.Material"/> and it is not yet a runtime-instance,
/// a new runtime-instance will be created and returned in its place. If you want to get the actual material instance,
/// use <see cref="SharedMaterial"/> instead.
/// <para>
/// <b>Note:</b> While the behaviour differs for getting <see cref="SharedMaterial"/> and <see cref="Material"/>,
/// the setter behaves identical for both properties.
/// </para>
/// </remarks>
public Material Material
{
@@ -24,4 +29,30 @@ public class MeshRenderer : Component
}
set => ScriptGlue.MeshRenderer_SetMaterial(_uuid, value._uuid);
}
/// <summary>
/// Gets or sets the shared Material of this <see cref="MeshRenderer"/>.
/// </summary>
/// <remarks>
/// <para>
/// In contrast to the <see cref="Material"/>-property this will not create a new runtime-instance and instead
/// always return the actual Material-Asset.
/// Making changes to the instance returned by this property will have global effect.
/// <see cref="GlitchyEngine.Graphics.Material"/> is used (and obviously instances/children of it).
/// </para>
/// <para>
/// <b>Note:</b> While the behaviour differs for getting <see cref="SharedMaterial"/> and <see cref="Material"/>,
/// the setter behaves identical for both properties.
/// </para>
/// </remarks>
public Material SharedMaterial
{
get
{
ScriptGlue.MeshRenderer_GetSharedMaterial(_uuid, out UUID materialHandle);
return new Material { _uuid = materialHandle };
}
set => ScriptGlue.MeshRenderer_SetMaterial(_uuid, value._uuid);
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace GlitchyEngine.Graphics;
public class Texture : Asset
{
}
+15
View File
@@ -13,6 +13,7 @@ namespace GlitchyEngine;
/// <summary>
/// All methods in here are glued to the ScriptGlue.bf in the engine.
/// TODO: This could be auto-generated fairly easily
/// </summary>
internal static class ScriptGlue
{
@@ -321,6 +322,8 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MeshRenderer_SetMaterial(UUID entityId, UUID materialId);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MeshRenderer_GetSharedMaterial(UUID entityId, out UUID materialId);
#endregion
@@ -404,6 +407,15 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Material_ResetVariable(UUID assetId, string variableName);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Material_SetTexture(UUID materialId, string textureName, UUID textureId);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Material_GetTexture(UUID materialId, string textureName, out UUID textureId);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool Material_ResetTexture(UUID materialId, string textureName);
#endregion
#region ImGui
@@ -411,5 +423,8 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ImGuiExtension_ListElementGrabber();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool ImGuiExtension_ShowAssetDropTarget(ref UUID assetId);
#endregion
}