Skip to content

Commit

Permalink
Add current time parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed Jan 17, 2025
1 parent 839d9ac commit 420a835
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 48 deletions.
7 changes: 5 additions & 2 deletions data/examples/clock_analog.shader
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//Based on https://www.shadertoy.com/view/XdKXzy

uniform int current_time_ms;
uniform int current_time_sec;
uniform int current_time_min;
uniform int current_time_hour;
uniform float3 hour_handle_color = {1.0,1.0,1.0};
uniform float3 minute_handle_color = {1.0,1.0,1.0};
uniform float3 second_handle_color = {1.0,0.0,0.0};
Expand Down Expand Up @@ -133,7 +136,7 @@ float4 mainImage(VertData v_in) : TARGET
}

if (centerdist < 1.0) {
float elapsed_time = (floor(local_time+(time_offset_hours*3600.0))+pow(frac(local_time),16.0));
float elapsed_time = float((time_offset_hours+current_time_hour)*3600+current_time_min*60+current_time_sec) + pow(float(current_time_ms)/1000.0,16.0);
// hour
color = styleHandle(color, px,
rln(uv, -0.05, 0.5, elapsed_time / 3600.0 / 12.0),
Expand Down
107 changes: 68 additions & 39 deletions data/examples/clock_digital_led.shader
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#define mod(x,y) (x - y * floor(x / y))
#endif

uniform int current_time_sec;
uniform int current_time_min;
uniform int current_time_hour;

uniform int timeMode<
string label = "Time mode";
string widget_type = "select";
Expand Down Expand Up @@ -115,48 +119,73 @@ float4 mainImage(VertData v_in) : TARGET
uv.x += 3.5;
float seg = 0.0;

float timeSecs = 0.0;
if(timeMode == 0){
timeSecs = local_time;
}else if(timeMode == 1){
timeSecs = elapsed_time_enable;
}else if(timeMode == 2){
timeSecs = elapsed_time_active;
}else if(timeMode == 3){
timeSecs = elapsed_time_show;
}else if(timeMode == 4){
timeSecs = elapsed_time_start;
}
timeSecs += offsetSeconds + offsetHours*3600;
if(timeSecs < 0)
timeSecs = 0.9999-timeSecs;
seg += showNum(uv,int(mod(timeSecs,60.0)),false);

timeSecs = floor(timeSecs/60.0);

uv.x -= 1.75;
seg += showNum(uv,current_time_sec,false);
uv.x -= 1.75;
seg += dots(uv);
uv.x -= 1.75;
seg += showNum(uv,current_time_min,false);
uv.x -= 1.75;
seg += dots(uv);
uv.x -= 1.75;
if (ampm) {
if(current_time_hour == 0){
seg += showNum(uv,12,true);
}else if(current_time_hour > 12){
seg += showNum(uv,current_time_hour-12,true);
}else{
seg += showNum(uv,current_time_hour,true);
}
} else {
seg += showNum(uv,current_time_hour,true);
}
}else{
float timeSecs = 0.0;
if(timeMode == 1){
timeSecs = elapsed_time_enable;
}else if(timeMode == 2){
timeSecs = elapsed_time_active;
}else if(timeMode == 3){
timeSecs = elapsed_time_show;
}else if(timeMode == 4){
timeSecs = elapsed_time_start;
}

seg += dots(uv);

uv.x -= 1.75;

seg += showNum(uv,int(mod(timeSecs,60.0)),false);

timeSecs = floor(timeSecs/60.0);
if (ampm)
{
if (timeSecs > 12.0)
timeSecs = mod(timeSecs,12.0);
}else if (timeSecs > 24.0) {
timeSecs = mod(timeSecs,24.0);
timeSecs += offsetSeconds + offsetHours*3600;
if(timeSecs < 0)
timeSecs = 0.9999-timeSecs;
seg += showNum(uv,int(mod(timeSecs,60.0)),false);

timeSecs = floor(timeSecs/60.0);

uv.x -= 1.75;

seg += dots(uv);

uv.x -= 1.75;

seg += showNum(uv,int(mod(timeSecs,60.0)),false);

timeSecs = floor(timeSecs/60.0);
if (ampm)
{
if(timeSecs == 0.0){
timeSecs = 12.0;
}else if (timeSecs > 12.0){
timeSecs = mod(timeSecs,12.0);
}
}else if (timeSecs > 24.0) {
timeSecs = mod(timeSecs,24.0);
}

uv.x -= 1.75;

seg += dots(uv);

uv.x -= 1.75;
seg += showNum(uv,int(mod(timeSecs,60.0)),true);
}

uv.x -= 1.75;

seg += dots(uv);

uv.x -= 1.75;
seg += showNum(uv,int(mod(timeSecs,60.0)),true);


if (seg==0.0){
return image.Sample(textureSampler, v_in.uv);
Expand Down
15 changes: 9 additions & 6 deletions data/examples/clock_digital_nixie.shader
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//based on https://www.shadertoy.com/view/fsBcRm

uniform int current_time_ms;
uniform int current_time_sec;
uniform int current_time_min;
uniform int current_time_hour;
uniform int timeMode<
string label = "Time mode";
string widget_type = "select";
Expand Down Expand Up @@ -381,7 +384,7 @@ float4 mainImage(VertData v_in) : TARGET
float2 uv = ((float2(v_in.uv.x,1.0-v_in.uv.y) * uv_size)/uv_size.x);
float time = 0.0;
if(timeMode == 0){
time = local_time;
time = float(current_time_hour*3600+current_time_min*60+current_time_sec) + float(current_time_ms)/1000.0;
}else if(timeMode == 1){
time = elapsed_time_enable;
}else if(timeMode == 2){
Expand Down Expand Up @@ -434,14 +437,14 @@ float4 mainImage(VertData v_in) : TARGET
// "flare" is a wide blurry region around the characters, and
// "flarenoise" is a spatio-temporal modulation of its extents
// (slight flickering, but not all characters at the same time)
float flarenoise = psrdnoise(float2(p.x*0.1,5.0*time), float2(0.0,0.0), 0.0, g);
float flarenoise = psrdnoise(float2(p.x*0.1,5.0*elapsed_time), float2(0.0,0.0), 0.0, g);
float flare = 1.0-smoothstep(0.0, 2.5, d + 0.05*flarenoise);
flare *= flare; // A more rapid decline towards the edge
// "glow" is a variation in the intensity of the glowy cathode (core)
float glow = 0.8+0.2*psrdnoise(p - float2(0.0, 2.0*time), float2(0.0,0.0), 4.0*time, g);
float glow = 0.8+0.2*psrdnoise(p - float2(0.0, 2.0*elapsed_time), float2(0.0,0.0), 4.0*time, g);
// Now we mess up the distance field a little for the "halo" effect
d += 0.1*psrdnoise(p - float2(0.0, 2.0*time), float2(0.0,0.0), 8.0*time, g);
d += 0.05*psrdnoise(2.0*p - float2(0.0, 4.0*time) + 0.15*g, float2(0.0,0.0), -16.0*time, g);
d += 0.1*psrdnoise(p - float2(0.0, 2.0*elapsed_time), float2(0.0,0.0), 8.0*time, g);
d += 0.05*psrdnoise(2.0*p - float2(0.0, 4.0*elapsed_time) + 0.15*g, float2(0.0,0.0), -16.0*time, g);
// "halo" is a kind of flame/plasma cloud near the core. A real Nixie tube
// doesn't have this, but it adds some appealing visual detail.
// Looks more like hot filaments than "cold cathodes", but... oh, well.
Expand Down
76 changes: 75 additions & 1 deletion obs-shaderfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
#include <util/dstr.h>
#include <util/darray.h>
#include <util/platform.h>

#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#include <util/threading.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif

#include "version.h"

Expand Down Expand Up @@ -179,6 +183,15 @@ struct shader_filter_data {
gs_eparam_t *param_uv_scale;
gs_eparam_t *param_uv_pixel_interval;
gs_eparam_t *param_uv_size;
gs_eparam_t *param_current_time_ms;
gs_eparam_t *param_current_time_sec;
gs_eparam_t *param_current_time_min;
gs_eparam_t *param_current_time_hour;
gs_eparam_t *param_current_time_day_of_week;
gs_eparam_t *param_current_time_day_of_month;
gs_eparam_t *param_current_time_month;
gs_eparam_t *param_current_time_day_of_year;
gs_eparam_t *param_current_time_year;
gs_eparam_t *param_elapsed_time;
gs_eparam_t *param_elapsed_time_start;
gs_eparam_t *param_elapsed_time_show;
Expand Down Expand Up @@ -296,6 +309,15 @@ static char *load_shader_from_file(const char *file_name) // add input of visite

static void shader_filter_clear_params(struct shader_filter_data *filter)
{
filter->param_current_time_ms = NULL;
filter->param_current_time_sec = NULL;
filter->param_current_time_min = NULL;
filter->param_current_time_hour = NULL;
filter->param_current_time_day_of_week = NULL;
filter->param_current_time_day_of_month = NULL;
filter->param_current_time_month = NULL;
filter->param_current_time_day_of_year = NULL;
filter->param_current_time_year = NULL;
filter->param_elapsed_time = NULL;
filter->param_elapsed_time_start = NULL;
filter->param_elapsed_time_show = NULL;
Expand Down Expand Up @@ -498,6 +520,24 @@ static void shader_filter_reload_effect(struct shader_filter_data *filter)
filter->param_uv_pixel_interval = param;
} else if (strcmp(info.name, "uv_size") == 0) {
filter->param_uv_size = param;
} else if (strcmp(info.name, "current_time_ms") == 0) {
filter->param_current_time_ms = param;
} else if (strcmp(info.name, "current_time_sec") == 0) {
filter->param_current_time_sec = param;
} else if (strcmp(info.name, "current_time_min") == 0) {
filter->param_current_time_min = param;
} else if (strcmp(info.name, "current_time_hour") == 0) {
filter->param_current_time_hour = param;
} else if (strcmp(info.name, "current_time_day_of_week") == 0) {
filter->param_current_time_day_of_week = param;
} else if (strcmp(info.name, "current_time_day_of_month") == 0) {
filter->param_current_time_day_of_month = param;
} else if (strcmp(info.name, "current_time_month") == 0) {
filter->param_current_time_month = param;
} else if (strcmp(info.name, "current_time_day_of_year") == 0) {
filter->param_current_time_day_of_year = param;
} else if (strcmp(info.name, "current_time_year") == 0) {
filter->param_current_time_year = param;
} else if (strcmp(info.name, "elapsed_time") == 0) {
filter->param_elapsed_time = param;
} else if (strcmp(info.name, "elapsed_time_start") == 0) {
Expand Down Expand Up @@ -2632,6 +2672,40 @@ void shader_filter_set_effect_params(struct shader_filter_data *filter)
if (filter->param_uv_pixel_interval != NULL) {
gs_effect_set_vec2(filter->param_uv_pixel_interval, &filter->uv_pixel_interval);
}
if (filter->param_current_time_ms != NULL) {
#ifdef _WIN32
SYSTEMTIME system_time;
GetSystemTime(&system_time);
gs_effect_set_int(filter->param_current_time_ms, system_time.wMilliseconds);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
gs_effect_set_int(filter->param_current_time_ms, tv.tv_usec / 1000);
#endif
}
if (filter->param_current_time_sec != NULL || filter->param_current_time_min != NULL ||
filter->param_current_time_hour != NULL || filter->param_current_time_day_of_week != NULL ||
filter->param_current_time_day_of_month != NULL || filter->param_current_time_month != NULL ||
filter->param_current_time_day_of_year != NULL || filter->param_current_time_year != NULL) {
time_t t = time(NULL);
struct tm *lt = localtime(&t);
if (filter->param_current_time_sec != NULL)
gs_effect_set_int(filter->param_current_time_sec, lt->tm_sec);
if (filter->param_current_time_min != NULL)
gs_effect_set_int(filter->param_current_time_min, lt->tm_min);
if (filter->param_current_time_hour != NULL)
gs_effect_set_int(filter->param_current_time_hour, lt->tm_hour);
if (filter->param_current_time_day_of_week != NULL)
gs_effect_set_int(filter->param_current_time_day_of_week, lt->tm_wday);
if (filter->param_current_time_day_of_month != NULL)
gs_effect_set_int(filter->param_current_time_day_of_month, lt->tm_mday);
if (filter->param_current_time_month != NULL)
gs_effect_set_int(filter->param_current_time_month, lt->tm_mon);
if (filter->param_current_time_day_of_year != NULL)
gs_effect_set_int(filter->param_current_time_day_of_year, lt->tm_yday);
if (filter->param_current_time_year != NULL)
gs_effect_set_int(filter->param_current_time_year, lt->tm_year);
}
if (filter->param_elapsed_time != NULL) {
gs_effect_set_float(filter->param_elapsed_time, filter->elapsed_time);
}
Expand Down

0 comments on commit 420a835

Please sign in to comment.