Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Passing a Texture reference from Unity to xcode

Discussion in 'iOS and tvOS' started by Dover8, Jan 8, 2014.

  1. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    I am trying to pass a reference of a cameras texture into xcode so that I can then use it for face detection. Most methods I have tried have left me stumped, but I think I am close with what I currently have.

    In unity I get the texture id and pass this through a plugin:
    Code (csharp):
    1.  
    2. [DllImport("__Internal")]
    3.     public static extern void SendBackground(int id, int width, int height);
    4.  
    5. //in Update
    6. Texture bg_Img = GetComponentInChildren<VideoTextureBehaviour>().GetTexture(); //this is from Vuforia
    7. int texture_Id = bg_Img.GetNativeTextureID();
    8. SendBackground(texture_Id, bg_Img.width, bg_Img.height);
    9.  
    xcode receives the texture ID fine, I then try to access it from OpenGL and get it to a CIImage

    Code (csharp):
    1.  
    2.  
    3. - (void) DetectFaces:(int)tex_id :(int)width :(int)height
    4. {
    5.     glGenTextures(1, (GLuint)&tex_id);
    6.     glBindTexture(GL_TEXTURE_2D, (GLuint)tex_id);
    7.  
    8.     CIImage *image = [[CIImage alloc]  initWithTexture:(GLuint)tex_id size:CGSizeMake(width, height) flipped:YES colorSpace: (CGColorSpaceRef) kCGColorSpaceModelRGB];
    9. }
    Currently that last line results in a bad access exception at run time. Any help is greatly appreciated.
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    wrong - if this is the texture id you grabbed from your texture - you should just use it (as in - it is proper gl texture already)
    by adding glGenTextures you effectively create new texture with undefined data, so just kill this line and you should be good
     
  3. kyewong

    kyewong

    Joined:
    Sep 11, 2014
    Posts:
    22
    Hi Alexey, if I wanna update the texture using glTexImage2D in c++ function, do I have to do it in the Update() function in unity? Is it possible to call glTexImage2D in a created thread funcino in c++ when I obtained a texture data in that thread in c++?
     
  4. _clewis_

    _clewis_

    Joined:
    Mar 16, 2014
    Posts:
    2
    I'm trying to achieve a similar effect and even though I don't have a call to glGenTextures I still am receiving a bad access exception. Has anyone been able to confirm a solution to this?
     
    Last edited: Jun 30, 2015
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    can you please bug report with small repro? drop case number here
    Also it seems i really need to create sample project about *tweaking* unity texture (instead of using external texture from get-go)