Skip to content

Commit

Permalink
Expose function for getting the parent of the material instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nxrighthere committed Sep 21, 2020
1 parent de4e536 commit 833f130
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 5 deletions.
8 changes: 8 additions & 0 deletions API/MaterialInstance-GetParent().md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### [UnrealEngine.Framework](./UnrealEngine-Framework.md 'UnrealEngine.Framework').[MaterialInstance](./MaterialInstance.md 'UnrealEngine.Framework.MaterialInstance')
## MaterialInstance.GetParent() Method
Returns the parent material or `null` on failure
```csharp
public UnrealEngine.Framework.MaterialInstanceDynamic GetParent();
```
#### Returns
[MaterialInstanceDynamic](./MaterialInstanceDynamic.md 'UnrealEngine.Framework.MaterialInstanceDynamic')
1 change: 1 addition & 0 deletions API/MaterialInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Inheritance [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.O
Derived
↳ [MaterialInstanceDynamic](./MaterialInstanceDynamic.md 'UnrealEngine.Framework.MaterialInstanceDynamic')
### Methods
- [GetParent()](./MaterialInstance-GetParent().md 'UnrealEngine.Framework.MaterialInstance.GetParent()')
- [IsChildOf(UnrealEngine.Framework.MaterialInterface)](./MaterialInstance-IsChildOf(MaterialInterface).md 'UnrealEngine.Framework.MaterialInstance.IsChildOf(UnrealEngine.Framework.MaterialInterface)')
2 changes: 1 addition & 1 deletion API/PrimitiveComponent-GetMaterial(int).md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### [UnrealEngine.Framework](./UnrealEngine-Framework.md 'UnrealEngine.Framework').[PrimitiveComponent](./PrimitiveComponent.md 'UnrealEngine.Framework.PrimitiveComponent')
## PrimitiveComponent.GetMaterial(int) Method
Returns the material at the specified element index
Returns the material at the specified element index or `null` on failure
```csharp
public UnrealEngine.Framework.MaterialInstanceDynamic GetMaterial(int elementIndex);
```
Expand Down
4 changes: 3 additions & 1 deletion Source/Managed/Framework/Codegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace UnrealEngine.Framework {
// Automatically generated

internal static class Shared {
internal const int checksum = 0x281;
internal const int checksum = 0x282;
internal static Dictionary<int, IntPtr> userFunctions = new();
private const string dynamicTypesAssemblyName = "UnrealEngine.DynamicTypes";
private static readonly ModuleBuilder moduleBuilder = AssemblyBuilder.DefineDynamicAssembly(new(dynamicTypesAssemblyName), AssemblyBuilderAccess.RunAndCollect).DefineDynamicModule(dynamicTypesAssemblyName);
Expand Down Expand Up @@ -931,6 +931,7 @@ internal static unsafe Dictionary<int, IntPtr> Load(IntPtr* events, IntPtr funct
IntPtr* materialInstanceFunctions = (IntPtr*)buffer[position++];

MaterialInstance.isChildOf = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, Bool>)materialInstanceFunctions[head++];
MaterialInstance.getParent = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)materialInstanceFunctions[head++];
}

unchecked {
Expand Down Expand Up @@ -1996,6 +1997,7 @@ unsafe partial class Material {

unsafe partial class MaterialInstance {
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, Bool> isChildOf;
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> getParent;
}

unsafe partial class MaterialInstanceDynamic {
Expand Down
23 changes: 21 additions & 2 deletions Source/Managed/Framework/Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8235,9 +8235,16 @@ public Vector3 GetPhysicsAngularVelocityInRadians(string boneName = null) {
}

/// <summary>
/// Returns the material at the specified element index
/// Returns the material at the specified element index or <c>null</c> on failure
/// </summary>
public MaterialInstanceDynamic GetMaterial(int elementIndex) => new(getMaterial(Pointer, elementIndex));
public MaterialInstanceDynamic GetMaterial(int elementIndex) {
IntPtr pointer = getMaterial(Pointer, elementIndex);

if (pointer != IntPtr.Zero)
return new(pointer);

return null;
}

/// <summary>
/// Retrieves distance to closest collision
Expand Down Expand Up @@ -9856,6 +9863,18 @@ public bool IsChildOf(MaterialInterface material) {

return isChildOf(Pointer, material.Pointer);
}

/// <summary>
/// Returns the parent material or <c>null</c> on failure
/// </summary>
public MaterialInstanceDynamic GetParent() {
IntPtr pointer = getParent(Pointer);

if (pointer != IntPtr.Zero)
return new(pointer);

return null;
}
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions Source/Managed/Tests/DynamicEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void OnBeginPlay() {
leftStaticMeshComponent.SetEnableGravity(false);
leftStaticMeshComponent.SetSimulatePhysics(true);

Assert.IsNotNull(leftStaticMeshComponent.GetMaterial(0));

rightActor.RegisterEvent(ActorEventType.OnActorBeginOverlap);
rightActor.RegisterEvent(ActorEventType.OnActorEndOverlap);
rightActor.RegisterEvent(ActorEventType.OnActorHit);
Expand All @@ -93,6 +95,8 @@ public void OnBeginPlay() {
rightStaticMeshComponent.UpdateToWorld(TeleportType.ResetPhysics);
rightStaticMeshComponent.SetEnableGravity(false);
rightStaticMeshComponent.SetSimulatePhysics(true);

Assert.IsNotNull(rightStaticMeshComponent.GetMaterial(0));
}

public void OnTick(float deltaTime) {
Expand Down
7 changes: 6 additions & 1 deletion Source/Managed/Tests/SkeletalMeshes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public void OnBeginPlay() {
rightSkeletalMeshComponent.SetWorldLocation(new(-700.0f, 70.0f, -100.0f));
rightSkeletalMeshComponent.SetWorldRotation(Maths.Euler(0.0f, 0.0f, 90.0f));
rightSkeletalMeshComponent.SetAnimationMode(AnimationMode.Asset);
rightSkeletalMeshComponent.CreateAndSetMaterialInstanceDynamic(0).SetVectorParameterValue("AccentColor", new(0.0f, 0.5f, 1.0f));

MaterialInstanceDynamic prototypeMaterial = rightSkeletalMeshComponent.CreateAndSetMaterialInstanceDynamic(0);

prototypeMaterial.SetVectorParameterValue("AccentColor", new(0.0f, 0.5f, 1.0f));

Assert.IsNotNull(prototypeMaterial.GetParent());

AnimationMontage rightPrototypeAnimationMontage = AnimationMontage.Load("/Game/Tests/Characters/Animations/RunAnimationMontage");

Expand Down
1 change: 1 addition & 0 deletions Source/Native/Source/UnrealCLR/Private/UnrealCLR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ void UnrealCLR::Module::StartupModule() {
Shared::Functions[position++] = Shared::MaterialInstanceFunctions;

Shared::MaterialInstanceFunctions[head++] = (void*)&UnrealCLRFramework::MaterialInstance::IsChildOf;
Shared::MaterialInstanceFunctions[head++] = (void*)&UnrealCLRFramework::MaterialInstance::GetParent;

checksum += head;
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Native/Source/UnrealCLR/Private/UnrealCLRFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3349,6 +3349,10 @@ namespace UnrealCLRFramework {
bool IsChildOf(UMaterialInstance* MaterialInstance, UMaterialInterface* Material) {
return MaterialInstance->IsChildOf(Material);
}

UMaterialInstanceDynamic* GetParent(UMaterialInstance* MaterialInstance) {
return UMaterialInstanceDynamic::Create(MaterialInstance->Parent, MaterialInstance);
}
}

namespace MaterialInstanceDynamic {
Expand Down
1 change: 1 addition & 0 deletions Source/Native/Source/UnrealCLR/Public/UnrealCLRFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ namespace UnrealCLRFramework {

namespace MaterialInstance {
static bool IsChildOf(UMaterialInstance* MaterialInstance, UMaterialInterface* Material);
static UMaterialInstanceDynamic* GetParent(UMaterialInstance* MaterialInstance);
}

namespace MaterialInstanceDynamic {
Expand Down

0 comments on commit 833f130

Please sign in to comment.