HoloToolkit視頻共享接受發送腳本

接受數據

RemoteCamera.cs

using UnityEngine;

public class RemoteCamera : MonoBehaviour {

	public static byte[] currentFrame;
	private Texture2D viewTexture;

	public static int CameraID;

    // Use this for initialization
    void Start () {

		currentFrame = new byte[640 * 480 * 4];
		viewTexture = new Texture2D(640, 480);
	}
	
	// Update is called once per frame
	void Update ()
    {
		viewTexture.LoadImage(currentFrame);
		viewTexture.Apply();

		GetComponent<Renderer>().material.mainTexture = viewTexture;
	}
		

}

LocalCustomMessagesManager.cs


        /// <summary>
        /// Called when a remote user sends a message.
        /// </summary>
        /// <param name="msg"></param>
        private void UpdateMessage(NetworkInMessage msg)
        {
            // Parse the message
            long userID = msg.ReadInt64();

			//接受video
			int videoLen =  msg.ReadInt32();
			if (videoLen > 1) {
				msg.ReadArray (RemoteCamera.currentFrame, (uint)videoLen);
			}

            //接受場景位置
            Scene.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
            Scene.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);
            //接收頭部位置
            remotehead.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
            remotehead.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);
            //接收眼動位置
            remotegaze.transform.localPosition = LocalCustomMessages.ReadVector3(msg);
            remotegaze.transform.localRotation = LocalCustomMessages.ReadQuaternion(msg);


}

發送數據

LocalCamera.cs

using UnityEngine;
using System;

public class LocalCamera : MonoBehaviour
{
    public static Texture2D viewTexture;
	private WebCamTexture viewTextureCamera;


    static public int CameraID;

	// Use this for initialization
	void Start ()
	{
		WebCamDevice[] devices = WebCamTexture.devices;
		viewTextureCamera = new WebCamTexture(devices[0].name, 1280, 720, 30);

		viewTextureCamera.Play();

		viewTexture = new Texture2D(viewTextureCamera.width, viewTextureCamera.height);
	}

	// Update is called once per frame
	void Update ()
	{
		viewTexture.SetPixels(viewTextureCamera.GetPixels());
		viewTexture.Apply();

		GetComponent<Renderer>().material.mainTexture = viewTexture;
	}
    
}

LocalCustomMessages.cs

 public void SendMessage()
        {
            // If we are connected to a session, broadcast our head info
            if (serverConnection != null && serverConnection.IsConnected())
            {
                // Create an outgoing network message to contain all the info we want to send
                NetworkOutMessage msg = CreateMessage((byte)TestMessageID.StringMessageID);
                //send video
                byte[] currentLocalVideo = LocalCamera.viewTexture.EncodeToJPG();
                msg.Write(currentLocalVideo.Length);
                msg.WriteArray(currentLocalVideo, (uint)currentLocalVideo.Length);
 

               //發送場景位置
                AppendVector3(msg, Scene.transform.localPosition);
                AppendQuaternion(msg, Scene.transform.localRotation);
                //發送頭部位置
                AppendVector3(msg, headPos.transform.localPosition);
                AppendQuaternion(msg, headPos.transform.localRotation);
                //發送眼動位置
                AppendVector3(msg, GazePos.transform.localPosition);
                AppendQuaternion(msg, GazePos.transform.localRotation);
           }

}

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章