Advertisement
Guest User

Untitled

a guest
Apr 30th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. //For the CV stuff
  4. //using UnityEngine;
  5. //using System.Collections;    
  6. using Emgu.CV;    
  7. using Emgu.CV.Util;    
  8. using Emgu.CV.UI;          
  9. using Emgu.CV.CvEnum;    
  10. using Emgu.CV.Structure;    
  11. using System.Runtime.InteropServices;    
  12. using System;    
  13. using System.Drawing;
  14.  
  15. public class WebcamTextureScript : MonoBehaviour {
  16.     public Material mat;
  17.     public GameObject quad;
  18.     public GameObject cam;
  19.     public float imageOffsetX;
  20.     public float imageOffsetY;//
  21.     private TextureBridge bridge;
  22.     private WebCamDevice[] devices;
  23.     private WebCamTexture texture;
  24.     private int deviceHeight;
  25.     private int deviceWidth;
  26.     private int scaleFactor;//
  27.     private int totalFPS;
  28.     private int cameraFPS; //
  29.     private Color32[] data;
  30.     private Image<Bgr, byte> picture;
  31.     private Texture2D augmentedTexture;
  32.    
  33.     //  private int current = 0;
  34.    
  35.     // Use this for initialization
  36.     void Start () {
  37.         devices = WebCamTexture.devices;
  38.         deviceHeight = 480;
  39.         deviceWidth = 640;
  40.         scaleFactor = 30;
  41.  
  42.         data = new Color32[deviceWidth * deviceHeight];
  43.         bridge = new TextureBridge();
  44.        
  45.         if (devices.Length > 0)
  46.         {
  47.             if (cam.name == "CameraLeft")
  48.             {
  49.                 texture = new WebCamTexture( devices[ 0 ].name, deviceWidth, deviceHeight, 60 );
  50.             }
  51.             if (cam.name == "CameraRight")
  52.             {
  53.                 texture = new WebCamTexture( devices[ 1 ].name, deviceWidth, deviceHeight, 60 );
  54.             }
  55.  
  56.             texture.requestedFPS = 100.0f;
  57.             texture.Play();
  58.  
  59.             quad.transform.localScale = new Vector3 ( scaleFactor * ( (float)texture.width / (float)texture.height ), scaleFactor, 1.0f );
  60.             quad.transform.Translate(imageOffsetX , imageOffsetY, 0 );
  61.  
  62.             mat.mainTexture = texture;
  63.         }
  64.        
  65.     }
  66.    
  67.     // Update is called once per frame
  68.     void Update () {
  69.         if ( texture != null && texture.didUpdateThisFrame )
  70.         {
  71.             //CVTests
  72.  
  73.         //  print(texture.GetPixels32(data).ToString());
  74.  
  75.         //  picture = new Image<Bgra, byte>(data);
  76.             //picture = bridge.WebcamTextureToEmguCVImage(texture.GetPixels32(data), texture.width, texture.height);
  77.  
  78.         //  Bgr myWhiteColor = new Bgr (255, 255, 255);    
  79.  
  80.         //  for (int i=0; i<200; i++) {
  81.                
  82.         //      picture [i, i] = myWhiteColor;
  83.         //  }
  84.  
  85.             //augmentedTexture = bridge.EmguCVImageToTexture2D(picture, deviceWidth, deviceHeight);
  86.  
  87.             //End CVTests
  88.  
  89.             quad.transform.rotation = cam.transform.rotation;
  90.             quad.transform.Rotate( 0, 0, -90);
  91.             //print(1/Time.deltaTime); //Print Framerate to console
  92.         }
  93.  
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement