Author Topic: Soft clipping on a scaled panel  (Read 9315 times)

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Soft clipping on a scaled panel
« on: June 20, 2012, 09:04:16 PM »
I have a group of widgets containing a couple of soft-clipped panels for things like a scrolling text area.  Everything's set up and working fine, but when I add a TweenScale to the parent of these widgets to simulate a flipping transition (tweening just the X scale from 1 to 0 and back), the clipping stops working.  This appears to be true for hard clipping and alpha clipping, as well, and also if I try scaling the clipped panel directly rather than a parent object.  Is this a known issue with clipping and non-uniformly scaled panels?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #1 on: June 20, 2012, 09:56:10 PM »
You can only scale clipped panels uniformly. It's a limitation.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #2 on: June 20, 2012, 09:56:54 PM »
Btw, you can achieve the same effect by simply rotating the panel around the left-side pivot. With a 2D camera it will look like you're shrinking it.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #3 on: June 21, 2012, 02:07:00 PM »
Ah, I had tried a couple of rotations, but that caused some of the widgets to disappear, I guess because of Z positioning.  (I've got two or three atlases involved and a render texture.)  It looks like that only happens in one direction though, so rotating in the other direction might work if I'm careful.  Thanks for making me take a second look at that.

I'm curious about the clipping limitation.  Is it a limitation in the shader? A performance optimization in UIPanel?  I guess I'm wondering if it's something I might be able to get working if I end up really wanting it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #4 on: June 21, 2012, 02:35:16 PM »
Shader-related, yup. The panel's clipping is specified in 2D, so the checks are also done in 2D. The scale is in 3D.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #5 on: July 16, 2012, 04:22:31 PM »
I circled back to this on Friday, and it turns out rotating my panel's not going to work.  I've just got too many components, so they end up fighting with each other or causing weird visual artifacts when rotation is involved.  So a co-worker and I were looking at the shader today trying to figure out exactly what was going on with clipping and scaling, and one strange thing that we've noticed is that the input vertex (v.vertex in the vert function) changes based on whether or not the panel is uniformly scaled.

I'm not sure if this is fully generalizable, but with a very simple test case (a 512x512 sprite), the vertices passed in to the shader were always +/-256 as long as the panel was uniformly scaled, regardless of what that scale actually was.  Once I started scaling X or Y independently of each other, those values were scaled based on the scale of the UIDrawCall.  So if my UIRoot was scaled down to 0.01, all of a sudden the vertices given to the shader would drop to +/-2.56, along with whatever further scaling I was doing to the panel.  Scaling the width of my panel to 0.9 would give me 2.304 instead of 230.4, etc.

Anyway, I was able to modify UIDrawCall to pass its local scale in as a shader property and have the vertex function, when the difference between x and y scales was greater than a very small value, pass a scaled value into TRANSFORM_TEX to get the soft clipping working the way I expected it to.  It's not the most elegant solution, but maybe some of the information above can help with getting a proper fix in place.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #6 on: July 16, 2012, 04:45:11 PM »
It's an issue in Unity related to dynamic batching. This is why I always keep advising to keep everything leading up to your widgets uniformly scaled (prefferably 1, 1, 1).

P.S. Draw call's local scale? Draw calls are objects you cannot modify. They're positioned at 0, and have a scale of 1. Not sure what you were referring to there.
« Last Edit: July 16, 2012, 04:47:49 PM by ArenMook »

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #7 on: July 16, 2012, 06:35:16 PM »
Re: Draw call's local scale, I'm referring to this in UIPanel:

  1.                         // Set the draw call's transform to match the panel's.
  2.                         // Note that parenting directly to the panel causes unity to crash as soon as you hit Play.
  3.                         Transform dt = dc.transform;
  4.                         dt.position = t.position;
  5.                         dt.rotation = t.rotation;
  6.                         dt.localScale = t.lossyScale;
  7.  

When I set a panel's Debug Info to Geometry in the editor, my draw calls generally have a scale of 0.01, since my root UI object is 0.01 and everything below that is 1.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Soft clipping on a scaled panel
« Reply #8 on: July 16, 2012, 06:42:14 PM »
Oh, that's the panel's transform.