Skip to content

Commit

Permalink
perf: remove network transform allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Aug 17, 2019
1 parent 812c643 commit 9e3ecc1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Assets/Mirror/Components/NetworkTransformBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ void DeserializeFromReader(NetworkReader reader)
else if (compressRotation == Compression.Lots)
{
// read 2 byte, 5 bits per float
float[] xyz = FloatBytePacker.UnpackUShortIntoThreeFloats(reader.ReadUInt16(), 0, 360);
temp.localRotation = Quaternion.Euler(xyz[0], xyz[1], xyz[2]);
Vector3 xyz = FloatBytePacker.UnpackUShortIntoThreeFloats(reader.ReadUInt16(), 0, 360);
temp.localRotation = Quaternion.Euler(xyz.x, xyz.y, xyz.z);
}

temp.localScale = reader.ReadVector3();
Expand Down
6 changes: 4 additions & 2 deletions Assets/Mirror/Runtime/FloatBytePacker.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using UnityEngine;

namespace Mirror
{
public static class FloatBytePacker
Expand Down Expand Up @@ -42,7 +44,7 @@ public static ushort PackThreeFloatsIntoUShort(float u, float v, float w, float
}

// see PackThreeFloatsIntoUShort for explanation
public static float[] UnpackUShortIntoThreeFloats(ushort combined, float minTarget, float maxTarget)
public static Vector3 UnpackUShortIntoThreeFloats(ushort combined, float minTarget, float maxTarget)
{
byte lower = (byte)(combined & 0x1F);
byte middle = (byte)((combined >> 5) & 0x1F);
Expand All @@ -52,7 +54,7 @@ public static float[] UnpackUShortIntoThreeFloats(ushort combined, float minTarg
float u = ScaleByteToFloat(lower, 0x00, 0x1F, minTarget, maxTarget);
float v = ScaleByteToFloat(middle, 0x00, 0x1F, minTarget, maxTarget);
float w = ScaleByteToFloat(upper, 0x00, 0x1F, minTarget, maxTarget);
return new float[]{u, v, w};
return new Vector3(u, v, w);
}
}
}

0 comments on commit 9e3ecc1

Please sign in to comment.