Author Topic: How to use half pixel offset?  (Read 7252 times)

Euthyphro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
How to use half pixel offset?
« on: October 23, 2013, 10:09:51 PM »
I'm having trouble with a UITexture. it seems that the texture loses its crispness after I move it. I've created a script that drags a UITexture gameobject around when I click on it, but as soon as its moved, the crispness is lost. I'm certain it is the half texture offset, but my question is, how do I correct this in c#?


Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to use half pixel offset?
« Reply #1 on: October 24, 2013, 09:33:54 PM »
Things can lose their crispness when you drag them due to the fact that it's done via floats rather than ints. When you release the drag, it should even out to an integer value though.

Euthyphro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: How to use half pixel offset?
« Reply #2 on: October 25, 2013, 01:08:35 AM »
Things can lose their crispness when you drag them due to the fact that it's done via floats rather than ints. When you release the drag, it should even out to an integer value though.


  1.  
  2. Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  3. this.transform.position = p;
  4.  

The following code results in the fuzzy image. When I try to round p to either 0.5 or a full number, movement will not occur when dragging the image.

For example,  this.transform.position = new Vector3(Mathf.Round (p.x), Mathf.Round(p.y), p.z); does not move the image because rounding will keep rounding the number down before it can move, making it impossible to move. Likewise, usint Mathf.CeilToInt has the same problem, and FloorToInt also same problem.

My brain is fried right now after a long day of moving, any suggestions on how to overcome this issue?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to use half pixel offset?
« Reply #3 on: October 25, 2013, 03:01:23 AM »
It's not the half-pixel offset.

The sprite is an odd width (67) then because it is centered, it has to "land" on a .5 value to be crisp. The same applies with the height.

Euthyphro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: How to use half pixel offset?
« Reply #4 on: October 25, 2013, 02:03:14 PM »
It's not the half-pixel offset.

The sprite is an odd width (67) then because it is centered, it has to "land" on a .5 value to be crisp. The same applies with the height.

I noticed that in the editor, the transform position is x -28,y 36, z 0, but when I print out "this.transform.position.x, y, z"  the position is not even a whole number, instead it is coming out as:
-0.1230769 0.1516484 0 UnityEngine.MonoBehaviour:print(Object) ItemScript:Update() (at Assets/ItemScript.cs:35)

I think this is the source of my problem when repositioning. Any idea how to get the actual position, not a small unwhole number? I'm simply using "this.transform.position" to get the position from within the UITexture gameobject.
« Last Edit: October 25, 2013, 02:40:11 PM by Euthyphro »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to use half pixel offset?
« Reply #5 on: October 25, 2013, 04:02:11 PM »
Use localPosition instead of position - position is global in the scene, while local is inside the hierarchy and the one shown in the inspector.

Euthyphro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: How to use half pixel offset?
« Reply #6 on: October 25, 2013, 06:09:38 PM »
Use localPosition instead of position - position is global in the scene, while local is inside the hierarchy and the one shown in the inspector.

Perfect, got it. Thanks! Now working properly.

I had to write my own offset method:

  1. void OnDrag (Vector2 delta)
  2. {
  3.         [...]
  4.         transf.localPosition += (Vector3)delta * rootElement.pixelSizeAdjustment;
  5.         transf.localPosition = new Vector3(HalfPixelOffset(transf.localPosition.x), HalfPixelOffset(transf.localPosition.y), transf.localPosition.z);
  6. }
  7.  
  8. float HalfPixelOffset(float input)
  9. {
  10.                 float r = 0;
  11.                 int i = (int)input;
  12.                 int result = (int)((input - (int)input) * 100);
  13.                 if(i > 0)
  14.                 {
  15.                         if(result > 50)
  16.                         {
  17.                                 r = i + 1 + 0.5f;
  18.                         }
  19.                         else if(result <= 50)
  20.                         {
  21.                                 r = i + 0.5f;
  22.                         }
  23.                 }
  24.                 else if(i < 0)
  25.                 {
  26.                         if(result > 50)
  27.                         {
  28.                                 r = i - 1 - 0.5f;
  29.                         }
  30.                         else if(result <= 50)
  31.                         {
  32.                                 r = i - 0.5f;
  33.                         }
  34.                 }
  35.                 return r;
  36. }
  37.