Author Topic: All sprites are shifted to the left slightly  (Read 3689 times)

darthviper107

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
All sprites are shifted to the left slightly
« on: December 16, 2015, 05:06:30 PM »
Upgraded to the latest version of NGUI to start on a new project, but all of the sprites are a pixel or two shifted to the left from where they are supposed to be(they don't line up correctly with their bounds), anyone have this problem?
Also, I have to set the sprite size by the actual image size, not the size of what it ends up on the atlas after it cuts out the empty space around it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #1 on: December 16, 2015, 10:37:42 PM »
When NGUI trims sprites (which is an option on the atlas maker btw), it replaces pixels with padding. When a sprite is drawn, it uses original dimensions of the sprite anyway. So if your original sprite was 50x50, that's exactly what UISprite will be when you add it to the scene. The trimming is only noticeable if you examine the actual triangles via wireframe mode -- the drawn rectangle is smaller than the sprite. This helps reduce fillrate and improves performance.

Make sure your sprites are not sliced, as sliced sprite behaviour is different when trimming occurred.

darthviper107

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #2 on: January 04, 2016, 01:21:47 PM »
I've tried messing with all of the settings for the atlas maker and none of them change it, all of the sprites are slightly off to the left no matter if they're a simple sprite or if they're sliced.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #3 on: January 05, 2016, 07:03:56 AM »
Google "DirectX 9 half-pixel offset". :P

It's necessary on Windows for sprites to not appear fuzzy.

darthviper107

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #4 on: January 06, 2016, 06:46:32 PM »
The UI isn't fuzzy--the sprites are just shifted to the left a bit, also, this is running in DX11

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #5 on: January 06, 2016, 11:54:39 PM »
The UI is not fuzzy because the half-pixel offset is there. If it wasn't, your game view would be all blurry.

If you don't believe me, find UIPanel.drawCallOffset and change it to not use mHalfPixelOffset like so:
  1.         public Vector3 drawCallOffset
  2.         {
  3.                 get
  4.                 {
  5. #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
  6.                         if (anchorCamera != null && mCam.isOrthoGraphic)
  7. #else
  8.                         if (anchorCamera != null && mCam.orthographic)
  9. #endif
  10.                         {
  11.                                 Vector2 size = GetWindowSize();
  12.                                 float pixelSize = (root != null) ? root.pixelSizeAdjustment : 1f;
  13.                                 float mod = (pixelSize / size.y) / mCam.orthographicSize;
  14.  
  15.                                 bool x = false; // <--
  16.                                 bool y = false; // <--
  17.  
  18.                                 if ((Mathf.RoundToInt(size.x) & 1) == 1) x = !x;
  19.                                 if ((Mathf.RoundToInt(size.y) & 1) == 1) y = !y;
  20.  
  21.                                 return new Vector3(x ? -mod : 0f, y ? mod : 0f);
  22.                         }
  23.                         return Vector3.zero;
  24.                 }
  25.         }

darthviper107

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #6 on: January 07, 2016, 12:24:37 PM »
Still, what does that have to do with the issue I'm having?

I used to be using version 2.7 and the sprites looked correct, had to update to the latest version since there were a number of script errors in Unity5

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #7 on: January 07, 2016, 12:38:57 PM »
He just explained that due to DirectX 9, he has to apply an half pixel offset to all sprites(so this is why it's not at the center of the rect), to render the sprite crisp and not blurry.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #8 on: January 07, 2016, 01:21:13 PM »
Switch your renderer to DX11 and make sure your game view size uses even dimensions (dividable by two) and it will disappear.

darthviper107

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #9 on: January 11, 2016, 03:40:03 PM »
It is in DX11, why doesn't it work like it did in 2.7? Never had an issue with blurry textures, or with sprites being shifted to the wrong position, and that was also in DX9

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: All sprites are shifted to the left slightly
« Reply #10 on: January 11, 2016, 05:38:36 PM »
NGUI 2 used transform position and scale instead of widget size. Its drawing of borders was also very different.

The selection border is only visible in the Unity editor, you realize that, don't you? You're getting all upset that it doesn't align for you by half a pixel, when it's only something you can see in the Unity editor, only in DX9 mode, and only noticeable if you zoom in on the sprites.