Skip to content

Commit

Permalink
perf: remove redundant transform calls on NT (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymdun committed May 27, 2021
1 parent 00f4833 commit 2d10305
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions Assets/Mirage/Components/NetworkTransformBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Q
public override bool OnSerialize(NetworkWriter writer, bool initialState)
{
// use local position/rotation/scale for VR support
SerializeIntoWriter(writer, TargetComponent.transform.localPosition, TargetComponent.transform.localRotation, TargetComponent.transform.localScale);
SerializeIntoWriter(writer, TargetComponent.localPosition, TargetComponent.localRotation, TargetComponent.localScale);
return true;
}

Expand Down Expand Up @@ -117,7 +117,7 @@ void DeserializeFromReader(NetworkReader reader)

// movement speed: based on how far it moved since last time
// has to be calculated before 'start' is overwritten
temp.MovementSpeed = EstimateMovementSpeed(goal, temp, TargetComponent.transform, syncInterval);
temp.MovementSpeed = EstimateMovementSpeed(goal, temp, TargetComponent, syncInterval);

// reassign start wisely
// -> first ever data point? then make something up for previous one
Expand All @@ -128,9 +128,9 @@ void DeserializeFromReader(NetworkReader reader)
{
TimeStamp = Time.time - syncInterval,
// local position/rotation for VR support
LocalPosition = TargetComponent.transform.localPosition,
LocalRotation = TargetComponent.transform.localRotation,
LocalScale = TargetComponent.transform.localScale,
LocalPosition = TargetComponent.localPosition,
LocalRotation = TargetComponent.localRotation,
LocalScale = TargetComponent.localScale,
MovementSpeed = temp.MovementSpeed
};
}
Expand Down Expand Up @@ -175,11 +175,11 @@ void DeserializeFromReader(NetworkReader reader)
//
// local position/rotation for VR support
//
if (Vector3.Distance(TargetComponent.transform.localPosition, start.LocalPosition) < oldDistance + newDistance)
if (Vector3.Distance(TargetComponent.localPosition, start.LocalPosition) < oldDistance + newDistance)
{
start.LocalPosition = TargetComponent.transform.localPosition;
start.LocalRotation = TargetComponent.transform.localRotation;
start.LocalScale = TargetComponent.transform.localScale;
start.LocalPosition = TargetComponent.localPosition;
start.LocalRotation = TargetComponent.localRotation;
start.LocalScale = TargetComponent.localScale;
}
}

Expand Down Expand Up @@ -289,9 +289,9 @@ bool HasEitherMovedRotatedScaled()
{
// moved or rotated or scaled?
// local position/rotation/scale for VR support
bool moved = Vector3.Distance(lastPosition, TargetComponent.transform.localPosition) > LocalPositionSensitivity;
bool scaled = Vector3.Distance(lastScale, TargetComponent.transform.localScale) > LocalScaleSensitivity;
bool rotated = Quaternion.Angle(lastRotation, TargetComponent.transform.localRotation) > LocalRotationSensitivity;
bool moved = Vector3.Distance(lastPosition, TargetComponent.localPosition) > LocalPositionSensitivity;
bool scaled = Vector3.Distance(lastScale, TargetComponent.localScale) > LocalScaleSensitivity;
bool rotated = Quaternion.Angle(lastRotation, TargetComponent.localRotation) > LocalRotationSensitivity;

// save last for next frame to compare
// (only if change was detected. otherwise slow moving objects might
Expand All @@ -301,9 +301,9 @@ bool HasEitherMovedRotatedScaled()
if (change)
{
// local position/rotation for VR support
lastPosition = TargetComponent.transform.localPosition;
lastRotation = TargetComponent.transform.localRotation;
lastScale = TargetComponent.transform.localScale;
lastPosition = TargetComponent.localPosition;
lastRotation = TargetComponent.localRotation;
lastScale = TargetComponent.localScale;
}
return change;
}
Expand All @@ -312,9 +312,9 @@ bool HasEitherMovedRotatedScaled()
void ApplyPositionRotationScale(Vector3 position, Quaternion rotation, Vector3 scale)
{
// local position/rotation for VR support
TargetComponent.transform.localPosition = position;
TargetComponent.transform.localRotation = rotation;
TargetComponent.transform.localScale = scale;
TargetComponent.localPosition = position;
TargetComponent.localRotation = rotation;
TargetComponent.localScale = scale;
}

void Update()
Expand Down Expand Up @@ -352,7 +352,7 @@ void UpdateClient()
// local position/rotation for VR support
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
SerializeIntoWriter(writer, TargetComponent.transform.localPosition, TargetComponent.transform.localRotation, TargetComponent.transform.localScale);
SerializeIntoWriter(writer, TargetComponent.localPosition, TargetComponent.localRotation, TargetComponent.localScale);

// send to server
CmdClientToServerSync(writer.ToArray());
Expand All @@ -379,9 +379,9 @@ void UpdateClient()
else
{
// local position/rotation for VR support
ApplyPositionRotationScale(InterpolatePosition(start, goal, TargetComponent.transform.localPosition),
InterpolateRotation(start, goal, TargetComponent.transform.localRotation),
InterpolateScale(start, goal, TargetComponent.transform.localScale));
ApplyPositionRotationScale(InterpolatePosition(start, goal, TargetComponent.localPosition),
InterpolateRotation(start, goal, TargetComponent.localRotation),
InterpolateScale(start, goal, TargetComponent.localScale));
}
}
}
Expand Down

0 comments on commit 2d10305

Please sign in to comment.