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 Start() { InputController.InitKeyMapping(this); commandHandler = new CommandHandler(); commandHandler.Init(); foreach (var kvp in generalSettings.registeredMaterials) { RegisteredMaterialsMap.Add(kvp.name, kvp.material); } Renderer renderer = gameObject.AddComponent <MeshRenderer>(); renderer.material.SetFloat("_Metallic", 0.1f); renderer.material.SetFloat("_Glossiness", 0.1f); if (defaultMaterial == null) { renderer.material.mainTexture = (Texture2D)Resources.Load("wood"); defaultMaterial = renderer.material; } RegisteredMaterialsMap.Add("_sys_default", defaultMaterial); UnityServerAPI.RPCStartServer(generalSettings.ServerPort); Debug.Log("Waiting for data"); KinematicsServerEvents.InvokeOnServerInitialize(); }
public void ExecutePendingCommands() { RPCCommandBuffer cmdBuffer = UnityServerAPI.RPCGetCommandBuffer(); int nCmd = cmdBuffer.GetNumOfAvailableElements(); for (int i = 0; i < nCmd; i++) { Command cmd = cmdBuffer.ReadAndErase(0); commandHandler.HandleCommand(cmd); } }
public static void Send(Command cmd) { int code = UnityServerAPI.SendCommand(cmd); if (code == -1) { Debug.LogWarning("Command failed, remote is not responding"); } if (code == -2) { Debug.LogWarning("Command failed, no remote connected"); } }
private void FeedCommands(int frameId) { if (!isActiveAndEnabled) { return; } int prevCommandPointer = commandPointer; while (commandList.Count > commandPointer && frameId == commandList[commandPointer].frameId) { UnityServerAPI.RPCGetCommandBuffer().Write(commandList[commandPointer]); commandPointer++; } if (commandPointer > prevCommandPointer) { KinematicsServer.instance.ExecutePendingCommands(); } }
void OnApplicationQuit() { UnityServerAPI.RPCStopServer(); Debug.Log("RPC Stopped"); }
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); } }