Exemplo n.º 1
0
    public unsafe void setMocap(NetworkStream stream)
    {
        if (nmocap > 0)
        {
            ReadAll(stream, 28 * nmocap);
            fixed(byte *pos = buffer, quat = &buffer[12 * nmocap])
            {
                MJP.SetMocap((float *)pos, (float *)quat);
            }

            MJP.Kinematics();
            UpdateModel();
        }
    }
Exemplo n.º 2
0
    public unsafe void setQpos(NetworkStream stream)
    {
        if (nqpos > 0)
        {
            ReadAll(stream, 4 * nqpos);
            fixed(byte *qpos = buffer)
            {
                MJP.SetQpos((float *)qpos);
            }

            MJP.Kinematics();
            UpdateModel();
        }
    }
Exemplo n.º 3
0
    // per-frame update
    unsafe void Update()
    {
        //New Stuff
//        Debug.Log(maze_agent_pos.position.ToString());


        // mouse interaction
        ProcessMouse();
        UpdateModel();

        // check conection each 0.1 sec
        if (lastcheck + 0.1f < Time.time)
        {
            // broken connection: clear
            if (!CheckConnection())
            {
                client = null;
                stream = null;
            }

            lastcheck = Time.time;
        }

        // not connected: accept connection if pending
        if (client == null || !client.Connected)
        {
            if (listener.Pending())
            {
                // make connection
                client = listener.AcceptTcpClient();
                stream = client.GetStream();

                // send 20 bytes: nqpos, nmocap, ncamera, width, height
                stream.Write(BitConverter.GetBytes(nqpos), 0, 4);
                stream.Write(BitConverter.GetBytes(nmocap), 0, 4);
                stream.Write(BitConverter.GetBytes(ncamera), 0, 4);
                stream.Write(BitConverter.GetBytes(offwidth), 0, 4);
                stream.Write(BitConverter.GetBytes(offheight), 0, 4);
            }
        }

        // data available: handle communication
        while (client != null && client.Connected && stream != null && stream.DataAvailable)
        {
            // get command
            ReadAll(4);
            int cmd = BitConverter.ToInt32(buffer, 0);

            // process command
            switch ((Command)cmd)
            {
            // GetInput: send lastkey, select, active, refpos[3], refquat[4]
            case Command.GetInput:
                MJP.TPerturb perturb;
                MJP.GetPerturb(&perturb);
                stream.Write(BitConverter.GetBytes(lastkey), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.select), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.active), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[3]), 0, 4);
                lastkey = 0;
                break;

            // GetImage: send 3*width*height bytes
            case Command.GetImage:
                RenderToTexture();
                stream.Write(offtex.GetRawTextureData(), 0, 3 * offwidth * offheight);
                break;

            // SaveSnapshot: no data exchange
            case Command.SaveSnapshot:
                RenderToTexture();
                byte[] bytes = offtex.EncodeToPNG();
                File.WriteAllBytes(Application.streamingAssetsPath + "/../../" + "img_" +
                                   snapshots + ".png", bytes);
                snapshots++;
                break;

            // SaveVideoframe: no data exchange
            case Command.SaveVideoframe:
                if (videofile == null)
                {
                    videofile = new FileStream(Application.streamingAssetsPath + "/../../" + "video.raw",
                                               FileMode.Create, FileAccess.Write);
                }
                RenderToTexture();
                videofile.Write(offtex.GetRawTextureData(), 0, 3 * offwidth * offheight);
                break;

            // SetCamera: receive camera index
            case Command.SetCamera:
                ReadAll(4);
                camindex = BitConverter.ToInt32(buffer, 0);
                camindex = Math.Max(-1, Math.Min(ncamera - 1, camindex));
                break;

            // SetQpos: receive qpos vector
            case Command.SetQpos:
                if (nqpos > 0)
                {
                    ReadAll(4 * nqpos);
                    fixed(byte *qpos = buffer)
                    {
                        MJP.SetQpos((float *)qpos);
                    }

                    MJP.Kinematics();
                    UpdateModel();
                }
                break;

            // SetMocap: receive mocap_pos and mocap_quat vectors
            case Command.SetMocap:
                if (nmocap > 0)
                {
                    ReadAll(28 * nmocap);
                    fixed(byte *pos = buffer, quat = &buffer[12 * nmocap])
                    {
                        MJP.SetMocap((float *)pos, (float *)quat);
                    }

                    MJP.Kinematics();
                    UpdateModel();
                }
                break;

            // GetTarget: send maze_agent_pos
            case Command.GetTarget:
                float target_x = -1f * maze_agent_pos.position.x;
                float target_y = -1f * maze_agent_pos.position.z;
                if (move_maze == true)
                {
                    break;
                }
                float dist = Vector3.Distance(humanoid_pos.position, maze_agent_pos.position);
