# Sprite with rotation support

**URL:** <https://forum.playcanvas.com/t/sprite-with-rotation-support/1025>\
**Category:** Help & Support\
**Created:** [October 8, 2015, 4:53pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025 "2015-10-08T16:53:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/richard/32/4645_2.png) [@Richard](https://forum.playcanvas.com/u/Richard)\
**Post date:** [October 8, 2015, 4:53pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/1 "2015-10-08T16:53:41Z")

</div>

Hi, hopefully a simple question;

I need to have a sprite with rotation; I’m using the sprite.js file from the github project, but I’m unfamiliar with GLSL or shaders in general and can’t figure out how to add simple rotation support. Can anyone help me out ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Richard](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/richard/32/4645_2.png) [@Richard](https://forum.playcanvas.com/u/Richard)\
**Post date:** [October 13, 2015, 6:44pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/2 "2015-10-13T18:44:28Z")

</div>

Found [link text](https://github.com/mrdoob/three.js/issues/1891#issuecomment-5923058), but can’t figure out how to apply the example or if it would even translate to use with PlayCanvas’ sprite. Anyone have any ideas ?

---

<div class="post-metadata">

**Author:** ![vaios](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/vaios/32/4566_2.png) [@vaios](https://forum.playcanvas.com/u/vaios)\
**Post date:** [October 14, 2015, 11:24am UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/3 "2015-10-14T11:24:46Z")

</div>

Do you absolutely need to use a sprite for this? Would some HTML suffice perhaps? You can easily rotate HTML elements using CSS.

---

<div class="post-metadata">

**Author:** ![Richard](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/richard/32/4645_2.png) [@Richard](https://forum.playcanvas.com/u/Richard)\
**Post date:** [October 14, 2015, 1:50pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/4 "2015-10-14T13:50:51Z")

</div>

I assume it needs to be a sprite, it’s in game below other sprites, not an overlay that I could just place HTML ontop of the canvas. Is it overly difficult to add rotation to the shader (I’m talking simple 2d rotation, not 3d) ?

---

<div class="post-metadata">

**Author:** ![Richard](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/richard/32/4645_2.png) [@Richard](https://forum.playcanvas.com/u/Richard)\
**Post date:** [October 19, 2015, 7:58pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/5 "2015-10-19T19:58:26Z")

</div>

I’ve been able to get to the following point;

![](https://forum-files-playcanvas-com.s3-eu-west-1.amazonaws.com/original/1X/38c70792ed4aa3270b08db037d7fecd6ca025764.jpg) ![](https://forum-files-playcanvas-com.s3-eu-west-1.amazonaws.com/original/1X/42bd227114a75c853af54df2eada9e9cf6c9e0de.jpg)

using the following shader definition;

```
vshader: [
    "attribute vec2 aPosition;",
    "attribute vec2 aUv0;",
    "varying vec2 vUv0;",
    "uniform vec2 uResolution;",
    "uniform vec2 uOffset;",
    "uniform vec2 uScale;",
    "",
    "void main(void)",
    "{",
    " gl_Position = vec4(2.0 * ((uScale * aPosition.xy + uOffset) / uResolution) - 1.0, -0.9, 1.0);",
    " vUv0 = aUv0;",
    "}"
].join("\n"),
fshader: [
    "precision " + gd.precision + " float;",
    "varying vec2 vUv0;",
    "uniform vec4 vTint;",
    "uniform sampler2D uColorMap;",
    "",
    "void main(void)",
    "{",
    " float mid = 0.5;",
    " float rot = -0.5;",
    " vec2 rotated = vec2(cos(rot) * (vUv0.x - mid) + sin(rot) * (vUv0.y - mid) + mid, cos(rot) * (vUv0.y - mid) - sin(rot) * (vUv0.x - mid) + mid);",
    " vec4 color = texture2D(uColorMap, rotated);",
    " gl_FragColor = vec4(color.rgb * vTint.rgb, color.a * vTint.a);",
    "}"
].join("\n")

```

Is anyone aware of any setting or something that defines how the image is cropped/wrapped?

---

<div class="post-metadata">

**Author:** ![dave](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/dave/32/4558_2.png) [@dave](https://forum.playcanvas.com/u/dave)\
**Post date:** [October 20, 2015, 1:51pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/6 "2015-10-20T13:51:41Z")

</div>

Hey there,

I don’t think this is going to work if you are rotating in the fragment shader, because the quad is still square on. You need to apply the rotation in the vertex shader script to rotate the vertices. The fragment shader should be able to remain unchanged.

---

<div class="post-metadata">

**Author:** ![Richard](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/richard/32/4645_2.png) [@Richard](https://forum.playcanvas.com/u/Richard)\
**Post date:** [October 20, 2015, 1:59pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/7 "2015-10-20T13:59:50Z")

</div>

Ah, that makes sense. Do you have any idea on how to accomplish this ? (I have no idea what I’m looking at, this shader stuff is pretty alien still).

Thanks.

---

<div class="post-metadata">

**Author:** ![dave](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/dave/32/4558_2.png) [@dave](https://forum.playcanvas.com/u/dave)\
**Post date:** [October 20, 2015, 2:14pm UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/8 "2015-10-20T14:14:02Z")

</div>

I’m not an expert…

`gl_Position = vec4(2.0 * ((uScale * aPosition.xy + uOffset) / uResolution) - 1.0, -0.9, 1.0);`

This section `2.0 * ((uScale * aPosition.xy + uOffset) / uResolution)` is transforming a [x,y] co-ordinate `aPosition.xy` into a scaled, offset position.

The mesh is made up of two triangle using corners `(0,-h), (w, -h), (w, 0), (0, 0) (w = width, h = height)`

It should be relative straightforward to rotate these points by some angle… but I’ll need a pen and paper 🙂

---

<div class="post-metadata">

**Author:** ![michael](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/michael/32/1194_2.png) [@michael](https://forum.playcanvas.com/u/michael)\
**Post date:** [June 14, 2016, 12:36am UTC](https://forum.playcanvas.com/t/sprite-with-rotation-support/1025/9 "2016-06-14T00:36:08Z")

</div>

Pretty much finished implementing a minimap until I hit this issue.

Fix: Be lazy and make the player icon a circle instead of a triangle heh heh heh… Argh why doesn’t the sprite.js have rotation support? 😛
