Triangle Wave

Simple triangle wave shader for transitions.

I believe the part that calculates the aspect ratio doesn’t work with the compatibility renderer, but I haven’t tested. If not, you can just:

1) delete “deriv” variable (line 16)

2) delete “aspect” variable (line 17)

3) add “uniform float aspect = 1.0” at the top (around line 8 with the other shader parameters)

4) set the new “aspect” variable in gdscript with node.size.y / node.size.x (the node being whatever node has the shader applied)

Shader code
shader_type canvas_item;

uniform bool horizontal = true;
uniform bool reverse = false;
uniform float wave_size : hint_range(0.0, 1.0) = 0.09;
uniform float wave_freq : hint_range(0.01, 0.2) = 0.075;
uniform float cutoff : hint_range(0.0, 1.0) = 0.5;

void fragment() {
	vec2 uv = UV;
	float dir_uv = horizontal ? uv.x : uv.y;
	float opp_dir_uv = horizontal ? uv.y : uv.x;
	float prim_uv = reverse ? 1.0 - dir_uv : dir_uv;
	float sec_uv = reverse ? 1.0 - opp_dir_uv : opp_dir_uv;
	
	vec2 deriv = fwidthFine(UV);
	float aspect = deriv.x / deriv.y;
	
	float aspect_wave_size = horizontal ? wave_size * aspect : wave_size;
	float aspect_wave_freq = horizontal ? wave_freq / aspect : wave_freq;
	
	float t = fract(sec_uv / aspect_wave_freq);
	t = 1.0 - abs(2.0 * t - 1.0);
	
	float adj_cutoff = cutoff * (1.0 + aspect_wave_size);
	
	float c = (adj_cutoff - prim_uv) / aspect_wave_size;
	float smooth_c = smoothstep(t - .02, t + 0.02, c);
	float alpha = 1.0 - smooth_c;
	
	COLOR.a = alpha;
}
Live Preview
Tags
saw, sawtooth, scene, sin, transition, triangle, wave
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted