Author Topic: problem with bounds and screen coordinates  (Read 2027 times)

Eibis

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
problem with bounds and screen coordinates
« on: July 09, 2014, 03:56:01 AM »
hello,
I want to first of all congratulate you for the plugin, really well done.
I have a small problem that I think is not really Ngui's fault.
I want to use ReadPixels () to save a screenshot of a portion of the screen that fits exactly with a UITexture of Ngui.

This is the code I used:

  1.  
  2. Bounds b = NGUIMath.CalculateAbsoluteWidgetBounds(main_foto_texture.transform);
  3. Rect r = BoundsToScreenRect(b);
  4. snap.ReadPixels(r, 0, 0);
  5. snap.Apply();
  6.  
  7.  

where main_foto_texture is a UITexture.

The function:

  1.  
  2. public Rect BoundsToScreenRect(Bounds bounds)
  3. {
  4.         // Get mesh origin and farthest extent (this works best with simple convex meshes)
  5.         Vector3 origin = uiCamera.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.max.y, 0f));
  6.         Vector3 extent = uiCamera.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0f));
  7.         Debug.Log("Origin: " + origin + " extent: " + extent);
  8.  
  9.         return new Rect(origin.x, Screen.height - origin.y, extent.x - origin.x, origin.y - extent.y);
  10. }
  11.  
  12.  

I attach the result... is not big enough and the buttons should'nt be shown (the UITexture is gray and ends over them).
The only camera in the scene is the UI camera.

I tried to use relativewidgetbounds without casting it to screen coordinates but it gives me some negative value that I think are not correct (how can a screen coordinate be negative?)

Could you please help me? Thank you in advance! And sorry for my english... ^^

Matteo

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: problem with bounds and screen coordinates
« Reply #1 on: July 09, 2014, 05:11:27 PM »
Unfortunately this is outside of NGUI's scope of support as this is a Unity math question. NGUI gives you the bounds via the Calculate function you used. The rest -- conversion to screen coordinates and reading pixels isn't NGUI-related. Your BoundstoScreenRect function is also strange. Origin and extent implies center of a rectangle, yet you use it as min/max. You're also using extent - origin in one place and origin - extent in another... Double-check your math there.