SebastianLawe

Untitled

Jun 20th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.IO;
  6. using UnityEngine.UI;
  7. using System.Diagnostics;
  8. using System.Threading;
  9. using System;
  10. using System.Collections.Generic;
  11.  
  12. public class ScreenServer : MonoBehaviour {
  13.     private Texture2D imageB;
  14.     private Texture2D imageA;
  15.     public RawImage imageLeft;
  16.     public RawImage imageRight;
  17.     private byte[] lastPacket;
  18.     private UdpClient server;
  19.     private bool startedAServer;
  20.     private Stopwatch framerateTimer;
  21.     private Stopwatch latencyTimer;
  22.     private IPEndPoint remoteEP;
  23.     private int lastFrameCount;
  24.     private int frameCount;
  25.     public float leftEyeOffset;
  26.     public float rightEyeOffset;
  27.     public GUISkin skin;
  28.     public float scale = 0.75f;
  29.     bool swapImage = false;
  30.     private Rect left;
  31.     private Rect right;
  32.     private bool is3D;
  33.     private IPEndPoint clientRemote;
  34.     private Thread serverThread;
  35.     private byte[] currentPacket;
  36.  
  37.     public byte[] CurrentPacket {
  38.         get {
  39.             return currentPacket;
  40.         }
  41.         set {
  42.             currentPacket = value;
  43.         }
  44.     }
  45.     private Thread latencyThread;
  46.     Queue frameQueue = new Queue(new List<byte[]>());
  47.     private long latency;
  48.     public int delayPerFrame;
  49.     private int boxWidth;
  50.     private float width;
  51.     private float height;
  52.     private Texture2D fragment;
  53.     private Thread thread;
  54.     // Use this for initialization
  55.     void Start() {
  56.         FindObjectOfType<Text>().text = Network.player.ipAddress;
  57.         imageB = new Texture2D((int) Screen.width, (int) Screen.height);
  58.         imageA = new Texture2D((int) Screen.width, (int) Screen.height);
  59.         framerateTimer = Stopwatch.StartNew();
  60.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
  61.         latencyTimer = new Stopwatch();
  62.         remoteEP = new IPEndPoint(IPAddress.Any, 18000);
  63.         thread = new Thread(new ThreadStart(delegate {
  64.             while (true) {
  65.                 latencyTimer.Start();
  66.                 using (MemoryStream stream = new MemoryStream(server.Receive(ref remoteEP))) {
  67.                     currentPacket = stream.ToArray();
  68.                     frameCount++;
  69.                     latencyTimer.Stop();
  70.                     latency = latencyTimer.ElapsedMilliseconds;
  71.                     latencyTimer.Reset();
  72.                 }
  73.             }
  74.         }));
  75.         thread.Start();
  76.     }
  77.  
  78.     public static Texture2D DrawImage(Texture2D background, Texture2D screenUpdate, int startX, int startY) {
  79.         // Create a new writable texture.
  80.         background.SetPixels32(startX, startY, screenUpdate.width, screenUpdate.height, screenUpdate.GetPixels32());
  81.         background.Apply();
  82.         return background;
  83.     }
  84.  
  85.     // Update is called once per frame
  86.     void Update() {
  87.         CheckFrames();
  88.     }
  89.  
  90.     private void CheckFrames() {
  91.         if (!thread.IsAlive) {
  92.             thread = new Thread(new ThreadStart(delegate {
  93.                 while (true) {
  94.                     latencyTimer.Start();
  95.                     using (MemoryStream stream = new MemoryStream(server.Receive(ref remoteEP))) {
  96.                         currentPacket = stream.ToArray();
  97.                         frameCount++;
  98.                         latencyTimer.Stop();
  99.                         latency = latencyTimer.ElapsedMilliseconds;
  100.                         latencyTimer.Reset();
  101.                     }
  102.                 }
  103.             }));
  104.             thread.Start();
  105.         }
  106.         if (startedAServer) {
  107.             UpdateFrames();
  108.         }
  109.     }
  110.  
  111.     private void UpdateFrames() {
  112.         if (currentPacket != null) {
  113.             //if (is3D) {
  114.             //  if (lastPacket != null) {
  115.             //    imageA.LoadImage(currentPacket);
  116.             //    imageA.Apply();
  117.             //    imageB.LoadImage(lastPacket);
  118.             //    imageB.Apply();
  119.             //  }
  120.             //  lastPacket = currentPacket;
  121.             //} else {
  122.                 imageA.LoadImage(currentPacket);
  123.                 imageA.Apply();
  124.                 //imageB.LoadImage(currentPacket);
  125.                 //imageB.Apply();
  126.             //}
  127.         }
  128.         if (framerateTimer.ElapsedMilliseconds >= 1000) {
  129.             lastFrameCount = frameCount;
  130.             frameCount = 0;
  131.             framerateTimer.Reset();
  132.             framerateTimer.Start();
  133.         }
  134.         imageLeft.texture = imageA;
  135.         imageRight.texture = imageA;
  136.         RectTransform rectTransform = imageLeft.GetComponent<RectTransform>();
  137.         rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, ((rectTransform.sizeDelta.x) * ((float) imageA.height / (float) imageA.width)));
  138.         RectTransform rectTransform2 = imageRight.GetComponent<RectTransform>();
  139.         rectTransform2.sizeDelta = new Vector2(rectTransform.sizeDelta.x, ((rectTransform2.sizeDelta.x) * ((float) imageA.height / (float) imageA.width)));
  140.     }
  141.  
  142.     void FixedUpdate() {
  143.         if (!startedAServer) {
  144.             if (framerateTimer.ElapsedMilliseconds >= 0) {
  145.                 server = new UdpClient(18000);
  146.                 startedAServer = true;
  147.             }
  148.         }
  149.     }
  150.  
  151.     void OnGUI() {
  152.         GUI.skin = skin;
  153.         //if (currentPacket != null && latency < 2000) {
  154.         boxWidth = (Screen.width / 2);
  155.         width = (float) boxWidth * scale;
  156.         height = ((float) imageB.height / (float) imageB.width) * (float) (width);
  157.         float yPos = (Screen.height / 2) - (height / 2);
  158.         float xPos = (boxWidth / 2) - (width / 2);
  159.         left = new Rect(0 + leftEyeOffset + xPos, yPos, width, height);
  160.         right = new Rect(boxWidth + rightEyeOffset + xPos, yPos, width, height);
  161.         GUI.Label(left, lastFrameCount.ToString());
  162.         GUI.Label(right, lastFrameCount.ToString());
  163.     }
  164.  
  165.     void OnApplicationQuit() {
  166.         server.Close();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment