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

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Lubnevsky
Newbie
Posts: 4
Joined: Sun Oct 09, 2022 11:52 pm
Tumblr: lubnevsky
Deviantart: Lubnevsky
itch: lubnevsky
Discord: realjonahofficial
Contact:

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

#1 Post 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 2902 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! :)

Post Reply

Who is online

Users browsing this forum: No registered users