Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using UnityEngine.UI;
- using System.Diagnostics;
- using System.Threading;
- using System;
- using System.Collections.Generic;
- public class ScreenServer : MonoBehaviour {
- private Texture2D imageB;
- private Texture2D imageA;
- public RawImage imageLeft;
- public RawImage imageRight;
- private byte[] lastPacket;
- private UdpClient server;
- private bool startedAServer;
- private Stopwatch framerateTimer;
- private Stopwatch latencyTimer;
- private IPEndPoint remoteEP;
- private int lastFrameCount;
- private int frameCount;
- public float leftEyeOffset;
- public float rightEyeOffset;
- public GUISkin skin;
- public float scale = 0.75f;
- bool swapImage = false;
- private Rect left;
- private Rect right;
- private bool is3D;
- private IPEndPoint clientRemote;
- private Thread serverThread;
- private byte[] currentPacket;
- public byte[] CurrentPacket {
- get {
- return currentPacket;
- }
- set {
- currentPacket = value;
- }
- }
- private Thread latencyThread;
- Queue frameQueue = new Queue(new List<byte[]>());
- private long latency;
- public int delayPerFrame;
- private int boxWidth;
- private float width;
- private float height;
- private Texture2D fragment;
- private Thread thread;
- // Use this for initialization
- void Start() {
- FindObjectOfType<Text>().text = Network.player.ipAddress;
- imageB = new Texture2D((int) Screen.width, (int) Screen.height);
- imageA = new Texture2D((int) Screen.width, (int) Screen.height);
- framerateTimer = Stopwatch.StartNew();
- Screen.sleepTimeout = SleepTimeout.NeverSleep;
- latencyTimer = new Stopwatch();
- remoteEP = new IPEndPoint(IPAddress.Any, 18000);
- thread = new Thread(new ThreadStart(delegate {
- while (true) {
- latencyTimer.Start();
- using (MemoryStream stream = new MemoryStream(server.Receive(ref remoteEP))) {
- currentPacket = stream.ToArray();
- frameCount++;
- latencyTimer.Stop();
- latency = latencyTimer.ElapsedMilliseconds;
- latencyTimer.Reset();
- }
- }
- }));
- thread.Start();
- }
- public static Texture2D DrawImage(Texture2D background, Texture2D screenUpdate, int startX, int startY) {
- // Create a new writable texture.
- background.SetPixels32(startX, startY, screenUpdate.width, screenUpdate.height, screenUpdate.GetPixels32());
- background.Apply();
- return background;
- }
- // Update is called once per frame
- void Update() {
- CheckFrames();
- }
- private void CheckFrames() {
- if (!thread.IsAlive) {
- thread = new Thread(new ThreadStart(delegate {
- while (true) {
- latencyTimer.Start();
- using (MemoryStream stream = new MemoryStream(server.Receive(ref remoteEP))) {
- currentPacket = stream.ToArray();
- frameCount++;
- latencyTimer.Stop();
- latency = latencyTimer.ElapsedMilliseconds;
- latencyTimer.Reset();
- }
- }
- }));
- thread.Start();
- }
- if (startedAServer) {
- UpdateFrames();
- }
- }
- private void UpdateFrames() {
- if (currentPacket != null) {
- //if (is3D) {
- // if (lastPacket != null) {
- // imageA.LoadImage(currentPacket);
- // imageA.Apply();
- // imageB.LoadImage(lastPacket);
- // imageB.Apply();
- // }
- // lastPacket = currentPacket;
- //} else {
- imageA.LoadImage(currentPacket);
- imageA.Apply();
- //imageB.LoadImage(currentPacket);
- //imageB.Apply();
- //}
- }
- if (framerateTimer.ElapsedMilliseconds >= 1000) {
- lastFrameCount = frameCount;
- frameCount = 0;
- framerateTimer.Reset();
- framerateTimer.Start();
- }
- imageLeft.texture = imageA;
- imageRight.texture = imageA;
- RectTransform rectTransform = imageLeft.GetComponent<RectTransform>();
- rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, ((rectTransform.sizeDelta.x) * ((float) imageA.height / (float) imageA.width)));
- RectTransform rectTransform2 = imageRight.GetComponent<RectTransform>();
- rectTransform2.sizeDelta = new Vector2(rectTransform.sizeDelta.x, ((rectTransform2.sizeDelta.x) * ((float) imageA.height / (float) imageA.width)));
- }
- void FixedUpdate() {
- if (!startedAServer) {
- if (framerateTimer.ElapsedMilliseconds >= 0) {
- server = new UdpClient(18000);
- startedAServer = true;
- }
- }
- }
- void OnGUI() {
- GUI.skin = skin;
- //if (currentPacket != null && latency < 2000) {
- boxWidth = (Screen.width / 2);
- width = (float) boxWidth * scale;
- height = ((float) imageB.height / (float) imageB.width) * (float) (width);
- float yPos = (Screen.height / 2) - (height / 2);
- float xPos = (boxWidth / 2) - (width / 2);
- left = new Rect(0 + leftEyeOffset + xPos, yPos, width, height);
- right = new Rect(boxWidth + rightEyeOffset + xPos, yPos, width, height);
- GUI.Label(left, lastFrameCount.ToString());
- GUI.Label(right, lastFrameCount.ToString());
- }
- void OnApplicationQuit() {
- server.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment