# Skew a sprite

**URL:** <https://forum.defold.com/t/skew-a-sprite/30864>\
**Category:** Questions\
**Created:** [October 2, 2018, 3:10am UTC](https://forum.defold.com/t/skew-a-sprite/30864 "2018-10-02T03:10:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 2, 2018, 3:10am UTC](https://forum.defold.com/t/skew-a-sprite/30864/1 "2018-10-02T03:10:01Z")

</div>

![2018-10-01%2020_06_35-LNPOverlayWindow](https://forum-defold-uploads.b-cdn.net/original/2X/b/b8d68777c9b7a7c526a201f90cd3b4c845ef8c59.png)

I want to skew a sprite like this. Any ideas on how to easily do this?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [October 2, 2018, 7:08am UTC](https://forum.defold.com/t/skew-a-sprite/30864/2 "2018-10-02T07:08:07Z")

</div>

I have not tinkered with this but I believe there are two options.

1. Move the vertices in a shader.
2. Render the sprite using a skew matrix with the projection.

---

<div class="post-metadata">

**Author:** ![88.josh](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/88.josh/32/20137_2.png) [@88.josh](https://forum.defold.com/u/88.josh)\
**Post date:** [October 2, 2018, 10:41am UTC](https://forum.defold.com/t/skew-a-sprite/30864/3 "2018-10-02T10:41:14Z")

</div>

I am going to learn about shaders pretty soon. I am not looking forward to it at all.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 2, 2018, 8:09pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/4 "2018-10-02T20:09:37Z")

</div>

Here is some progress with using a shader

[SkewSprite.zip](https://forum-defold-uploads.b-cdn.net/original/2X/d/d2f7ec814f4461d57ffd98492603394c6c6d31aa.zip) (2.7 KB)

But if there are multiple sprites with it this happens? Caused by batching?

 ![2018-10-02%2013_07_09-Window](https://forum-defold-uploads.b-cdn.net/original/2X/5/590193dff67bc184a545b4c6defbc88710458bd4.png)

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 2, 2018, 9:33pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/5 "2018-10-02T21:33:33Z")

</div>

Trying to break batching with changing the fp doesn’t help, it does increase drawcalls though.

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [October 3, 2018, 9:49am UTC](https://forum.defold.com/t/skew-a-sprite/30864/6 "2018-10-03T09:49:19Z")

</div>

Haven’t looked at your code, but the vertex positions are (currently) in world space when they arrive to the shader. That may cause problems.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 10:15am UTC](https://forum.defold.com/t/skew-a-sprite/30864/7 "2018-10-03T10:15:10Z")

</div>

Here’s the vp code

```lua
uniform mediump mat4 view_proj;

// positions are in world space
attribute mediump vec4 position;
attribute mediump vec2 texcoord0;

varying mediump vec2 var_texcoord0;
uniform lowp vec4 skew;

void main()
{
    mat4 transform = mat4(
        1,skew.x,0,0,
        skew.y,1,0,0,
        0,0,1,0,
        0,0,0,1);        
        gl_Position = view_proj * (vec4(position.xyz, 1.0) * transform);
    var_texcoord0 = texcoord0;
}

```

Any idea on how to change it?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [October 3, 2018, 10:20am UTC](https://forum.defold.com/t/skew-a-sprite/30864/8 "2018-10-03T10:20:43Z")

</div>

You apply the skew to the world space positions of the vertices. You need to first get hold of model space coordinates. I think it’s possible to calculate an inverse of the world space transform and apply that first to get back to model space.

We have a couple of issues in the backlog to sort this out.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 10:23am UTC](https://forum.defold.com/t/skew-a-sprite/30864/9 "2018-10-03T10:23:45Z")

</div>

I’m not much of a shader pro yet to know how to do that unfortunately.

It does make sense now that you mention that why this behavior is seen though. If I set the material y skew in the editor to 0.2 I see this in the editor.

 ![0%20-%20SkewSprite](https://forum-defold-uploads.b-cdn.net/original/2X/3/372092328e993c36adf49638ce9ed89da8765ac0.png)

So the process would be to convert back to model space, apply the skew transform, then convert back to world space however that’s done?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [October 3, 2018, 11:21am UTC](https://forum.defold.com/t/skew-a-sprite/30864/11 "2018-10-03T11:21:29Z")

</div>

> [@Pkeod](#):
>
> So the process would be to convert back to model space, apply the skew transform, then convert back to world space however that’s done?

Yes, currently that is the only way. There are plans to make it possible to get vertices in model space instead which would make this much easier.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 11:50am UTC](https://forum.defold.com/t/skew-a-sprite/30864/12 "2018-10-03T11:50:05Z")

</div>

Is it possible now or do I need to wait for features to change?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 9:28pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/13 "2018-10-03T21:28:04Z")

</div>

Did a forum search, DEF-2518 “[adding inverse world matrix so you can translate positions to object space if needed](https://forum.defold.com/t/billboard-shader-model-matrix/12983/2)” if this can be prioritized in the future it would be nice!

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 9:53pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/14 "2018-10-03T21:53:10Z")

</div>

Was talking with a friend about this and he said if we could get local UV space for sprites (rather than shared atlas UV space) we could use the UVs for skew shaders easily too. Local UV space is also needed for many effects that would be nice such as placing a gradient over the actual size of a sprite and not the entire atlas space the sprite texture is on.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 10:10pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/15 "2018-10-03T22:10:57Z")

</div>

[https://forum-defold-uploads.b-cdn.net/original/2X/8/8709ab64d16e394eb28b555ce91c153156622414.mp4](https://forum-defold-uploads.b-cdn.net/original/2X/8/8709ab64d16e394eb28b555ce91c153156622414.mp4)

Even though inverse() isn’t supported it still works in desktop builds… but things break still.

```lua
uniform mediump mat4 view_proj;
uniform mediump mat4 world;

// positions are in world space
attribute mediump vec4 position;
attribute mediump vec2 texcoord0;

varying mediump vec2 var_texcoord0;
uniform lowp vec4 skew;
varying highp vec4 var_modelSpacePosition;

void main()
{
    mat4 transform = mat4(
        1,skew.x,0,0,
        skew.y,1,0,0,
        0,0,1,0,
        0,0,0,1);        
    //
    gl_Position = view_proj * world * transform * inverse(world) * vec4(position.xyz, 1.0);
    var_texcoord0 = texcoord0;
}

```

Appears that batching is the issue with this version currently assuming I was also using a custom inverse function as the model matrix is for the entire batch. I tried to break batching with custom values but had not impact.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 3, 2018, 10:27pm UTC](https://forum.defold.com/t/skew-a-sprite/30864/16 "2018-10-03T22:27:38Z")

</div>

Also note I have not been able to reproduce what happened in that previous video in the editor where moving one sprite impacted the others. No idea what is going on there!

At the time I was testing adding / removing the world / inverse(world) from the vp.
