Good day, everyone! It’s me, again, with yet another sprite-related problem (sorry!)
This one seems like it might be easily solved, except sprites don’t seem to support what I’m trying to do (apply local translation).
Here’s the scenario:
- I’m animating a 2D Sprite created via SpritePackedManager, in a 3D environment (think 2.5D graphics)
- The image displayed by the sprite needs to be moved as part of the animation, in the two local image dimensions (U,V)
- The goal is to apply this translation locally, so that the movement always appear correct regardless of the camera direction
My initial approach was to simply change the absolute position by the U, V offsets. Unfortunately this doesn’t work if the camera isn’t aligned, i.e. its direction isn’t roughly (0, 0, 1), since in those cases the sprite’s local axes aren’t identical to the world axes. The result is that the underlying mesh moves “towards” and “away” from the camera instead, at varying angles, which of course breaks the animation and causes it to “jump”.
The effect I want to achieve is movement similar to this playground, except the box’s red face (“sprite”) always facing the camera (as they normally do): https://www.babylonjs-playground.com/#EYZE4Q#275
I’ve considered the following solutions:
- Calculate the local transformation in terms of world coordinates, manually. I can’t seem to get it right, however
- Creating an actual mesh and rendering the texture on top of it. Since there can be many sprites it might be inefficient, plus it feels awkward to do this manually. I’d rather use the existing Sprite APIs instead, which also fit better logically and allow me to not deal with the desired “billboard” effect myself
- Poking around in the Sprite or SpritePackedManager objects. Sadly it doesn’t appear there’s an actual mesh object here that I could locally translate (similar to the playground above), which would be the easiest solution. In fact, it looks like the sprite manager simply has all the vertices stored in its private _vertexData field, so I can’t really use this to move an individual sprite instance even if I wanted to
So yeah, I hope one of you more experienced folks has done something similar or knows a way to apply the local translation to a given Sprite instance without breaking everything
Thanks for your time! I appreciate any suggestions you might have.