//构建帧快照 public void BuildSnapshot(string tag) { //清除指定tag的网格 { var tmp = new SortedDictionary <int, MeshFrame>(); foreach (var curr in FrameSnapshot) { if (curr.Value.tag != tag) { tmp.Add(curr.Key, curr.Value); } } FrameSnapshot = tmp; } int index = 0; foreach (MFABone curr in Skeletals) { index++; string[] ns = curr.gameObject.name.Split('#'); string ctag = (ns.Length > 1) ? ns[ns.Length - 1] : ""; if (!string.IsNullOrEmpty(tag) && ctag != tag) { continue; } MeshFrame f = new MeshFrame(); SkinnedMeshRenderer skMR = curr.gameObject.GetComponent <SkinnedMeshRenderer>(); //新建一个mesh并进行蒙皮计算 Mesh mesh = new Mesh(); f.tag = ctag; skMR.BakeMesh(mesh); Matrix4x4 mxParent = new Matrix4x4(); mxParent.SetTRS( Vector3.zero, RootBones[0].gameObject.transform.localRotation, Vector3.one ); Matrix4x4 mx4 = new Matrix4x4(); Vector3 localpos = curr.gameObject.transform.localPosition; Vector3 scale = RootBones[0].gameObject.transform.lossyScale; mx4.SetTRS( new Vector3(localpos.x * scale.x, localpos.y * scale.y, localpos.z * scale.z) + RootBones[0].gameObject.transform.localPosition, curr.gameObject.transform.localRotation, Vector3.one ); f.TransformMX = mxParent * mx4; f.mesh = (mesh); FrameSnapshot.Add(index, f); } foreach (MFABone curr in Meshs) { index++; string[] ns = curr.gameObject.name.Split('#'); string ctag = (ns.Length > 1) ? ns[ns.Length - 1] : ""; if (!string.IsNullOrEmpty(tag) && ctag != tag) { continue; } MeshFrame f = new MeshFrame(); f.mesh = (curr.gameObject.GetComponent <MeshFilter>().sharedMesh); f.TransformMX = curr.TransformMX; f.tag = ctag; FrameSnapshot.Add(index, f); } }
/// <summary> /// This sends all the frames though the message bus. /// </summary> private static void Flush() { // This task should run while the service is running Console.WriteLine("Messaging: Starting publisher on thread #" + Thread.CurrentThread.ManagedThreadId); while (Service.IsRunning) { try { // Measure one send using (Profiler.Default.Measure("MessageQueue.Flush")) { // Go through all the connections foreach (var server in Service.Mesh.Members) { // Get the message queue var mq = server.Session as MessageQueue; if (mq == null || server.State != ServerState.Online) { continue; } // Acquire a lock on the current frame so we can push it to the pending queue lock (mq.PublishLock) { // If frame is empty, ignore if (mq.CurrentFrame.Length > 0) { // The frame is not full, but push it anyway mq.FrameQueue.Enqueue(mq.CurrentFrame); mq.CurrentFrame = MessageFrame.Acquire(); } } MessageFrame frame; while (mq.FrameQueue.TryDequeue(out frame)) { try { // Send the buffer server.Send( MeshFrame.Acquire(frame.AsSegment()) ); } catch (Exception ex) { // Catch all exceptions here and print it out Service.Logger.Log(ex); } finally { // Once the frame is sent, release it frame.TryRelease(); } } } } // Wait for the messages to queue up Thread.Sleep(2); } catch (Exception ex) { // Log the exception Service.Logger.Log(ex); } } }