2D Sonar

Simple sonar shader.

Note: If you use this in godot 3, you have to replace tau with a constant equivalent, so pi * 2.

Shader code
shader_type canvas_item;

uniform float scrollSpeed = -4.0;
uniform vec4 sweepColor : source_color = vec4(0.3, 1.0, 0.6, 1.0);
uniform float alphaMod = 1.0;
uniform float sonarWidth = 0.8;

float when_lt(float x, float y) {
  return max(sign(y - x), 0.0);
}

float when_ge(float x, float y) {
  return 1.0 - when_lt(x, y);
}

void fragment(){
	float scrollAmount = TIME * scrollSpeed + 0.2*sin(TIME*1.8);
	vec2 centerUV = UV*2.0-vec2(1.0);
	vec2 circUV = vec2(mod(atan(centerUV.y, centerUV.x)+TAU + scrollAmount, TAU)/TAU, length(centerUV));
	COLOR = vec4(sweepColor.rgb, when_ge(1.0, circUV.y) * alphaMod * clamp((max(circUV.x, sonarWidth)-sonarWidth)/max(1.0-sonarWidth, 0.05), 0.0, 1.0));
}
Tags
sonar
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 flytrap

2D Procedural Water

2d Nebula Shader

Related shaders

Sonar Effect Shader

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
BlueOctopus
1 year ago

i Made a game about this shader and made a video about it if you’re curious (trying to get feedback 🙂
https://www.youtube.com/watch?v=TH7Ipa3tt10&ab_channel=BlueOctopus