Topic was automatically imported from the old Question2Answer platform.
Asked By
Farflame
I’ve managed to get parallax scrolling working fine, but only if I set my sprite to be as big as or bigger than the screen itself. I tried originally with a tileable 256256 texture and it didn’t work as intended at all (it just covered the top-left part of the screen). When I tiled it to 10241024 in a paint package (my screen is currently only 800*600) everything went well.
The reason I ask is that I intend to do several parallax layers and I’m wondering if I need to do this with each image.
I stumbled upon this as well, not sure if that’s an actual issue but I had to account for various stuff like viewport size + custom transform caused by camera zoom etc, here’s my function that tries to cover these cases in my project, hopefully useful:
func _update_size():
for canvas_idx in get_child_count():
# traversing multiple CanvasLayer/ParallaxBackground nodes...
var canvas_layer = get_child(canvas_idx)
if not canvas_layer is CanvasLayer:
continue
for idx in canvas_layer.get_child_count():
# traversing multiple ParallaxLayer nodes...
var layer_node = canvas_layer.get_child(idx)
if not layer_node is ParallaxLayer:
continue
var canvas = layer_node.get_node('canvas')
var tex_size = canvas.texture.get_size()
var vp_size = viewport.size
var vp_scale = viewport.canvas_transform.get_scale()
var q = vp_size / tex_size
q = q / vp_scale
q = q.ceil()
var x = q.x * tex_size.x
var y = q.y * tex_size.y
var new_size = Vector2(x, y)
new_size *= 2 # just to be sure
var mirroring = layer_node.motion_mirroring
if mirroring.x > 0.0:
mirroring.x = new_size.x
if mirroring.y > 0.0:
mirroring.y = new_size.y
layer_node.motion_mirroring = mirroring
var canvas_size
if canvas is TextureRect:
canvas_size = canvas.rect_size
elif canvas is Sprite:
canvas_size = canvas.region_rect.size
if mirroring.x > 0.0:
canvas_size.x = new_size.x
if mirroring.y > 0.0:
canvas_size.y = new_size.y
if canvas is TextureRect:
canvas.rect_size = canvas_size
elif canvas is Sprite:
canvas.region_rect.size = canvas_size