feat: Added Layer Weight to NetworkAnimator (#2134)

* Added Layer Weight to NetworkAnimator

Now NetworkAnimator is aware of each layer weight on it's references
Animator and will sync layer weights across the network.

* NetworkAnimator layers Float equality fix

* Update Assets/Mirror/Components/NetworkAnimator.cs

Co-authored-by: James Frowen <jamesfrowen5@gmail.com>

* Update Assets/Mirror/Components/NetworkAnimator.cs

Co-authored-by: James Frowen <jamesfrowen5@gmail.com>

* Remove GetLayerWeight check every incoming packet as is extern call

Co-authored-by: James Frowen <jamesfrowen5@gmail.com>
This commit is contained in:
Keir Smith 2020-08-05 23:54:46 +01:00 committed by GitHub
parent 87a96d17ca
commit 23b9fb0214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,7 @@ public class NetworkAnimator : NetworkBehaviour
// multiple layers // multiple layers
int[] animationHash; int[] animationHash;
int[] transitionHash; int[] transitionHash;
float[] layerWeight;
float nextSendTime; float nextSendTime;
bool SendMessagesAllowed bool SendMessagesAllowed
@ -78,6 +79,7 @@ void Awake()
animationHash = new int[animator.layerCount]; animationHash = new int[animator.layerCount];
transitionHash = new int[animator.layerCount]; transitionHash = new int[animator.layerCount];
layerWeight = new float[animator.layerCount];
} }
void FixedUpdate() void FixedUpdate()
@ -102,16 +104,24 @@ void FixedUpdate()
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{ {
WriteParameters(writer); WriteParameters(writer);
SendAnimationMessage(stateHash, normalizedTime, i, writer.ToArray()); SendAnimationMessage(stateHash, normalizedTime, i, layerWeight[i], writer.ToArray());
} }
} }
} }
bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layerId) bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layerId)
{ {
bool change = false;
stateHash = 0; stateHash = 0;
normalizedTime = 0; normalizedTime = 0;
float lw = animator.GetLayerWeight(layerId);
if (Mathf.Abs(lw - layerWeight[layerId]) > 0.001f)
{
layerWeight[layerId] = lw;
change = true;
}
if (animator.IsInTransition(layerId)) if (animator.IsInTransition(layerId))
{ {
AnimatorTransitionInfo tt = animator.GetAnimatorTransitionInfo(layerId); AnimatorTransitionInfo tt = animator.GetAnimatorTransitionInfo(layerId);
@ -122,7 +132,7 @@ bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int laye
animationHash[layerId] = 0; animationHash[layerId] = 0;
return true; return true;
} }
return false; return change;
} }
AnimatorStateInfo st = animator.GetCurrentAnimatorStateInfo(layerId); AnimatorStateInfo st = animator.GetCurrentAnimatorStateInfo(layerId);
@ -139,7 +149,7 @@ bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int laye
animationHash[layerId] = st.fullPathHash; animationHash[layerId] = st.fullPathHash;
return true; return true;
} }
return false; return change;
} }
void CheckSendRate() void CheckSendRate()
@ -157,15 +167,15 @@ void CheckSendRate()
} }
} }
void SendAnimationMessage(int stateHash, float normalizedTime, int layerId, byte[] parameters) void SendAnimationMessage(int stateHash, float normalizedTime, int layerId, float weight, byte[] parameters)
{ {
if (isServer) if (isServer)
{ {
RpcOnAnimationClientMessage(stateHash, normalizedTime, layerId, parameters); RpcOnAnimationClientMessage(stateHash, normalizedTime, layerId, weight, parameters);
} }
else if (ClientScene.readyConnection != null) else if (ClientScene.readyConnection != null)
{ {
CmdOnAnimationServerMessage(stateHash, normalizedTime, layerId, parameters); CmdOnAnimationServerMessage(stateHash, normalizedTime, layerId, weight, parameters);
} }
} }
@ -181,7 +191,7 @@ void SendAnimationParametersMessage(byte[] parameters)
} }
} }
void HandleAnimMsg(int stateHash, float normalizedTime, int layerId, NetworkReader reader) void HandleAnimMsg(int stateHash, float normalizedTime, int layerId, float weight, NetworkReader reader)
{ {
if (hasAuthority && clientAuthority) if (hasAuthority && clientAuthority)
return; return;
@ -194,6 +204,8 @@ void HandleAnimMsg(int stateHash, float normalizedTime, int layerId, NetworkRead
animator.Play(stateHash, layerId, normalizedTime); animator.Play(stateHash, layerId, normalizedTime);
} }
animator.SetLayerWeight(layerId, weight);
ReadParameters(reader); ReadParameters(reader);
} }
@ -340,6 +352,7 @@ public override bool OnSerialize(NetworkWriter writer, bool initialState)
writer.WriteInt32(st.fullPathHash); writer.WriteInt32(st.fullPathHash);
writer.WriteSingle(st.normalizedTime); writer.WriteSingle(st.normalizedTime);
} }
writer.WriteSingle(animator.GetLayerWeight(i));
} }
WriteParameters(writer, initialState); WriteParameters(writer, initialState);
return true; return true;
@ -360,6 +373,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
{ {
int stateHash = reader.ReadInt32(); int stateHash = reader.ReadInt32();
float normalizedTime = reader.ReadSingle(); float normalizedTime = reader.ReadSingle();
animator.SetLayerWeight(i, reader.ReadSingle());
animator.Play(stateHash, i, normalizedTime); animator.Play(stateHash, i, normalizedTime);
} }
@ -468,7 +482,7 @@ public void ResetTrigger(int hash)
#region server message handlers #region server message handlers
[Command] [Command]
void CmdOnAnimationServerMessage(int stateHash, float normalizedTime, int layerId, byte[] parameters) void CmdOnAnimationServerMessage(int stateHash, float normalizedTime, int layerId, float weight, byte[] parameters)
{ {
// Ignore messages from client if not in client authority mode // Ignore messages from client if not in client authority mode
if (!clientAuthority) if (!clientAuthority)
@ -479,8 +493,8 @@ void CmdOnAnimationServerMessage(int stateHash, float normalizedTime, int layerI
// handle and broadcast // handle and broadcast
using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(parameters)) using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(parameters))
{ {
HandleAnimMsg(stateHash, normalizedTime, layerId, networkReader); HandleAnimMsg(stateHash, normalizedTime, layerId, weight, networkReader);
RpcOnAnimationClientMessage(stateHash, normalizedTime, layerId, parameters); RpcOnAnimationClientMessage(stateHash, normalizedTime, layerId, weight, parameters);
} }
} }
@ -532,10 +546,10 @@ void CmdOnAnimationResetTriggerServerMessage(int hash)
#region client message handlers #region client message handlers
[ClientRpc] [ClientRpc]
void RpcOnAnimationClientMessage(int stateHash, float normalizedTime, int layerId, byte[] parameters) void RpcOnAnimationClientMessage(int stateHash, float normalizedTime, int layerId, float weight, byte[] parameters)
{ {
using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(parameters)) using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(parameters))
HandleAnimMsg(stateHash, normalizedTime, layerId, networkReader); HandleAnimMsg(stateHash, normalizedTime, layerId, weight, networkReader);
} }
[ClientRpc] [ClientRpc]