Exemple #1
0
        public void FeedData()
        {
            if (!isActiveAndEnabled)
            {
                return;
            }
            Debug.Log("Feed data from file");

            commandPointer = 0;

            var cmdBuffer   = UnityServerAPI.RPCGetCommandBuffer();
            var frameBuffer = UnityServerAPI.RPCGetFrameBuffer();

            var dataList = ReadFromBinaryFile <List <SerializableData> >(pathToFile);

            frameList = (
                from d in dataList
                where d.GetType() == typeof(SerializableFrameState)
                select d as SerializableFrameState
                ).ToList();
            commandList = (
                from d in dataList
                where d.GetType() == typeof(SerializableCommand)
                select d as SerializableCommand
                ).ToList();

            frameList.ForEach(f => UnityServerAPI.RPCGetFrameBuffer().Write(f));
        }
        void Update()
        {
            ExecutePendingCommands();
            CheckKeyboard();

            renderFrameCount++;
            if (renderFrameCount % skipRate != 0)
            {
                return;
            }
            if (enableSpeedMatch && Time.time < AccumulatedFrameDuration)
            {
                return;
            }

            RPCFrameBuffer frameBuffer = UnityServerAPI.RPCGetFrameBuffer();
            int            n           = frameBuffer.GetNumOfAvailableElements();

            if (n > 0)
            {
                KinematicsServerEvents.InvokeOnBeforeNewFrame(physicalFrameCount++);

                FrameState data = frameBuffer.ReadAndErase(0);

                AccumulatedFrames++;
                AccumulatedFrameDuration += data.duration;

                foreach (GroupState groupState in data.groups)
                {
                    groupNames.Add(groupState.groupName);

                    GameObject groupObj = GameObject.Find(groupState.groupName);
                    if (groupObj == null)
                    {
                        Debug.LogWarning($"RPC: Group {groupState.groupName} does not exist");
                        return;
                    }

                    foreach (ObjectState state in groupState.objectStates)
                    {
                        string    name  = state.objectName;
                        Transform trans = groupObj.transform.Find(name);
                        if (trans)
                        {
                            GameObject obj     = trans.gameObject;
                            Vector3    p       = new Vector3(state.x, state.y, state.z) * generalSettings.scalingFactor;
                            Quaternion q       = new Quaternion(state.qx, state.qy, state.qz, state.qw);
                            bool       pHasNaN = float.IsNaN(p.x) || float.IsNaN(p.y) || float.IsNaN(p.z);
                            bool       qHasNaN = float.IsNaN(q.x) || float.IsNaN(q.y) || float.IsNaN(q.z) || float.IsNaN(q.w);
                            if (!pHasNaN)
                            {
                                obj.transform.position = CoordinateTransform.RightHandToLeftHand(p);
                            }
                            if (!qHasNaN)
                            {
                                obj.transform.rotation = CoordinateTransform.RightHandToLeftHand(q);
                            }
                            if (pHasNaN || qHasNaN)
                            {
                                Debug.LogWarning("Receive NaN in frame data, discard");
                            }
                        }
                        else
                        {
                            Debug.LogWarning($"RPC: Object with name {name} does not exist");
                        }
                    }
                }

                KinematicsServerEvents.InvokeOnAfterNewFrame(data);
            }
        }