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();
        }
Esempio n. 2
0
        internal void HandleCommand(Command cmd)
        {
            switch (cmd.name)
            {
            case "_sys_create_primitive":
                CmdCreatePrimitive(cmd);
                break;

            default:
                KinematicsServerEvents.InvokeOnCommand(cmd);
                return;
            }
            KinematicsServerEvents.InvokeOnSystemCommand(cmd);
        }
        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);
            }
        }