Sprite Shader Scroll Test.zip (1.4 MB)
Hi,
I’m writing this post to both ask help with scrolling a sprite ((its texture inside the sprite bounds)) of arbitrary size (not necessarily sizes that are power of 2 ) and to clarify if this is even possible, without resorting to adding a 3D plane hack, which, for a 2D game, seems unnecessarily complex and can potential introduce other issues that have to be dealt with (like adding a camera in the scene, having to fiddle with it and so on).
This is the demo project using the 3D plane - GitHub - FlexYourBrain/Texture_Scrolling_Example: Scrolling texture in a shader example
I tried scrolling a sprite with a custom material, fragment and vertex program/shader (all 3 were copies of the builtin sprite ones) - unsuccessfully.
I tried all day yesterday with Claude Opus 4.1, Sonnet 4 Agent, chatGPT 5, chatGPT-Codex, they ground for hours, fetching all possible online official docs, forum posts etc. trying all possible techniques, but to no avail.
The closest I got was vertically scrolling the texture of the sprite, but as the sprite was disappearing at the bottom edge, it left a dark area in the top edge. Eventually I figured out that the issue was that the whole atlas was being scrolled and since the sprite was just a random cat sprite I quickly grabbed from the internet and which had an arbitrary size (473 × 600), it doesn’t fully cover the atlas’ size (which is always the power of 2), and thus the black area I was seeing was the transparent (alpha) part of the atlas page. Then I tried forcing the atlas page to match the size of the png, but shaders/materials don’t seem to support this approach.
I know that another way of achieving, for example a seamless sprite scroll (vertically or horizontally) is to add 2 sprites on top of each other or side by side and just animate their positions in tandem, reseting their positions once they are fully offscreen, but, again, in my opinion this adds unnecessary complexity, having to worry about more events, positions, callbacks etc. (especially since there is no way to lock positions in the editor for nodes, and sometimes you accidentally move something and then you have to spend time figuring out why stuff doesn’t align anymore).
Furthermore, in my particular case, all I wanted was to vertically scroll a checkerboard png, so just doubling the sprite and scrolling both in tandem was not going to work out of the box, since the top part of the sprite matched precisely with the bottom part, so I got double the vertical size of the cell of the checkerboard pattern, which means there is even more complexity to deal with - make another mirror checkerboard image, increasing the game’s size, dealing with double the assets, positions, events, callbacks etc. Also, I wanted to achieve a small sideways parallax effect such that when the player moves sideways the texture would slightly translate to the left or to the right, but for this you have to increase the horizontal size of the sprite (further increasing the game’s size) and then you have to deal with how to hide the vertical parts of the sprite such that they are not visible and only reveal themselves as they are being offset/scrolled sideways.
I really love Defold’s simplicity (recently it feels even faster, snappier - although I haven’t used it in a couple of years, so that’s a great improvement), but some basic functionality that can be achieved in Godot with a single shader file and 5-ish lines of code (as opposed to 3 files like in Defold), is missing and spending time with workarounds and hacks kind of discourages me (and probably a lot of other beginner developers) to fully settle and commit to this engine, even though it feels like it should be a no brainer for anyone doing small(ish) 2D games, especially for Web. Could be just me, but I feel like I am spending more time researching workarounds, quirks and limitations than actually developing the projects.
I uploaded a basic Defold project with a random cat image (473 × 600 - not power of 2 size, obviously ), I copied the builtin sprite material, vertex and fragment, and applied them to the sprite. If somebody can either help me figure out how to vertically and horizontally scroll the sprite’s texture only with shaders or at least help understand that this is impossible in Defold so I can move on, I would greatly appreciate it.
Thank you!