How to make shift of Image?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nikas

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

:bust_in_silhouette: Reply From: Zylann

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()

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)

Nikas | 2019-12-03 15:04

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.

Zylann | 2019-12-03 21:51

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

Nikas | 2019-12-04 02:36

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.

Zylann | 2019-12-04 18:34