示例#1
0
    // Updating the mesh every time a new buffer is received from the network
    private void DataProvider_OnNewData(object sender, EventArgs <byte[]> e)
    {
        // Starting the stopwatch which counts the time needed to process a buffer until the mesh is rendered
        stopWatch = System.Diagnostics.Stopwatch.StartNew();

        lock (e)
        {
            if (e.Value != null && received_new_frame == false)
            {
                //// Storing the received buffer to a .bin file
                //File.WriteAllBytes(@"C:\VCL_User_Prod\RealSenzBinsCorto\RealSenzBin_" + ind + ".bin", e.Value);
                //Debug.Log("file saved");

                stopWatch = System.Diagnostics.Stopwatch.StartNew();
                stopWatch.Start();

                // Flaging that a new buffer is received
                int               size        = Marshal.SizeOf(e.Value[0]) * e.Value.Length;                                   // Buffer 's size
                var               buffer      = e.Value;                                                                       // Buffer 's data
                var               gcRes       = GCHandle.Alloc(buffer, GCHandleType.Pinned);                                   // GCHandler for the buffer
                var               pnt         = gcRes.AddrOfPinnedObject();                                                    // Buffer 's address
                IntPtr            meshPtr     = DllFunctions.callTVMFrameDLL(pnt, buffer.Length, mesh_id);                     // Pointer of the returned structure
                DllFunctions.Mesh currentMesh = (DllFunctions.Mesh)Marshal.PtrToStructure(meshPtr, typeof(DllFunctions.Mesh)); // C# struct equivalent of the one produced by the native C++ DLL

                // Clearing the lists of the deserialiazed buffer 's data
                m_vertices.Clear();
                m_faces.Clear();
                m_participatingCams.Clear();
                m_colorExts.Clear();
                m_colorInts.Clear();


                try
                {
                    // Defining the textures from the returned struct
                    DefineTexture(currentMesh);

                    // Defining the mesh data from the returned struct
                    CreateShape(currentMesh);

                    // Defining the shader 's parameters from the returned struct
                    DefineShaderParams(currentMesh);

                    // Freeing the GCHandler
                    gcRes.Free();

                    received_new_frame = true;
                }
                catch (UnityException ex)
                {
                    Debug.Log(ex.Message);
                }
                stopWatch.Stop();
                deserializeTime.Add(stopWatch.ElapsedMilliseconds);
                if (deserializeTime.Count == 1000)
                {
                    Debug.Log("Deserialization time for 1000 frames -> Mean: " + deserializeTime.Average() + ", " + "Std: " + CalculateStdDev(deserializeTime));
                }
            }
        }
    }