engine icon indicating copy to clipboard operation
engine copied to clipboard

9 Slice Tiling seems to be broke

Open malDuffin opened this issue 7 years ago • 4 comments

9 Slice tiling seems to be broke...

https://forum.playcanvas.com/t/display-sprite-animations-on-2d-screen/9456/4?u=mal_duffin

malDuffin avatar Mar 26 '19 12:03 malDuffin

Please make tickets that describe the error.

This looks like it is behaving as expected.

daredevildave avatar Mar 26 '19 16:03 daredevildave

Apologies Dave, I'll try to describe the issue below.

Here's how PlayCanvas renders a 9-slice tiled image. image

And here's how it should be rendered ( Unity ) image

If you need any more info on how it's broken, I can try to put together a better example.

malDuffin avatar Mar 26 '19 17:03 malDuffin

I was able to make a bit of progress on this, I was able to feed specific values into the scaling to get correct results for specific cases. However, as I didn't find a way to see what the values of the variables with the fragment shader were as the code was running ( do you guys have a way to view these? ), I wasn't able to figure out the correct formula to automatically generate the corrrect values - this will hopefully be fairly easy to figure out for the original author of the shader code.

You can see an animated gif, showing the original and modified version of the shader - only the tiling of the UVs of the "cross" section are affected.

PlayCanvas_TileBugFixAnim

I took this file... https://github.com/playcanvas/engine/blob/master/src/graphics/program-lib/chunks/startNineSlicedTiled.frag

And added a vec2 scaleFactor ( as the U and V will need different values for different setups ).

The formula I included below isn't correct due to not having access to what the actual values are for eg z and w, but the scaling should be based on the width and height of each inner offset relative to the parent width and height of the rect of the sprite within the atlas.

Hope that makes sense!

    vec2 tileMask = step(vMask, vec2(0.99999));

// Calculate scale factor for U and V, based on the inner offset and rect within the atlas
// This needs tweaking, due to lack of info of the values of the variables below

    float scaleFactorU = (1.0 / (innerOffset.z * atlasRect.z));
    float scaleFactorV = (1.0 / (innerOffset.w * atlasRect.w));

    vec2 scaleFactor = vec2(scaleFactorU, scaleFactorV);
	
    vec2 clampedUv = mix(innerOffset.xy*0.5, vec2(1.0) - innerOffset.zw*0.5, fract(vTiledUv*scaleFactor));

    clampedUv = clampedUv * atlasRect.zw + atlasRect.xy;
    nineSlicedUv = vUv0 * tileMask + clampedUv * (vec2(1.0) - tileMask);

malDuffin avatar Mar 27 '19 16:03 malDuffin

Reopened as we reverted the PR that fixed this due to side effects on 9 sliced tiled sprites that are placed in the world.

yaustar avatar Jan 20 '21 11:01 yaustar