Skip to content

Instantly share code, notes, and snippets.

@margnus1
Last active August 27, 2020 20:13
Show Gist options
  • Save margnus1/901ebb2a7f5f25ebeb393334b1500f04 to your computer and use it in GitHub Desktop.
Save margnus1/901ebb2a7f5f25ebeb393334b1500f04 to your computer and use it in GitHub Desktop.
A ReShade macro allowing smooth variation of presets based on Guild Wars 2 in-game time.
/*
* The GW2DayNight(,) macro
*
* The following defines a macro (think function) that allows parameters of a
* reshade preset to fade smoothly with no stutter or sudden changes between one
* value at daytime (in the Guild Wars 2 game world), and another at nighttime.
*
* The macro is used as follows: Instead of a plain number in your preset, say
* '1.02', you put 'GW2DayNight(1.02,1.06)'. Now, that number will be 1.02 at
* daytime, 1.06 at nighttime, and something inbetween (changing every second)
* during dusk and dawn.
*
* The macro needs to know your time-zone to work. It can either be provided by
* the GW2ReshadeMap tool (https://github.com/margnus1/gw2-reshade-map), or
* manually with a line as follows (UTC+4 as example):
* '#define TimeZone (4*60*60)'
* In either case, the line above *or* the '#include "gw2map.h"' line needs to
* be put before here.
*/
#define GW2DayOrigin (25 * 60)
#define GW2DayDuration (120 * 60)
uniform float4 Date < source = "date"; >;
#if TimeZone % GW2DayDuration
# define SecsOfDay ((Date.w - TimeZone + 24 * 60 * 60) % (24 * 60 * 60))
#else
# define SecsOfDay Date.w
#endif
#define TimeOfDay (((SecsOfDay - GW2DayOrigin + GW2DayDuration) \
/ GW2DayDuration) % 1.0)
#define GW2Dawn 0
#define GW2Day (5 / 120.0)
#define GW2Dusk (75 / 120.0)
#define GW2Night (80 / 120.0)
/*
* Defines a value that is 1 at daytime, 0 at nighttime, and
* decreases/increases linearly during dusk and dawn.
*
* GW2DaySaw
* ^ ______________
* | ____/ \____
* | ^ Dawn ^ Dusk
* +---------------------------> In-game time
*/
#define GW2DayMidpoint ((GW2Day + GW2Dusk) / 2)
#define GW2SawMult (1 / (GW2Day - GW2Dawn))
#define UnclampedGW2DaySaw \
((((GW2DayMidpoint - GW2Day) + (1 / GW2SawMult)) \
- abs(GW2DayMidpoint - TimeOfDay)) \
* GW2SawMult)
#define GW2DaySaw (clamp(UnclampedGW2DaySaw, 0.0, 1.0))
/*
* Is Day at daytime, Night at nighttime, and fades between them at dusk and
* dawn.
*/
#define GW2DayNight(Day, Night) \
lerp(Night, Day, smoothstep(0.0, 1.0, UnclampedGW2DaySaw))
/*
* Fades from Day to Twilight for the first 2.5 minutes of dusk, and from
* Twilight to Night for the last 2.5 minutes. It does the reverse during dawn.
*/
#define GW2DayTwilightNight(Day, Twilight, Night) \
lerp(Night, \
lerp(Twilight, Day, \
smoothstep(0.0, 1.0, 2.0*UnclampedGW2DaySaw-1.0)), \
smoothstep(0.0, 1.0, 2.0*UnclampedGW2DaySaw))
/*
* Fades from Day to Dusk for the first 2.5 minutes of dusk, and from
* Dusk to Night for the last 2.5 minutes.
* Fades from Night to Dawn for the first 2.5 minutes of dawn, and from Dawn to
* Day for the last 2.5 minutes.
*/
#define GW2DayDuskNightDawn(Day, Dusk, Night, Dawn) \
GW2DayTwilightNight(Day, \
TimeOfDay > GW2DayMidpoint \
? (Dusk) : (Dawn), \
Night)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment