0 votes

There is Image with w,h size. Need to add line of pixels on top and delete line of pixels from bottom. As a result, size isn't changing, but Image is downshifting

in Engine by (36 points)

1 Answer

+1 vote
Best answer

You can do it like this:

func shift(im: Image):

    # Copy image except last row of pixels
    var sub = im.get_rect(Rect2(0, 0, im.get_width(), im.get_height() - 1))

    # Paste part of image one pixel lower
    im.blit_rect(sub, Rect2(0, 0, sub.get_width(), sub.get_height()), Vector2(0, 1))

    # Write line of white pixels at the top:
    # I don't know what you want them to be, there might be better way depending on what you want
    im.lock()
    for x in im.get_width():
        im.set_pixel(x, 0, Color(1,1,1))
    im.unlock()
by (28,966 points)
selected by

O. im.get_rect(..) I didn't notice this function. Cool! Thank you! I have done a bit more complicated:

    var shift : int 
    image.crop(1024,600-shift)
    var tmpImg = Image.new()
    tmpImg.copy_from(image)
    image.crop(1024,600)
    image.fill(Color(0,0,0,1))
    image.blit_rect(tmpImg,Rect2(0,0,1024,600-shift), Vector2(0,shift))
    imageTexture.set_data(image)

Note: if you are doing this only to draw an image that slides repeating itself vertically every frame, there are immensely faster ways of doing it. Just set your texture to be repeat, and either:
- Animate the texture coordinates of the geometry you draw (using a Polygon2D for example)
- Draw the texture in two parts of variable rectangle heights
- Animate texture coordinates with a few lines of shader

Re-creating a 1024x600 image on the CPU and uploading it every frame on the graphics card is quite heavy^^" Keep it in mind if you need performance.

I have a snowtree which is fullscreen ImageTexture containing static snowflakes. And adding new snowflakes continuously at top leads to make scrolling down the static part and free the bottom part which under viewport's border. enter image description here

This shifting is not every frame: some times per sec max - although the operation is heavy. May be there is another way to do it without reload fullscreen image to gpu? To be much faster, especially on mobile devices

You could even use particles (or normal sprites?) for handling those snowflakes. And yeah, definitely avoid this image upload method if you want peace of mind running it on most mobile phones.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.