{"id":215,"date":"2020-07-23T22:36:18","date_gmt":"2020-07-23T22:36:18","guid":{"rendered":"https:\/\/sgarces.com\/?p=215"},"modified":"2020-07-23T22:36:18","modified_gmt":"2020-07-23T22:36:18","slug":"9-slice-control-in-ue4","status":"publish","type":"post","link":"https:\/\/sgarces.com\/?p=215","title":{"rendered":"9-Slice control in UE4"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"550\" height=\"300\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice_banner-1.jpg?resize=550%2C300&#038;ssl=1\" alt=\"\" class=\"wp-image-227\" srcset=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice_banner-1.jpg?w=550&amp;ssl=1 550w, https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice_banner-1.jpg?resize=300%2C164&amp;ssl=1 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Theory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A 9-slice control is a way to render an image that has become very popular when building UI, with widespread support. It comes down to dividing an image in 9 regions and scaling them independently. See this <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/9-slice_scaling\" target=\"_blank\">Wikipedia article<\/a> for more information. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why is this important? Consider this image:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"400\" height=\"136\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle.png?resize=400%2C136&#038;ssl=1\" alt=\"\" class=\"wp-image-216\" srcset=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle.png?w=400&amp;ssl=1 400w, https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle.png?resize=300%2C102&amp;ssl=1 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If we scale it uniformly, you will end up with results like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"197\" height=\"195\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/UE4Editor-Win64-DebugGame_2020-07-23_15-02-49.png?resize=197%2C195&#038;ssl=1\" alt=\"\" class=\"wp-image-217\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">However, if we split the image in 9 regions:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"478\" height=\"214\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice-1.png?resize=478%2C214&#038;ssl=1\" alt=\"\" class=\"wp-image-218\" srcset=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice-1.png?w=478&amp;ssl=1 478w, https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice-1.png?resize=300%2C134&amp;ssl=1 300w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Then we can scale each region independently, keeping the size of the border constant and scaling the interior to fit the available space:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"198\" height=\"200\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/UE4Editor-Win64-DebugGame_2020-07-23_15-03-14-1.png?resize=198%2C200&#038;ssl=1\" alt=\"\" class=\"wp-image-221\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">9-slice in Unreal Engine 4<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So I have good news and bad news for you. The good news is that UMG has support for 9-slice images, through the Brush interface. The bad news is that it&#8217;s pretty rigid and hard to use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to use 9-slice scaling on a Brush, you simply select <strong>Box<\/strong> in the <strong>Draw As<\/strong> option and set the <strong>Margins<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"405\" height=\"360\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/UE4Editor-Win64-DebugGame_2020-07-23_15-08-16.png?resize=405%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-222\" srcset=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/UE4Editor-Win64-DebugGame_2020-07-23_15-08-16.png?w=405&amp;ssl=1 405w, https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/UE4Editor-Win64-DebugGame_2020-07-23_15-08-16.png?resize=300%2C267&amp;ssl=1 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The Margins will specify the percentage of the image that will correspond to the slices, for each side. Unfortunately, that&#8217;s all the control you get. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you place an image on your UI, you&#8217;re applying a scale to change the image from the original texture size to the size it will have on the screen (at the reference resolution of 1920&#215;1080). Ideally, you could specify the size of the slices independently, but this is not currently possible in Unreal Engine as of 4.24. <strong>The only thing that scales in the middle part<\/strong>. The size of the borders will be fixed and predetermined by the size of the original texture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to have thinner borders, you need to create a separate, lower resolution texture. In this GIF Rectangle_thin is the same texture as Rectangle, but at half resolution:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"677\" height=\"251\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/2020-07-23_14-54-58.gif?resize=677%2C251&#038;ssl=1\" alt=\"\" class=\"wp-image-223\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The only way I&#8217;ve found to scale the border itself without changing the texture is using the Render Transform, which is far from ideal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Things get weirder<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">But wait, there&#8217;s more. Unreal provides another 9-slice mode, called a <strong>Border<\/strong>. It&#8217;s meant to work in the same way but avoid drawing the center piece. This is extremely useful to reduce fillrate when you have an image with a transparent center section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But Border works&#8230;funny in Unreal. Looking at the code, I can see that the implementation of Box and Border are independent (one is in <code>FSlateElementBatcher::AddBoxElement()<\/code> the other in <code>FSlateElementBatcher::AddBorderElement()<\/code>). And while Border has the same restriction of relying on the original texture size, it also add a layout scale that depends on your DPI Scale and even your Zoom, while working in the Editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is what it looks like when you switch between Border and Box. As long as DPI scale is set to 1.0 (I select Screen Size as Monitor 21.5&#8243;-24&#8243; in the viewport&#8217;s top right ) the Zoom is set to 1:1, they match. But if you zoom in, they don&#8217;t:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"780\" height=\"287\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/2020-07-23_14-53-04.gif?resize=780%2C287&#038;ssl=1\" alt=\"\" class=\"wp-image-224\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I wish the 9-slice received some love as it&#8217;s an incredibly useful tool. In the meantime, here&#8217;s what I&#8217;d recommend:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Avoid<\/strong> the Border tool, as it will look different depending on the resolution of the game. Use Box and pay the price of drawing transparent pixels.<\/li><li><strong>Preview<\/strong> the margins using the Border tool, if the image is opaque. It&#8217;s the only way to see what the slices are, as there is no debug visualization.<\/li><li><strong>Scale <\/strong>the original texture to get the desired border size.<\/li><li>If the Box brush just won&#8217;t do, you&#8217;ll have to construct it <strong>manually<\/strong> using layout controls.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Theory A 9-slice control is a way to render an image that has become very popular when building UI, with widespread support. It comes down to dividing an image in 9 regions and scaling them independently. See this Wikipedia article for more information. Why is this important? Consider this image: If we scale it&#8230;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/sgarces.com\/?p=215\" class=\"themebutton3\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[3],"class_list":["post-215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unreal-engine","tag-unreal-engine-4"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/sgarces.com\/wp-content\/uploads\/2020\/07\/Rectangle_9slice_banner-1.jpg?fit=550%2C300&ssl=1","_links":{"self":[{"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sgarces.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":2,"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sgarces.com\/index.php?rest_route=\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/sgarces.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sgarces.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sgarces.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}