# Shader sprite global to local

**URL:** <https://forum.defold.com/t/shader-sprite-global-to-local/64696>\
**Category:** Feature requests (USE GITHUB)\
**Created:** [March 24, 2020, 9:55pm UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696 "2020-03-24T21:55:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [March 24, 2020, 9:55pm UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/1 "2020-03-24T21:55:23Z")

</div>

Any way to convert global position in sprite shader to local?

Or maybe local position of sprite to global?

I tryed it with world matrix.  
But it looks like sprites get mat4(1) as world matrix. So i can’t use it.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 25, 2020, 7:41am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/2 "2020-03-25T07:41:24Z")

</div>

There currently is no good way to do that.  
One could do it like [this](https://forum.defold.com/t/2d-light-implemetations/3636/5) and make the calculation yourself in the vertex shader.

What do you want it for? Perhaps there’s a another way of solving it?

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [March 25, 2020, 7:53am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/3 "2020-03-25T07:53:58Z")

</div>

> [@Get sprite center in shader](https://forum.defold.com/t/get-sprite-center-in-shader/55655):
>
> I need to get sprite center in world coordinates inside shader. How can i get it? I make a shader that tint sprites, using light map texture.World consist of cells, every cell have color. Something like this. play_button Example When sprite is fully inside cell, everything ok. But when sprite bigger then cell, or near the cell border.It use two diffetent lights color.And it looks bad. My idea is use center of sprite to find it light color. But i can’t get center in shader. I can’t use sprite.set\_consta…

For now I set position of sprite center as shader uniform. But if I can do it in shader it will be better.

> [@Mathias\_Westerdahl](#):
>
> There currently is no good way to do that.

Why? You set world matrix for models. Why not set it for sprites?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 25, 2020, 8:09am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/4 "2020-03-25T08:09:20Z")

</div>

> Why? You set model matrix for models. Why not set it for sprites?

It will render every single sprite in a separate draw call. We’ve preferred batching of sprites for performance reasons. Models tend to have far more vertices, and the benefit of batching doesn’t apply there.

From your other post:

> I can’t use sprite.set\_constant(). Because it will be break batching.

Your feature request would break batching too.

Sometimes we add a first version of a feature, but not to a 100% to at least get some of our users unblocked. We haven’t painted ourselves into a corner, we can add to the feature later.  
In this case, I don’t think we thought anyone would actually want it on sprites .  
Adding it to sprites will of course increase the engine size (unclear by how much), for a feature that is currently more of an “idea” as you put it in the other post.

I actually think the current way of setting the material constant is a valid approach given the current circumstances.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [March 25, 2020, 8:16am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/5 "2020-03-25T08:16:03Z")

</div>

Thanks for your explanation.  
I don’t know gapi.  
But I always think that setting world matrix not break batching. It is not same as setting position in shader? Every sprite have different position and it not break batching?

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [March 25, 2020, 8:39am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/6 "2020-03-25T08:39:54Z")

</div>

> [@Mathias\_Westerdahl](#):
>
> I don’t think we thought anyone would actually want it on sprites .

I understand that it is not blocking and needed for small amount for people.  
But in some cases it can be usefull. Like skew sprite or billboard shader.

Please think about it=)

> [@Skew a sprite](https://forum.defold.com/t/skew-a-sprite/30864/):
>
> [2018-10-01%2020\_06\_35-LNPOverlayWindow] I want to skew a sprite like this. Any ideas on how to easily do this?

> [@Billboard shader, Model matrix?](https://forum.defold.com/t/billboard-shader-model-matrix/12983/):
>
> Today I was trying to make a billboard vertex shader (i.e. planes rotated to always face the camera). From my googling it seemed relatively simple to do, but I didn’t see any way of accessing the model matrix from Defold? Is there any way to do that? Or maybe I am misunderstanding what I need to do this? I know I could just rotate the game object parents of my sprites/quads, I was specifically trying to do it via a shader.

> [@Best Method To Make 3rd Person Car Race Game?](https://forum.defold.com/t/best-method-to-make-3rd-person-car-race-game/24810/5):
>
> There are different ways of doing this. E.g. skewing 2d sprites to create arbitrary polygons, to get a 3D perspective. You can also rasterize the polygons yourself. As usual, google has some good resources on making games: [http://www.extentofthejam.com/pseudo/](http://www.extentofthejam.com/pseudo/)[http://kometbomb.net/2016/04/03/how-does-pico-racer-work/](http://kometbomb.net/2016/04/03/how-does-pico-racer-work/)[https://codeincomplete.com/posts/javascript-racer-v1-straight/](https://codeincomplete.com/posts/javascript-racer-v1-straight/) The first link helped me a lot when I did a racer a couple of months back. I chose to rasterize the image mysel…

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 25, 2020, 9:00am UTC](https://forum.defold.com/t/shader-sprite-global-to-local/64696/7 "2020-03-25T09:00:20Z")

</div>

I think you make a good case for it! I like it!

We will definitely think about it.
