Search Unity

How to avoid the input lag on my code?

Discussion in 'AR' started by Psifiaka, Jan 3, 2019.

  1. Psifiaka

    Psifiaka

    Joined:
    Jan 3, 2019
    Posts:
    1
    Hello I am trying to make an ar game but the response is lagging here's the code
    Code (CSharp):
    1. public class Controls : MonoBehaviour {
    2.      public float speed;
    3.      private WebCamTexture cam;
    4.      public GameObject picture;
    5.      private int[] points = new int [9];//The points with 0=UL and 9=DR
    6.      int a, b, medianX, medianY;
    7.      private Rigidbody rb;
    8.      private Vector3 move = new Vector3(0,0,0);
    9.      // Use this for initialization
    10.      void Start () {
    11.          rb = GetComponent<Rigidbody>();
    12.          cam = new WebCamTexture();
    13.          picture.GetComponent<Renderer>().material.mainTexture = cam;
    14.          cam.Play();
    15.          medianX = cam.width / 2;
    16.          medianY = cam.height / 2;
    17.          a = (cam.width*20) / 100;
    18.          b = (cam.height*20) / 100;
    19.      }
    20.    
    21.      // Update is called once per frame
    22.      void Update () {
    23.          for (int i=0;i<cam.height;i++) {
    24.              for (int j=0;j<cam.width;j++) {
    25.                  float h, s, v;
    26.                  Color.RGBToHSV(cam.GetPixel(j,i),out h,out s,out v);
    27.                  if (h*360>=210 && h*360<=220 && s*100>=70 && v*100>=30) {
    28.                      if (j >= medianX - a && j <= medianX + a) {
    29.                          if (i>=0 && i<=medianY-b-1) {
    30.                              points[7]++;
    31.                          } else if (i>=medianY-b && i<=medianY+b) {
    32.                              points[4]++;
    33.                          }else {
    34.                              points[1]++;
    35.                          }
    36.                      } else if (j>=0 && j<=medianX-a-1) {
    37.                          if (i>=0 && i<=medianY-b-1) {
    38.                              points[6]++;
    39.                          } else if (i>=medianY-b && i<=medianY+b) {
    40.                              points[3]++;
    41.                          } else {
    42.                              points[0]++;
    43.                          }
    44.                      }else {
    45.                          if (i>=0 && i<=medianY-b-1) {
    46.                              points[8]++;
    47.                          } else if (i>=medianY-b && i<=medianY+b) {
    48.                              points[5]++;
    49.                          } else {
    50.                              points[2]++;
    51.                          }
    52.                      }
    53.                  }
    54.              }
    55.          }
    56.          int max = 0;
    57.          for (int i = 1; i < points.Length; i++){
    58.              if (points[max]<=points[i]) {
    59.                  max = i;
    60.              }
    61.          }
    62.          Vector3 move = new Vector3(0,0,0);
    63.          if (max==0) {
    64.              move = new Vector3(-1,0,1);
    65.          } else if (max==1) {
    66.              move = new Vector3(0,0,1);
    67.          } else if (max==2) {
    68.              move = new Vector3(1,0,1);
    69.          } else if (max==3) {
    70.              move = new Vector3(-1,0,0);
    71.          } else if (max==4) {
    72.              move = new Vector3(0,0,0);
    73.          } else if (max==5) {
    74.              move = new Vector3(1,0,0);
    75.          } else if (max==6) {
    76.              new Vector3(-1,0,-1);
    77.          } else if (max==7) {
    78.              new Vector3(0,0,-1);
    79.          } else{
    80.              new Vector3(1, 0, -1);
    81.          }
    82.          Debug.Log(max);
    83.          rb.velocity = move * speed;
    84.          if (Input.GetKeyUp(KeyCode.Space)) {
    85.              ptrSc(cam);
    86.          }
    87.      }
    88.      private void ptrSc(WebCamTexture cam) {
    89.          StreamWriter sw = new StreamWriter("C:\\Users\\George\\Desktop\\Projects\\Game3\\Pic.txt", false);
    90.          for (int i=cam.height-1;i>=0;i--) {
    91.              for (int j=cam.width-1;j>=0; j--) {
    92.                  float h, s, v;
    93.                  Color.RGBToHSV(cam.GetPixel(j,i),out h,out s,out v);
    94.                  if (h * 360 >= 210 && h * 360 <= 220 && s * 100 >= 70 && v * 100 >= 30) {
    95.                      sw.Write(255 + " ");
    96.                  }else {
    97.                      sw.Write(0 + " ");
    98.                  }
    99.              }
    100.              sw.WriteLine();
    101.          }
    102.      }
    103. }
    The code is recognize the blue cap of a pencil cap and its splits the array at 9 pieces checks every pixel for the color(condition) and if it finds one pixel sum the region. UL=0 UC=1 UR=2 CL=3 CC=4 CR=5 DL=6 DR=8 DC=7