Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by halfwet · Apr 19, 2012 at 02:44 AM · webcam

Problem with capture image from web camera.

Hi, I am going to pass the image captured with my own plugin to unity. But I found that Texture2D.Apply() function is very slow. And the game runs only on 12-15 fps. Is there any possibility to walk around the Apply() function? May I pass the image to the VRAM directly?

Thanks in advance!

My code is as follow.

 using UnityEngine;

using System.Collections; using System.Runtime.InteropServices; using System.Threading;

public class WebCamera : MonoBehaviour {

 [DllImport("OpencvKinectGrabber.dll", EntryPoint = "InitSystem")]  
  public static extern void InitSystem();  
 
 [DllImport("OpencvKinectGrabber.dll", EntryPoint = "GetImage")]  
  public static extern bool GetImage(byte[] data);  
 
 [DllImport("OpencvKinectGrabber.dll", EntryPoint = "InitSystem")]  
  public static extern void EndSystem();  
 
 WebCamTexture webcamTex ;
 WebCamDevice[] devices;
 
 Texture2D cvTexture;
 
 Color32[] colors;
 
 byte[] data;
 
 Thread hThread;
 
 
 public RenderTexture renderTexture;
 
 bool quit = false;

 // Use this for initialization
 void Start () {
     
      /*
     devices = WebCamTexture.devices;
     webcamTex = new WebCamTexture(devices[1].name,640,480,30);
     print(devices[1].name);
     renderer.material.mainTexture = webcamTex;
     webcamTex.Play();
     */
     
     InitSystem();
     
     cvTexture = new Texture2D(640,480);
     colors = new Color32[640*480];
     data = new byte[640*480*4];
     
     renderer.material.mainTexture = cvTexture;
     
     hThread = new Thread(Loop);
     hThread.Start();
         
 }
 
 void Loop()
 {
     while(!quit)
     {
         bool result = GetImage(data);
 
         if(!result)
             return;
      
         for (int i=0;i<480;i++)  
         {  
             for (int j = 0;j<640;j++)  
             {  
                 colors[(i*640+j)].b = data[(i*640+j)*4+0];  
                 colors[(i*640+j)].g = data[(i*640+j)*4+1];  
                 colors[(i*640+j)].r = data[(i*640+j)*4+2];  
                 colors[(i*640+j)].a = 255;  
             }  
         } 
      
     
     }
     
 }
 
 
 // Update is called once per frame
 void Update () {

      cvTexture.SetPixels32(colors);
     cvTexture.Apply(false);
     
      
 }
 
 
 void OnApplicationQuit()
 {
     quit = true;
  
     EndSystem();
 }
 
 
     

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 20, 2012 at 02:43 PM 0
Share

I'm a bit confused at what you're trying to do. From what I understand, you want to display a picture created in your plugin in Unity.

It looks like you get the pixels from your plugin with GetImage() and apply it on a texture2D.

In this case, why are you using a WebcamTexture?

avatar image halfwet · Apr 20, 2012 at 03:32 PM 0
Share

Hi Dnjata , I am not using the webcamTexture. What I am going to do is to write the image data into the VRA$$anonymous$$ directly, not using the Texture2D.Apply() function. Because it is quite slow. Thx.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Webcam Support in Unity (iSight or USB Camera) 8 Answers

What is required from a device to be read in a WebcamTexture? 0 Answers

How can i find max resolution for WebCamTexture 2 Answers

Adjust the exposure of phone camera manually 1 Answer

Get FOV of webcam 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges