Page 1 of 1

Layered Parallax (new method using 3D Stage + optional ease)

Posted: Mon Sep 18, 2023 9:53 am
by Lubnevsky
Clip from Perceptions of the Dead by Ithaqua Labs
Clip from Perceptions of the Dead by Ithaqua Labs
parallax_example_potd1.gif (3.6 MiB) Viewed 9489 times
(Yes, I'm using the example gif from the original thread because I'm too lazy to make a new one. The new code is able to produce the same result, so there shouldn't really be a need for a new example anyways.)

A newer version of the layered parallax in this thread. This code is based on the "Ren'Py How To: parallax camera and 'drunken' blur" devlog by Midge on Itch, with some QOL adjustments and the addition of an easing factor by me.

Code: Select all

default persistent.parallax = True #Add a toggle in your settings! Your players will appreciate it.

transform parallax:
    perspective True
    subpixel True
    function moving_camera

# Distances (define more transforms as needed).

transform near:
    truecenter()
    ypos int(config.screen_height*0.528) #shifting the image downwards a bit so the parallax effect doesnt show its cutoff

transform far:
    truecenter()
    zzoom True
    zpos -750

# Setting our images to be near by default, and bgs as far.

define config.default_transform = near
define config.tag_transform = {"bg" : far}

# Moving camera logic

init python:
    def moving_camera(trans, st, at):
        if persistent.parallax:
            x, y = renpy.display.draw.get_mouse_pos()
            
            parallax_factor = 0.055
            target_xoffset = (x - config.screen_width / 2) * parallax_factor
            target_yoffset = (y - config.screen_height / 2) * parallax_factor
            
            easing_factor = 0.055 #closer to 0 = slower, closer to 1 = faster, with 1 being no ease
            trans.xoffset += (target_xoffset - trans.xoffset) * easing_factor
            trans.yoffset += (target_yoffset - trans.yoffset) * easing_factor
        else:
            trans.xoffset = 0
            trans.yoffset = 0
        
        return 0
    
You can just drag and drop this code into your project, wherever you keep custom functions. Since we're applying the parallax-related transforms by default, you don't need to change anything in your script besides adding

Code: Select all

camera at parallax
in the very beginning! :)