Skip to content

Commit 59bb93b

Browse files
[Serialization] Fixes to Scene serialization of layer masks and guid asset loading;
[Rendering] Minor work on Animation State Machine;
1 parent a4b5913 commit 59bb93b

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

Engine/Core/Rendering/Animation/AnimationStateMachine.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Staple;
55

6-
public class AnimationStateMachine : IStapleAsset
6+
public class AnimationStateMachine : IStapleAsset, IGuidAsset
77
{
88
public enum AnimationCondition
99
{
@@ -47,4 +47,11 @@ public class AnimationState
4747
public Mesh mesh;
4848

4949
public List<AnimationState> states = new();
50+
51+
public string Guid { get; set; }
52+
53+
public static object Create(string guid)
54+
{
55+
return Resources.Load<AnimationStateMachine>(guid);
56+
}
5057
}

Engine/Core/Rendering/Animation/SkinnedMeshAnimator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ internal class Item
1414
public string animation;
1515
public bool repeat = true;
1616

17+
public AnimationStateMachine stateMachine;
18+
1719
internal bool playInEditMode;
1820
internal float playTime;
1921
internal Dictionary<string, Item> nodeRenderers = new();
2022
internal SkinnedMeshAnimationEvaluator evaluator;
23+
internal AnimationStateMachine.AnimationState currentState;
2124
}

Engine/Core/Rendering/Animation/SkinnedMeshAnimatorSystem.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Numerics;
45

56
namespace Staple;
@@ -46,6 +47,26 @@ public Type RelatedComponent()
4647
return typeof(SkinnedMeshAnimator);
4748
}
4849

50+
private void SetState(SkinnedMeshAnimator animator, string name)
51+
{
52+
if(name == null)
53+
{
54+
return;
55+
}
56+
57+
var state = animator.stateMachine.states.FirstOrDefault(x => x.name == name);
58+
59+
if(state == null)
60+
{
61+
return;
62+
}
63+
64+
animator.repeat = state.repeat;
65+
animator.animation = state.animation;
66+
animator.playTime = 0;
67+
animator.currentState = state;
68+
}
69+
4970
public void Prepare()
5071
{
5172
}
@@ -73,6 +94,17 @@ public void Process(Entity entity, Transform transform, IComponent relatedCompon
7394
animator.nodeRenderers = GatherNodes(transform, animator.mesh.meshAsset.rootNode);
7495
}
7596

97+
if(Platform.IsPlaying)
98+
{
99+
if (animator.stateMachine != null)
100+
{
101+
if (animator.currentState == null)
102+
{
103+
SetState(animator, animator.stateMachine.states.FirstOrDefault()?.name);
104+
}
105+
}
106+
}
107+
76108
if (Platform.IsPlaying || animator.playInEditMode)
77109
{
78110
if (animator.evaluator == null ||

Engine/Core/Serialization/Asset/AssetSerialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static object GetGuidAsset(
9797
{
9898
var parameters = method.GetParameters();
9999

100-
if (parameters.Length != 1 || parameters[0].Name != "path")
100+
if (parameters.Length != 1 || parameters[0].ParameterType != typeof(string))
101101
{
102102
continue;
103103
}

Engine/Core/Serialization/Scene/SceneSerialization.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ public static void DeserializeField(FieldInfo field, ref IComponent componentIns
177177
field.SetValue(componentInstance, (Color)color);
178178
}
179179
}
180+
else if (field.FieldType == typeof(LayerMask) && element.ValueKind == JsonValueKind.Number)
181+
{
182+
var mask = new LayerMask()
183+
{
184+
value = element.GetUInt32(),
185+
};
186+
187+
field.SetValue(componentInstance, mask);
188+
}
180189
}
181190

182191
public static void DeserializeField(FieldInfo field, ref IComponent componentInstance, SceneComponentParameter parameter)
@@ -374,18 +383,10 @@ public static void DeserializeField(FieldInfo field, ref IComponent componentIns
374383
}
375384
else if (field.FieldType == typeof(LayerMask))
376385
{
377-
var mask = new LayerMask();
378-
379-
if (parameter.stringValue.ToUpperInvariant() == "EVERYTHING")
386+
var mask = new LayerMask()
380387
{
381-
mask = LayerMask.Everything;
382-
}
383-
else
384-
{
385-
var layers = LayerMask.GetMask(parameter.stringValue.Split(" ".ToCharArray()));
386-
387-
mask.value = layers;
388-
}
388+
value = (uint)parameter.intValue,
389+
};
389390

390391
field.SetValue(componentInstance, mask);
391392
}

0 commit comments

Comments
 (0)