Curve Graph

Shader to display a graph using respresenting a Curve resource.

Shader code
shader_type canvas_item;

uniform float line_thickness = 0.01;
uniform vec4 line_color : source_color = vec4(1, 1, 1, 1);
uniform vec4 bg_top_color : source_color = vec4(0.25, 0.25, 0.25, 1);
uniform vec4 bg_bottom_color : source_color = vec4(0.5, 0.5, 0.5, 1);
uniform sampler2D curve_texture : repeat_disable, filter_linear;


void fragment() {
	float curve = texture(curve_texture, UV).r;
	float line = curve - (1.0 - UV.y);
	float top_bottom = ceil(clamp(line, 0.0, 1.0));
	line = abs(line);
	line = step(line, line_thickness * (1.0 + fwidth(curve) * 300.0)); // fwidth for uniform line thickness but i'm not sure if it's good enough
	COLOR = mix(mix(bg_bottom_color, bg_top_color, top_bottom), line_color, line);
}
Tags
curve, graph, plot
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.

More from squash

2d sprite based vfx gradient shader

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments