/// <summary> /// Serialise messages to generate <paramref name="mesh"/>. /// </summary> /// <param name="mesh">The mesh of interest.</param> /// <param name="packet">Packet buffer to compose messages in</param> /// <param name="writer">Writer to export completed message packets to.</param> /// <returns></returns> /// <remarks> /// Writes: /// <list type="bullet"> /// <item><see cref="MeshCreateMessage"/></item> /// <item><see cref="MeshComponentMessage"/> for each component type from /// <see cref="MeshMessageType"/></item> /// <item><see cref="MeshFinaliseMessage"/> only when <paramref name="mesh"/> is already /// finalised.</item> /// </list> /// </remarks> protected Error Serialise(MeshDetails mesh, PacketBuffer packet, BinaryWriter writer) { // First write a create message. MeshCreateMessage msg = new MeshCreateMessage(); packet.Reset((ushort)RoutingID, (ushort)MeshCreateMessage.MessageID); msg.MeshID = mesh.ID; msg.VertexCount = (uint)mesh.Builder.VertexCount; msg.IndexCount = (uint)(mesh.Builder.ExplicitIndices ? mesh.Builder.IndexCount : 0); msg.DrawType = mesh.DrawType; msg.Attributes.X = mesh.LocalPosition.x; msg.Attributes.Y = mesh.LocalPosition.y; msg.Attributes.Z = mesh.LocalPosition.z; msg.Attributes.RotationX = mesh.LocalRotation.x; msg.Attributes.RotationY = mesh.LocalRotation.y; msg.Attributes.RotationZ = mesh.LocalRotation.z; msg.Attributes.RotationW = mesh.LocalRotation.w; msg.Attributes.ScaleX = mesh.LocalScale.x; msg.Attributes.ScaleY = mesh.LocalScale.y; msg.Attributes.ScaleZ = mesh.LocalScale.z; msg.Attributes.Colour = ShapeComponent.ConvertColour(mesh.Tint); msg.Write(packet); if (!packet.FinalisePacket()) { return(new Error(ErrorCode.SerialisationFailure)); } packet.ExportTo(writer); // Now use the MeshResource methods to complete serialisation. MeshSerialiser serialiser = new MeshSerialiser(mesh); TransferProgress prog = new TransferProgress(); prog.Reset(); while (!prog.Complete) { serialiser.Transfer(packet, 0, ref prog); if (!packet.FinalisePacket()) { return(new Error(ErrorCode.SerialisationFailure)); } packet.ExportTo(writer); } return(new Error()); }