//                    Debug.Log(dist);
                if (dist < 1.0f)
                {
                    move_maze = true;
                    Debug.Log(dist);
                }
                stream.Write(BitConverter.GetBytes(target_x), 0, 4);
                stream.Write(BitConverter.GetBytes(target_y), 0, 4);

                break;

            case Command.SetInit:
                ag_reset = true;

                break;
            }
        }
    }
Exemplo n.º 4
0
    // per-frame update
    unsafe void Update()
    {
        // mouse interaction
        ProcessMouse();
        UpdateModel();

        // check conection each 0.1 sec
        if (lastcheck + 0.1f < Time.time)
        {
            // broken connection: clear
            if (!CheckConnection())
            {
                client           = null;
                stream           = null;
                counter.enabled  = false;
                title.enabled    = true;
                subtitle.enabled = true;
                prompt.SetActive(false);
            }
            else
            {
                if (!prompt_mode)
                {
                    counter.enabled = true;
                }
                title.enabled    = false;
                subtitle.enabled = false;
            }

            lastcheck = Time.time;
        }

        OVRInput.Update();

        if (!prompt_mode)
        {
            Vector3    controllerpos  = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
            Quaternion controllerquat = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
            posbuf[0]  = Mathf.Clamp(0.8f + -1 * controllerpos.x * 1.5f, 0.4f, 1.2f);
            posbuf[1]  = Mathf.Clamp(1f + -1 * controllerpos.z * 1.5f, 0.4f, 0.9f);
            posbuf[2]  = Mathf.Clamp(3.2f + controllerpos.y * 4f, 0.0f, 0.8f);
            quatbuf[0] = controllerquat.x;
            quatbuf[1] = controllerquat.y;
            quatbuf[2] = controllerquat.z;
            quatbuf[3] = controllerquat.w;
            grip       = 1f - 2f * OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.RTouch);

            Vector2 lstick = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, OVRInput.Controller.LTouch);
            camera_pos.position = new Vector3(camera_pos.position.x + 0.03f * lstick.x, camera_pos.position.y, camera_pos.position.z + 0.03f * lstick.y);
            if (OVRInput.Get(OVRInput.Button.PrimaryThumbstick, OVRInput.Controller.LTouch))
            {
                camera_pos.position = camera_init_pos;
            }

            //controllerpos = new Vector3(posbuf[0], posbuf[1], posbuf[2]);
            //controllerpos = new Vector3(-10*0.8f, 10 * 0.4f,-10 *0.65f);
            controllerpos = new Vector3(-10 * posbuf[0], 10 * posbuf[2], -10 * posbuf[1]);
            controller_tracker.position = controllerpos;

            //Debug.Log(mocap_pos.position - controllerpos);

            float delta_x = -1 * Mathf.Sign(controllerpos.x - mocap_pos.position.x) * Mathf.Min(Mathf.Abs(controllerpos.x - mocap_pos.position.x), 2.0f) / 2.0f;
            float delta_y = -1 * Mathf.Sign(controllerpos.z - mocap_pos.position.z) * Mathf.Min(Mathf.Abs(controllerpos.z - mocap_pos.position.z), 1.25f) / 1.25f;
            float delta_z = Mathf.Sign(controllerpos.y - mocap_pos.position.y) * Mathf.Min(Mathf.Abs(controllerpos.y - mocap_pos.position.y), 2.0f) / 2.0f;

            posbuf[0] = delta_x;
            posbuf[1] = delta_y;
            posbuf[2] = delta_z;

            //Debug.Log(delta_x);
            counter.text = steps.ToString();
        }
        else
        {
            if (OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.RTouch))
            {
                stream.Write(BitConverter.GetBytes(1), 0, 4);
                prompt_mode     = false;
                counter.enabled = true;
                prompt.SetActive(false);
            }
            else if (OVRInput.Get(OVRInput.Button.Two, OVRInput.Controller.RTouch))
            {
                stream.Write(BitConverter.GetBytes(0), 0, 4);
                prompt_mode     = false;
                counter.enabled = true;
                prompt.SetActive(false);
            }
            else if (OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.LTouch))
            {
                stream.Write(BitConverter.GetBytes(-1), 0, 4);
                Application.Quit();
            }
        }



        // not connected: accept connection if pending
        if (client == null || !client.Connected)
        {
            if (listener.Pending())
            {
                // make connection
                client = listener.AcceptTcpClient();
                stream = client.GetStream();

                // send 20 bytes: nqpos, nmocap, ncamera, width, height
                stream.Write(BitConverter.GetBytes(nqpos), 0, 4);
                stream.Write(BitConverter.GetBytes(nmocap), 0, 4);
                stream.Write(BitConverter.GetBytes(ncamera), 0, 4);
                stream.Write(BitConverter.GetBytes(offwidth), 0, 4);
                stream.Write(BitConverter.GetBytes(offheight), 0, 4);
            }
        }

        // data available: handle communication
        while (client != null && client.Connected && stream != null && stream.DataAvailable)
        {
            // get command
            ReadAll(4);
            int cmd = BitConverter.ToInt32(buffer, 0);

            // process command
            switch ((Command)cmd)
            {
            // GetInput: send lastkey, select, active, refpos[3], refquat[4]
            case Command.GetInput:
                MJP.TPerturb perturb;
                MJP.GetPerturb(&perturb);
                stream.Write(BitConverter.GetBytes(lastkey), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.select), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.active), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refpos[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(perturb.refquat[3]), 0, 4);
                lastkey = 0;
                break;

            // GetImage: send 3*width*height bytes
            case Command.GetImage:
                RenderToTexture();
                stream.Write(offtex.GetRawTextureData(), 0, 3 * offwidth * offheight);
                break;

            // SaveSnapshot: no data exchange
            case Command.SaveSnapshot:
                RenderToTexture();
                byte[] bytes = offtex.EncodeToPNG();
                File.WriteAllBytes(Application.streamingAssetsPath + "/../../" + "img_" +
                                   snapshots + ".png", bytes);
                snapshots++;
                break;

            // SaveVideoframe: no data exchange
            case Command.SaveVideoframe:
                if (videofile == null)
                {
                    videofile = new FileStream(Application.streamingAssetsPath + "/../../" + "video.raw",
                                               FileMode.Create, FileAccess.Write);
                }
                RenderToTexture();
                videofile.Write(offtex.GetRawTextureData(), 0, 3 * offwidth * offheight);
                break;

            // SetCamera: receive camera index
            case Command.SetCamera:
                ReadAll(4);
                camindex = BitConverter.ToInt32(buffer, 0);
                camindex = Math.Max(-1, Math.Min(ncamera - 1, camindex));
                break;

            // SetQpos: receive qpos vector
            case Command.SetQpos:
                steps += 1;
                if (nqpos > 0)
                {
                    ReadAll(4 * nqpos);
                    fixed(byte *qpos = buffer)
                    {
                        MJP.SetQpos((float *)qpos);
                    }

                    MJP.Kinematics();
                    UpdateModel();
                }
                break;

            // SetMocap: receive mocap_pos and mocap_quat vectors
            case Command.SetMocap:
                if (nmocap > 0)
                {
                    ReadAll(28 * nmocap);
                    fixed(byte *pos = buffer, quat = &buffer[12 * nmocap])
                    {
                        MJP.SetMocap((float *)pos, (float *)quat);
                    }

                    MJP.Kinematics();
                    UpdateModel();
                }
                break;

            // GetOVRInput: send Oculus Controller Data (32 Bytes)
            case Command.GetOVRInput:
                stream.Write(BitConverter.GetBytes(grip), 0, 4);
                //stream.Write(BitConverter.GetBytes(0.8f), 0, 4);
                //stream.Write(BitConverter.GetBytes(0.65f), 0, 4);
                //stream.Write(BitConverter.GetBytes(0.4f), 0, 4);
                stream.Write(BitConverter.GetBytes(posbuf[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(posbuf[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(posbuf[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(quatbuf[0]), 0, 4);
                stream.Write(BitConverter.GetBytes(quatbuf[1]), 0, 4);
                stream.Write(BitConverter.GetBytes(quatbuf[2]), 0, 4);
                stream.Write(BitConverter.GetBytes(quatbuf[3]), 0, 4);
                break;

            // SetTarget: receive target pos
            case Command.SetTarget:
                ReadAll(12);
                steps = 0;
                float x = System.BitConverter.ToSingle(buffer.Take(4).ToArray(), 0);
                float y = System.BitConverter.ToSingle(buffer.Skip(4).Take(4).ToArray(), 0);
                float z = System.BitConverter.ToSingle(buffer.Skip(8).Take(4).ToArray(), 0);
                target_pos.position = new Vector3(-10 * x, 10 * z, -10 * y);
                break;

            // SetTargetStatus: change target material based on distance
            case Command.SetTargetStatus:
                ReadAll(4);
                float value = BitConverter.ToInt32(buffer, 0);
                if (value < 0)
                {
                    target_obj.GetComponent <MeshRenderer>().material = normalmat;
                }
                else
                {
                    target_obj.GetComponent <MeshRenderer>().material = glowmat;
                }
                break;

            // GetSaveStatus: send whether to save demo or not
            case Command.GetSaveStatus:
                prompt_mode      = true;
                counter.enabled  = false;
                title.enabled    = false;
                subtitle.enabled = false;
                prompt.SetActive(true);
                break;
            }
        }
    }