Rectangle.cginc 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. float4 _Roundness; // tl, tr, bl, br
  2. fixed4 frag(v2f i) : SV_Target {
  3. float2 pos = prepare(i.uv, i.modelPos.z);
  4. // get the roundness for this corner in world units, avoiding conditionals because
  5. // they tend to break things on various platforms (gles3) and they are slow in
  6. // shaders anyway
  7. float tl = and(when_le(pos.x, 0), when_ge(pos.y, 0)) * _Roundness.x;
  8. float tr = and(when_ge(pos.x, 0), when_ge(pos.y, 0)) * _Roundness.y;
  9. float bl = and(when_le(pos.x, 0), when_le(pos.y, 0)) * _Roundness.z;
  10. float br = and(when_ge(pos.x, 0), when_le(pos.y, 0)) * _Roundness.w;
  11. float roundness = tl + tr + bl + br;
  12. // radius of the circle at this corner, making sure not to go past halfway
  13. float radius = min(min(_XScale / 2, roundness), _YScale / 2);
  14. // handy distance to round rectangle formula
  15. float2 extents = float2(_XScale, _YScale) / 2 - radius;
  16. float2 delta = abs(pos) - extents;
  17. // first component is distance to closest side when not in a corner circle, second
  18. // is distance to the rounded part when in a corner circle
  19. float dist = radius - (min(max(delta.x, delta.y), 0) + length(max(delta, 0)));
  20. fixed4 color = color_from_distance(dist, fill(i.uv), _OutlineColor) * i.color;
  21. if (_PreMultiplyAlpha == 1)
  22. color.rgb *= color.a;
  23. if (_UseClipRect == 1)
  24. color.a *= UnityGet2DClipping(i.modelPos.xy, _ClipRect);
  25. clip(color.a - 0.001);
  26. return color;
  27. }