/// <summary> /// Writes TransformSyncData into a writer. /// </summary> /// <param name="writer"></param> /// <param name="syncData"></param> public static void WriteTransformSyncData(this NetworkWriter writer, TransformSyncData syncData) { //SyncProperties. SyncProperties sp = (SyncProperties)syncData.SyncProperties; writer.WriteByte(syncData.SyncProperties); //SequenceId. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Sequenced)) { writer.WriteUInt32(syncData.SequenceId); } //Position. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Position)) { writer.WriteVector3(syncData.Position); } //Rotation. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Rotation)) { WriteCompressedQuaternion(writer, syncData.Rotation); } //Scale. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Scale)) { writer.WriteVector3(syncData.Scale); } //Platform. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Platform)) { writer.WriteUInt32(syncData.PlatformNetId); } }
/// <summary> /// Converts reader data into a new TransformSyncData. /// </summary> /// <param name="reader"></param> /// <returns></returns> public static TransformSyncData ReadTransformSyncData(this NetworkReader reader) { SyncProperties sp = (SyncProperties)reader.ReadByte(); TransformSyncData syncData = new TransformSyncData(); syncData.SyncProperties = (byte)sp; //SequenceId. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Sequenced)) { syncData.SequenceId = reader.ReadUInt32(); } //Position. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Position)) { syncData.Position = reader.ReadVector3(); } //Rotation. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Rotation)) { syncData.Rotation = ReadCompressedQuaternion(reader); } //scale. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Scale)) { syncData.Scale = reader.ReadVector3(); } return(syncData); }
/// <summary> /// Writes TransformSyncData into a writer. /// </summary> /// <param name="writer"></param> /// <param name="syncData"></param> public static void WriteTransformSyncData(this NetworkWriter writer, TransformSyncData syncData) { //SyncProperties. SyncProperties sp = (SyncProperties)syncData.SyncProperties; writer.WriteByte(syncData.SyncProperties); //NetworkIdentity. //Get compression level for netIdentity. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Id1)) { writer.WriteByte((byte)syncData.NetworkIdentity); } else if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Id2)) { writer.WriteUInt16((ushort)syncData.NetworkIdentity); } else { writer.WriteUInt32(syncData.NetworkIdentity); } //ComponentIndex. writer.WriteByte(syncData.ComponentIndex); //Position. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Position)) { if (EnumContains.SyncPropertiesContains(sp, SyncProperties.CompressSmall)) { WriteCompressedVector3(writer, syncData.Position); } else { writer.WriteVector3(syncData.Position); } } //Rotation. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Rotation)) { writer.WriteUInt32(Quaternions.CompressQuaternion(syncData.Rotation)); } //Scale. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Scale)) { if (EnumContains.SyncPropertiesContains(sp, SyncProperties.CompressSmall)) { WriteCompressedVector3(writer, syncData.Scale); } else { writer.WriteVector3(syncData.Scale); } } //Platform. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Platform)) { writer.WriteUInt32(syncData.PlatformNetId); } }
/// <summary> /// Writes a compressed Vector3 to the writer. /// </summary> /// <param name="writer"></param> /// <param name="ca"></param> /// <param name="v"></param> private static void WriteCompressedVector3(NetworkWriter writer, Vector3 v) { CompressedAxes ca = CompressedAxes.None; //If can compress X. float absX = Mathf.Abs(v.x); if (absX <= MAX_COMPRESSION_VALUE) { ca |= (Mathf.Sign(v.x) > 0f) ? CompressedAxes.XPositive : CompressedAxes.XNegative; } //If can compress Y. float absY = Mathf.Abs(v.y); if (absY <= MAX_COMPRESSION_VALUE) { ca |= (Mathf.Sign(v.y) > 0f) ? CompressedAxes.YPositive : CompressedAxes.YNegative; } //If can compress Z. float absZ = Mathf.Abs(v.z); if (absZ <= MAX_COMPRESSION_VALUE) { ca |= (Mathf.Sign(v.z) > 0f) ? CompressedAxes.ZPositive : CompressedAxes.ZNegative; } //Write compresed axes. writer.WriteByte((byte)ca); //X if (EnumContains.CompressedAxesContains(ca, CompressedAxes.XNegative) || EnumContains.CompressedAxesContains(ca, CompressedAxes.XPositive)) { writer.WriteUInt16((ushort)Mathf.Round(absX * 100f)); } else { writer.WriteSingle(v.x); } //Y if (EnumContains.CompressedAxesContains(ca, CompressedAxes.YNegative) || EnumContains.CompressedAxesContains(ca, CompressedAxes.YPositive)) { writer.WriteUInt16((ushort)Mathf.Round(absY * 100f)); } else { writer.WriteSingle(v.y); } //Z if (EnumContains.CompressedAxesContains(ca, CompressedAxes.ZNegative) || EnumContains.CompressedAxesContains(ca, CompressedAxes.ZPositive)) { writer.WriteUInt16((ushort)Mathf.Round(absZ * 100f)); } else { writer.WriteSingle(v.z); } }
/// <summary> /// Reads a compressed Vector3. /// </summary> /// <param name="writer"></param> /// <param name="ca"></param> /// <param name="v"></param> private static Vector3 ReadCompressedVector3(NetworkReader reader) { CompressedAxes ca = (CompressedAxes)reader.ReadByte(); //Sign of compressed axes. If 0f, no compression was used for the axes. float sign; //X float x; if (EnumContains.CompressedAxesContains(ca, CompressedAxes.XNegative)) { sign = -1f; } else if (EnumContains.CompressedAxesContains(ca, CompressedAxes.XPositive)) { sign = 1f; } else { sign = 0f; } //If there is compression. if (sign != 0f) { x = (reader.ReadUInt16() / 100f) * sign; } else { x = reader.ReadSingle(); } //Y float y; if (EnumContains.CompressedAxesContains(ca, CompressedAxes.YNegative)) { sign = -1f; } else if (EnumContains.CompressedAxesContains(ca, CompressedAxes.YPositive)) { sign = 1f; } else { sign = 0f; } //If there is compression. if (sign != 0f) { y = (reader.ReadUInt16() / 100f) * sign; } else { y = reader.ReadSingle(); } //Z float z; if (EnumContains.CompressedAxesContains(ca, CompressedAxes.ZNegative)) { sign = -1f; } else if (EnumContains.CompressedAxesContains(ca, CompressedAxes.ZPositive)) { sign = 1f; } else { sign = 0f; } //If there is compression. if (sign != 0f) { z = (reader.ReadUInt16() / 100f) * sign; } else { z = reader.ReadSingle(); } return(new Vector3(x, y, z)); }
/// <summary> /// Converts reader data into a new TransformSyncData. /// </summary> /// <param name="reader"></param> /// <returns></returns> public static TransformSyncData ReadTransformSyncData(this NetworkReader reader) { TransformSyncData syncData = new TransformSyncData(); //Sync properties. SyncProperties sp = (SyncProperties)reader.ReadByte(); syncData.SyncProperties = (byte)sp; //NetworkIdentity. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Id1)) { syncData.NetworkIdentity = reader.ReadByte(); } else if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Id2)) { syncData.NetworkIdentity = reader.ReadUInt16(); } else { syncData.NetworkIdentity = reader.ReadUInt32(); } //ComponentIndex. syncData.ComponentIndex = reader.ReadByte(); //Position. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Position)) { if (EnumContains.SyncPropertiesContains(sp, SyncProperties.CompressSmall)) { syncData.Position = ReadCompressedVector3(reader); } else { syncData.Position = reader.ReadVector3(); } } //Rotation. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Rotation)) { syncData.Rotation = Quaternions.DecompressQuaternion(reader.ReadUInt32()); } //scale. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Scale)) { if (EnumContains.SyncPropertiesContains(sp, SyncProperties.CompressSmall)) { syncData.Scale = ReadCompressedVector3(reader); } else { syncData.Scale = reader.ReadVector3(); } } //Platformed. if (EnumContains.SyncPropertiesContains(sp, SyncProperties.Platform)) { syncData.PlatformNetId = reader.ReadUInt32(); } return(syncData); }